Declaratively Upgrading NixOS with nixos-rebuild switch: A Practical Task Guide
Learn how to safely apply a new NixOS configuration, upgrade the system, and verify the changes with nixos-rebuild. The guide covers prerequisites, step‑by‑step commands, checks, and rollback options.
12 Oct 2025, 12:15 UTC

Desired Outcome
Apply a new /etc/nixos/configuration.nix or a flake‑based configuration, build the updated system, and make the new generation the active boot entry without reinstalling or manually copying files.
Prerequisites
- Working NixOS installation with root access.
- The
nixos-rebuildcommand (bundled withnixos-utils). - Optional: Nix 2.4+ for flake support. If using a flake, ensure the
nixbinary is in the PATH. - At least 2 GB free space in
/nix/storefor the build (large kernel or glibc upgrades can consume more). - Back‑up of
/etc/nixos/configuration.nixor the flake repository if you want a quick revert before the build.
Focused Procedure
- Prepare the configuration
Either edit
/etc/nixos/configuration.nixor create a flake. For a flake, placeflake.nixin the repository root and add ahostsdirectory with a host‑specificdefault.nix.# flake.nix { inputs, outputs, ... }: { outputs = { nixosConfigurations, ... }: let system = "x86_64-linux"; host = "my-host"; in { ${host} = nixosConfigurations.${host}.override { modules = [ ./hosts/${host}/default.nix ]; }; }; } - Verify the configuration syntax
Run
nix flake check .(flake) ornixos-rebuild test(classic) to ensure no errors. No state change occurs.sudo nix flake check . # or sudo nixos-rebuild test - Build and activate the new generation
Use
--upgradeto fetch the latest packages andswitchto activate the new system.- Classic configuration:
sudo nixos-rebuild switch --upgrade - Flake‑based configuration:
sudo nixos-rebuild switch --upgrade --flake .#my-host
During the build you’ll see stages: building the Nix store, generating the new system profile, and installing the boot loader. If the kernel or initrd changes, a reboot will be required for the new generation to take effect.
- Classic configuration:
- Automatic reboot (if needed)
When the kernel is upgraded,
nixos-rebuildprints a message like:
Execute that command or simply press Ctrl‑Alt‑Del.run `systemctl reboot` to use the new kernel.
Expected Checks
- List generations
Verify the new generation is current:
sudo nixos-rebuild list-generations # The last line should have a '*' indicating the active generation. - Kernel version consistency
Compare the running kernel with the one in the new system profile:
Both should match.uname -r /run/current-system/sw/bin/uname -r - Service status
Test a service that was enabled or modified. For example, if
services.sshd.enable = true;:systemctl is-active sshd.service # Should output "active". - Boot loader entry
Reboot and confirm GRUB lists the new generation as the default entry. The entry name contains the generation number and the date.
Recovery Options
- Boot from a previous generation
During boot, select a previous entry in the GRUB menu. Each entry corresponds to a NixOS generation.
- Rollback from a running system
If you are already booted into the new generation but want to revert:
sudo nixos-rebuild boot --rollback # This sets the previous generation as the next boot target. # Reboot to activate it. - Delete an unwanted generation
After verifying the new system works, free space by removing old generations:
sudo nixos-rebuild remove 123 # Replace 123 with the generation number.
Limitations & Practical Tips
- Large upgrades (e.g., kernel, glibc) can take minutes and require several gigabytes of temporary space. Check
df -h /nix/storebefore starting. - Using
--flakerequires Nix 2.4+. Older systems must either upgrade Nix or use the classicconfiguration.nixworkflow. - Always keep a recent backup of
/etc/nixos/configuration.nixor the flake repository; a corrupted configuration can preventnixos-rebuildfrom succeeding. - After a major release upgrade, review the release notes for any configuration changes that need to be migrated.
Concrete Example: Upgrading to NixOS 24.05 with a Flake
- Clone the flake repository:
git clone https://github.com/example/nixos-hosts.git my-hosts cd my-hosts - Confirm the flake is valid:
sudo nix flake check . - Upgrade and switch:
sudo nixos-rebuild switch --upgrade --flake .#my-host - Reboot if prompted, then verify:
sudo nixos-rebuild list-generations uname -r systemctl is-active sshd.service
Diagram
| Label |
|---|
| Configuration File |
| nixos-rebuild |
| System Generation |
| Boot Loader |
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.