Nix package manager/zh-tw: Difference between revisions

From NixOS Wiki
X807x (talk | contribs)
Created page with "更多設定可用 管家 (Home Manager) 來管理宣告式環境給一個用戶。對於系統級的 Linux 設定,你可以使用 [https://github.com/numtide/system-manager 系統管家 (System Manager)]。對於系統級的 MacOS 設定, [https://github.com/LnL7/nix-darwin nix-darwin] 是一個推薦的解方。"
X807x (talk | contribs)
Created page with "早期嘗試就在 [https://riir-nix.github.io/ riir-nix]"
 
(12 intermediate revisions by the same user not shown)
Line 32: Line 32:
=== 設定檔 (Profiles) ===
=== 設定檔 (Profiles) ===


<div lang="en" dir="ltr" class="mw-content-ltr">
為了構建一個一致的用戶和系統設定,Nix 連結 Nix 商店入口進入 ''設定檔 (profile)'' 中。這些是 Nix 進行回滾的前端:由於商店是不可變的且保留先前版本的設定文件,將系統還原到先前的狀態只是簡單地將符號連結更改為先前的設定。更精確來說,Nix 將二進位檔案符號連結到 Nix 商店中的入口,這些入口代表了使用者環境。這些用戶環境隨後被符號連結到儲存在 <code>/nix/var/nix/profiles</code> 中被標注的設定檔,而它們則進一步符號連結到使用者的 <code>~/.nix-profile</code>
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 <code>/nix/var/nix/profiles</code>, which are in turn symlinked to the user's <code>~/.nix-profile</code>.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
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>.
=== Sandboxing ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Sandboxing"></span>
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 <code>fetch*</code> 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 [https://nixos.org/nix/manual/#sec-conf-file nix.conf section] in the Nix manual for details.
=== 沙盒 (Sandbox) ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
當啟用沙盒時,Nix 會建立獨立的環境給每個建造過程。這用於刪除更多建造環境的隱藏的依賴組來增加可再線性。這包括在建造過程中禁止訪問網路(除了 fetch* 函數)以及禁止讀取 Nix 商店之外的文件。根據不同的作業系統,對其他資源的讀取也會被封鎖(例如,Linux 中進程間通信會被隔離);更多細節請看 [https://nixos.org/nix/manual/#sec-conf-file nix.conf 段落] 在 Nix 手冊中。
Sandboxing is enabled by default on Linux, and disabled by default on macOS.
In pull requests for [https://github.com/NixOS/nixpkgs/ Nixpkgs] people are asked to test builds with sandboxing enabled (see <code>Tested using sandboxing</code> in the pull request template) because in [https://nixos.org/hydra/ official Hydra builds] sandboxing is also used.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
沙盒在 Linux 上預設是開啟的,在 MacOS 上則否。
To configure Nix for sandboxing, set <code>sandbox = true</code> in <code>/etc/nix/nix.conf</code>; to configure NixOS for sandboxing set <code>nix.useSandbox = true;</code> in <code>configuration.nix</code>. The <code>nix.useSandbox</code> option is <code>true</code> by default since NixOS 17.09.
在 [https://github.com/NixOS/nixpkgs/ Nixpkgs] 的提取請求 (pull request) 中,請開啟沙盒模式進行測試(請見 <code>使用沙盒進行測試</code> 在提取請求模版),因為在 [https://nixos.org/hydra/ 官方 Hydra 建設] 中,沙盒會被使用的。
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
要設定 Nix 沙盒,設定 <code>sandbox = true</code> 在 <code>/etc/nix/nix.conf</code>;設定 NixOS 沙盒,設定 <code>nix.useSandbox = true;</code> 在 <code>configuration.nix</code>。 <code>nix.useSandbox</code> 預設為 <code>true</code> 自從 NixOS 17.09。
=== Alternative Interpreters ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Alternative_Interpreters"></span>
There is an ongoing effort to reimplement Nix, from the ground up, in Rust.
=== 替代的直譯器 ===
</div>
 
目前正在進行一項計畫,從零開始使用 Rust 重新實作 Nix


<div lang="en" dir="ltr" class="mw-content-ltr">
* [https://cs.tvl.fyi/depot/-/tree/tvix tvix]
* [https://cs.tvl.fyi/depot/-/tree/tvix tvix]
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
有一個社群領導的 Nix 2.18 分支叫做 Lix,專注於正確性、可用性和成長性。同時它還為 Nix 部份組件加入 Rust 端口,但不像從零開始重寫的 Tvix
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.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
* [https://lix.systems/ lix]
* [https://lix.systems/ lix]
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
早期嘗試就在 [https://riir-nix.github.io/ riir-nix]
Earlier attempts can be found on [https://riir-nix.github.io/ riir-nix]
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
==註記==
==Notes==
<references />
<references />
</div>




<div lang="en" dir="ltr" class="mw-content-ltr">
[[Category:Pedias|百科]]
[[Category:Pedias]]
[[Category:Nix]]
[[Category:Nix]]
[[Category:Incomplete]]
[[Category:Incomplete|未完成]]
[[Category:Software]]
[[Category:Software|軟體]]
</div>

Latest revision as of 06:53, 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)

當啟用沙盒時,Nix 會建立獨立的環境給每個建造過程。這用於刪除更多建造環境的隱藏的依賴組來增加可再線性。這包括在建造過程中禁止訪問網路(除了 fetch* 函數)以及禁止讀取 Nix 商店之外的文件。根據不同的作業系統,對其他資源的讀取也會被封鎖(例如,Linux 中進程間通信會被隔離);更多細節請看 nix.conf 段落 在 Nix 手冊中。

沙盒在 Linux 上預設是開啟的,在 MacOS 上則否。 在 Nixpkgs 的提取請求 (pull request) 中,請開啟沙盒模式進行測試(請見 使用沙盒進行測試 在提取請求模版),因為在 官方 Hydra 建設 中,沙盒會被使用的。

要設定 Nix 沙盒,設定 sandbox = true/etc/nix/nix.conf;設定 NixOS 沙盒,設定 nix.useSandbox = true;configuration.nixnix.useSandbox 預設為 true 自從 NixOS 17.09。

替代的直譯器

目前正在進行一項計畫,從零開始使用 Rust 重新實作 Nix。

有一個社群領導的 Nix 2.18 分支叫做 Lix,專注於正確性、可用性和成長性。同時它還為 Nix 部份組件加入 Rust 端口,但不像從零開始重寫的 Tvix。

早期嘗試就在 riir-nix

註記

  1. 數值在計算過程中不能改變。 函式總是輸出相同的值只要他們的輸入不變。