Nix
Nix 是一个纯函数式包管理器,旨在使包管理可重现、声明式且可靠。
安装
配置
要使 Nix 守护程序在系统启动时启动,启用 nix-daemon.service。
将需要运行 Nix 的用户添加到 nix-users 用户组中以获取与守护程序套接字连接的权限。重启会话以使更改生效。
添加一个频道并更新它。
$ nix-channel --add https://nixos.org/channels/nixpkgs-unstable $ nix-channel --update
使用
~/.nix-profile/bin 目录添加到您的 PATH 中。在 shell 配置完成后,以下内容应该将 hello 安装到您已经更新过的 PATH 中:
$ nix-env -iA nixpkgs.hello
二进制文件本身将位于 /nix/store/[hash]-hello-[version]/bin/hello。
运行 hello 并确保它在正确的 PATH 中。如果它工作正常,您可以卸载它:
$ nix-env --uninstall hello
或者您可以检查已安装的程序列表:
$ nix-env --query
您还可以列出迭代版本:
$ nix-env --list-generations
更多详细信息请参阅 nix-env(1) 手册。
提示与技巧
最大任务数
nix 默认只同时进行一个构建。以下内容将可以使 nix 同时进行与 CPU 数量相同的构建:
/etc/nix/nix.conf
max-jobs = auto
图形加速
要运行 OpenGL 和 Vulkan 应用程序,请使用 NixGL。
桌面集成
要将 Nix 应用程序与您的桌面环境集成,请将 ~/.nix-profile/share 目录添加到您的 $XDG_DATA_DIRS 中,例如使用 export XDG_DATA_DIRS=$HOME/.nix-profile/share:$XDG_DATA_DIRS。
Zsh 集成用于 nix-shell
nix-shell 默认启动 Bash。zsh-nix-shellAUR 允许您在 nix-shell 环境中使用 Zsh 作为默认 shell。一些提示插件如 zsh-theme-powerlevel10kAUR 和 zsh-pure-promptAUR 提供了 nix-shell 指示器。
命令补全
Zsh
nix-zsh-completionsAUR 提供了 Zsh 对 nix 命令的补全,如 nix-env 和 nix-shell。
故障排查
打开的文件太多
一些构建可能会遇到类似如下的问题:
error: opening directory '/nix/store/...': Too many open files
编辑 nix-daemon.service 并增大文件限制:
[Service] LimitNOFILE=65536
关于 root 用户更新频道的警告
如果在使用 Nix 时出现以下错误:
warning: Nix search path entry '/nix/var/nix/profiles/per-user/root/channels' does not exist, ignoring
root 用户需要更新他们的频道:
# nix-channel --update
沙盒构建问题
其他沙盒问题
error: while setting up the build environment: mounting /proc: Operation not permitted error: program '/usr/bin/nix-env' failed with exit code 1
1: package 'utils' in options("defaultPackages") was not found
2: package 'stats' in options("defaultPackages") was not found
Error: .onLoad failed in loadNamespace() for 'utils', details:
call: system(paste(which, shQuote(names[i])), intern = TRUE, ignore.stderr = TRUE)
error: cannot popen '/nix/store/fnkvlbls29d01jcx3wsdnhykyrl7087r-which-2.21/bin/which 'uname' 2>/dev/null', probable reason 'Cannot allocate memory'
这是已知的上游问题:#2311、#3000 和 #4636。
nixpkgs 中的所有内容都指望在启用沙盒的情况下构建。最常见的解决方法是在配置文件中禁用沙盒:
/etc/nix/nix.conf
# 禁用沙盒 sandbox = false
然后重启 nix-daemon 服务。
区域设置警告
Nixos wiki 建议运行 export LOCALE_ARCHIVE=/usr/lib/locale/locale-archive。或者,导出环境变量 LC_ALL=C。