nix-shell: 如何从 env 文件加载环境变量?
nix-shell: how to load environment variables from env file?
与此问题相关:nix-shell: how to specify a custom environment variable?
有了这个推导:
stdenv.mkDerivation rec {
FOO = "bar";
}
FOO
将在 nix shell 中作为环境变量可用,但是否可以从 env 文件加载环境变量?
您可以使用 nix-shell
's shellHook
to load environment variables from a file by sourcing 它们作为 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
'';
}
如果您的 shell 不是一个包裹,您可以从 stdenv.mkDerivation
切换到 mkShell
。
与此问题相关:nix-shell: how to specify a custom environment variable?
有了这个推导:
stdenv.mkDerivation rec {
FOO = "bar";
}
FOO
将在 nix shell 中作为环境变量可用,但是否可以从 env 文件加载环境变量?
您可以使用 nix-shell
's shellHook
to load environment variables from a file by sourcing 它们作为 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
'';
}
如果您的 shell 不是一个包裹,您可以从 stdenv.mkDerivation
切换到 mkShell
。