From fa004b9eecf229f6bd49982745b2c8d2aa3816ee Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Mon, 12 Feb 2024 18:08:41 +0100 Subject: [PATCH] fix: automatic garbage collection --- NixOS.md | 64 +++++++++++++++++++++++++++++---------------- modules/base-gc.nix | 41 +++++++++++++++++++++++++++++ modules/base.nix | 20 +++++++------- 3 files changed, 92 insertions(+), 33 deletions(-) create mode 100644 modules/base-gc.nix diff --git a/NixOS.md b/NixOS.md index b511989..64cefe4 100644 --- a/NixOS.md +++ b/NixOS.md @@ -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: diff --git a/modules/base-gc.nix b/modules/base-gc.nix new file mode 100644 index 0000000..9364d24 --- /dev/null +++ b/modules/base-gc.nix @@ -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 + ''; + }; +} diff --git a/modules/base.nix b/modules/base.nix index b5067df..18f9692 100644 --- a/modules/base.nix +++ b/modules/base.nix @@ -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 `: https://nixos.org/manual/nix/stable/command-ref/nix-env/delete-generations#generations-time - # There, two meanings of are described. The second one is: - # +: 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;