direnv
的use_nix
和use_flake
的一个更快、持久化的实现,用于替代内置版本。
主要特点:
nix-shell
环境,在首次运行后显著提高速度gcroots
中创建结果shell派生的符号链接,防止构建依赖被垃圾回收(生命太短暂了,不能在没有网络连接的飞机上丢失项目的构建缓存)lorri
?与lorri相比,nix-direnv更简单(不需要外部守护进程)并且支持flakes。此外,lorri有时会在每次更改时重新评估整个nixpkgs(导致持续高CPU负载)。
<details> <summary> 通过home-manager(推荐)</summary>注意:nix-direnv需要现代版本的Bash。macOS自带的是2007年的bash 3.2版本。作为解决方案,我们建议macOS用户通过Nix或Homebrew安装
direnv
。安装nix-direnv有多种方式,选择你喜欢的方式:
请注意,虽然推荐使用home-manager集成,但某些用例可能需要只在特定版本的nix-direnv中存在的功能。使用这种方法很难控制安装的nix-direnv版本。如果你需要这种特定控制,请使用其他安装nix-direnv的方法。
在$HOME/.config/home-manager/home.nix
中添加
{ # ...其他配置... programs = { direnv = { enable = true; enableBashIntegration = true; # 关于其他shell的注意事项见下文 nix-direnv.enable = true; }; bash.enable = true; # 关于其他shell的注意事项见下文 }; }
查看当前的Home Manager选项以了解与Bash以外的shell的集成。确保也让home-manager
管理你的shell,设置programs.<你的shell>.enable = true
。
在你的.envrc
中添加以下行:
</details> <details> <summary>通过NixOS的系统配置</summary>if ! has nix_direnv_version || ! nix_direnv_version 3.0.5; then source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.5/direnvrc" "sha256-RuwIS+QKFj/T9M2TFXScjBsLR6V3A17YVoEW/Q6AZ1w=" fi
对于NixOS 23.05+,只需要
{ programs.direnv.enable = true; }
其他可用选项有:
</details> <details> <summary>使用`nix profile`</summary>{ pkgs, ... }: { #设置为默认值 programs.direnv = { package = pkgs.direnv; silent = false; loadInNixShell = true; direnvrcExtra = ""; nix-direnv = { enable = true; package = pkgs.nix-direnv; }; }
nix profile
以