跳至內容

为应对本站因各种原因无法访问,建议用户安装 arch-wiki-docs-zh-cnCNRepo 或者 arch-wiki-docs-zh-twCNRepo 离线文档包备用,也可尝试使用经由Cloudflare CDN的替代版本aw.lilydjwg.me

Nix

出自 Arch Linux 中文维基

Nix 是一個純函數式包管理器,旨在使包管理可重現、聲明式且可靠

安裝 nix

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

將需要運行 Nix 的用户添加到 nix-users 用户組中以獲取與守護程序套接字連接的權限。重啟會話以使更改生效。

添加一個頻道並更新它。

$ nix-channel --add https://nixos.org/channels/nixpkgs-unstable
$ nix-channel --update
注意:如果您從 官方倉庫 安裝了 Nix,您必須手動將 ~/.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 默認啟動 Bashzsh-nix-shellAUR 允許您在 nix-shell 環境中使用 Zsh 作為默認 shell。一些提示插件如 zsh-theme-powerlevel10kAURzsh-pure-promptAUR 提供了 nix-shell 指示器。

命令補全

[編輯 | 編輯原始碼]

nix-zsh-completionsAUR 提供了 Zsh 對 nix 命令的補全,如 nix-envnix-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