LinuxCommandLibrary

nixos-rebuild

Apply NixOS configuration changes

TLDR

Build and switch to the new configuration, making it the boot default

$ sudo nixos-rebuild switch
copy

Build and switch to the new configuration, making it the boot default and naming the boot entry
$ sudo nixos-rebuild switch [[-p|--profile-name]] [name]
copy

Build and switch to the new configuration, making it the boot default and installing updates
$ sudo nixos-rebuild switch --upgrade
copy

Rollback changes to the configuration, switching to the previous generation
$ sudo nixos-rebuild switch --rollback
copy

Build the new configuration and make it the boot default without switching to it
$ sudo nixos-rebuild boot
copy

Build and activate the new configuration, but don't make a boot entry (for testing purposes)
$ sudo nixos-rebuild test
copy

Build the configuration and open it in a virtual machine
$ sudo nixos-rebuild build-vm
copy

List available generations similar to the boot loader menu
$ nixos-rebuild list-generations
copy

SYNOPSIS

nixos-rebuild [options...] subcommand [args...]
where subcommand is switch|boot|test|build|dry-run|dry-build|list-generations

PARAMETERS

-h, --help
    display help and exit

-v, --version
    display version information

-I, --extra-nix-path PATH
    add PATH to NIX_PATH

--impure
    allow impure builds

--upgrade
    upgrade all inputs from channels

--rollback
    switch to the previous system generation

--install-bootloader
    install the bootloader even if not changed

--fast
    skip flake locking and channel updates

--check
    check whether the new configuration is valid

--use-remote-sudo
    use sudo on remote target

--target-host HOST
    build and activate on remote HOST

--build-host HOST
    build on remote HOST

--flake REF
    build from REF (e.g., .#myhost)

--commit-lock-file
    commit lock file changes in flakes

--arg NAME VALUE
    pass Nix argument NAME = VALUE

--argstr NAME STRING
    pass string argument NAME = STRING

DESCRIPTION

nixos-rebuild is the primary command for managing NixOS system configurations. NixOS is a declarative Linux distribution where the entire system state is defined in /etc/nixos/configuration.nix and imported modules. This command parses the configuration, evaluates it using the Nix expression language, builds necessary packages and derivations in the Nix store (/nix/store), and activates the new system generation.

Key benefits include reproducibility, atomic upgrades, and easy rollbacks. Generations are stored in /nix/var/nix/profiles/system, allowing nixos-rebuild switch --rollback to revert changes instantly. It supports building for the current host or remote targets, integrates with Nix flakes for modern input pinning, and handles bootloader installation.

Typically run as root after editing configuration.nix, it performs syntax checks, upgrades channels if needed, and switches the profile symlink. Subcommands like switch activate immediately, boot prepares for next reboot, and test enables quick testing with automatic rollback on failure. It's idempotent and safe, preventing inconsistent states.

CAVEATS

Must run as root; misconfigurations can cause boot failure (use test subcommand); large configs may take time to evaluate/build; remote options require SSH setup.

SYSTEM GENERATIONS

Each rebuild creates a new isolated generation. List with nixos-rebuild list-generations. Bootloader menu shows them for recovery.
Active symlink: /run/current-system.

ACTIVATION PROCESS

Builds system derivation, runs switch-to-configuration script: stops services, updates profiles, starts new services atomically.

HISTORY

Developed as part of NixOS by Eelco Dolstra starting ~2003 with Nix package manager. nixos-rebuild introduced in early NixOS versions (~2011) for declarative rebuilds. Evolved with flakes support in Nix 2.4 (2022), enhancing reproducibility.

SEE ALSO

nixos-install(8), nixos-generate-config(8), nix-channel(1), nix(1), nix flake(1)

Copied to clipboard