NixOS/zh: Difference between revisions

Weijia (talk | contribs)
Created page with "''Main Article: Nix vs. Linux Standard Base''"
Tags: Mobile edit Mobile web edit
Weijia (talk | contribs)
No edit summary
 
(29 intermediate revisions by the same user not shown)
Line 29: Line 29:


<span id="32-bit_x86_architectures"></span>
<span id="32-bit_x86_architectures"></span>
==== 32位 x86 架构 ====
==== 32 位 x86 架构 ====


于32位 x86 架构(即 <code>i686</code>)的支持正在减少。大多数包仍然可以编译和运行,但它们的缓存可用性显著降低<ref>https://discourse.nixos.org/t/limited-cache-availability-for-i686-32-bits-x86-architecture/37626</ref>。32位 x86 架构不再提供预构建的 ISO 镜像文件,但其仍可手动构建。
于 32 位 x86 架构即 <code>i686</code>的支持正在减少。大多数包仍然可以编译和运行,但它们的缓存可用性显著降低<ref>https://discourse.nixos.org/t/limited-cache-availability-for-i686-32-bits-x86-architecture/37626</ref>。32 位 x86 架构不再提供预构建的 ISO 镜像文件,但其仍可手动构建。


<span id="64-bit_x86_architectures"></span>
<span id="64-bit_x86_architectures"></span>
==== 64位 x86 架构 ====
==== 64 位 x86 架构 ====


大多数 <code>x86_64</code> 设备都能顺利运行 NixOS。
大多数 <code>x86_64</code> 设备都能顺利运行 NixOS。


<span id="32-bit_ARM_architectures"></span>
<span id="32-bit_ARM_architectures"></span>
==== 32位 ARM 架构 ====
==== 32 位 ARM 架构 ====


{{Main|NixOS on ARM}}
{{Main|NixOS on ARM}}
Line 45: Line 45:


<span id="64-bit_ARM_architectures"></span>
<span id="64-bit_ARM_architectures"></span>
==== 64位 ARM 架构 ====
==== 64 位 ARM 架构 ====


{{Main|NixOS on ARM}}
{{Main|NixOS on ARM}}
Line 98: Line 98:
''Main Article: [[Special:MyLanguage/Nix vs. Linux Standard Base|Nix vs. Linux Standard Base]]''
''Main Article: [[Special:MyLanguage/Nix vs. Linux Standard Base|Nix vs. Linux Standard Base]]''


