If you’ve ever felt overwhelmed by NixOS commands, flakes, or the dreaded garbage collection panic, this guide is for you. It’s not just a cheatsheet—it’s a practical survival kit for running NixOS with flakes, keeping your system clean, and always having a rollback strategy when things go sideways.


🖥️ NixOS Essentials

TaskCommand
Show versionnixos-version
Edit config (classic)sudo nano /etc/nixos/configuration.nix
Rebuild + switchsudo nixos-rebuild switch
Build only (no switch)sudo nixos-rebuild build
Roll back last rebuildsudo nixos-rebuild switch --rollback
List generationssudo nix-env --list-generations --profile /nix/var/nix/profiles/system

⚡ Flakes Basics

TaskCommand
Init flakenix flake init -t templates#nixos
Show flake outputsnix flake show
Update all inputsnix flake update --commit-lock-file
Update one inputnix flake lock --update-input nixpkgs --commit-lock-file
Build systemsudo nixos-rebuild build --flake .#<hostname>
Switch systemsudo nixos-rebuild switch --flake .#<hostname>
Boot (safe)sudo nixos-rebuild boot --flake .#<hostname>

🔄 Safe Updates & Rollbacks

Keeping your system updated is great… until it doesn’t boot. Here’s how to avoid disasters.

ScenarioCommand / Action
Preview changesnix store diff-closures /nix/var/nix/profiles/system ./result
Safe upgrade (boot next gen only after reboot)sudo nixos-rebuild boot --flake .#<host>
Roll back after bad switchsudo nixos-rebuild switch --rollback
Boot into old systemSelect an older generation at bootloader
Revert to last good commitgit checkout <commit> + sudo nixos-rebuild boot --flake .#<host>

Quick Routine

nix flake update --commit-lock-file

sudo nixos-rebuild build --flake .#<host>

nix store diff-closures /nix/var/nix/profiles/system ./result

sudo nixos-rebuild boot --flake .#<host>

sudo reboot

🧹 Garbage Collection & Store Management

NixOS keeps everything by default—which is great for rollbacks but eats disk space fast.

TaskCommand
Collect garbagesudo nix-collect-garbage
Delete old gens + GCsudo nix-collect-garbage -d
GC older than 14 dayssudo nix-collect-garbage --delete-older-than 14d
Optimize store (deduplication)sudo nix store optimise
Check system sizenix path-info -Sh /run/current-system

Configure Automatic GC (NixOS module)

{
 boot.loader.systemd-boot.configurationLimit = 20;
 nix.gc = {
 automatic = true;
 dates = "weekly";
 options = "--delete-older-than 14d";
 };
}

🧭 Pro Tips

  • Always prefer boot over switch when testing major updates. Reboot to try it, rollback at bootloader if it fails.
  • Keep your lockfile in git. Rollbacks are trivial when you can checkout an old commit.
  • GC smartly. Don’t delete all history immediately—keep 14+ days for safety.
  • Use nix store diff-closures before switching, to see what really changed.

🚀 Final Thoughts

Running NixOS with flakes doesn’t have to feel like juggling chainsaws. With a solid update routine, rollback plan, and controlled garbage collection, you’ll have both cutting-edge packages and peace of mind.

This cheatsheet is meant to be your daily companion—bookmark it, print it, or stick it on your wall. Happy hacking!

Blogpost created with AI help