尼克斯壳 : how to load environment variables from env file?

标签 nix nixpkgs

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

有了这个推导:

stdenv.mkDerivation rec {
  FOO = "bar";
}
FOO将在 nix shell 中作为环境变量可用,但是否可以从 env 文件加载环境变量?

最佳答案

您可以使用 nix-shell shellHook通过 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
  '';
}

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

关于尼克斯壳 : how to load environment variables from env file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61600333/

相关文章:

python - 在 NixOS 中,如何解决冲突?

git - nix-prefetch-git sha256 要求对标签使用修订版

haskell - nixOS:如何从同一包、同一版本的两个实例之间的冲突中恢复

rust - nixos:我的自定义包派生在 `cargo build` 上失败

nix - nix 路径类型何时进入 nix 存储,何时不进入?

Docker — 无法连接到 unix :///var/run/docker. sock 上的 Docker 守护进程

nix - 编写本地 nix 包

haskell - 如何通过 Nix 覆盖 nixpkgs 中的 Haskell 包?