Jump to content

Nix (package manager)/zh: Difference between revisions

From NixOS Wiki
Mayer (talk | contribs)
No edit summary
Weijia (talk | contribs)
 
(37 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Nix是一个包管理器和构建系统, 解析[[Nix Expression Language|Nix表达式语言]] (惰性求值的纯函数式语言) 指定的可现的构建指令. Nix表达式是纯函数<ref>计算过程中,值改变. 当函数的输入不变时,函数总是输出相同的结果. </ref>, 接受依赖作为参数,并为包产生一个指定了可再现的构建环境的 ''[[Derivations|derivation]]'' . Nix把构建的结果存储在由完整依赖树的哈希值指定的唯一地址中, 创建了一个不可变的包存储 (即[[#Nix store|nix存储]]), 这使得原子升级, 回滚, 以及不同版本的包的同时安装成为可能, 从根本上消除了[https://en.wikipedia.org/wiki/Dependency_hell 依赖地狱].
<languages/>
{{Navbox Nix}}
{{Disambiguation|message=本条目介绍的是 Nix 软件包管理器。请勿与 [[Nix ecosystem|Nix 生态系统]]、[[Nix (language)|Nix 语言]] 或 [[Nix (command)|Nix 命令]] 混淆。}}
Nix 是一个软件包管理器和构建系统,它解析[[Nix Expression Language|Nix 表达式语言]] (惰性求值的纯函数式语言) 指定的可现的构建指令。Nix 表达式是纯函数<ref>值在计算过程中不改变当函数的输入不变时,函数总是输出相同的结果</ref>,它接受依赖作为参数,并生一个 ''[[Derivations|derivation]]'',用于指定该软件包的可复现的构建环境。Nix 把构建的结果存储在由完整依赖树的哈希值指定的唯一地址中,从而创建了一个不可变的包存储 (即 [[#Nix store|nix store]])使得原子升级回滚以及同时安装同一软件包的不同版本成为可能从根本上消除了[https://en.wikipedia.org/wiki/Dependency_hell 依赖地狱]


<span id="Usage"></span>
<span id="Usage"></span>
Line 5: Line 8:


=== 安装 ===
=== 安装 ===
NixOS: 当你在安装NixOS时, Nix就已经安装好了.
在 [[NixOS]] 上,Nix 已经自动安装好了


如果你想在另一个Linux发行版或一台Mac电脑上使用Nix, 你可以进行独立安装: [https://nixos.org/manual/nix/stable/installation/installation Nix手册的安装部分]讲述了从二进制文件或源代码的独立的Nix安装过程
如果你想在其他 Linux 发行版或在 macOS 上使用 Nix, 你可以按照 [https://nixos.org/manual/nix/stable/installation/installation Nix 手册的安装章节]安装 Nix。


<span id="Nix_commands"></span>
<span id="Nix_commands"></span>
=== Nix命令 ===
=== Nix 命令 ===


[[Nix command|Nix命令]]被记录在[https://nixos.org/manual/nix/stable/command-ref/command-ref Nix参考手册]: 主要命令, 工具命令和实验性命令. 在2.0版本(于2018年2月发布)之前, 命令有所不同.
[[Nix (command line utilities)|Nix 命令]]在 [https://nixos.org/manual/nix/stable/command-ref/command-ref Nix 参考手册]中有详细记录:主要命令工具命令和实验性命令。在 2.0 版本(于 2018 年 2 月发布之前命令有所不同


=== 配置 ===
=== 配置 ===
在NixOS上, Nix通过[https://search.nixos.org/options?query=nix. <code>nix</code> 选项]进行配置.
在 NixOS 上, Nix 可以通过 [https://search.nixos.org/options?query=nix. <code>nix</code> 选项]进行配置


独立的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]] 进一步配置声明式环境的管理. 于Linux上的系统配置, 你可以使用 System Manager. 对于macOS的系统级配置, nix-darwin 是更佳的解决方案. 
你也使用 [[Home Manager]] 配置 Nix,它为单用户管理声明式环境系统范围的配置可以在 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>
== 内部组成 ==
== 内部组成 ==


<span id="Nix_store"></span>
=== Nix store ===
=== Nix store(Nix存储库) ===


由Nix构建的包被放只读''Nix存储库'', 通常位于<code>/nix/store</code>. 每个包会被赋予一个由加密hash指定的一无二的地址, 该地址位于包的名称和版本之后, 例如<code>/nix/store/nawl092prjblbhvv16kxxbk6j9gkgcqm-git-2.14.1</code>. 这些前缀的哈希处理使用到了对构建过程的所有输入, 包括源件, 完整依赖关系树, 编译器标志, 等等. 这让Nix可以同时安装同一个包的不同版本, 甚至同一版本的不同构建, 比如由不同的编译器构建的变体. 当添加, 移除, 或者更新一个包时, 什么都没有从Nix存储库中被移除; 作为替代,指向这些包的符号链接在''profile''中被添加, 移除或者修改.
{{Split|reason=nix store 概念上有足够独立性,得单。}}


<span id="Profiles"></span>
由 Nix 构建的软件包会被放置在只读的 ''Nix store'' 中,通常位于 <code>/nix/store</code>。每个软件包都会被赋予一个唯一的地址,该地址由加密哈希值、软件包名称和版本组成,例如 <code>/nix/store/nawl092prjblbhvv16kxxbk6j9gkgcqm-git-2.14.1</code>。这些前缀对构建过程中的所有输入进行哈希,包括源文件、完整的依赖树、编译器参数等。这使得 Nix 能够同时安装同一软件包的不同版本,甚至是同一版本的不同构建(例如使用不同编译器构建的变体)。在添加、移除或更新软件包时,存储中的内容不会被删除;相反,''profiles'' 中指向这些软件包的符号链接会被添加、移除或更改。
=== Profiles(系统画像) ===


了建个连贯的用户或统环, Nix将Nix存储库中的目符号链接到''profile''. 这些是Nix允许"前端": 因为存储库是不可, 而的profile版本被保留了下来, 回更早状态只需要简单号链接指向一个先前的profile. 更精说, Nix将进制文件号链接到Nix存储库描述户环境的目中. 这些用户环随之被符号链接到<code>/nix/var/nix/profiles</code>中带有标签的profile, 这些profile又被号链接到用的<code>~/.nix-profile</code>.
<span id="Cleaning_the_Nix_store"></span>
==== 清理 Nix store ====
 
有关清理 Nix store 的信息,请参阅 {{NixOS Manual|name=NixOS 手册:章节 - 清理 Nix 存储|anchor=#sec-nix-gc}}。
 
<span id="Nix_store_corruption"></span>
==== Nix store 损坏 ====
 
有关修复 Nix store 损坏的信息,请参阅 {{NixOS Manual|name=NixOS 手册:章节 - Nix store 损坏|anchor=#sec-nix-store-corruption}}。
 
<span id="Valid_Nix_store_names"></span>
==== 有效的 Nix store 名称 ====
 
{{main|Valid Nix store path names}}
 
=== Profiles ===
 
建一個一致的用戶和統環,Nix 會將 Nix store 中的目符號連結到 ''profiles'' 中。這是 Nix 實現的前端:由於 store 是不可的且保留先前版本的 profile,將系統還原先前狀態只需簡單號連結更改為先前的 profile。更精說,Nix 將進位檔號連結到 Nix store 代表戶環境的目中。這些用戶環隨後被符號連結儲存在 <code>/nix/var/nix/profiles</code> 被標注的 profile,它們則進一步號連結使的 <code>~/.nix-profile</code>


<span id="Sandboxing"></span>
<span id="Sandboxing"></span>
=== 沙盒===
=== 沙盒 ===


当沙盒构建被启用, Nix会为每个构建程设置一个孤立的环境.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.
启用沙盒构建时,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">
沙盒在 Linux 上預設是開啟的,在 macOS 上則否。
Sandboxing is enabled by default on Linux, and disabled by default on macOS.
[https://github.com/NixOS/nixpkgs/ Nixpkgs] 的提取請求 (pull request) 中,請開啟沙盒模式進行測試(請見提取請求模版中的 <code>Tested using sandboxing</code>),因為在 [https://nixos.org/hydra/ 官方 Hydra 構建]中,沙盒會被使用。
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">
要为 Nix 配置沙盒,请在 <code>/etc/nix/nix.conf</code> 中设置 <code>sandbox = true</code>;要为 NixOS 配置沙盒,请在 <code>configuration.nix</code> 中设置 <code>nix.useSandbox = true;</code>。自 NixOS 17.09 起,<code>nix.useSandbox</code> 选项默认为 <code>true</code>
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.
</div>


<span id="Alternative_Interpreters"></span>
<span id="Alternative_Interpreters"></span>
=== 可选的解释器 ===
=== 可选的解释器 ===


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


<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 21:35, 28 August 2025

Introduction to Nix

Tools and applications

⤧︎
Disambiguation: 本条目介绍的是 Nix 软件包管理器。请勿与 Nix 生态系统Nix 语言Nix 命令 混淆。

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

↱︎
This section is a candidate for splitting off into a separate article. nix store 在概念上有足够的独立性,值得单独成文。 For more information, consult the related discussion page.

由 Nix 构建的软件包会被放置在只读的 Nix store 中,通常位于 /nix/store。每个软件包都会被赋予一个唯一的地址,该地址由加密哈希值、软件包名称和版本组成,例如 /nix/store/nawl092prjblbhvv16kxxbk6j9gkgcqm-git-2.14.1。这些前缀对构建过程中的所有输入进行哈希,包括源文件、完整的依赖树、编译器参数等。这使得 Nix 能够同时安装同一软件包的不同版本,甚至是同一版本的不同构建(例如使用不同编译器构建的变体)。在添加、移除或更新软件包时,存储中的内容不会被删除;相反,profiles 中指向这些软件包的符号链接会被添加、移除或更改。

清理 Nix store

有关清理 Nix store 的信息,请参阅 NixOS 手册:章节 - 清理 Nix 存储

Nix store 损坏

有关修复 Nix store 损坏的信息,请参阅 NixOS 手册:章节 - Nix store 损坏

有效的 Nix store 名称

Main article: Valid Nix store path names

Profiles

為了構建一個一致的用戶和系統環境,Nix 會將 Nix store 中的條目符號連結到 profiles 中。這是 Nix 實現回滾的前端:由於 store 是不可變的且保留先前版本的 profile,將系統還原到先前的狀態只需簡單地將符號連結更改為先前的 profile。更精確地說,Nix 將二進位檔符號連結到 Nix store 中代表用戶環境的條目中。這些用戶環境隨後被符號連結到儲存在 /nix/var/nix/profiles 中被標注的 profile,而它們則進一步符號連結到使用者的 ~/.nix-profile

沙盒

当启用沙盒构建时,Nix 会为每个构建过程设置一个隔离的环境。这样可以去除构建环境中额外的隐藏依赖,以提高可复现性。这包括在构建过程中禁止访问 fetch* 函数之外的网络,以及禁止访问 Nix store 之外的文件。根据操作系统的不同,对其他资源的访问也会被阻止(例如在 Linux 上进程间通信会被隔离);详细信息请参阅 Nix 手册中的 nix.conf 章节

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

要为 Nix 配置沙盒,请在 /etc/nix/nix.conf 中设置 sandbox = true;要为 NixOS 配置沙盒,请在 configuration.nix 中设置 nix.useSandbox = true;。自 NixOS 17.09 起,nix.useSandbox 选项默认为 true

可选的解释器

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

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

早期的尝试可以在 riir-nix 找到。

备注

  1. 值在计算过程中不会改变。当函数的输入不变时,函数总是输出相同的结果。