Jump to content

Nix (package manager)/zh: Difference between revisions

From Official NixOS Wiki
Mayer (talk | contribs)
No edit summary
Tags: Mobile edit Mobile web edit
Mayer (talk | contribs)
No edit summary
Line 28: Line 28:
=== Nix存储 ===
=== Nix存储 ===


由Nix构建的软件包被放在只读的''Nix存储'', 通常位于<code>/nix/store</code>. 每个软件包会被赋予一个由加密hash值指定的独一无二的地址, 该地址位于软件包名称和版本之后, 例如<code>/nix/store/nawl092prjblbhvv16kxxbk6j9gkgcqm-git-2.14.1</code>. 这些前缀的哈希处理使用到了对构建过程的所有输入, 包括源文件, 完整依赖关系树, 编译器标志, 等等. 这让Nix可以同时安装同一软件包的不同版本, 甚至同一版本的不同构建, 比如由不同的编译器构建的变体. . When adding, removing or updating a package, nothing is removed from the store; instead, symlinks to these packages are added, removed or changed in ''profiles''.
由Nix构建的包被放在只读的''Nix存储'', 通常位于<code>/nix/store</code>. 每个包会被赋予一个由加密hash值指定的独一无二的地址, 该地址位于包名称和版本之后, 例如<code>/nix/store/nawl092prjblbhvv16kxxbk6j9gkgcqm-git-2.14.1</code>. 这些前缀的哈希处理使用到了对构建过程的所有输入, 包括源文件, 完整依赖关系树, 编译器标志, 等等. 这让Nix可以同时安装同一包的不同版本, 甚至同一版本的不同构建, 比如由不同的编译器构建的变体. 当添加, 移除, 或者更新一个包时, 什么都没有从Nix存储中被移除; 作为替代,指向这些包的符号链接在''profile''中被添加, 移除或者修改.


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">

Revision as of 14:42, 25 February 2025

Nix是一個包管理器和構建系統, 解析用 Nix表達式語言 (惰性求值的純函數式語言) 指定的可再現的構建指令. Nix表達式是純函數[1], 接受依賴作為參數,並為包產生一個指定了可再現的構建環境的 derivation . Nix把構建的結果存儲在由完整依賴樹的哈希值指定的唯一地址中, 創建了一個不可變的包存儲 (即nix存儲), 這使得原子升級, 回滾, 以及不同版本的包的同時安裝成為可能, 從根本上消除了依賴地獄.

用法

安裝

NixOS: 當你在安裝NixOS時, Nix就已經安裝好了.

如果你想在另一個Linux發行版或一台Mac電腦上使用Nix, 你可以進行獨立安裝: Nix手冊的安裝部分講述了從二進制文件或原始碼的獨立的Nix安裝過程

Nix命令

Nix命令被記錄在Nix參考手冊: 主要命令, 工具命令和實驗性命令. 在2.0版本(於2018年2月發布)之前, 命令有所不同.

配置

在NixOS上, Nix通過nix 選項進行配置.

獨立的Nix的配置需要通過nix.conf (通常位於 /etc/nix/), 它定義了一系列和求值,構建,垃圾回收,沙盒,以及用戶權限的設置. 這些可用選項的細節可在Nix參考手冊中找到.

單用戶可使用 Home Manager 進一步配置對聲明式環境的管理. 對於Linux上的系統級配置, 你可以使用 System Manager. 對於macOS上的系統級配置, nix-darwin 是更佳的解決方案.

Internals

Nix存儲

由Nix構建的包被放在只讀的Nix存儲, 通常位於/nix/store. 每個包會被賦予一個由加密hash值指定的獨一無二的地址, 該地址位於包的名稱和版本之後, 例如/nix/store/nawl092prjblbhvv16kxxbk6j9gkgcqm-git-2.14.1. 這些前綴的哈希處理使用到了對構建過程的所有輸入, 包括源文件, 完整依賴關係樹, 編譯器標誌, 等等. 這讓Nix可以同時安裝同一個包的不同版本, 甚至同一版本的不同構建, 比如由不同的編譯器構建的變體. 當添加, 移除, 或者更新一個包時, 什麼都沒有從Nix存儲中被移除; 作為替代,指向這些包的符號連結在profile中被添加, 移除或者修改.

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

  1. 計算過程中,值不可改變. 當函數的輸入不變時,函數總是輸出相同的結果.