NixOS

Revision as of 20:05, 28 August 2025 by Weijia (talk | contribs) (Created page with "然而,在 NixOS 中,<code>/lib</code> 和 <code>/usr/lib</code> 目录并不存在。相反,所有系统库文件、可执行文件、内核、固件和配置文件都存放在 Nix store 中。<code>/nix/store</code> 下的文件和目录均由构建数据的描述信息经哈希后命名。所有位于 Nix Store 的文件与目录均不可变。 <code>/bin</code> 和 <code>/usr/bin</code> 几乎不存在:它们分别只包...")

NixOS 是一個基於 Nix 包管理器與構建系統的 Linux 發行版。它支持聲明式的系統級配置管理以及原子化升級和回滾,同時它仍支持命令式的包管理和用戶管理。在 NixOS 中,發行版的所有組件 — 包括系統內核、已安裝的軟件包和系統配置文件 — 均由 Nix 從被稱為 Nix 表達式純函數中構建。

Introduction to Nix

Tools and applications

☶︎
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.

由於 Nix 使用了二進制緩存機制,這便為面向二進制分發(如 Debian)和面向源碼分發(如 Gentoo)的方法提供了一種獨特的折中方案。預編譯的二進制程序被視作標準組件,在其無法獲取時,自助編譯的軟件包與模塊將被自動構建。

NixOS 穩定版本每年發布兩次(大約在五月底和十一月底)。NixOS 由 Eelco DolstraArmijn Hemel 創建,並於 2003 年首次發布。目前由 NixOS 基金會 管理下的社區開發與維護。

安裝

完整的安裝指南請參閱 NixOS 手冊的 Installation 章節。此維基還包含替代或補充指南,例如 桌面設備上的 NixOS

大多數用戶通過 任一 ISO 鏡像 安裝 NixOS。每個支持架構均有 「graphical」(圖形化安裝)和 「minimal」(最小化安裝)兩種 ISO 變體;「graphical」 鏡像適用於計劃安裝桌面環境的用戶,而 「minimal」 鏡像適用於計劃將 NixOS 充當服務器或期望更小 ISO 鏡像文件的用戶。ISO 鏡像為混合鏡像,可以刻錄到光盤介質或原封不動地複製到 USB 驅動器上並直接啟動。請參閱安裝指南以了解詳情。

除了 ISO 鏡像,下載頁面 還提供了多種安裝 NixOS 的替代方法。這些方法包括:

  • OVA 格式的虛擬機(兼容 VirtualBox);
  • Amazon EC2 AMIs;

此外,許多現有的 Linux 安裝可以通過 nixos-infectnixos-in-place 轉換為 NixOS 安裝;這對於在不原生支持 NixOS 的主機提供商平台安裝 NixOS 十分有用。

系統架構

NixOS 對大多數 x86_64 設備和通用 ARM64 設備提供了開箱即用的支持。

32位 x86 架構

對於32位 x86 架構(即 i686)的支持正在減少。大多數包仍然可以編譯和運行,但它們的緩存可用性顯著降低[1]。32位 x86 架構不再提供預構建的 ISO 鏡像文件,但其仍可手動構建。

64位 x86 架構

大多數 x86_64 設備都能順利運行 NixOS。

32位 ARM 架構

Main article: NixOS on ARM

NixOS 不官方支持 ARM32 設備(例如 armv6arm71),不過對於其中部分設備,可能存在社區支持。

64位 ARM 架構

Main article: NixOS on ARM

只要設備支持通用 systemd 引導過程,NixOS 便可開箱即用。但是,使用專有引導加載程序的特定設備可能存在運行問題。

MIPS 架構

Main article: NixOS on MIPS

NixOS 對於 MIPS 架構的支持有限, Nixpkgs 中可能存在部分對於此架構的支持。但並未有官方支持。

用法

聲明式配置

NixOS 的一個核心特徵便是其聲明式配置模型,其中整個系統狀態 — 包括已安裝的軟件包、系統服務和設置 — 均在配置文件中描述。主配置文件通常位於 /etc/nixos/configuration.nix

配置的更改通過使用 nixos-rebuild switch 原子化應用,從而確保可復現性並能回滾到之前狀態。大多數用戶使用版本控制系統來追蹤配置文件,以實現一致且可移植的系統設置。在其他系統中,這些缺陷通常是事後才通過 Puppet、Ansible 或 Chef 等配置管理解決方案來彌補,甚至完全無法彌補。這些工具將系統配置與預期的狀態描述進行協調。然而,其並未集成到操作系統的設計中,而只是簡單地疊加在操作系統之上。因此,當操作系統配置的某個方面未在預期狀態描述中指定時,其配置仍然可能會有所不同。

