Nix package manager/zh-tw: Difference between revisions
Created page with "為了構建一個一致的用戶和系統設定,Nix 連結 Nix 商店入口進入 ''設定檔 (profile)'' 中。這些是 Nix 進行回滾的前端:由於商店是不可變的且保留先前版本的設定文件,將系統還原到先前的狀態只是簡單地將符號連結更改為先前的設定。更精確來說,Nix 將二進位檔案符號連結到 Nix 商店中的入口,這些入口代表了使用者環境。這些用戶環境隨後被符號連結到儲..." |
Created page with "=== 沙盒 (Sandbox) ===" |
||
Line 36: | Line 36: | ||
These user environments are then symlinked into labeled profiles stored in <code>/nix/var/nix/profiles</code>, which are in turn symlinked to the user's <code>~/.nix-profile</code>. | These user environments are then symlinked into labeled profiles stored in <code>/nix/var/nix/profiles</code>, which are in turn symlinked to the user's <code>~/.nix-profile</code>. | ||
< | <span id="Sandboxing"></span> | ||
=== | === 沙盒 (Sandbox) === | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> |
Revision as of 03:03, 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)
為了構建一個一致的用戶和系統設定,Nix 連結 Nix 商店入口進入 設定檔 (profile) 中。這些是 Nix 進行回滾的前端:由於商店是不可變的且保留先前版本的設定文件,將系統還原到先前的狀態只是簡單地將符號連結更改為先前的設定。更精確來說,Nix 將二進位檔案符號連結到 Nix 商店中的入口,這些入口代表了使用者環境。這些用戶環境隨後被符號連結到儲存在 /nix/var/nix/profiles
中被標注的設定檔,而它們則進一步符號連結到使用者的 ~/.nix-profile
。
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
.
沙盒 (Sandbox)
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
- ↑ 數值在計算過程中不能改變。 函式總是輸出相同的值只要他們的輸入不變。