NixOS Declarative Configuration and Instant Rollback: A Practical Guide
Learn how NixOS treats the entire OS as a pure function of its declarative config, enabling atomic upgrades and instant rollbacks with a practical nginx example.
19 Dec 2025, 20:00 UTC

The problem: configuration drift in traditional Linux
When administrators edit files manually, run ad‑hoc scripts, or mix package managers, the system state diverges from what is documented. Auditing becomes a forensic exercise and recovering from a broken update often means reinstalling or chasing down missing dependencies.
Thesis: NixOS makes the whole OS a pure function of a declarative file
In NixOS every package, service, and filesystem layout is expressed in "\/etc\/nixos\/configuration.nix". Changing a line triggers a deterministic rebuild that yields a new system generation. Each generation is stored immutably under "\/nix\/var\/nix\/profiles" and can be booted or rolled back instantly.
Worked example: adding nginx and rolling back
- Edit
"\/etc\/nixos\/configuration.nix"and addservices.nginx.enable = true;. - Run the rebuild as root:
sudo nixos-rebuild switch - Verify the service:
should show active (running).systemctl status nginx - To test rollback, intentionally break the config: set
services.nginx.enable = false;and add a bogus line likeservices.nginx.nonExistent = true;. - Run
again; the build will fail.sudo nixos-rebuild switch - Roll back to the last working generation:
.sudo nixos-rebuild switch --rollback - Confirm the system is back:
shows active andsystemctl status nginxls -l "\/run\/current-system"points to the previous generation directory.
Trade‑offs and practical limits
- Learning curve: the Nix language is functional and lazy; initial investment is required to write modules or overlays.
- Build time: if a substitute is not available, NixOS compiles packages from source, which can be lengthy on low‑power or bandwidth‑constrained hosts.
- Imperative leaks: running commands like
apt-get installor editing files outside"\/etc\/nixos"breaks reproducibility; such changes must be encapsulated in Nix modules or avoided.
Actionable closing
Start by experimenting in a virtual machine, commit your "\/etc\/nixos\/configuration.nix" to Git, and make it a habit to list generations with
sudo nix-env --list-generations -p "\/nix\/var\/nix\/profiles\/system" before major updates. Over time the atomic upgrades and instant rollbacks turn system maintenance from a risky chore into a reliable, version‑controlled process.0 replies
A thoughtful contribution can make all the difference. Be the first to share one.