OFFSET偏移
← All articlesOFFSET / FIELD NOTESNO. 013

NixOS: the foundations of a declarative system

Understand how NixOS works through configuration, build outputs, and generations.

English translation · Code and commands follow the Chinese original.

IN THESE READING PATHSNixOS system foundations and configuration ↗
About 3 min readStart reading

What is NixOS?#

NixOS is a Linux distribution based on the Nix package manager, developed and maintained by the NixOS Foundation. Its distinctive declarative configuration approach lets you define and manage the entire system through configuration files.

Core features#

1. Declarative configuration

Traditional Linux distributions usually require you to install packages, edit configuration files, and enable services manually. In NixOS, all these operations are defined in a single configuration.nix file:

{ config, pkgs, ... }:

{
  # 启用 OpenSSH 服务
  services.openssh.enable = true;
  
  # 安装软件包
  environment.systemPackages = with pkgs; [
    vim
    git
    htop
  ];
  
  # 配置用户
  users.users.alice = {
    isNormalUser = true;
    extraGroups = [ "wheel" ];
  };
}

2. Reproducibility

Pinning the configuration, dependency revisions, and build inputs lets you repeatedly build the system environment described by the declaration. Writable data, hardware differences, and nondeterministic builds still need separate handling. This means:

  • You can deploy the same declarative environment on different machines
  • Team development environments can stay consistent
  • Production and development environments can share the same dependency and configuration definitions

3. Atomic upgrades & rollbacks

NixOS system updates are atomic:

  • New build outputs do not overwrite old ones, and switching the system profile supports atomic updates
  • Activation scripts, service restarts, bootloaders, and writable data can still encounter failures; rollback is no substitute for backups
  • You can roll back to a previous working version at any time
# 查看可回滚的系统版本
sudo nixos-rebuild list-generations

# 切换到特定版本
sudo nix-env --profile /nix/var/nix/profiles/system --switch-generation 42
sudo /nix/var/nix/profiles/system/bin/switch-to-configuration switch
Argument Switches the running system immediately Sets the default boot entry Takes effect after reboot
switch Yes Yes Yes
boot No Yes Yes
test Yes No No; reverts on reboot

switch activates the configuration immediately and makes it the boot default; boot only builds it and makes it the boot default, leaving the running system unchanged; test activates it immediately without adding it to the boot menu. NixOS Manual

When replacing a login manager or critical service, both switch and test may interrupt the current session. If you want the new configuration to apply at the next boot, you can use boot first:

sudo nixos-rebuild boot --flake .#workstation
# 确认工作已保存后才重启
sudo systemctl reboot

4. Reliable package management

Features of the Nix package manager:

  • Isolated package paths: different packages and versions do not overwrite one another; activation and program execution can still have side effects
  • Coexisting versions: different versions of the same software can exist at the same time
  • Garbage collection: automatically removes packages no longer in use
  • Binary caches: prebuilt packages can be installed quickly

Where NixOS fits#

Use case NixOS advantages
Development environments Quickly create reproducible development environments
Server deployment Declarative configuration makes management and rollback straightforward
Embedded systems Custom builds containing only the required components
CI/CD Consistent build environments
Desktop systems Choose a stable branch or unstable while retaining generation-based rollback

NixOS architecture#

┌─────────────────────────────────────┐
│          用户空间应用层              │
│    (桌面环境、浏览器、编辑器等)       │
├─────────────────────────────────────┤
│          系统服务层                  │
│    (systemd 服务、网络服务等)         │
├─────────────────────────────────────┤
│          NixOS 配置层                │
│    (configuration.nix)               │
├─────────────────────────────────────┤
│          Nix 包管理器                │
│    (nixpkgs、flakes)                 │
├─────────────────────────────────────┤
│          Linux 内核                  │
└─────────────────────────────────────┘

Learning recommendations#

Learning NixOS requires an understanding of the following:

  1. Nix language basics - for writing configuration files
  2. The NixOS module system - how configuration options work
  3. nixpkgs - how to use the Nix package collection
  4. Flakes - Nix's next-generation project-management tools

Comparison with other Linux distributions#

Feature NixOS Ubuntu Arch Linux
Configuration approach Declarative Imperative Imperative
Package management Nix APT Pacman
Reproducibility Native support Requires additional tools Requires additional tools
Version rollback Native support Limited support Limited support
Learning curve Steep Gentle Moderate

Further resources#



References & further reading

  1. NixOS Manual ↗
  2. Nix Language Manual ↗

Adapted from technical notes. Examples use generic names.

Continue along a reading path

NixOS system foundations and configuration

  1. 01NixOS: the foundations of a declarative system
  2. 02Installing NixOS: installation media, partitions, and initial configuration
  3. 03NixOS configuration basics: options and modules
  4. 04Managing a system with Flakes and Home Manager
  5. 05The NixOS command line: old and new interfaces and everyday maintenance
  6. 06NixOS generation management and garbage collection
  7. 07Specialisation: multiple configuration variants in one generation
  8. 08If configuration is reproducible, why do apps still forget?
CONTINUE READING / NEXT ARTICLEInstalling NixOS: installation media, partitions, and initial configuration

Understand prerequisites and steps for both graphical and manual installation.