Jump to content

Nix (package manager)/zh: Difference between revisions

From Official NixOS Wiki
Weijia (talk | contribs)
Created page with "早期尝试可以在 [https://riir-nix.github.io/ riir-nix] 找到"
Ardenet (talk | contribs)
No edit summary
Tags: Mobile edit Mobile web edit
 
(36 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<languages/>
<languages/>
{{Navbox Nix}}
{{Navbox Nix}}
{{Disambiguation|message=本条目介绍的是 Nix 软件包管理器。请勿与 [[Nix ecosystem|Nix 生态系统]]、[[Nix (language)|Nix 语言]] 或 [[Nix (command)|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 依赖地狱]。  
 
Nix 是一个软件包管理器和构建系统,它解析由 [[Nix (language)|Nix 表达式语言]] (惰性求值的纯函数式语言) 指定的可复现的构建指令。Nix 表达式是纯函数<ref>值在计算过程中不会改变。当函数的输入不变时,函数总是输出相同的结果。</ref>,它接受依赖作为参数,并生成一个 ''[[Derivations|derivation]]'',用于指定该软件包的可复现的构建环境。Nix 把构建结果存储在由完整依赖树的哈希值指定的唯一地址中,从而创建了一个不可变的包存储 (即 [[#Nix store|nix store]]),使得原子升级、回滚、以及同时安装同一软件包的不同版本成为可能,从根本上消除了 [[Wikipedia:Dependency hell|依赖地狱]]。


<span id="Usage"></span>
<span id="Usage"></span>
== 用法 ==
== 用法 ==


<span id="Installation"></span>
=== 安装 ===
=== 安装 ===
在 [[NixOS]] 上,Nix 已经自动安装好了。


如果想在其他 Linux 发行版或在 macOS 上使用 Nix, 可以按照 [https://nixos.org/manual/nix/stable/installation/installation Nix 手册的安装章节]来安装 Nix。
在 [[NixOS|NixOS]] 上,Nix 已经自动安装好了。
 
如果想在其他 Linux 发行版或在 macOS 上使用, 可以按照 [https://nix.dev/manual/nix/stable/installation/ Nix 手册的安装章节] 来安装 Nix。


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


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


<span id="Configuration"></span>
=== 配置 ===
=== 配置 ===
在 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 参考手册]中找到.
在 NixOS 上, Nix 可以通过 [https://search.nixos.org/options?query=nix <code>nix</code> 选项] 进行配置。  


你也可以使用 [[Home Manager]] 配置 Nix,它为单用户管理声明式环境。对于系统范围的配置,可以在 Linux 上使用 [https://github.com/numtide/system-manager System Manager],在 macOS 上使用 [https://github.com/LnL7/nix-darwin nix-darwin]。
独立安装的 Nix 通过 <code>nix.conf</code> 进行配置(其通常位于 <code>/etc/nix/</code>)。可用选项的详细说明可在 [https://nix.dev/manual/nix/stable/command-ref/conf-file Nix 参考手册] 中找到.
 
你也可以使用 [[Home Manager|Home Manager]] 配置 Nix,它为单用户提供管理声明式环境的能力。对于系统范围的配置,可以在 Linux 上使用 [https://github.com/numtide/system-manager System Manager],在 macOS 上使用 [https://github.com/nix-darwin/nix-darwin nix-darwin]。


<span id="Internals"></span>
<span id="Internals"></span>
== 内部组成 ==
== 内部机制 ==


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


<div lang="en" dir="ltr" class="mw-content-ltr">
{{Split|reason=Nix store 在概念上有足够的独立性,值得单独成文。}}
{{Split|reason=The nix store is conceptually separate enough that it warrants a separate article.}}
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
Nix 构建的软件包会被放置在只读的 ''Nix store'' 中,通常位于 <code>/nix/store</code>。每个软件包都会被赋予一个唯一的地址,该地址由加密哈希值、软件包名称和版本组成,例如 <code>/nix/store/nawl092prjblbhvv16kxxbk6j9gkgcqm-git-2.14.1</code>。这些前缀对构建过程中的所有输入进行哈希,包括源文件、完整的依赖树、编译器参数等。这使得 Nix 能够同时安装同一软件包的不同版本,甚至是同一版本的不同构建(例如使用不同编译器构建的变体)。在添加、移除或更新软件包时,存储中的内容不会被删除;相反,''profiles'' 中指向这些软件包的符号链接会被添加、移除或更改。
Packages built by Nix are placed in the read-only ''Nix store'', normally found in <code>/nix/store</code>. Each package is given a unique address specified by a cryptographic hash followed by the package name and version, for example <code>/nix/store/nawl092prjblbhvv16kxxbk6j9gkgcqm-git-2.14.1</code>. 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''.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Cleaning_the_Nix_store"></span>
==== Cleaning the Nix store ====
==== 清理 Nix store ====
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
有关清理 Nix store 的信息,请参阅 {{NixOS Manual|name=NixOS 手册:章节 - 清理 Nix 存储|anchor=#sec-nix-gc}}
For information relating to cleaning the Nix store, refer to {{NixOS Manual|name=NixOS Manual: Chapter - Cleaning the Nix Store|anchor=#sec-nix-gc}}.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Nix_store_corruption"></span>
==== Nix store corruption ====
==== Nix store 损坏 ====
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
有关修复 Nix store 损坏的信息,请参阅 {{NixOS Manual|name=NixOS 手册:章节 - Nix store 损坏|anchor=#sec-nix-store-corruption}}
For information relating to fixing a corrupted Nix store, refer to {{NixOS Manual|name=NixOS Manual: Chapter - Nix Store Corruption|anchor=#sec-nix-store-corruption}}.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Valid_Nix_store_names"></span>
==== Valid Nix store names ====
==== 有效的 Nix store 名称 ====
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
{{main|Valid Nix store path names}}
{{main|Valid Nix store path names}}
</div>


<span id="Profiles"></span>
=== Profiles ===
=== Profiles(系统画像) ===


为了建一个连的用户或系统环境, Nix将Nix存储库中的条目符号链接到''profile''. 这些是Nix允许回滚的"前端": 因为存储库是不可变的, 而的profile版本被保留了下来, 回更早的状态只需要简单地让符号链接指向一个先前的profile. 确地说, Nix将二进制文件符号链接到Nix存储库中描述用户环境的条目中. 这些用户环境随符号链接到<code>/nix/var/nix/profiles</code>标签的profile, 而这些profile又被符号链接到用户的<code>~/.nix-profile</code>.
为了建一个连的用户或系统环境,Nix 会将 Nix 存储库中的条目链接到 ''配置(profiles)'' 中。这些配置文件是 Nix 实现回滚的前端:由于存储库是不可变的,并会保留配置文件的先前版本,因此恢复之前的状态只需更改指向先前配置文件的符号链接即可。确地说,Nix 会将二进制文件链接到 Nix 存储库中代表用户环境的条目这些用户环境随后会链接到存储在 <code>/nix/var/nix/profiles</code> 目录下的带标签的配置文件中,而这些配置文件链接到用户的 <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 上,进程间通信也是被隔离的)。


<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] 的拉取请求中,提交者被要求启用沙盒进行构建测试(请参阅拉取请求模板中的 <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>
=== 可选的解释器 ===
=== 可选的解释器 ===


目前正在行一項計畫始使用 Rust 重新實作 Nix。
目前正在行一项计划始使用 Rust 重新实现 Nix。


* [https://cs.tvl.fyi/depot/-/tree/tvix tvix]
* [https://code.tvl.fyi/tree/tvix tvix]


有一個社群領導的 Nix 2.18 分支叫做 Lix性、用性和成長性。同時還為 Nix 部份組加入 Rust 端口,但不像始重Tvix
Nix 2.18 还有一个名为 Lix 的社区分支,性、用性和可扩展性。虽然也把 Nix 的一些组移植到了 Rust,但不像 Tvix 那样是从始重的。


* [https://lix.systems/ lix]
* [https://lix.systems/ lix]


早期尝试可以在 [https://riir-nix.github.io/ riir-nix] 找到
早期尝试可以在 [https://riir-nix.github.io/ riir-nix] 找到
 
<span id="Notes"></span>
== 备注 ==


<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 15:52, 2 June 2026

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 参考手册 中有详细记录:主要命令、工具命令和实验性命令。在 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 存储库中的条目软链接到 配置(profiles) 中。这些配置文件是 Nix 实现回滚的前端:由于存储库是不可变的,并且会保留配置文件的先前版本,因此恢复到之前的状态只需更改指向先前配置文件的符号链接即可。更准确地说,Nix 会将二进制文件软链接到 Nix 存储库中代表用户环境的条目。这些用户环境随后会被软链接到存储在 /nix/var/nix/profiles 目录下的带标签的配置文件中,而这些配置文件又会被软链接到用户的 ~/.nix-profile 文件。

沙盒

启用沙盒构建后,Nix 将为每个构建过程设置一个隔离环境。这用于移除构建环境中的的其它隐藏依赖项,以提高构建结果的可复现性。这包括在构建过程中对 fetch* 函数之外的网络访问,以及对 Nix Store 之外的文件访问的不可行。根据操作系统的不同,对其他资源的访问也会被阻止(例如,在 Linux 上,进程间通信也是被隔离的)。

Linux 系统默认启用沙盒,而 macOS 系统则默认禁用沙盒。 在 Nixpkgs 的拉取请求中,提交者被要求启用沙盒进行构建测试(请参阅拉取请求模板中的 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. 值在计算过程中不会改变。当函数的输入不变时,函数总是输出相同的结果。