Storage optimization: Difference between revisions
→Garbage collection: Small improvement on phrasing |
→Garbage collection: In general a user reading this section will care about removing old NixOS generations first. Thus, I moved the section above the GC roots details |
||
Line 26: | Line 26: | ||
By default, the Nix store will not remove any entries that are no longer used, thus it can accumulate derivations you might no longer need.<ref group="cf.">[https://nixos.org/nix/manual/#sec-garbage-collection Nix Manual, 11. Garbage Collection]</ref> They can be deleted with {{ic|nix-collect-garbage}} <ref group="cf.">{{man|nix-collect-garbage|sec=1}}</ref> or {{ic|nix-store --gc}}.<ref group="cf.">{{man|nix-store|sec=1}}, under {{ic|OPERATION --GC}}</ref> | By default, the Nix store will not remove any entries that are no longer used, thus it can accumulate derivations you might no longer need.<ref group="cf.">[https://nixos.org/nix/manual/#sec-garbage-collection Nix Manual, 11. Garbage Collection]</ref> They can be deleted with {{ic|nix-collect-garbage}} <ref group="cf.">{{man|nix-collect-garbage|sec=1}}</ref> or {{ic|nix-store --gc}}.<ref group="cf.">{{man|nix-store|sec=1}}, under {{ic|OPERATION --GC}}</ref> | ||
=== Removing old generations === | |||
NixOS keeps old configurations of your system around so that you can always rollback to a previous configuration if something goes wrong. You can also select which generation to boot into via GRUB. However these previous generations are [[Storage optimization#Garbage collection roots|GC roots]] (see below) that can keep around old, unnecessary software in your nix store. You can check what system generations you have by:<syntaxhighlight lang="console">$ sudo nix-env -p /nix/var/nix/profiles/system --list-generations | |||
... | |||
58 2021-09-04 02:56:54 | |||
59 2021-09-05 07:12:43 | |||
60 2021-09-05 22:12:13 (current)</syntaxhighlight> | |||
You can remove all other NixOS generations besides the current one: | |||
<syntaxhighlight lang="console"> | |||
$ sudo nix-collect-garbage -d | |||
... | |||
4394 store paths deleted, 3467.28 MiB freed | |||
</syntaxhighlight> | |||
There are also user-specific generations for different things (eg. home-manager). These can be removed with: | |||
<syntaxhighlight lang="console"> | |||
$ nix-collect-garbage -d | |||
</syntaxhighlight> | |||
=== Garbage collection roots === | |||
Note that if a result file still exists in the file system, and your Nix configuration has both <code>keep-outputs = true</code> and <code>keep-derivations = true</code>, all the dependencies used to build it will be kept. To see which result files prevent garbage collection, run: | Note that if a result file still exists in the file system, and your Nix configuration has both <code>keep-outputs = true</code> and <code>keep-derivations = true</code>, all the dependencies used to build it will be kept. To see which result files prevent garbage collection, run: | ||
Line 31: | Line 52: | ||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
$ nix-store --gc --print-roots | $ nix-store --gc --print-roots | ||
... | ... | ||
/nix/var/nix/profiles/per-user/root/channels-4-link -> /nix/store/254b6pkhhnjywvj5c0lp2vdai8nz4p0g-user-environment | /nix/var/nix/profiles/per-user/root/channels-4-link -> /nix/store/254b6pkhhnjywvj5c0lp2vdai8nz4p0g-user-environment | ||
/nix/var/nix/profiles/system-398-link -> /nix/store/wmndyzzrbc9fyjw844jmvzwgwgcinq7s-nixos-system-iron-16.0916.09pre.custom | /nix/var/nix/profiles/system-398-link -> /nix/store/wmndyzzrbc9fyjw844jmvzwgwgcinq7s-nixos-system-iron-16.0916.09pre.custom | ||
Line 97: | Line 100: | ||
Look for system derivations in particular. Those are created on many occasions, for example when running <code>nixos-rebuild build-vm</code> | Look for system derivations in particular. Those are created on many occasions, for example when running <code>nixos-rebuild build-vm</code> | ||
=== Reboot === | === Reboot === | ||
As you see, the reference in <code>/run/booted-system</code> is a GC root, so it won't be cleared until reboot. If you don't want to reboot, just <code>rm /run/booted-system</code> that link and rerun <code>sudo nix-collect-garbage</code>. | As you see, the reference in <code>/run/booted-system</code> is a GC root, so it won't be cleared until reboot. If you don't want to reboot, just <code>rm /run/booted-system</code> that link and rerun <code>sudo nix-collect-garbage</code>. | ||