nix-shell: how to specify a custom environment variable?

巧了我就是萌 提交于 2020-07-17 11:22:05

问题


I'm learning about nixos and nix expressions. In a project folder I created a shell.nix and I when I run nix-shell I want it to preset an environment variable for me. For example to set the PGDATA env var.

I know there are several ways to write nix expression files (I'm not yet used to most of them). Here is my sample:

shell.nix

let 
  pkgs = import <nixpkgs> {};
  name = "test";
in pkgs.myEnvFun {
  buildInputs = [
    pkgs.python
    pkgs.libxml2
  ];
  inherit name;
  extraCmds = ''
    export TEST="ABC"
  '';
 }

回答1:


Use buildPythonPackage function (that uses mkDerivation). Passing anything to it will set env variables in bash shell:

with import <nixpkgs> {};

buildPythonPackage {
  name = "test";

  buildInputs = [ pkgs.python pkgs.libxml2 ];

  src = null;

  PGDATA = "...";
}



回答2:


You may also use pkgs.stdenv.mkDerivation.shellHook.

let 
  pkgs = import <nixpkgs> {};
  name = "test";
in pkgs.stdenv.mkDerivation {
  buildInputs = [
    pkgs.python
    pkgs.libxml2
  ];
  inherit name;
  shellHook = ''
    export TEST="ABC"
  '';
 }


来源:https://stackoverflow.com/questions/27713707/nix-shell-how-to-specify-a-custom-environment-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!