1. The NixOS Mental Model

Why a whole Linux system can be a single, rebuildable description

Think about how a normal Linux machine comes to be. You install the distro, then over the following months you apt install a compiler, edit /etc/ssh/sshd_config to lock down SSH, drop a script into /usr/local/bin, enable a systemd service, pip install something globally that you later half-forget about. A year later the machine works, but nobody — including you — can say exactly what was done to get it there. The system's current state is the sum of hundreds of one-off commands, and the only record of them is your fading memory and your shell history.

That accumulated, undocumented drift is the source of an enormous amount of pain: the dreaded "works on my machine," servers that can't be rebuilt from scratch after a disk dies, upgrades that break in ways nobody can reproduce. NixOS is a Linux distribution built from the ground up to make that whole class of problem go away. This article won't teach you any syntax — that starts in the next article. The goal here is to install the mental model, because almost everything surprising about NixOS follows naturally from a few core ideas.

The whole model in one paragraph

On NixOS, you don't change your system by running commands that mutate it. You describe the system you want in a set of files, and one command builds a whole new system matching that description. Every package lives at an immutable, hash-named path in /nix/store, so many versions coexist without conflict. Each rebuild produces a numbered generation, and switching between them — including rolling back a broken upgrade — is just flipping a symlink. That's it. Everything else is a consequence of those three facts.

Imperative vs. declarative

The traditional package manager is imperative: you give it a sequence of instructions that change the current state. apt install nginx mutates the live system — it writes files into /usr/bin, /etc, and /var, right now, in place. Run it twice and the second run behaves differently from the first, because the state it's acting on has changed. There is no single artifact that says "this is what the system should be"; there is only the ever-shifting result of every command ever run.