與傳統發行版通常將系統配置散布於各處需手動編輯的文件中不同,NixOS 將配置管理直接集成到操作系統中。這種方式消除了配置漂移,使得 NixOS 十分適合自動化、可復現的部署。

關於 NixOS 配置的更多細節和示例,請參閱 NixOS 系統配置

命令式操作

儘管 NixOS 通常儘可能以聲明式方式進行配置,但在某些情況下,命令式操作仍然是必需的;這包括了用戶環境管理和頻道管理。

用戶環境

除了聲明式系統配置之外,NixOS 用戶還可以使用 Nix 的命令式 nix-env 指令在用戶層面安裝軟件包,而無需更改系統狀態。有關更多信息,請參閱 Nix 文檔的用戶環境部分

頻道

Nix 生態系統 中,頻道(channels)是一種用於分發 Nix 軟件包NixOS 模塊定義的機制。頻道代表一組經過精心維護、版本控制的軟件包定義和系統配置,通常對應於一個特定版本或最新的可用開發狀態。

在使用頻道時,您的系統或 用戶環境 會從一個 URL 拉取軟件包定義和選項,該 URL 指向 Nix 軟件包集合(Nixpkgs)及相關 NixOS 模塊的特定快照。

關於如何使用和配置 Nix 頻道的更多信息,請參閱 頻道分支

內部機制

與傳統 Linux 發行版比較

Main Article: Nix vs. Linux Standard Base

NixOS 與其他 Linux 發行版的主要區別在於,NixOS 不遵循 Linux 標準規範(LSB) 的文件系統結構。在遵循 LSB 的系統中,軟件通常存儲在 /{,usr}/{bin,lib,share} 目錄下,而配置文件通常存儲在 /etc。如果程序的可執行文件被放置在 LSB 的某個 /bin 目錄下,那麼它就可以在用戶環境中被訪問。當一個程序引用動態鏈接庫時,它將在 LSB 目錄(/lib/usr/lib)中搜索所需的庫。

然而,在 NixOS 中,/lib/usr/lib 目錄並不存在。相反,所有系統庫文件、可執行文件、內核、固件和配置文件都存放在 Nix store 中。/nix/store 下的文件和目錄均由構建數據的描述信息經哈希後命名。所有位於 Nix Store 的文件與目錄均不可變。 /bin/usr/bin 幾乎不存在:它們分別只包含 /bin/sh/usr/bin/env,以提供與使用 shebang 行的現有腳本的最低兼容性。用戶級環境通過大量指向所需軟件包和輔助文件的符號鏈接來實現。 這些環境被稱作 Profiles,被存儲於 /nix/var/nix/profiles,每個用戶擁有其獨有的 Profiles。 通過這種結構化的系統組織方式,NixOS 獲得了優於傳統 Linux 發行版的核心優勢,例如原子化操作和回滾支持。

Usage of the Nix store

A lot of confusion for newcomers arises from the fact that configuration is stored in the read-only /nix/store 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 /etc/nixos/configuration.nix file and executing nixos-rebuild switch. NixOS provides the module system for editing all required configurations. Users should first use 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 /etc/nixos/configuration.nix you will get the exact same system state.

Modules

The NixOS module system as defined in 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 /etc/nixos/configuration.nix. 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:

❄︎ /etc/nixos/configuration.nix
{
  imports = [
    ./basic-webserver.nix
    ./blog.nix
  ];
}
❄︎ /etc/nixos/basic-webserver.nix
{
  services.nginx.enable = true;
  services.nginx.virtualHosts."example.com" = {
    root = "/var/www/example.com";
  };
}
❄︎ /etc/nixos/blog.nix
{
  services.nginx.virtualHosts."blog.example.com" = {
    root = "/var/www/blog.example.com";
  };
}

See the Modules section of the NixOS Manual for more details.

Generations

Every time the system state is rebuilt using nixos-rebuild switch, 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.

You can roll back via:

$ nix-env --rollback               # roll back a user environment
$ 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

$ /run/current-system/bin/switch-to-configuration boot

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:

# delete generations older than 30 days
$ 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

List generations:

# as root
$ nix-env --list-generations --profile /nix/var/nix/profiles/system

Switch generations:

# as root switch to generation 204
$ nix-env --profile /nix/var/nix/profiles/system --switch-generation 204

delete broken generation(s):

# as root delete broken generations 205 and 206 
$ nix-env --profile /nix/var/nix/profiles/system --delete-generations 205 206

You can configure automatic garbage collection by setting the nix.gc options in /etc/nixos/configuration.nix. This is recommended, as it keeps the size of the Nix store down.

See also