5. Generations, the Store & Rollbacks

The commands and symlinks behind atomic upgrades and one-reboot recovery

Back in the mental model, we said a generation is "just a symlink," that rolling back is "flipping that symlink," and that a bad upgrade is undone by picking the previous entry from the boot menu. That's true — and it's a great picture to carry around. But if you're going to run a NixOS machine day to day, you want the actual levers: which command builds without activating, how to see the list of generations, how to roll back from a shell instead of a reboot, and how to stop months of old builds from quietly filling your disk.

This article is the hands-on companion to that concept. We'll open the hood on /nix/var/nix/profiles/, walk through the four things nixos-rebuild can do, and finish with the garbage collector — the one piece of NixOS housekeeping you actually have to think about.

The one sentence to keep in mind

A generation is a numbered symlink in /nix/var/nix/profiles/ pointing at a system in the store; "the current system" is one more symlink pointing at whichever generation is live. Every operation in this article is ultimately about which store path those symlinks point to — and, separately, about deleting the store paths nothing points to anymore.

What a generation actually is, on disk

Let's make it concrete. NixOS keeps its profiles under /nix/var/nix/profiles/. Peek inside a running system and you'll see something like this:

$ ls -l /nix/var/nix/profiles/
system -> system-44-link
system-42-link -> /nix/store/2q1x…-nixos-system-bluewhale-24.11
system-43-link -> /nix/store/8kf3…-nixos-system-bluewhale-24.11
system-44-link -> /nix/store/pr9m…-nixos-system-bluewhale-24.11

Read that from the bottom up. Each system-N-link is a symlink named after a generation number, pointing at a specific store path. That store path — the nixos-system-… directory — is a complete, immutable system: a tree containing the kernel, the initrd, every unit file, the activation script, and a sw/ directory whose bin/ becomes your system PATH. Generation 42, 43, and 44 are three whole systems sitting side by side, sharing whatever store paths they have in common and differing only where your config changed.

Above them sits the plain system symlink with no number. That is the current system: right now it points at system-44-link, so generation 44 is live. This is the symlink the "flip" refers to. There's a matching per-user story too — /nix/var/nix/profiles/per-user/<you>/profile and, if you use it, your nix profile — but the system profile is the one nixos-rebuild manages and the one that shows up in your boot menu.

/nix/var/nix/profiles/systemthe current systemsystem-42-linksystem-43-linksystem-44-linkpoints at the newest generation/nix/store/2q1x…-nixos-system-24.118kf3…-nixos-system-24.11pr9m…-nixos-system-24.11boot menuNixOS — gen 44 (default)NixOS — gen 43NixOS — gen 42one entry per generationRolling back = point system at an older link and re-run its activation script.Nothing is copied or rebuilt — the older system's store path was never deleted.The bootloader writes one menu entry per generation link, newest first.

The bootloader reads this same list. When you run a rebuild, part of the work is regenerating the boot menu so there's one entry per generation — which is exactly why the menu lets you reach into the past. Each entry just tells the loader "boot the kernel and init found at this store path."

The four things nixos-rebuild can do

Most guides tell you to type nixos-rebuild switch and leave it there. But switch is only one of four subcommands, and knowing the other three turns rebuilding from a leap of faith into something you can stage and preview. They vary along two independent axes: does it change the system running right now? and does it change what boots next time?

changes the boot default?changes the running system now?noyesnoyesbootbuilds + new generationsets boot defaulttakes effect next rebootswitchbuilds + new generationactivates nowand sets boot defaultdry-activatebuilds onlyno generationprints what would changetestbuilds + activates nowno generation, no boot entrygone after rebootThe four verbs, placed by what they touch

Here's each one precisely:

switch

Build the configuration, register it as a new generation, update the bootloader so it's the default, and run the activation script to make it live right now. This is the everyday command: after it finishes, your running system and your next boot are both the new configuration.

boot

Build, register a new generation, and make it the boot default — but do not activate it now. The machine keeps running exactly as it is until you reboot. Ideal for kernel or bootloader changes you'd rather apply cleanly on the next restart than hot-swap into a running system.

test

Build and activate the new configuration now, but do not register a generation and do not touch the bootloader. Because nothing was written to the boot menu, a reboot drops you straight back to your current default. Perfect for trying a change you're unsure about: if it wedges something, just reboot to undo it.

dry-activate

Build the configuration (so you see any evaluation or build errors), then print what activating it would do — which services would restart, reload, or stop — without changing a thing. A pure preview. There's also dry-build, which stops even earlier: it only reports what would be built or fetched.

