Nix package manager/zh-tw: Difference between revisions
Created page with "=== 設定檔 (Profiles) ===" Tags: Mobile edit Mobile web edit |
Created page with "更多設定可用 管家 (Home Manager) 來管理宣告式環境給一個用戶。對於系統級的 Linux 設定,你可以使用 [https://github.com/numtide/system-manager 系統管家 (System Manager)]。對於系統級的 MacOS 設定, [https://github.com/LnL7/nix-darwin nix-darwin] 是一個推薦的解方。" |
||
Line 19: | Line 19: | ||
獨立 Nix 由 <code>nix.conf</code>(通常在 <code>/etc/nix/</code> 被找到)設定,它定義一系列關於評估、建造、資源回收、沙盒及用戶權限的設定。更多細節就在 [https://nixos.org/manual/nix/stable/command-ref/conf-file Nix 參考手冊] 中。 | 獨立 Nix 由 <code>nix.conf</code>(通常在 <code>/etc/nix/</code> 被找到)設定,它定義一系列關於評估、建造、資源回收、沙盒及用戶權限的設定。更多細節就在 [https://nixos.org/manual/nix/stable/command-ref/conf-file Nix 參考手冊] 中。 | ||
更多設定可用 [[Home Manager|管家 (Home Manager)]] 來管理宣告式環境給一個用戶。對於系統級的 Linux 設定,你可以使用 [https://github.com/numtide/system-manager 系統管家 (System Manager)]。對於系統級的 MacOS 設定, [https://github.com/LnL7/nix-darwin nix-darwin] 是一個推薦的解方。 | |||
<span id="Internals"></span> | <span id="Internals"></span> |
Revision as of 02:51, 10 March 2025
Nix 是一個軟體包管理器和建造系統,它會分析可重現的建造指令由 Nix 表達式語言 撰寫 (一個純函式化的延遲求值語言)。Nix 表達式是純函式的[1],將依賴當作變數並產生一個 衍生物 為套件指定一個可重現的建置環境。 Nix 將建成的結果儲存在一個由雜湊整個相依樹指定的地址中,建立一個不可改變的套件商店(又或是 nix 商店),它允許原子升級 (atomic upgrade)、回滾 (rollback)和多版本並存,從本質上消滅 相依性地獄。
用法
安裝
NixOS: Nix 已經在你安裝 NixOS 時被安裝。
如果你打算使用 Nix 並在不同的 Linux 發行版或蘋果電腦上,那你可以進行獨立安裝:Nix 手冊中的安裝階段 說明的如何從二進檔或原始碼進行獨立 Nix 安裝。
Nix 指令
Nix 指令 的說明在 Nix 參考手冊: 主要指令、用途和實驗性指令。 在 2.0 版本(於 2018 年二月釋出)以前有不同的指令。
系統設定
在 NixOS,Nix 是由 nix
選項 設定。
獨立 Nix 由 nix.conf
(通常在 /etc/nix/
被找到)設定,它定義一系列關於評估、建造、資源回收、沙盒及用戶權限的設定。更多細節就在 Nix 參考手冊 中。
更多設定可用 管家 (Home Manager) 來管理宣告式環境給一個用戶。對於系統級的 Linux 設定,你可以使用 系統管家 (System Manager)。對於系統級的 MacOS 設定, nix-darwin 是一個推薦的解方。
內部組成
Nix 商店
由 Nix 建立的軟體套件會被放在唯讀的 Nix 商店 中,通常在 /nix/store
中。每個套件都有一個獨一無二的地址由加密的雜湊和套件名稱、版本構成,例如: /nix/store/nawl092prjblbhvv16kxxbk6j9gkgcqm-git-2.14.1
。這些前綴將建造過程的輸入,包含原始碼當暗、整個相依樹、編譯器選項等等。這讓 Nix 可以同時安裝不同版本的相同套件,甚至同個版本的不同建成套件,例如:利用不同編譯器建成。當加入、移除或升級套件,沒有任何東西被從商店移除;相反的,指向這些套件在 設定檔 (profiles) 中的符號連結 (symlink) 被加入、移除、或改變。
設定檔 (Profiles)
In order to construct a coherent user or system environment, Nix symlinks entries of the Nix store into profiles. These are the front-end by which Nix allows rollbacks: since the store is immutable and previous versions of profiles are kept, reverting to an earlier state is simply a matter of change the symlink to a previous profile. To be more precise, Nix symlinks binaries into entries of the Nix store representing the user environments. These user environments are then symlinked into labeled profiles stored in /nix/var/nix/profiles
, which are in turn symlinked to the user's ~/.nix-profile
.
Sandboxing
When sandbox builds are enabled, Nix will setup an isolated environment for each build process. It is used to remove further hidden dependencies set by the build environment to improve reproducibility. This includes access to the network during the build outside of fetch*
functions and files outside the Nix store. Depending on the operating system access to other resources are blocked as well (ex. inter process communication is isolated on Linux); see nix.conf section in the Nix manual for details.
Sandboxing is enabled by default on Linux, and disabled by default on macOS.
In pull requests for Nixpkgs people are asked to test builds with sandboxing enabled (see Tested using sandboxing
in the pull request template) because in official Hydra builds sandboxing is also used.
To configure Nix for sandboxing, set sandbox = true
in /etc/nix/nix.conf
; to configure NixOS for sandboxing set nix.useSandbox = true;
in configuration.nix
. The nix.useSandbox
option is true
by default since NixOS 17.09.
Alternative Interpreters
There is an ongoing effort to reimplement Nix, from the ground up, in Rust.
There is also a community-led fork of Nix 2.18 named Lix, focused on correctness, usability, and growth. While it has also ported some components of Nix to Rust, it is not a ground-up rewrite like Tvix.
Earlier attempts can be found on riir-nix
Notes
- ↑ 數值在計算過程中不能改變。 函式總是輸出相同的值只要他們的輸入不變。