nix-shell:如何从环境文件中加载环境变量?

时间:2020-05-04 19:46:19

标签: nix nixpkgs

有关此问题:nix-shell: how to specify a custom environment variable?

通过这种推导:

stdenv.mkDerivation rec {
  FOO = "bar";
}

FOO将在nix shell中作为环境变量提供,但是是否可以从env文件中加载环境变量?

1 个答案:

答案 0 :(得分:2)

您可以使用nix-shell作为外壳程序代码,使用sourcingshellHook从文件中加载环境变量。例如:

stdenv.mkDerivation {
  name = "my-shell";
  shellHook = ''
    # Mark variables which are modified or created for export.
    set -a
    source env.sh
    # or to make it relative to the directory of this shell.nix file
    # source ${toString ./env.sh}
    set +a
  '';
}

如果您的外壳也不是包,则可以从stdenv.mkDerivation切换到mkShell