NixOS is declarative: instead of a list of steps, you write down the desired end state — which packages, which services, which users, which kernel options — and a tool figures out how to make the machine match. A tiny slice of a NixOS configuration looks roughly like this (don't worry about the syntax yet, just notice the shape):

The shape of a declarative config

{
  networking.hostName = "bluewhale";
  services.openssh = {
    enable = true;
    settings.PasswordAuthentication = false;
  };
  environment.systemPackages = [ pkgs.git pkgs.htop pkgs.ripgrep ];
  users.users.jo = {
    isNormalUser = true;
    extraGroups = [ "wheel" ];
  };
}

This isn't a script that runs top to bottom. It's a description: "the hostname is bluewhale, SSH is on but password login is off, these three packages are installed, this user exists." You hand it to nixos-rebuild and the machine is made to match.

Imperative (apt / dnf / pacman)

A sequence of commands that mutate global state in place. The "config" is the accumulated side effects of every command ever run. Re-running is not idempotent. Reproducing the machine means remembering — and replaying — that whole history.

Declarative (NixOS)

A description of the desired end state. Applying it is idempotent — rebuild from the same files and you get the same system. The config is the machine, and it fits in a git repo.

The key property here is idempotence: applying the same description twice leaves you in the same place. Because the files describe an end state rather than a set of changes, there's no "half-applied" middle ground and no drift. If two machines are built from the same description, they are the same machine.

The Nix store: where everything actually lives

Here's the first genuinely strange thing about NixOS, and the one that unlocks everything else. Software is not installed into /usr/bin, /usr/lib, or the other familiar global locations. Instead, every package — every version of every package — lives in its own immutable directory under /nix/store, with a name like this:

A store path, dissected

/nix/store/0d71ygfwbmy1xjlbj1v027dfmy9bcyd3-hello-2.12.1
           └──────────── hash ────────────┘ └── name ──┘

The 32-character prefix is a hash; the rest is a human-readable name and version. Inside that directory you'll find the usual bin/, share/, and so on — but scoped to just this one package build rather than dumped into a shared global tree.

Two things make this powerful. First, store paths are immutable: once built, a directory in /nix/store is never modified in place. NixOS even mounts the store read-only. An "upgrade" doesn't overwrite the old files — it builds a new store path alongside the old one. That's why multiple versions of the same package coexist happily: they simply have different hashes and live in different directories.

Second, the hash isn't random. It's derived from everything that went into building the package — the source code, the compiler, the build flags, every dependency, all the way down. (Under the hood the hash is a SHA-256 digest, folded down to 160 bits and written in a filename-safe base-32 alphabet.) This is called input-addressing: change any input, even a single compiler flag, and you get a different hash and therefore a different store path. Nothing collides, and the path itself is a fingerprint of exactly how the thing was built.

/nix/store — immutable, hash-named paths 0d71yg…-hello-2.12.1 GNU hello, current version 9a3kqm…-hello-2.10 older version, still on disk c7p2wd…-python3-3.12.4 one Python v1r8fq…-python3-3.11.9 a different Python, coexisting m4z0tk…-ripgrep-14.1.0 no version conflicts, ever your profile / PATH a tree of symlinks that select which store paths are "installed" for you bin/hello → …hello-2.12.1 Symlinks — not copies — point PATH at the exact builds this system uses.

So if nothing is in /usr/bin, how does typing hello find the program? Through profiles, which are just carefully built trees of symlinks. Your system profile contains a bin/ directory full of symlinks, each pointing at the right store path, and that directory is what's on your PATH. "Installing" a package, in NixOS terms, means rebuilding this symlink tree so it now includes a link into the package's store path. Nothing is copied; the store path was already there. Switching which version you use is just a matter of pointing the symlinks somewhere else.

Derivations: pure recipes for builds

Where do those store paths come from? Each one is produced by a derivation — Nix's term for a build recipe. A derivation is a precise, self-contained description of how to build one thing: which sources to fetch, which dependencies to use (themselves specified by their exact store paths), which build commands to run, and what environment to run them in. Crucially, derivations aim to be pure: a build is not allowed to reach out to the network at random or read arbitrary files from your machine. Its only inputs are the ones the derivation declares.

Purity is what buys reproducibility. Because the output path's hash is computed from all of a derivation's inputs, the rule becomes simple and absolute:

The reproducibility guarantee

Same inputs → same output path. If two people, on two continents, a year apart, evaluate the same derivation, they compute the same hash and therefore the same /nix/store path. If that path already exists locally or in a binary cache, Nix skips the build entirely and just downloads (or reuses) it. Builds become a pure function of their inputs.

We're keeping derivations at arm's length for now — they get a full hands-on article, Packaging & Derivations, later in the series. The only thing you need to carry forward is the intuition: a store path is the output of a pure build recipe, and its hash is a fingerprint of every input that recipe used.

Generations, atomic upgrades, and rollback

This is the payoff — the feature that makes NixOS feel less like a distro and more like a time machine. Every time you run nixos-rebuild switch, Nix builds a complete new system (a new set of store paths and a new profile of symlinks) and registers it as a numbered generation. Generation 1 was your first successful build; generation 2 came after your next change; and so on. Old generations aren't deleted — they sit in the store, fully intact, until you explicitly garbage-collect them.

A "generation" is, concretely, just another symlink. There's a system profile symlink that points at whichever generation is current. Making a new build take effect is therefore atomic: Nix builds everything first, off to the side, and only at the very end swaps that one symlink to point at the new generation. There's no window where the system is half-upgraded — either you're fully on the old generation or fully on the new one. If the build fails partway through, the swap never happens and your running system is untouched.

System generations over time gen 1 first build gen 2 added SSH gen 3 known-good gen 4 broke boot current → rollback: reboot into gen 3, the last known-good system

Now put those pieces together. Suppose you upgrade, gen 4 gets built, the symlink flips — and the new kernel refuses to boot, or your display manager won't start. On any other distro this is a bad evening. On NixOS, the bootloader menu lists every generation. You reboot, pick generation 3 from the list, and you're back in your last known-good system in under a minute — because gen 3's store paths were never deleted and its profile of symlinks is still sitting right there. Once you're back in, nixos-rebuild --rollback switch makes that the default again.

Why NixOS is so hard to truly break

A bad upgrade can't destroy your working system, because the working system's files are immutable and were never overwritten — the new build lives at entirely new store paths. The worst a broken generation can do is fail to boot, and the fix is to pick the previous entry from the boot menu. Your configuration is in a file, your old builds are on disk, and switching between them is atomic. There is very little left to go permanently wrong.

This is also why experimenting is cheap. Want to try a risky kernel option or a bleeding-edge package? Rebuild, test it, and if you don't like it, roll back. The old generation is right there waiting.

Flakes, and why this series is flakes-first

There's one loose end. We've said "same inputs → same output," but what pins the inputs in the first place — the exact version of the entire nixpkgs package collection your system is built from? Historically that was done with channels: a global, mutable pointer that you'd update with a command, updating everything at once in a way that was easy to forget and hard to reproduce on another machine.

Flakes are the modern replacement, and they're where this series lives. A flake is a directory with a flake.nix file that explicitly declares its inputs (typically "nixpkgs at such-and-such") and its outputs (such as "the NixOS configuration for the host bluewhale"). The first time you build it, Nix writes a flake.lock file recording the exact commit of every input:

Channels (the old way)

A global, mutable pointer shared across the machine. "What version am I on?" depends on when you last ran nix-channel --update. Reproducing a build on another machine means hoping its channels match yours.

Flakes (this series)

Inputs are declared in flake.nix and pinned to exact revisions in flake.lock. Commit both to git and anyone — including future you — rebuilds the byte-for-byte same system. No hidden global state.

That flake.lock is the whole point: with it committed alongside your configuration, "build my system" becomes fully reproducible across machines and across time, with no reliance on hidden global state. Flakes also standardize how configurations reference each other, which makes sharing and composing setups far easier.

One honest caveat: flakes are still officially an experimental feature of Nix. They landed in Nix 2.4 back in November 2021 and, as of this writing, remain gated behind an experimental-features flag rather than being stabilized. In practice, though, they've become the de-facto standard — the overwhelming majority of new configurations, and most current community documentation, are written with flakes. So that's what we'll use, start to finish. Enabling them is a one-line setting, which we'll cover when we build your first flake.

Key Takeaways

  • Declarative, not imperative: you describe the system you want in files, and nixos-rebuild makes the machine match. Applying the same description is idempotent.
  • The Nix store is the foundation: every package version lives at its own immutable, hash-named path under /nix/store, and profiles are just symlink trees that select which paths are "installed."
  • Input-addressing: a store path's hash is a fingerprint of every input that built it, so many versions coexist without conflict and nothing installs into /usr/bin.
  • Derivations are pure build recipes: same inputs → same output path, which is what makes builds reproducible and cacheable.
  • Generations make NixOS nearly unbreakable: each rebuild is a new numbered generation; switching is an atomic symlink flip; a bad upgrade is undone by picking the previous entry from the boot menu.
  • Flakes pin your inputs: flake.nix + flake.lock make a configuration reproducible across machines and time. Still "experimental," but the de-facto standard — and the basis for this whole series.

You now have the model: a system is a description, that description builds immutable store paths, and generations let you move between whole systems at will. But those descriptions are written in a real language — one with a few features that trip up newcomers. Next, we'll learn just enough of the Nix language to read and write a configuration with confidence.