1<chapter xmlns="http://docbook.org/ns/docbook"
2 xmlns:xlink="http://www.w3.org/1999/xlink"
3 xmlns:xi="http://www.w3.org/2001/XInclude"
4 version="5.0"
5 xml:id="sec-nix-gc">
6
7<title>Cleaning the Nix Store</title>
8
9<para>Nix has a purely functional model, meaning that packages are
10never upgraded in place. Instead new versions of packages end up in a
11different location in the Nix store (<filename>/nix/store</filename>).
12You should periodically run Nix’s <emphasis>garbage
13collector</emphasis> to remove old, unreferenced packages. This is
14easy:
15
16<screen>
17$ nix-collect-garbage
18</screen>
19
20Alternatively, you can use a systemd unit that does the same in the
21background:
22
23<screen>
24# systemctl start nix-gc.service
25</screen>
26
27You can tell NixOS in <filename>configuration.nix</filename> to run
28this unit automatically at certain points in time, for instance, every
29night at 03:15:
30
31<programlisting>
32nix.gc.automatic = true;
33nix.gc.dates = "03:15";
34</programlisting>
35
36</para>
37
38<para>The commands above do not remove garbage collector roots, such
39as old system configurations. Thus they do not remove the ability to
40roll back to previous configurations. The following command deletes
41old roots, removing the ability to roll back to them:
42<screen>
43$ nix-collect-garbage -d
44</screen>
45You can also do this for specific profiles, e.g.
46<screen>
47$ nix-env -p /nix/var/nix/profiles/per-user/eelco/profile --delete-generations old
48</screen>
49Note that NixOS system configurations are stored in the profile
50<filename>/nix/var/nix/profiles/system</filename>.</para>
51
52<para>Another way to reclaim disk space (often as much as 40% of the
53size of the Nix store) is to run Nix’s store optimiser, which seeks
54out identical files in the store and replaces them with hard links to
55a single copy.
56<screen>
57$ nix-store --optimise
58</screen>
59Since this command needs to read the entire Nix store, it can take
60quite a while to finish.</para>
61
62</chapter>