A natural workflow falls out of this: nixos-rebuild dry-activate to preview, then nixos-rebuild test to try it live, then nixos-rebuild switch to commit it once you're happy — or nixos-rebuild boot when the change is best applied at the next reboot.

Why test is your safety net for risky changes

Because test never writes a boot entry, the worst case is bounded: if the change breaks networking, your desktop, or a service you're logged in through, a hard reboot returns you to the last switch/boot configuration — no boot-menu spelunking required. It's the difference between "let me try this" and "let me commit to this."

With a flake, each of these takes the usual host selector, for example sudo nixos-rebuild test --flake .#bluewhale. Everything below works the same whether or not you use flakes — generations and profiles are a Nix-level concept, not a flake one.

Listing what you have

Two commands answer "what generations exist?" The classic one operates directly on the profile:

$ nix-env --list-generations --profile /nix/var/nix/profiles/system
  42   2026-06-30 09:14:02
  43   2026-07-02 21:41:55
  44   2026-07-06 10:02:18   (current)

That works on any profile — swap in a per-user profile path and you'll see that profile's history instead. The newer, friendlier option is built into nixos-rebuild and enriches each row with the NixOS version, the kernel, and the config revision:

$ nixos-rebuild list-generations
Generation  Build-date           NixOS version   Kernel   Configuration Revision  Specialisation
42          2026-06-30 09:14:02  24.11.20260628  6.6.35   a1b2c3d
43          2026-07-02 21:41:55  24.11.20260701  6.6.37   e4f5a6b
44          2026-07-06 10:02:18  24.11.20260705  6.6.40   9c8d7e6                 *

The * marks the current generation. If you manage user packages with the newer nix profile command, it has its own view — nix profile history — which shows a diff between successive profile versions (what was added, removed, or upgraded), which is often more useful than a bare list.

Rolling back

Now the payoff. Say generation 44 was a bad upgrade — a new kernel that won't find your root device, or a display manager that refuses to start. You have three ways back, in rough order of how broken things are.

  1. From a working shell — roll back one step. If the system still boots and you can get a terminal, the cleanest fix is sudo nixos-rebuild switch --rollback. This activates the previous generation and makes it the default again. Under the hood it points the system symlink back one link and runs that generation's activation script — so it takes effect immediately, no reboot needed.
  2. From a working shell — jump to a specific generation. To go back to an exact number rather than just "the previous one," use sudo nix-env --switch-generation 43 --profile /nix/var/nix/profiles/system. That repoints the symlink; you then apply it with sudo /nix/var/nix/profiles/system/bin/switch-to-configuration switch (or just reboot). This is the low-level lever --rollback is built on.
  3. From the boot menu — when it won't boot at all. If generation 44 won't even start, reboot and pick an earlier NixOS entry from the bootloader menu. You boot straight into generation 43, exactly as it was. Once you're back in a working shell, run sudo nixos-rebuild switch --rollback to make the good generation the default so you're not choosing it by hand every boot.

Why rollback is instant and atomic

Rolling back doesn't rebuild or reinstall anything — generation 43's store paths were never deleted, so they're already on disk, complete. The rollback is just a symlink move plus running one activation script. And it's atomic for the same reason a forward switch is: the switch of the system symlink happens in a single step, so the system is either fully on 44 or fully on 43, never stuck halfway between them.

One caveat worth internalizing: rollback restores the system, not your data. A generation captures packages, services, and configuration — not the contents of your home directory or a database on disk. If a bad upgrade ran an irreversible migration on your files, booting an old generation won't un-migrate them. Generations are a time machine for the system, not for state.

Garbage collection: reclaiming the disk

Here's the flip side of "old generations are never deleted": they're never deleted. Every switch and boot leaves another full system in the store, and the packages those old systems reference stay pinned on disk. Left alone, /nix/store grows without bound. This is the one bit of NixOS upkeep you have to do deliberately.

The mechanism is the garbage collector, and it runs on a simple rule. Nix keeps a set of GC roots — store paths that are declared "in use." Every generation of every profile is a GC root. The collector starts from all roots, follows every dependency, and anything in the store not reachable from a root is safe to delete.

The chain that keeps a package alive

A store path survives GC only if something points at it: a generation references your system, the system references its packages, and each package references its dependencies. So the way you actually free a package isn't to delete the package — it's to delete every generation that still references it. Remove the last generation holding an old kernel, and that kernel becomes garbage the collector can reclaim.

That gives us two distinct operations, and the distinction matters:

nix-collect-garbage

Delete store paths that no generation references anymore — the debris left by builds and by generations you've already removed. Old generations themselves are kept, so this is safe and non-destructive to your rollback history. It just sweeps up what's already unreferenced.

