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

Jos*_*uis 6 nix nixpkgs

与此问题相关:nix-shell:如何指定自定义环境变量?

有了这个推导:

stdenv.mkDerivation rec {
  FOO = "bar";
}
Run Code Online (Sandbox Code Playgroud)

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

Rob*_*ing 7

您可以使用nix-shell'sshellHook从文件中加载环境变量,方法是将它们作为 shell 代码获取。例如:

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
  '';
}
Run Code Online (Sandbox Code Playgroud)

如果你的 shell 不是一个包,你可以从 切换stdenv.mkDerivation到。mkShell