fix: automatic garbage collection

This commit is contained in:
Daniel Langbein 2024-02-12 18:08:41 +01:00
parent 09dfe9d227
commit fa004b9eec
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
3 changed files with 92 additions and 33 deletions

View File

@ -12,14 +12,17 @@ This document contains general notes about NixOS that are independent of my NixO
* [niv: Dependency management](#niv-dependency-management)
* [Add Home Manager with niv](#add-home-manager-with-niv)
* [Add NUR with niv](#add-nur-with-niv)
* [Garbage collection](#garbage-collection)
* [System information](#system-information)
* [Search for packages](#search-for-packages)
* [Search for options](#search-for-options)
* [Search which package owns a file](#search-which-package-owns-a-file)
* [List files of package](#list-files-of-package)
* [Package runtime dependency report](#package-runtime-dependency-report)
* [Compare two versions of NixOS system profile](#compare-two-versions-of-nixos-system-profile)
* [Nix profiles](#nix-profiles)
* [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)
* [Compare two system profiles](#compare-two-system-profiles)
* [NixOS configuration debugging](#nixos-configuration-debugging)
* [Show Nix configuration](#show-nix-configuration)
* [Evaluate NixOS configuration to JSON](#evaluate-nixos-configuration-to-json)
@ -144,25 +147,6 @@ niv add nix-community/home-manager -n home-manager -b release-23.05
niv add nix-community/NUR -n NUR
```
## Garbage collection
* 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
This is automated in [base.nix](modules/base.nix) with the `nix.gc` option.
Run manually for all profiles:
```shell
sudo nix-collect-garbage --delete-older-than 14d
```
Remove leftover EFI entries of removed generations:
```shell
sudo /run/current-system/bin/switch-to-configuration boot
```
## System information
```shell
@ -476,7 +460,43 @@ runtime dependencies of firefox-121.0.1:
- libXau-1.0.11 (no license) maintained by nobody
```
## Compare two versions of NixOS system profile
## Nix profiles
### List system profiles
```shell
sudo nix-env --list-generations --profile /nix/var/nix/profiles/system
```
### Garbage collection - Keep most recent X system profiles
A NixOS config example is given in [base.nix](modules/base.nix).
```shell
sudo nix-env --delete-generations +7 --profile /nix/var/nix/profiles/system
```
### Garbage collection - Delete system profiles older than X days
* 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:
```shell
sudo nix-collect-garbage --delete-older-than 14d
```
Remove leftover EFI entries of removed generations:
```shell
sudo /run/current-system/bin/switch-to-configuration boot
```
### Compare two system profiles
Get latest system profile. This is the profile (usually) being active after booting the system:

41
modules/base-gc.nix Normal file
View File

@ -0,0 +1,41 @@
{ config, pkgs, ... }:
{
# Based on https://github.com/NixOS/nixpkgs/blob/nixos-23.11/nixos/modules/services/misc/nix-gc.nix
assertions = [{
assertion = config.nix.enable;
message = ''nix-gc-7 requires nix.enable'';
}];
systemd.timers."nix-gc-7" = {
wantedBy = [ "timers.target" ];
partOf = [ "nix-gc-7.service" ];
timerConfig = {
OnCalendar = "weekly";
Persistent = true;
AccuracySec = "2d";
RandomizedDelaySec = "1d";
};
};
systemd.services."nix-gc-7" = {
description = "Keep only the most recent 7 system generations";
conflicts = [ "shutdown.target" "sleep.target" ];
before = [ "shutdown.target" "sleep.target" ];
serviceConfig = {
Type = "oneshot";
PrivateTmp = true;
Nice = 19;
IOSchedulingClass = "idle";
};
script = ''
set -eu -o pipefail
${config.nix.package.out}/bin/nix-env --delete-generations +7 --profile /nix/var/nix/profiles/system
'';
};
}

View File

@ -74,18 +74,16 @@
nix.settings.auto-optimise-store = true;
# Keep only most recent 7 system generations
imports = [ ./base-gc.nix ];
#
# Alternatively: Delete generations older than 7 days
# https://nixos.wiki/wiki/Storage_optimization#Automation
nix.gc = {
automatic = true;
dates = "weekly";
# Options given to nix-collect-garbage.
# They are documented here: https://nixos.org/manual/nix/stable/command-ref/nix-collect-garbage
# The `--delete-older-than <period` option refers the manual for `nix-env --delete-generations <period>`: https://nixos.org/manual/nix/stable/command-ref/nix-env/delete-generations#generations-time
# There, two meanings of <period> are described. The second one is:
# +<number>: Keep the last number generations, along with any newer than current.
# TODO: +7 does currently not work. There is an open issue: https://github.com/NixOS/nixpkgs/issues/282884
options = "--delete-older-than +7";
};
#nix.gc = {
# automatic = true;
# dates = "weekly";
# options = "--delete-older-than 7d";
#};
# Delete all files in /tmp during boot.
boot.tmp.cleanOnBoot = true;