From 54e0f94af5905864f3836e370d5722f72ccd491d Mon Sep 17 00:00:00 2001 From: Julian Mutter Date: Fri, 13 Mar 2026 07:51:57 +0100 Subject: [PATCH] Add garbage-collect for home-manager --- homes/julian/global/default.nix | 7 ++++ homes/julian/hm-standalone-config.nix | 14 ++++++++ modules/home-manager/default.nix | 1 + modules/home-manager/hm-expire.nix | 46 +++++++++++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 modules/home-manager/hm-expire.nix diff --git a/homes/julian/global/default.nix b/homes/julian/global/default.nix index bb8021d..5e9f2c5 100644 --- a/homes/julian/global/default.nix +++ b/homes/julian/global/default.nix @@ -35,6 +35,13 @@ # systemd.user.startServices = "sd-switch"; # TODO: what is this + # Expire old hm generations + hm-expire = { + enable = true; + dates = "weekly"; + expire = "-30 days"; + }; + programs = { home-manager.enable = true; git.enable = true; diff --git a/homes/julian/hm-standalone-config.nix b/homes/julian/hm-standalone-config.nix index 635bb62..d9b6f99 100644 --- a/homes/julian/hm-standalone-config.nix +++ b/homes/julian/hm-standalone-config.nix @@ -39,6 +39,20 @@ "ca-derivations" ]; + # Expire old hm generations + hm-expire = { + enable = true; + dates = "weekly"; + expire = "-30 days"; + }; + # Remove unused packets + services.nix-gc = { + enable = true; + automatic = true; + frequency = "weekly"; + persistent = true; + }; + # nix.settings. # warn-dirty = false; # TODO: do I want this # # Ensure we can still build when missing-server is not accessible diff --git a/modules/home-manager/default.nix b/modules/home-manager/default.nix index a6a800f..650c4ac 100644 --- a/modules/home-manager/default.nix +++ b/modules/home-manager/default.nix @@ -5,4 +5,5 @@ colors = import ./colors.nix; hostname = import ./hostname.nix; non-nixos = import ./non-nixos.nix; + hm-expire = import ./hm-expire.nix; } diff --git a/modules/home-manager/hm-expire.nix b/modules/home-manager/hm-expire.nix new file mode 100644 index 0000000..999757e --- /dev/null +++ b/modules/home-manager/hm-expire.nix @@ -0,0 +1,46 @@ +{ + config, + lib, + pkgs, + ... +}: let + cfg = config.hm-expire; +in { + options.hm-expire = { + enable = lib.mkEnableOption "Whether to enable hm-expire"; + dates = lib.mkOption { + type = lib.types.str; + default = "weekly"; + }; + expire = lib.mkOption { + type = lib.types.str; + default = "-30 days"; + }; + }; + + config = lib.mkIf cfg.enable { + # This creates a user-level systemd service + systemd.user.services.cleanup-home-manager = { + Unit = { + Description = "Cleanup old Home Manager generations"; + }; + Service = { + Type = "oneshot"; + ExecStart = "${pkgs.bash}/bin/bash -c '${pkgs.home-manager}/bin/home-manager expire-generations \"${cfg.expire}\"'"; + }; + }; + + systemd.user.timers.cleanup-home-manager = { + Unit = { + Description = "Weekly cleanup of Home Manager generations"; + }; + Timer = { + OnCalendar = cfg.dates; + Persistent = true; + }; + Install = { + WantedBy = ["timers.target"]; + }; + }; + }; +}