<div lang="en" dir="ltr" class="mw-content-ltr">
NixOS 与其他 Linux 发行版的主要区别在于,NixOS 不遵循 [https://en.wikipedia.org/wiki/Linux_Standard_Base Linux 标准规范(LSB)] 的文件系统结构。在遵循 LSB 的系统中,软件通常存储在 <code>/{,usr}/{bin,lib,share}</code> 目录下,而配置文件通常存储在 <code>/etc</code>。如果程序的可执行文件被放置在 LSB 的某个 <code>/bin</code> 目录下,那么它就可以在用户环境中被访问。当一个程序引用动态链接库时,它将在 LSB 目录(<code>/lib</code><code>/usr/lib</code>)中搜索所需的库。
The main difference between NixOS and other Linux distributions is that NixOS does not follow the [https://en.wikipedia.org/wiki/Linux_Standard_Base Linux Standard Base] file system structure. On LSB-compliant systems software is stored under <code>/{,usr}/{bin,lib,share}</code> and configuration is generally stored in <code>/etc</code>. Software binaries are available in the user environment if they are placed in one of the LSB's <code>/bin</code> directories. When a program references dynamic libraries it will search for the required libraries in the LSB folders (<code>/lib</code>, <code>/usr/lib</code>).
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
然而,在 NixOS 中,<code>/lib</code> <code>/usr/lib</code> 目录并不存在。相反,所有系统库文件、可执行文件、内核、固件和配置文件都存放在 [[Special:MyLanguage/Nix#Nix store|Nix store]] 中。<code>/nix/store</code> 下的文件和目录均由构建数据的描述信息经哈希后命名。所有位于 Nix Store 的文件与目录均不可变。
In NixOS however <code>/lib</code> and <code>/usr/lib</code> do not exist. Instead all system libraries, binaries, kernels, firmware and configuration files are placed in the [[Special:MyLanguage/Nix#Nix store|Nix store]]. The files and directories in <code>/nix/store</code> are named by hashes of the information describing the built data. All of the files and directories placed in the Nix store are immutable. <code>/bin</code> and <code>/usr/bin</code> are almost absent: they contain only <code>/bin/sh</code> and <code>/usr/bin/env</code> respectively, to provide minimal compatibility with existing scripts using shebang lines. User-level environments are implemented using a large number of symbolic links to all required packages and auxiliary files. These environments are called [[Special:MyLanguage/Nix#Profiles|profiles]] and are stored in <code>/nix/var/nix/profiles</code>, each user having their own profiles. Structuring the system in this way is how NixOS obtains its key advantages over conventional Linux distributions, such as atomicity and rollback support.
<code>/bin</code> <code>/usr/bin</code> 几乎不存在:它们分别只包含 <code>/bin/sh</code> <code>/usr/bin/env</code>,以提供与使用 shebang 行的现有脚本的最低兼容性。用户级环境通过大量指向所需软件包和辅助文件的符号链接来实现。
</div>
这些环境被称作 [[Special:MyLanguage/Nix#Profiles|Profiles]],被存储于 <code>/nix/var/nix/profiles</code>,每个用户拥有其独有的 Profiles。
通过这种结构化的系统组织方式,NixOS 获得了优于传统 Linux 发行版的核心优势,例如原子化操作和回滚支持。


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Usage_of_the_Nix_store"></span>
=== Usage of the Nix store ===
=== Nix Store 的使用 ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
新手的大多数困惑源于配置文件和所有已安装软件包均存储于只读的 <code>/nix/store</code> 目录树下这一事实。这实际上使得手动编辑系统配置变得不可能;所有配置更改必须通过编辑 <code>/etc/nixos/configuration.nix</code> 文件并运行 <code>nixos-rebuild switch</code> 后才可生效。NixOS 提供了[[Special:MyLanguage/NixOS_modules|模块系统]]以供编辑所有需要的配置。用户应在期望通过低层级的 NixOS 功能(如 activation scripts)手动添加文件或配置之前,首先使用[https://search.nixos.org/options 配置选项搜索工具]检查所需选项是否已存在。
A lot of confusion for newcomers arises from the fact that configuration is stored in the read-only <code>/nix/store</code> tree along with all the installed packages. This fact makes it impossible to manually edit system configuration; all configuration changes must be performed by editing the <code>/etc/nixos/configuration.nix</code> file and executing <code>nixos-rebuild switch</code>. NixOS provides the [[Special:MyLanguage/NixOS_modules|module system]] for editing all required configurations. Users should first use [https://search.nixos.org/options the option search tool] to check if the option they need exists before attempting to manually add files or configuration via low-level NixOS features like activation scripts.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
系统的纯粹性(purity)使得系统配置的集中存储成为可能,而无需编辑多个文件。此配置方式可以根据需要进行分发或版本控制。它还提供了确定性:如果您提供相同的输入、相同版本的 Nixpkgs 和相同的 <code>/etc/nixos/configuration.nix</code> 文件,您将得到完全相同的系统状态。
The system purity makes it possible to keep system configuration in a central place, without the need to edit multiple files. This configuration can be distributed or version controlled as desired. It also provides for determinism; if you provide the same inputs, the same version of Nixpkgs and the same <code>/etc/nixos/configuration.nix</code> you will get the exact same system state.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Modules"></span>
=== Modules ===
=== 模块 ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
定义于 [[Special:MyLanguage/Nixpkgs|Nixpkgs]] [[Special:MyLanguage/NixOS modules|NixOS 模块系统]] 提供了定制操作系统配置所需的方法。通常被用于启用并定制如 Nginx 这样的服务、启用固件以及定制内核。
The [[Special:MyLanguage/NixOS modules|NixOS module system]] as defined in  [[Special:MyLanguage/Nixpkgs|Nixpkgs]] provides the means necessary to customize the configuration of the OS. It is used to enable and customize services such as nginx, enable firmware and customize the kernel.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
所有模块配置通常都是通过向 <code>/etc/nixos/configuration.nix</code> 文件添加选项来完成。维基上的大多数示例都展示了如何使用此文件来配置操作系统。
All module configuration is generally performed by adding options to <code>/etc/nixos/configuration.nix</code>. Most of the examples in the wiki show how this file can be used to configure the OS.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
NixOS 的模块系统实现了一个类型系统,以允许对选项设置进行类型检查。它还支持将多处定义的选项进行自动合并。这使得您可以将配置分散到多个文件中,而您在这些文件中设置的选项最终都会被合并在一起:
The NixOS module system implements a typing system which allows typechecking of option settings. It also enables options defined in multiple places to be merged automatically. This allows you to spread your configuration over multiple files, and the options you set across all of those files will be merged together:
</div>


{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
Line 158: Line 145:
</nowiki>}}
</nowiki>}}


<div lang="en" dir="ltr" class="mw-content-ltr">
请参阅 [https://nixos.org/nixos/manual/index.html#sec-writing-modules NixOS 手册的模块部分] 以获取更多详细信息。
See the [https://nixos.org/nixos/manual/index.html#sec-writing-modules Modules section of the NixOS Manual] for more details.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Generations"></span>
=== Generations ===
=== 世代 ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
每当使用 <code>nixos-rebuild switch</code> 命令重构系统状态时,都将创建一个新的世代(generation)。您可以随时回滚到上一个世代,这在配置更改(或系统更新)被证明是有害时非常有用。
Every time the system state is rebuilt using <code>nixos-rebuild switch</code>, a new generation is created. You can revert to the previous generation at any time, which is useful if a configuration change (or system update) turns out to be detrimental.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
您可以通过以下命令回滚:
You can roll back via:
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang=shell>
<syntaxHighlight lang=shell>
$ nix-env --rollback              # roll back a user environment
$ nix-env --rollback              # 回滚用户环境
$ nixos-rebuild switch --rollback  # roll back a system environment
$ nixos-rebuild switch --rollback  # 回滚系统环境
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
NixOS 还会在引导加载程序菜单中放置前几代系统的条目,因此,作为最后的手段,您可以通过重启来恢复到之前的配置。将当前启动的版本设置为默认运行:
NixOS also places entries for previous generations in the bootloader menu, so as a last resort you can always revert to a previous configuration by rebooting. To set the currently booted generation as the default run
</div>


<syntaxHighlight lang=shell>
<syntaxHighlight lang=shell>
Line 189: Line 165:
</syntaxHighlight>
</syntaxHighlight>


<div lang="en" dir="ltr" class="mw-content-ltr">
由于 NixOS 会保留前几代系统状态以供回滚,因此更新后不会立即从系统中删除旧软件包版本。您可以手动删除旧版本:
Because NixOS keeps previous generations of system state available in case rollback is desired, old package versions aren't deleted from your system immediately after an update. You can delete old generations manually:
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<syntaxHighlight lang=shell>
<syntaxHighlight lang=shell>
# delete generations older than 30 days
# 删除早于 30 天前的世代
$ nix-collect-garbage --delete-older-than 30d
$ nix-collect-garbage --delete-older-than 30d
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
# 删除之前的所有世代 - 运行此命令后您将无法回滚
# delete ALL previous generations - you can no longer rollback after running this
$ nix-collect-garbage -d                       
$ nix-collect-garbage -d                       
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
List generations:
List generations:
<syntaxHighlight lang=shell>
<syntaxHighlight lang=shell>
# as root
# root 用户身份
$ nix-env --list-generations --profile /nix/var/nix/profiles/system
$ nix-env --list-generations --profile /nix/var/nix/profiles/system
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
Switch generations:
Switch generations:
<syntaxHighlight lang=shell>
<syntaxHighlight lang=shell>
# as root switch to generation 204
# root 用户身份切换至 204 世代
$ nix-env --profile /nix/var/nix/profiles/system --switch-generation 204
$ nix-env --profile /nix/var/nix/profiles/system --switch-generation 204
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
delete broken generation(s):
delete broken generation(s):
<syntaxHighlight lang=shell>
<syntaxHighlight lang=shell>
# as root delete broken generations 205 and 206  
# root 用户身份删除损坏的 205 206 世代
$ nix-env --profile /nix/var/nix/profiles/system --delete-generations 205 206
$ nix-env --profile /nix/var/nix/profiles/system --delete-generations 205 206
</syntaxHighlight>
</syntaxHighlight>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
您可以通过设置 <code>/etc/nixos/configuration.nix</code> 中的 [https://search.nixos.org/options?query=nix.gc nix.gc] 选项来配置自动垃圾回收。这是推荐操作,因为它可以降低 Nix Store 的大小。
You can configure automatic garbage collection by setting the [https://search.nixos.org/options?query=nix.gc nix.gc] options in <code>/etc/nixos/configuration.nix</code>. This is recommended, as it keeps the size of the Nix store down.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="See_also"></span>
== See also ==
== 另见 ==
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
* [[Special:MyLanguage/NixOS modules|NixOS 模块]] - 一个用于模块化 [[Special:MyLanguage/Overview of the Nix Expression Language#Expressions|Nix 表达式]]的库,为 [[#declarative-configuration|NixOS 的声明式配置]] 提供支持。
* [[Special:MyLanguage/NixOS modules|NixOS modules]], a library for modular [[Special:MyLanguage/Overview of the Nix Expression Language#Expressions|Nix expressions]] which powers [[#declarative-configuration|the declarative configuration of NixOS]].
* [[Special:MyLanguage/NixOS VM tests|NixOS VM 测试]] - 一个基于  [[Special:MyLanguage/Nixpkgs|Nixpkgs]][[Special:MyLanguage/NixOS|NixOS]]、QEMU 和 Perl 用于创建可复现基础设施测试的库。
* [[Special:MyLanguage/NixOS VM tests|NixOS VM tests]], a library for creating reproducible infrastructure tests, based on [[Special:MyLanguage/Nixpkgs|Nixpkgs]], [[Special:MyLanguage/NixOS|NixOS]], QEMU and Perl.
* [https://github.com/ryan4yin/nixos-and-flakes-book NixOS & Flakes Book] (Ryan4yin, 2023) - 🛠️ ❤️ 一本非官方的 NixOS & Flakes 新手入门书籍。
* [https://github.com/ryan4yin/nixos-and-flakes-book NixOS & Flakes Book] (Ryan4yin, 2023) - 🛠️ ❤️ An unofficial NixOS & Flakes book for beginners.
</div>


[[Category:Pedias]]
[[Category:Pedias]]
[[Category:NixOS]]
[[Category:NixOS]]
[[Category:Nix{{#translation:}}]]
[[Category:Nix{{#translation:}}]]