有关此问题:nix-shell: how to specify a custom environment variable?
通过这种推导:
stdenv.mkDerivation rec {
FOO = "bar";
}
FOO
将在nix shell中作为环境变量提供,但是是否可以从env文件中加载环境变量?
答案 0 :(得分:2)
您可以使用nix-shell
作为外壳程序代码,使用sourcing的shellHook
从文件中加载环境变量。例如:
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
。