From 28242a60d3cdcb6675dfe68ebbb64bf43c7be4e0 Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Tue, 12 Sep 2023 17:25:31 +0100 Subject: [PATCH] home-manager-auto-expire: init This commit introduces a new service that expires old Home-Manager generations. --- modules/modules.nix | 1 + modules/services/home-manager-auto-expire.nix | 100 ++++++++++++++++++ tests/default.nix | 1 + .../basic-configuration.nix | 18 ++++ .../home-manager-auto-expire/default.nix | 1 + 5 files changed, 121 insertions(+) create mode 100644 modules/services/home-manager-auto-expire.nix create mode 100644 tests/modules/services/home-manager-auto-expire/basic-configuration.nix create mode 100644 tests/modules/services/home-manager-auto-expire/default.nix diff --git a/modules/modules.nix b/modules/modules.nix index d631e762d..1fa4f66a1 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -340,6 +340,7 @@ let ./services/gpg-agent.nix ./services/grobi.nix ./services/gromit-mpx.nix + ./services/home-manager-auto-expire.nix ./services/home-manager-auto-upgrade.nix ./services/hound.nix ./services/hypridle.nix diff --git a/modules/services/home-manager-auto-expire.nix b/modules/services/home-manager-auto-expire.nix new file mode 100644 index 000000000..2a98a7347 --- /dev/null +++ b/modules/services/home-manager-auto-expire.nix @@ -0,0 +1,100 @@ +{ config, lib, pkgs, ... }: + +let + + cfg = config.services.home-manager.autoExpire; + + homeManagerPackage = pkgs.callPackage ../../home-manager { + path = config.programs.home-manager.path; + }; + +in { + meta.maintainers = [ lib.maintainers.thiagokokada ]; + + options = { + services.home-manager.autoExpire = { + enable = lib.mkEnableOption '' + the Home Manager expire service that periodically expire your + old Home Manager generations''; + + timestamp = lib.mkOption { + type = lib.types.str; + default = "-30 days"; + example = "-7 days"; + description = '' + Remove generations older than `TIMESTAMP` where `TIMESTAMP` is + interpreted as in the -d argument of the date tool. + ''; + }; + + frequency = lib.mkOption { + type = lib.types.str; + default = "monthly"; + example = "weekly"; + description = '' + The interval at which the Home Manager auto expire is run. + + This value is passed to the systemd timer configuration + as the `OnCalendar` option. + + The format is described in {manpage}`systemd.time(7)`. + ''; + }; + + store = { + cleanup = lib.mkEnableOption '' + to cleanup Nix store when the Home Manager expire service runs. + + It will use `nix-collect-garbage` to cleanup the store, + removing all unreachable store objects from the current user + (i.e.: not only the expired Home Manager generations). + + This may not be what you want, this is why this option is disabled + by default''; + + options = lib.mkOption { + type = lib.types.str; + description = '' + Options given to `nix-collection-garbage` when the service runs. + ''; + default = ""; + example = "--delete-older-than 30d"; + }; + }; + }; + }; + + config = lib.mkIf cfg.enable { + assertions = [ + (lib.hm.assertions.assertPlatform "services.home-manager.autoExpire" pkgs + lib.platforms.linux) + ]; + + systemd.user = { + timers.home-manager-auto-expire = { + Unit.Description = "Home Manager expire generations timer"; + + Install.WantedBy = [ "timers.target" ]; + + Timer = { + OnCalendar = cfg.frequency; + Unit = "home-manager-auto-expire.service"; + Persistent = true; + }; + }; + + services.home-manager-auto-expire = { + Unit.Description = "Home Manager expire generations"; + + Service.ExecStart = toString + (pkgs.writeShellScript "home-manager-auto-expire" ('' + echo "Expire old Home Manager generations" + ${homeManagerPackage}/bin/home-manager expire-generations '${cfg.timestamp}' + '' + lib.optionalString cfg.store.cleanup '' + echo "Clean-up Nix store" + ${pkgs.nix}/bin/nix-collect-garbage ${cfg.store.options} + '')); + }; + }; + }; +} diff --git a/tests/default.nix b/tests/default.nix index 07e8c43d5..e7197d5b1 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -514,6 +514,7 @@ in import nmtSrc { ./modules/services/git-sync ./modules/services/glance ./modules/services/gromit-mpx + ./modules/services/home-manager-auto-expire ./modules/services/home-manager-auto-upgrade ./modules/services/hypridle ./modules/services/hyprpaper diff --git a/tests/modules/services/home-manager-auto-expire/basic-configuration.nix b/tests/modules/services/home-manager-auto-expire/basic-configuration.nix new file mode 100644 index 000000000..0e6a4f448 --- /dev/null +++ b/tests/modules/services/home-manager-auto-expire/basic-configuration.nix @@ -0,0 +1,18 @@ +{ + config = { + services.home-manager.autoExpire = { + enable = true; + timestamp = "-7 days"; + frequency = "00:00"; + cleanup.store = true; + }; + + nmt.script = '' + serviceFile=home-files/.config/systemd/user/home-manager-auto-expire.service + assertFileExists $serviceFile + + timerFile=home-files/.config/systemd/user/home-manager-auto-expire.timer + assertFileExists $timerFile + ''; + }; +} diff --git a/tests/modules/services/home-manager-auto-expire/default.nix b/tests/modules/services/home-manager-auto-expire/default.nix new file mode 100644 index 000000000..3913849ef --- /dev/null +++ b/tests/modules/services/home-manager-auto-expire/default.nix @@ -0,0 +1 @@ +{ home-manager-auto-expire-basic-configuration = ./basic-configuration.nix; }