[https://nixos.org/ NixOS] is a Linux distribution based on the [[Special:MyLanguage/Nix|Nix]] package manager and build system. It supports [https://en.wikipedia.org/wiki/Declarative_programming declarative] system-wide [https://en.wikipedia.org/wiki/Configuration_management configuration management] as well as [https://en.wikipedia.org/wiki/Atomicity_(database_systems) atomic] upgrades and rollbacks, although it can additionally support [https://en.wikipedia.org/wiki/Imperative_programming imperative] package and user management. In NixOS, all components of the distribution — including the [https://en.wikipedia.org/wiki/Linux_kernel kernel], installed [https://en.wikipedia.org/wiki/Package_manager packages] and system configuration files — are built by [[Special:MyLanguage/Nix|Nix]] from [[Wikipedia:Pure function|pure functions]] called [[Special:MyLanguage/Nix Expression Language|Nix expressions]].
Since Nix uses [https://en.wikipedia.org/wiki/Executable binary] caching, this provides a unique compromise between the binary-oriented approach used by distributions such as Debian and the [https://en.wikipedia.org/wiki/Source_code source]-oriented approach used by distributions such as Gentoo. Binaries can be used for standard components, and custom-built packages and modules can be used automatically when a pre-built binary is not available.
Stable NixOS releases are delivered twice a year (around the end of May and the end of November). NixOS was created by [https://edolstra.github.io/ Eelco Dolstra] and [https://en.wikipedia.org/wiki/Armijn_Hemel Armijn Hemel], and initially released in 2003. It is community developed and maintained under the stewardship of the [[Special:MyLanguage/Nix_Community#NixOS_Foundation|NixOS Foundation]].
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Installation"></span>
== Installation ==
== 安装 ==
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
完整的安装指南请参阅 [https://nixos.org/nixos/manual/index.html#ch-installation NixOS 手册的 Installation 章节]。此维基还包含替代或补充指南,例如 [[Special:MyLanguage/NixOS as a desktop|桌面设备上的 NixOS]]。
For a full installation guide, see the [https://nixos.org/nixos/manual/index.html#ch-installation Installation chapter of the NixOS manual]. This wiki also includes alternative or supplemental guides, such as [[Special:MyLanguage/NixOS as a desktop|NixOS as a desktop]].
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
大多数用户通过 [https://nixos.org/download/#nixos-iso 任一 ISO 镜像] 安装 NixOS。每个支持架构均有 “graphical”(图形化安装)和 “minimal”(最小化安装)两种 ISO 变体;“graphical” 镜像适用于计划安装桌面环境的用户,而 “minimal” 镜像适用于计划将 NixOS 充当服务器或期望更小 ISO 镜像文件的用户。ISO 镜像为混合镜像,可以刻录到光盘介质或原封不动地复制到 USB 驱动器上并直接启动。请参阅安装指南以了解详情。
Most users will install NixOS via [https://nixos.org/download/#nixos-iso one of the ISO images.] Both "graphical" and "minimal" ISO variants are available for each supported architecture; the "graphical" images are suitable for users intending to install a desktop environment, and the "minimal" images are suitable for users intending to install NixOS in a server role or desiring a smaller ISO image. The ISO images are hybrid images which can be burnt to optical media or copied raw to a USB drive and booted as-is. See the installation guide for details.
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
除了 ISO 镜像,[https://nixos.org/download/#nixos-iso 下载页面] 还提供了多种安装 NixOS 的替代方法。这些方法包括:
In addition to the ISO images, the [https://nixos.org/download/#nixos-iso download page] provides a number of alternative methods for installing NixOS. These include:
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
* OVA 格式的虚拟机(兼容 VirtualBox);
* Virtual appliances in OVA format (compatible with VirtualBox);
Additionally, many existing Linux installations can be converted into NixOS installations using [https://github.com/elitak/nixos-infect nixos-infect] or [https://github.com/jeaye/nixos-in-place nixos-in-place]; this is particularly useful for installing NixOS on hosting providers which do not natively support NixOS.
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="System_architectures"></span>
=== System architectures ===
=== 系统架构 ===
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
NixOS 对大多数 x86_64 设备和通用 ARM64 设备提供了开箱即用的支持。
NixOS provides out of the box support for most x86_64 devices, and generic ARM64 devices.
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="32-bit_x86_architectures"></span>
==== 32-bit x86 architectures ====
==== 32 位 x86 架构 ====
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
对于 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 镜像文件,但其仍可手动构建。
Support for 32-bit x86 architectures (i.e. <code>i686</code>) has been declining. While most packages should still compile and run, their cache availability is significantly reduced<ref>https://discourse.nixos.org/t/limited-cache-availability-for-i686-32-bits-x86-architecture/37626</ref>. The 32-bit x86 ISO is no longer offered as a ready-built image, but it may still be built manually.
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="64-bit_x86_architectures"></span>
==== 64-bit x86 architectures ====
==== 64 位 x86 架构 ====
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
大多数 <code>x86_64</code> 设备都能顺利运行 NixOS。
Most <code>x86_64</code> devices should run NixOS without issues.
NixOS isn't officially supported on ARM32 devices (e.g. <code>armv6</code> and <code>armv7l</code>), however, for some of these devices, there may be community support.
As long as a device supports the generic systemd boot process, NixOS should run out of the box. However, specific devices with proprietary bootloaders may have issues running it.
There used to be limited support for MIPS architectures in NixOS, and remnants of this support may still be found in Nixpkgs. However, there is no official support.
One of NixOS's defining features is its declarative configuration model, where the entire system state — including installed packages, system services, and settings — is described in configuration files. The primary file is typically located at <code>/etc/nixos/configuration.nix</code>.
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
配置的更改通过使用 <code>nixos-rebuild switch</code> 原子化应用,从而确保可复现性并能回滚到之前状态。大多数用户使用版本控制系统来追踪配置文件,以实现一致且可移植的系统设置。在其他系统中,这些缺陷通常是事后才通过 Puppet、Ansible 或 Chef 等配置管理解决方案来弥补,甚至完全无法弥补。这些工具将系统配置与预期的状态描述进行协调。然而,其并未集成到操作系统的设计中,而只是简单地叠加在操作系统之上。因此,当操作系统配置的某个方面未在预期状态描述中指定时,其配置仍然可能会有所不同。
Changes to the configuration are applied atomically using <code>nixos-rebuild switch</code>, ensuring reproducibility and the ability to roll back to previous states. Most users track their configuration files in a version control system, enabling consistent and portable system setups. These shortcomings are often rectified after-the-fact if at all by configuration management solutions such as Puppet, Ansible or Chef. These tools reconcile system configuration with a description of the expected state. However, these tools are not integrated into the operating system design and are simply layered on top, and OS configuration may still vary where an aspect of OS configuration has not been specified in the description of expected state.
Unlike conventional distributions, where system configuration is often scattered across manually edited files, NixOS integrates configuration management directly into the operating system. This eliminates configuration drift and makes NixOS particularly well-suited for automated, reproducible deployments.
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
关于 NixOS 配置的更多细节和示例,请参阅 [[Special:MyLanguage/NixOS system configuration|NixOS 系统配置]]。
For more details and examples on NixOS configurations, see [[Special:MyLanguage/NixOS system configuration|NixOS system configuration]].
While NixOS is typically configured declaratively as much as possible, these are a few domains where imperative operations are still necessary; these include user environment management and channel management.
In addition to declarative system configuration, NixOS users can utilize Nix's imperative <code>nix-env</code> command to install packages at the user level, without changing the system state. See the [[Special:MyLanguage/Nix#User Environments| user environments section of the Nix article]] for more information.
In the [[Special:MyLanguage/Nix ecosystem|Nix ecosystem]], [[Special:MyLanguage/Channel branches|channels]] are a mechanism for distributing collections of [[Special:MyLanguage/Nixpkgs|Nix packages]] and [[Special:MyLanguage/NixOS|NixOS]] module definitions. A channel represents a curated, versioned set of package definitions and system configurations, typically corresponding to a particular release or the latest available development state.
When using channels, your system or [[Special:MyLanguage/User Environment|user environment]] pulls package definitions and options from a URL pointing to a specific snapshot of the Nix Packages collection (Nixpkgs) and associated NixOS modules.
=== Comparison with traditional Linux Distributions ===
=== 与传统 Linux 发行版比较 ===
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
''Main Article: [[Special:MyLanguage/Nix vs. Linux Standard Base|比较Nix和Linux标准基础]]''
''Main Article: [[Special:MyLanguage/Nix vs. Linux Standard Base|Nix vs. Linux Standard Base]]''
</div>
<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>).
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.
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.
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.
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.
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.
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:
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 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
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
您可以通过设置 <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.
This article or section needs to be expanded. 此篇文章需要是一个简单的入门介绍,因为它将是新用户在此处阅读的首批文章之一。因此,内容需要简化一些,更复杂的主题应转移到其他文章中。 Further information may be found in the related discussion page. Please consult the pedia article metapage for guidelines on contributing.
大多数用户通过 任一 ISO 镜像 安装 NixOS。每个支持架构均有 “graphical”(图形化安装)和 “minimal”(最小化安装)两种 ISO 变体;“graphical” 镜像适用于计划安装桌面环境的用户,而 “minimal” 镜像适用于计划将 NixOS 充当服务器或期望更小 ISO 镜像文件的用户。ISO 镜像为混合镜像,可以刻录到光盘介质或原封不动地复制到 USB 驱动器上并直接启动。请参阅安装指南以了解详情。