Nix
Nix 是一個純函數式包管理器,旨在使包管理可重現、聲明式且可靠。
有四種安裝 Nix 的方法:
- 原生的 Arch Linux 包,
- 官方支持的 Nix 上游安裝腳本,
- 由 Determinate Systems 提供的 Zero to Nix 腳本,該公司為 Nix 提供第三方支援,
- 以及由熱情的 Nix 社區成員提供的 Lix 腳本,旨在通過提供 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。
curl some-url | sh,如 Nix 文檔所建議的,被認為存在安全風險,因為它會執行未知代碼。因此建議在執行腳本前手動下載並檢查它。為了更簡單和更不複雜的 Nix 設置,您可以使用 Determinate Systems 提供的設置。使用 curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix -o nix-install.sh 下載文件,查看它:less ./nix-install.sh,然後運行腳本 sh ./nix-install.sh install 來開始安裝。
curl some-url | sh,如 Determinate Systems 所建議的,被認為存在安全風險,因為它會執行未知代碼。因此建議在執行腳本前手動下載並檢查它。sh ./nix-install.sh install --diagnostic-endpoint=""。更多信息請參閱 Determinate Systems 的手冊。
使用 curl --proto '=https' --tlsv1.2 -sSf -L https://install.lix.systems/lix -o lix-install.sh 下載文件,查看它:less ./lix-install.sh,然後運行腳本 sh ./lix-install.sh 來開始安裝。
curl some-url | sh,如 Lix 所建議的,被認為存在安全風險,因為它會執行未知代碼。因此建議在執行腳本前手動下載並檢查它。更多信息請參閱 Lix Installer 的手冊。
uninstallnix.sh
#!/bin/sh
for i in {01..10}; do
userdel "nixbld$i"
+done
+groupdel nixbld
+
要使 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。
nix-shell 默認啟動 Bash。zsh-nix-shellAUR 允許您在 nix-shell 環境中使用 Zsh 作為默認 shell。一些提示插件如 zsh-theme-powerlevel10kAUR 和 zsh-pure-promptAUR 提供了 nix-shell 指示器。
nix-zsh-completionsAUR 提供了 Zsh 對 nix 命令的補全,如 nix-env 和 nix-shell。
一些構建可能會遇到類似如下的問題:
error: opening directory '/nix/store/...': Too many open files
編輯 nix-daemon.service 並增大文件限制:
[Service] LimitNOFILE=65536
如果在使用 Nix 時出現以下錯誤:
warning: Nix search path entry '/nix/var/nix/profiles/per-user/root/channels' does not exist, ignoring
root 用戶需要更新他們的頻道:
# nix-channel --update
warning: Nix search path entry '/nix/var/nix/profiles/per-user/<USER>/channels' does not exist, ignoring
參見 FS#78828。或者,您可以使用 #上游安裝腳本 或 #Zero to Nix 安裝腳本,它們不會出現此問題。
參見 FS#78828。或者,您可以使用 #上游安裝腳本 或 #Zero to Nix 安裝腳本,它們不會出現此問題。
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。