Nix

出自 Arch Linux 中文维基

Nix 是一個純函數式包管理器,旨在使軟體包管理可靠且可重現。

安裝[編輯 | 編輯原始碼]

目前有兩種方法來安裝 Nix,一種是受 Arch Linux 支持的,另一種則是受 Nix 官方支持的。

通過 Pacman 安裝[編輯 | 編輯原始碼]

安裝 nix

使用 Nix 官方(上游)安裝腳本[編輯 | 編輯原始碼]

使用 curl --proto '=https' --tlsv1.2 -sSfL https://nixos.org/nix/install -o nix-install.sh 下載文件,審閱 less ./nix-install.sh,然後運行腳本 ./nix-install.sh --daemon 來開始安裝 Nix。

警告: 正如 Nix 文檔所述,直接運行 curl some-url | sh 有安全風險,因為它會執行未知的代碼,再者它可能在下載過程中已經損壞。因此,請在執行安裝腳本前手動檢查它。

配置[編輯 | 編輯原始碼]

要使 Nix 守護程序能夠在系統啟動時一併啟動,啟用 nix-daemon.service

將需要運行 Nix 的用戶添加到 nix-users 用戶組中以獲取與守護程序套接字連接的權限。

添加一個通道並將其更新。

$ nix-channel --add https://nixos.org/channels/nixpkgs-unstable
$ nix-channel --update

提示與技巧[編輯 | 編輯原始碼]

當 shell 配置完成後,以下內容應該將 hello 安裝到你已經更新過的 PATH(/nix/store/[hash]-hello-[version]/bin/hello)中。

如果你從官方倉庫安裝 Nix,你需要手動將 ~/.nix-profile/bin 添加到 PATH。

$ nix-env -iA nixpkgs.hello

運行 hello 程序以確保 PATH 配置正確,如果正常工作,你可以卸載它:

$ nix-env --uninstall hello

或者你可以檢查已經安裝的程序列表(q 意指 query):

$ nix-env -q

你也可以查詢迭代版本

$ nix-env --list-generations

更多信息參見 nix-env(1)

最大任務數[編輯 | 編輯原始碼]

nix 默認只同時進行一個構建。以下內容將可以使 nix 同時進行與 CPU 數量相同的構建:

/etc/nix/nix.conf
max-jobs = auto

故障排查[編輯 | 編輯原始碼]

權限錯誤[編輯 | 編輯原始碼]

運行 nix replnix-env -u 時提示:

cannot connect to daemon at '/nix/var/nix/daemon-socket/socket: permission denied

如果你剛安裝 nix,請重啟系統。

打開的文件太多[編輯 | 編輯原始碼]

一些構建可能會遇到類似如下的問題:

error: opening directory '/nix/store/...': Too many open files

編輯 nix-daemon.service 並增大文件限制:

[Service]
LimitNOFILE=65536

關於 root 用戶更新頻道的警告[編輯 | 編輯原始碼]

warning: Nix search path entry '/nix/var/nix/profiles/per-user/root/channels' does not exist, ignoring

如果在使用 Nix 時出現上述信息,則 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 中的所有內容都指望在啟用沙盒的情況下構建。

最常見的解決方法是在配置文件中禁用沙盒,然後重新啟動 nix-daemon 服務。

/etc/nix/nix.conf
# disable sandboxing
sandbox = false

參見[編輯 | 編輯原始碼]