nix-collect-garbage -d

The -d / --delete-old flag first deletes all old generations of your profiles (keeping only the current one), which removes their roots, and then collects garbage. This is what actually reclaims serious space — but it also throws away your ability to roll back to those generations.

Run it, and Nix reports how much it freed:

$ sudo nix-collect-garbage -d
removing old generations of profile /nix/var/nix/profiles/system
...
342 store paths deleted, 4271.83 MiB freed

A subtlety: sudo nix-collect-garbage -d removes old generations of the system profile, but each user's nix profile / home-manager generations live under that user's own directory and are only pruned when that user runs the collector. If disk is tight, run it both as your user and with sudo. If you'd rather keep some recent history, use nix-collect-garbage --delete-older-than 30d to delete only generations older than a window instead of all of them.

For the finer-grained, new-CLI equivalents there's nix store gc (the "collect unreferenced paths" half, matching nix-collect-garbage with no -d) and nix store delete for removing specific paths. And you don't have to run any of this by hand — NixOS can schedule it:

Automatic GC in your configuration

nix.gc = {
  automatic = true;
  dates = "weekly";
  options = "--delete-older-than 30d";
};
# also de-duplicate identical files in the store
nix.settings.auto-optimise-store = true;

This installs a systemd timer that runs nix-collect-garbage --delete-older-than 30d once a week, keeping a rolling month of generations and reclaiming the rest. auto-optimise-store hard-links identical files across store paths, which is a separate, complementary way to shrink the store.

Keeping N generations and the /boot gotcha

Garbage collection frees /nix/store, but there's a second, sneakier space problem: /boot. On a UEFI system the EFI System Partition is often only 512 MiB — and every generation the bootloader knows about copies a kernel and initrd there, easily 50–100 MiB a pair. A dozen generations and a "no space left on device" error at the end of a rebuild is a genuinely common first-year NixOS surprise.

The fix is to cap how many generations the bootloader keeps entries for:

Limit the boot menu

# systemd-boot
boot.loader.systemd-boot.configurationLimit = 10;
# or, if you use GRUB
boot.loader.grub.configurationLimit = 10;

With this set, a rebuild writes at most the newest 10 generations into the boot menu — and, crucially, only copies that many kernels and initrds into /boot. It's the direct lever for EFI-partition pressure.

Two different limits, two different disks

configurationLimit controls only the boot menu and the kernels in /boot. It does not delete generations from the store or remove their GC roots — a generation past the limit still exists and still pins its packages in /nix/store. Trimming /boot and reclaiming /nix are separate jobs: configurationLimit for the first, the garbage collector for the second.

One last ordering gotcha to save you a confused moment: if you delete old generations with nix-collect-garbage -d, the boot menu doesn't update on its own. The stale entries linger until the next nixos-rebuild boot or switch regenerates the bootloader from the generations that remain. So the tidy sequence to free space and clean the menu is: collect garbage, then rebuild.

Try it: drive a system profile

Below is a simulated /nix/var/nix/profiles/ you can operate on. Rebuild to add a generation and advance the current system, roll back to move it to an earlier one, and run the garbage collector to reclaim what's no longer referenced — and watch the system symlink and the boot menu move in response.

10 boot-menu entries (trims /boot, not /nix/store)

Key Takeaways

  • A generation is a symlink: /nix/var/nix/profiles/system-N-link points at a complete system in the store, and the plain system link points at whichever generation is current.
  • Four verbs: switch (activate now + set boot default + new generation), boot (set boot default only, applies next reboot + new generation), test (activate now, no generation, gone on reboot), dry-activate (preview only).
  • List them with nixos-rebuild list-generations or nix-env --list-generations --profile /nix/var/nix/profiles/system; nix profile history for the new-CLI user profile.
  • Roll back with nixos-rebuild switch --rollback, by picking an older entry from the boot menu, or with nix-env --switch-generation. It's instant and atomic because it's just a symlink move plus one activation script — nothing is rebuilt.
  • Generations are GC roots. nix-collect-garbage sweeps unreferenced paths; -d also deletes old generations (and your ability to roll back to them). Automate it with nix.gc.
  • Two space problems, two levers: the garbage collector frees /nix/store; configurationLimit trims the boot menu and /boot. After a -d collection, rebuild to refresh the boot menu.

You can now build, stage, preview, roll back, and clean up a NixOS system with intent rather than superstition. That covers the operational core of running the machine. Next we'll switch from managing systems to exploring them: the Nix REPL lets you poke at nixpkgs and your own configuration interactively, evaluating expressions and inspecting packages before you ever commit them to a rebuild.