Nix (package manager)/zh: Difference between revisions
No edit summary |
No edit summary Tags: Mobile edit Mobile web edit |
||
Line 68: | Line 68: | ||
=== 沙盒化 === | === 沙盒化 === | ||
当沙盒构建 | 当启用沙盒构建时,Nix 会为每个构建过程设置一个隔离的环境。这样可以去除构建环境中额外的隐藏依赖,以提高可复现性。这包括在构建过程中禁止访问 <code>fetch*</code> 函数之外的网络,以及禁止访问 Nix store 之外的文件。根据操作系统的不同,对其他资源的访问也会被阻止(例如在 Linux 上进程间通信会被隔离);详细信息请参阅 Nix 手册中的 [https://nixos.org/nix/manual/#sec-conf-file nix.conf 章节]。 | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | <div lang="en" dir="ltr" class="mw-content-ltr"> |
Revision as of 18:01, 28 August 2025
Nix 是一个软件包管理器和构建系统,它解析由 Nix 表达式语言 (惰性求值的纯函数式语言) 指定的可复现的构建指令。Nix 表达式是纯函数[1],它接受依赖作为参数,并生成一个 derivation,用于指定该软件包的可复现的构建环境。Nix 把构建的结果存储在由完整依赖树的哈希值指定的唯一地址中,从而创建了一个不可变的包存储 (即 nix store),使得原子升级、回滚、以及同时安装同一软件包的不同版本成为可能,从根本上消除了依赖地狱。
用法
安装
在 NixOS 上,Nix 已经自动安装好了。
如果你想在其他 Linux 发行版或在 macOS 上使用 Nix, 你可以按照 Nix 手册的安装章节来安装 Nix。
Nix 命令
Nix 命令在Nix 参考手册中有详细记录:主要命令,工具命令和实验性命令。在 2.0 版本(于 2018 年 2 月发布)之前,命令有所不同。
配置
在 NixOS 上, Nix 可以通过 nix
选项进行配置。
独立的 Nix 通过 nix.conf
进行配置(其通常位于 /etc/nix/
)。可用选项的详细说明可在 Nix 参考手册中找到.
你也可以使用 Home Manager 配置 Nix,它为单用户管理声明式环境。对于系统范围的配置,可以在 Linux 上使用 System Manager,在 macOS 上使用 nix-darwin。
内部组成
Nix store
Packages built by Nix are placed in the read-only Nix store, normally found in /nix/store
. Each package is given a unique address specified by a cryptographic hash followed by the package name and version, for example /nix/store/nawl092prjblbhvv16kxxbk6j9gkgcqm-git-2.14.1
. These prefixes hash all the inputs to the build process, including the source files, the full dependency tree, compiler flags, etc. This allows Nix to simultaneously install different versions of the same package, and even different builds of the same version, for example variants built with different compilers. 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.
Cleaning the Nix store
For information relating to cleaning the Nix store, refer to NixOS Manual: Chapter - Cleaning the Nix Store.
Nix store corruption
For information relating to fixing a corrupted Nix store, refer to NixOS Manual: Chapter - Nix Store Corruption.
Valid Nix store names
- Main article: Valid Nix store path names
Profiles
為了構建一個一致的用戶和系統設定,Nix 連結 Nix 商店入口進入 設定檔 (profile) 中。這些是 Nix 進行回滾的前端:由於商店是不可變的且保留先前版本的設定文件,將系統還原到先前的狀態只是簡單地將符號連結更改為先前的設定。更精確來說,Nix 將二進位檔案符號連結到 Nix 商店中的入口,這些入口代表了使用者環境。這些用戶環境隨後被符號連結到儲存在 /nix/var/nix/profiles
中被標注的設定檔,而它們則進一步符號連結到使用者的 ~/.nix-profile
。
沙盒化
当启用沙盒构建时,Nix 会为每个构建过程设置一个隔离的环境。这样可以去除构建环境中额外的隐藏依赖,以提高可复现性。这包括在构建过程中禁止访问 fetch*
函数之外的网络,以及禁止访问 Nix store 之外的文件。根据操作系统的不同,对其他资源的访问也会被阻止(例如在 Linux 上进程间通信会被隔离);详细信息请参阅 Nix 手册中的 nix.conf 章节。
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.
可选的解释器
目前正在進行一項計畫,從零開始使用 Rust 重新實作 Nix。
有一個社群領導的 Nix 2.18 分支叫做 Lix,專注於正確性、可用性和成長性。同時它還為 Nix 部份組件加入 Rust 端口,但不像從零開始重寫的 Tvix。
早期尝试可以在 riir-nix 找到
Notes
- ↑ 值在计算过程中不会改变。当函数的输入不变时,函数总是输出相同的结果。