The Nix store (/nix/store/) is the single directory where Nix puts
everything it installs: each package lives there in a subdirectory identified
by a cryptographic hash. Two identical installations produce exactly the
same path, two different versions coexist without conflict. On top of it,
profiles expose the binaries through symbolic links, and generations
keep a complete history that allows an instant rollback. This model removes
dependency conflicts, makes every operation atomic and lets you reclaim disk
space on demand. The outputs of this guide were captured on Nix 2.35.2.
What you will learn
- Understand the structure of the Nix store and the role of the hash in each path
- Explore a package in the store and its closure (complete dependency tree)
- Work with user profiles and see how binaries are exposed
- Create, list and switch between generations
- Perform an instant rollback to a previous state
- Free space with the garbage collector and store optimisation
In what context?
When you install a package with nix profile add, you do not touch the classic
system directories (/usr/bin, /usr/lib). The package lands in the store,
and a symbolic link appears in your profile. Understanding this mechanism is
essential to:
- diagnose why a binary is not found after installation,
- free disk space without breaking active packages,
- go back after a failed update,
- share a reproducible environment between machines,
- understand closures to optimise the size of your deployments.
The store: /nix/store/
The store alone replaces /usr/bin, /usr/lib and /usr/share: Nix never
writes into those directories. Every package it holds is read-only, and in
a multi-user installation the directory belongs to root (group nixbld),
even when you install a package as a normal user: the daemon writes for
you. The four subsections detail its naming convention, the content of a
package, the reason for its immutability and how its size evolves.
One directory, one hash
Every item of the Nix store lives in a path of the form:
/nix/store/<hash-32-characters>-<name>-<version>The hash is computed from all the inputs of the build: source code, dependencies, build flags, patches. If one of these inputs changes, the hash changes, and a new path is created.
ls /nix/store/ | grep ripgrepvli6118fdbngwy46j1w741ansmrzfszr-ripgrep-15.2.0Anatomy of a package in the store
Every store directory follows an FHS-like (but isolated) structure:
ls -la /nix/store/vli6118fdbngwy46j1w741ansmrzfszr-ripgrep-15.2.0/dr-xr-xr-x 4 root root 4096 Jan 1 1970 .drwxrwxr-t 173 root nixbld 143360 Oct 1 11:58 ..dr-xr-xr-x 2 root root 4096 Jan 1 1970 bindr-xr-xr-x 6 root root 4096 Jan 1 1970 shareThe rg binary is in bin/, the man pages in share/. But unlike a
Debian package or an RPM, nothing is copied into /usr/bin, the
package lives exclusively in the store.
Store immutability
Store directories are read-only (dr-xr-xr-x). Once built, a package can
no longer be modified, only deleted by the garbage collector. This
immutability guarantees that no process can silently corrupt a dependency. On
NixOS, the store is additionally mounted read-only; on another distribution,
the file permissions provide this protection.
Store size
After a few installations, the store grows fast, because every package pulls in its dependencies, which are also copied into the store:
du -sh /nix/store/1.2G /nix/store/ls /nix/store/ | wc -l2242Do not worry about this size: the garbage collector (described below) lets you reclaim the space of unused packages.
Closures: the complete dependency tree
A closure is the set of all the store paths needed for a package to work.
When Nix installs ripgrep, it does not just download the binary, it makes
sure that all its dependencies (glibc, pcre2, gcc-lib…) are present in the
store.
nix path-info -rsSh /nix/store/vli6118fdbngwy46j1w741ansmrzfszr-ripgrep-15.2.0/nix/store/g60m3aky9f59wgy1gi0y06xwmbw19d6m-xgcc-16.2.0-libgcc 197.5 KiB 197.5 KiB/nix/store/jp8ql2fnd61xhvb4vsfp0bqwrzf16qrp-libunistring-1.4.2 2.0 MiB 2.0 MiB/nix/store/q0lbdx5yv93a3md74kjhm0v2kljjii89-libidn2-2.3.8 363.6 KiB 2.3 MiB/nix/store/h4wfwic161kxrr74jlzla5lsm28hgary-glibc-2.44-25 33.9 MiB 36.5 MiB/nix/store/7v8b5idfzwfl06hx6n0cvd5ikd5p0sbg-pcre2-10.48 2.0 MiB 38.5 MiB/nix/store/q2mkz6ca1alqfqf750dygvakmq8kjrwz-gcc-16.2.0-libgcc 197.6 KiB 197.6 KiB/nix/store/j7qx4s4mr17j1wqgvqdzj33lmrnzb387-gcc-16.2.0-lib 10.3 MiB 47.0 MiB/nix/store/vli6118fdbngwy46j1w741ansmrzfszr-ripgrep-15.2.0 6.7 MiB 55.7 MiBThe first column of figures gives the own size of the package, the second
its closure size (the package + all its transitive dependencies). Here,
ripgrep weighs 6.7 MiB but its complete closure reaches 55.7 MiB, mainly
because of glibc.
Profiles: exposing the binaries
A package present in the store is not executable from your shell for all
that: nothing was added to your PATH. That is the job of the profile, an
aggregation directory that gathers, through symbolic links, the binaries of the
packages you explicitly asked for. This indirection is what makes rollback
possible: changing version means repointing a single link, without copying
or deleting anything.
The chain of symbolic links
Installing a package copies nothing into /usr/bin. Nix creates a chain of
symbolic links that connects your $PATH to the store:
~/.nix-profile → ~/.local/state/nix/profiles/profile └→ profile-2-link → /nix/store/z4q4...-profile/ └→ bin/ ├── jq → /nix/store/m3y7...-jq-1.8.2-bin/bin/jq └── rg → /nix/store/vli6...-ripgrep-15.2.0/bin/rgLet us check on a machine where ripgrep then jq were added to the profile:
ls -la ~/.nix-profile~/.nix-profile -> /home/lab/.local/state/nix/profiles/profilels -la ~/.local/state/nix/profiles/profile -> profile-2-linkprofile-1-link -> /nix/store/jp7s18kqhhg89mbw6yy2arlz4hydj32m-profileprofile-2-link -> /nix/store/z4q4b9x6bcmafk8fhmixc3id8rq1drzf-profilels ~/.nix-profile/bin/jq rgEach profile-N-link points to a store directory that aggregates all
the packages of that generation. When you add a package, Nix creates a
new aggregation directory in the store and points the profile link to
it.
System profile vs user profile
Nix manages two levels of profiles, and they have neither the same owner nor
the same consequences when something goes wrong. The user profile lives in
your HOME and only concerns you; the system profile holds the Nix
installation itself. Remember the bottom row, it is the one that breaks a
machine when it is cleaned up without thinking:
| Profile | Path | Usage |
|---|---|---|
| User | ~/.local/state/nix/profiles/profile | Packages installed by the current user |
| System | /nix/var/nix/profiles/default | Packages installed for the whole system (includes nix itself) |
Generations: history and rollback
Nix never modifies a profile in place: it builds a new version and moves a link. Each of these versions is a generation, numbered and kept as long as you do not delete it explicitly. Going back after a failed update therefore needs neither a backup nor a reinstallation, and takes the time of a symbolic link change.
Every change creates a generation
On every addition or removal of a package, Nix creates a new generation of the profile. The previous generation stays intact in the store, nothing is overwritten.
Let us install jq and look at the history:
nix profile add nixpkgs#jqnix profile historyVersion 1 (2026-10-01): flake:nixpkgs#legacyPackages.x86_64-linux.ripgrep: ∅ -> 15.2.0
Version 2 (2026-10-01) <- 1: flake:nixpkgs#legacyPackages.x86_64-linux.jq: ∅ -> 1.8.2-bin, 1.8.2-manVersion 1 contains only ripgrep. Version 2 adds jq and refers to
Version 1 as its parent (<- 1).
Instant rollback
To go back to the previous generation:
nix profile rollbackswitching profile from version 2 to 1After the rollback, only rg is available in the profile:
ls ~/.nix-profile/bin/rgjq has disappeared from the profile, but it is still present in the
store. Nix only changed the profile symbolic link to point to
profile-1-link instead of profile-2-link.
Switch to a specific generation
To go back to generation 2:
nix profile rollback --to 2switching profile from version 1 to 2ls ~/.nix-profile/bin/jq rgList the packages of the active profile
The output only shows the explicitly installed packages, not their
dependencies. For each of them, it gives the requested source, the
nixpkgs revision actually locked, and the associated store paths (here jq
provides two, one for the binary, one for the man pages).
nix profile listName: jqFlake attribute: legacyPackages.x86_64-linux.jqOriginal flake URL: flake:nixpkgsLocked flake URL: https://releases.nixos.org/nixpkgs/nixpkgs-26.11pre1082290.b6c8664de9b6/nixexprs.tar.zst?narHash=sha256-k8Fu4c9Z%2B4Nh7mUr0cfw%2B%2BITQiyEhlWxoJBOkI3tOcQ%3DStore paths: /nix/store/gpc36qrvyhyqb6axmj1c0ivx74ni7d8d-jq-1.8.2-man /nix/store/m3y7hqmy20nd1ps6qy8410a326255bf0-jq-1.8.2-bin
Name: ripgrepFlake attribute: legacyPackages.x86_64-linux.ripgrepOriginal flake URL: flake:nixpkgsLocked flake URL: https://releases.nixos.org/nixpkgs/nixpkgs-26.11pre1082290.b6c8664de9b6/nixexprs.tar.zst?narHash=sha256-k8Fu4c9Z%2B4Nh7mUr0cfw%2B%2BITQiyEhlWxoJBOkI3tOcQ%3DStore paths: /nix/store/vli6118fdbngwy46j1w741ansmrzfszr-ripgrep-15.2.0Freeing space: garbage collection and optimisation
The store never empties itself, it has no automatic purge. Cleaning happens in two steps, and the order matters: first delete the old generations, which removes the references that protected the packages, then run the garbage collector, which erases what is no longer referenced. A deduplication pass then trims what remains.
Why the store grows
The store accumulates packages over time: old generations, packages
tried with nix shell, intermediate build dependencies. As long as a GC
root references a path, it is protected from deletion.
The main GC roots are:
- the profile generations (
profile-1-link,profile-2-link…), - the
nix buildresults linked in./result, registered under/nix/var/nix/gcroots/auto/, - the system profiles.
Delete old generations
Before running the garbage collector, delete the obsolete generations to remove them from the GC roots:
nix profile wipe-history --older-than 1dThis command deletes the generations older than one day. The active generation is always kept, even if it is older, and generations younger than one day stay too: on a profile created the same day, the command therefore deletes nothing.
Run the garbage collector
The command walks the store and deletes every path that no GC root references. It prints each deletion then a figure-based summary, and it is irreversible: an erased package will be downloaded or recompiled again if it becomes necessary.
nix store gcdeleting '/nix/store/i4q069m3d29gi7dw1ijhjrp4hxw1c2l4-module-dir.patch'deleting '/nix/store/d77svhhwwqkvacx0gdyscjp4jdfjkh9d-gcc-15.patch'...deleting unused links...533 store paths deleted, 242.6 MiB freedThe GC reclaimed 242 MiB by deleting 533 paths that no root
referenced any more. To know beforehand what it would delete,
nix store gc --dry-run only prints the count.
du -sh /nix/store/660M /nix/store/The store went from 1.2 GB to 660 MB.
Optimise the store by deduplication
Even after the GC, some files are duplicated between packages (shared
libraries, identical configuration files). The optimise command replaces them
with hard links:
nix store optimise15.2 MiB freed by hard-linking 1230 filesdu -sh /nix/store/647M /nix/store/Summary of the maintenance commands
Here are the four commands to remember for periodic maintenance. The first two
are chained in this order; the last one, --max, is for when you want to
reclaim a given amount without emptying everything that can be reclaimed.
| Command | Effect |
|---|---|
nix profile wipe-history --older-than 7d | Deletes the generations older than 7 days, except the active one |
nix store gc | Deletes unreferenced paths |
nix store optimise | Deduplicates identical files with hard links |
nix store gc --max 1G | Frees at most 1 GB |
Summary of the data model
The diagram puts the three layers back in the order your shell goes through them. There is only one mutable link in the whole chain, the one that designates the active generation; everything below it is immutable. This asymmetry explains both the atomicity of installations and why rollback costs nothing.
Key points
- The store (
/nix/store/) holds every package, each identified by a unique hash, two versions coexist without conflict. - A closure is the complete set of dependencies of a package; it is the deployment unit of Nix.
- Profiles expose the binaries through chains of symbolic links, without touching
/usr/bin. - Every change to the profile creates a new generation; rollback is instant and atomic.
- The garbage collector only deletes paths without a GC root, your active packages are always protected.
nix store optimisededuplicates identical files with hard links to reduce the disk footprint.
Knowledge check
Check that the essentials of this guide are acquired. The questions only cover what has just been explained here.
Knowledge check
Check what you have learned with this interactive quiz
How it works
- The timer starts when you click Start
- Multiple choice, true/false and short answer questions
- You can move between questions
- Detailed results are shown at the end
Starts the quiz and the timer
Review
(0/0)Result saved on this device. It will be sent to your account at the next sync. Sync now
Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: Modern Nix commands: search, shell, run, build, profile
A new full quiz with random questions
Work again only on the questions you missed