fix: nix garbage collection

This commit is contained in:
Daniel Langbein 2024-11-11 11:48:49 +01:00
parent 426decfc4e
commit e19db18b71
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
2 changed files with 39 additions and 32 deletions

View File

@ -21,8 +21,8 @@ This document contains general notes about NixOS that are independent of my NixO
* [Nix profiles](#nix-profiles)
* [Switch to previous system profile](#switch-to-previous-system-profile)
* [List system profiles](#list-system-profiles)
* [Garbage collection - Keep most recent X system profiles](#garbage-collection---keep-most-recent-x-system-profiles)
* [Garbage collection - Delete system profiles older than X days](#garbage-collection---delete-system-profiles-older-than-x-days)
* [Deleting generations - Keep most recent X system profiles](#deleting-generations---keep-most-recent-x-system-profiles)
* [Garbage collection - Delete unreachable store objects](#garbage-collection---delete-unreachable-store-objects)
* [why-depends: Why is a package/dependency installed](#why-depends-why-is-a-packagedependency-installed)
* [Compare two system profiles](#compare-two-system-profiles)
* [NixOS configuration debugging](#nixos-configuration-debugging)
@ -448,33 +448,24 @@ sudo reboot
### List system profiles
Not sure why 432 is marked as current. It is the default generation that gets activated on next boot. The currently active generation is, however, 429.
```shell
sudo nix-env --list-generations --profile /nix/var/nix/profiles/system
```
```console
...
428 2024-03-06 12:37:56
429 2024-03-07 11:27:20
430 2024-03-10 12:48:34
431 2024-03-11 11:18:57
432 2024-03-12 22:19:53 (current)
282 2024-11-10 12:53:25
283 2024-11-11 10:45:46 (current)
```
```shell
ls -l /nix/var/nix/profiles/
```
```console
lrwxrwxrwx 1 root root 43 Dez 14 12:21 default -> /nix/var/nix/profiles/per-user/root/profile
drwxr-xr-x 1 root root 16 Sep 1 2023 per-user
lrwxrwxrwx 1 root root 15 Mär 12 22:19 system -> system-432-link
...
lrwxrwxrwx 1 root root 77 Mär 6 12:37 system-428-link -> /nix/store/damngdyial0i96xa46csdqk36j2njavi-nixos-system-yodaTux-23.11pre-git
lrwxrwxrwx 1 root root 77 Mär 7 11:27 system-429-link -> /nix/store/k7c7576sz868gzjci1z6l6nzmdnpd6xr-nixos-system-yodaTux-23.11pre-git
lrwxrwxrwx 1 root root 77 Mär 10 12:48 system-430-link -> /nix/store/7468m7rgk425kb23aq222a498ynfqdkc-nixos-system-yodaTux-23.11pre-git
lrwxrwxrwx 1 root root 77 Mär 11 11:18 system-431-link -> /nix/store/r51yw36ddfxq8hdsz2mm6axpbqvjaigy-nixos-system-yodaTux-23.11pre-git
lrwxrwxrwx 1 root root 77 Mär 12 22:19 system-432-link -> /nix/store/i9pxh3ax6m0yr7xlk7y7c5xdzp74xqi4-nixos-system-yodaTux-23.11pre-git
lrwxrwxrwx 1 root root 43 Dez 26 2023 default -> /nix/var/nix/profiles/per-user/root/profile
drwxr-xr-x 1 root root 16 Aug 15 2023 per-user
lrwxrwxrwx 1 root root 15 Nov 11 10:45 system -> system-283-link
lrwxrwxrwx 1 root root 77 Nov 10 12:53 system-282-link -> /nix/store/jb5krdbbylwwgvxiadiv6fxja2zgc4h3-nixos-system-yodaTab-24.05pre-git
lrwxrwxrwx 1 root root 77 Nov 11 10:45 system-283-link -> /nix/store/jf7ml8jzxrmg9djhfr8szq2f19hxysn5-nixos-system-yodaTab-24.05pre-git
```
The currently running system. This is the currently active generation. By comparing the store path with the above output, we see that it matches generation 429.
@ -483,33 +474,45 @@ The currently running system. This is the currently active generation. By compar
ls -l /run/current-system
```
```console
lrwxrwxrwx 1 root root 77 Mär 9 12:44 /run/current-system -> /nix/store/k7c7576sz868gzjci1z6l6nzmdnpd6xr-nixos-system-yodaTux-23.11pre-git
lrwxrwxrwx 1 root root 77 Nov 11 10:46 /run/current-system -> /nix/store/jf7ml8jzxrmg9djhfr8szq2f19hxysn5-nixos-system-yodaTab-24.05pre-git
```
### Garbage collection - Keep most recent X system profiles
### Deleting generations - Keep most recent X system profiles
A NixOS config example is given in [base.nix](modules/base.nix).
https://nix.dev/manual/nix/2.18/command-ref/nix-env/delete-generations
> Periodically deleting old generations is important to make garbage collection effective. The is because profiles are also garbage collection roots.
A NixOS config example is given in [nix-gc.nix](modules/nix-gc.nix).
```shell
sudo nix-env --delete-generations +7 --profile /nix/var/nix/profiles/system
```
TODO: Should we run `sudo nix-collect-garbage --delete-old` after the above?
After deleting some generations, it is still necessary to collect garbage in order to free up your system. See below.
### Garbage collection - Delete system profiles older than X days
### Garbage collection - Delete unreachable store objects
https://nix.dev/manual/nix/2.18/command-ref/nix-collect-garbage
> `nix-collect-garbage` deletes all unreachable store objects in the Nix store to clean up your system.
* https://nixos.org/manual/nix/stable/package-management/garbage-collection.html
* https://discourse.nixos.org/t/why-doesnt-nix-collect-garbage-remove-old-generations-from-efi-menu/17592/4
A NixOS config example is given in [base.nix](modules/base.nix).
Run manually for all profiles:
First delete unreachable store objects:
```shell
sudo nix-collect-garbage --delete-older-than 14d
# Delete unreachable store objects
sudo nix-collect-garbage
# Deletes all except the currently active generation prior to deleting unreachable store objects
#sudo nix-collect-garbage --delete-old
# Deletes all generations older than 14d prior to deleting unreachable store objects
#sudo nix-collect-garbage --delete-older-than 14d
```
Remove leftover EFI entries of removed generations:
Then remove leftover EFI entries of deleted generations:
```shell
sudo /run/current-system/bin/switch-to-configuration boot

View File

@ -1,10 +1,11 @@
{ config, pkgs, ... }:
{
# Keep only the 7 most recent system generations.
# Delete all system generations except the 7 most recent ones.
# Then delete all unreachable store objects.
#
# Based on https://github.com/NixOS/nixpkgs/blob/nixos-23.11/nixos/modules/services/misc/nix-gc.nix
#
# Alternatively: Delete generations older than 7 days
# Alternative: Delete generations older than 7 days and then delete unreachable store objects
# https://nixos.wiki/wiki/Storage_optimization#Automation
#nix.gc = {
# automatic = true;
@ -29,8 +30,10 @@
};
};
# View generated systemd service config with:
# cat "$(systemctl show -P FragmentPath nix-gc-7.service)" | grep ExecStart
systemd.services."nix-gc-7" = {
description = "Keep only the most recent 7 system generations";
description = "rm old system generations and unreachable store objects";
conflicts = [ "shutdown.target" "sleep.target" ];
before = [ "shutdown.target" "sleep.target" ];
@ -45,6 +48,7 @@
script = ''
${config.nix.package.out}/bin/nix-env --delete-generations +7 --profile /nix/var/nix/profiles/system
${config.nix.package.out}/bin/nix-collect-garbage
'';
};
}