Jump to content

Nix (package manager)/zh-hans: Difference between revisions

From NixOS Wiki
Loxodoromy (talk | contribs)
Created page with "在其他Linux发行版或macOS上,你可以按照[https://nixos.org/manual/nix/stable/installation/installation Nix手册中的安装部分]来安装Nix。"
Loxodoromy (talk | contribs)
Created page with "==== 有效的Nix存储名称 ===="
 
(19 intermediate revisions by the same user not shown)
Line 1: Line 1:
<languages/>
<languages/>
{{Navbox Nix}}
{{Navbox Nix}}
{{消歧义|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 Expression Language|Nix表达式语言]] (一种惰性求值的纯函数式语言) 指定的可复现的构建指令。Nix表达式是纯函数<ref>值在计算过程中不会改变。当输入不变时,函数总是得到相同的输出。</ref>,它接受依赖作为参数,并为该软件包生成一个指定可复现构建环境的''[[Derivations|derivation]]''。Nix把构建结果存储在由整个依赖树的哈希值指定的唯一地址,从而创建一个不可变的包存储 (即[[#Nix store|nix store]]),使得原子升级、回滚、同时安装同一软件包的不同版本成为可能,从根本上消除了[https://en.wikipedia.org/wiki/Dependency_hell 依赖地狱]。  


Line 12: Line 12:
在其他Linux发行版或macOS上,你可以按照[https://nixos.org/manual/nix/stable/installation/installation Nix手册中的安装部分]来安装Nix。
在其他Linux发行版或macOS上,你可以按照[https://nixos.org/manual/nix/stable/installation/installation Nix手册中的安装部分]来安装Nix。


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Nix_commands"></span>
=== Nix commands ===
=== Nix命令 ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
[[Nix (command line utilities)|Nix命令]][https://nixos.org/manual/nix/stable/command-ref/command-ref Nix参考手册]中有详细说明,分为主要命令、工具命令和实验性命令。在2.0版本(发布于2018年2月)之前,命令有所不同。
The [[Nix (command line utilities)|Nix commands]] are documented in the [https://nixos.org/manual/nix/stable/command-ref/command-ref Nix reference manual]: main commands, utilities and experimental commands. Prior to version 2.0 (released in February 2018) there have been different commands.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
=== 配置 ===
=== Configuration ===
在NixOS上,Nix可通过[https://search.nixos.org/options?query=nix. <code>nix</code>选项]配置。
On NixOS, Nix can be configured using the [https://search.nixos.org/options?query=nix. <code>nix</code> option].
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
独立的Nix通过<code>nix.conf</code>(通常在<code>/etc/nix/</code>下)配置。可用选项的细节[https://nixos.org/manual/nix/stable/command-ref/conf-file 在Nix参考手册中]
Standalone Nix is configured through <code>nix.conf</code> (usually found in <code>/etc/nix/</code>). Details on the available options are [https://nixos.org/manual/nix/stable/command-ref/conf-file found in the Nix reference manual].
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
[[Home Manager]]为单一用户管理声明式环境,也可用于配置Nix。对于系统级配置,可在Linux上使用System Manager,在macOS上使用nix-darwin。
You can also configure Nix using [[Home Manager]], which manages declarative environments for a single user. For system-wide configuration, you can use [https://github.com/numtide/system-manager System Manager] on Linux and [https://github.com/LnL7/nix-darwin nix-darwin] on macOS.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Internals"></span>
== Internals ==
== 内部细节 ==
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Nix_store"></span>
=== Nix store ===
=== Nix存储(Nix store) ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
{{Split|reason=Nix存储在概念上足够独立,有必要单独成文。}}
{{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存储'',正常情况下在<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存储 ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
有关清理Nix存储的信息,参考{{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存储损坏 ====
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
有关修复损坏的Nix存储的信息,参考{{NixOS Manual|name=NixOS手册:章节 - Nix存储损坏|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存储名称 ====
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
{{main|有效的Nix存储名称}}
{{main|Valid Nix store path names}}
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Profiles"></span>
=== Profiles ===
=== 配置文件(Profiles) ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
为了搭建一致的用户或系统环境,Nix将Nix存储的条目符号链接到''配置文件''。这是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">
<span id="Sandboxing"></span>
=== Sandboxing ===
=== 沙盒化 ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
当启用沙盒构建时,Nix会为每个构建过程设置一个隔离的环境。它用于移除构建环境中额外的隐藏依赖,以提高可复现性。这包括在构建过程中禁止<code>fetch*</code>函数之外对网络的访问,和Nix存储之外对文件的访问。根据操作系统的不同,对其他资源的访问也会被阻止(例如,进程间通信在Linux上被隔离);详细信息见Nix手册里的[https://nixos.org/nix/manual/#sec-conf-file nix.conf部分]
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.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
沙盒化在Linux上默认启用,在macOS上默认禁用。
Sandboxing is enabled by default on Linux, and disabled by default on macOS.
Nixpkgs的拉取请求(pull requests)中,要求在沙盒化启用条件下测试构建(见拉取请求模板中的<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>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Alternative_Interpreters"></span>
=== Alternative Interpreters ===
=== 可选的解释器 ===
</div>


<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">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 110: Line 73:
</div>
</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">
<div lang="en" dir="ltr" class="mw-content-ltr">
Line 118: Line 79:
</div>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
更早的尝试可在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>





Latest revision as of 12:47, 26 September 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参考手册中有详细说明,分为主要命令、工具命令和实验性命令。在2.0版本(发布于2018年2月)之前,命令有所不同。

配置

在NixOS上,Nix可通过nix选项配置。

独立的Nix通过nix.conf(通常在/etc/nix/下)配置。可用选项的细节在Nix参考手册中

Home Manager为单一用户管理声明式环境,也可用于配置Nix。对于系统级配置,可在Linux上使用System Manager,在macOS上使用nix-darwin。

内部细节

Nix存储(Nix store)

↱︎
This section is a candidate for splitting off into a separate article. Nix存储在概念上足够独立,有必要单独成文。 For more information, consult the related discussion page.

Nix构建得到的包放在只读的Nix存储,正常情况下在/nix/store。每个包被赋予唯一地址,该地址由加密哈希值后跟包名称和版本指定,比如/nix/store/nawl092prjblbhvv16kxxbk6j9gkgcqm-git-2.14.1。前缀哈希值来自对构建过程中所有输入的哈希,包括源文件、整个依赖树、编译器标志等。这让Nix能同时安装同一个包的不同版本,甚至同一版本的不同构建,比如不同编译器构建的变体。在添加、移除或更新包时,不会从存储中移除任何包,而是在配置文件(profiles)里添加、移除或修改指向这些包的符号链接。

清理Nix存储

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

Nix存储损坏

有关修复损坏的Nix存储的信息,参考 NixOS手册:章节 - Nix存储损坏

有效的Nix存储名称

Main article: 有效的Nix存储名称

配置文件(Profiles)

为了搭建一致的用户或系统环境,Nix将Nix存储的条目符号链接到配置文件。这是Nix允许回滚功能的前端:由于存储不可变,先前版本的配置文件被保留,恢复到更早的状态只需要改变符号链接到先前的配置文件。更精确地说,Nix将二进制文件符号链接到Nix存储中表示用户环境的条目。然后这些用户环境被符号链接到/nix/var/nix/profiles里被标记的配置文件,后者又被符号链接到该用户的~/.nix-profile

沙盒化

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

沙盒化在Linux上默认启用,在macOS上默认禁用。 Nixpkgs的拉取请求(pull requests)中,要求在沙盒化启用条件下测试构建(见拉取请求模板中的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. 值在计算过程中不会改变。当输入不变时,函数总是得到相同的输出。