diff --git a/modules/services/nix-gc.nix b/modules/services/nix-gc.nix index ad667e571..67a833d17 100644 --- a/modules/services/nix-gc.nix +++ b/modules/services/nix-gc.nix @@ -5,7 +5,7 @@ ... }: let - inherit (lib) mkOption types; + inherit (lib) mkChangedOptionModule mkOption types; cfg = config.nix.gc; @@ -15,6 +15,12 @@ in { meta.maintainers = [ lib.maintainers.shivaraj-bh ]; + imports = [ + (mkChangedOptionModule [ "nix" "gc" "frequency" ] [ "nix" "gc" "dates" ] ( + config: lib.toList (lib.getAttrFromPath [ "nix" "gc" "frequency" ] config) + )) + ]; + options = { nix.gc = { automatic = mkOption { @@ -27,8 +33,9 @@ in ''; }; - frequency = mkOption { - type = types.str; + dates = mkOption { + type = with types; either singleLineStr (listOf str); + apply = lib.toList; default = "weekly"; example = "03:15"; description = '' @@ -97,7 +104,7 @@ in Description = "Nix Garbage Collector"; }; Timer = { - OnCalendar = "${cfg.frequency}"; + OnCalendar = cfg.dates; RandomizedDelaySec = cfg.randomizedDelaySec; Persistent = cfg.persistent; Unit = "nix-gc.service"; @@ -110,7 +117,11 @@ in (lib.mkIf pkgs.stdenv.isDarwin { assertions = [ - (lib.hm.darwin.assertInterval "nix.gc.frequency" cfg.frequency pkgs) + { + assertion = (lib.length cfg.dates) == 1; + message = "On Darwin, `nix.gc.dates` must contain a single element."; + } + (lib.hm.darwin.assertInterval "nix.gc.dates.*" (lib.elemAt cfg.dates 0) pkgs) ]; launchd.agents.nix-gc = { @@ -120,7 +131,7 @@ in "${nixPackage}/bin/nix-collect-garbage" ] ++ lib.optional (cfg.options != null) cfg.options; - StartCalendarInterval = lib.hm.darwin.mkCalendarInterval cfg.frequency; + StartCalendarInterval = lib.hm.darwin.mkCalendarInterval (lib.elemAt cfg.dates 0); }; }; }) diff --git a/tests/modules/services/nix-gc/darwin/basic.nix b/tests/modules/services/nix-gc/darwin/basic.nix index 707ae44e6..0f0b9b4aa 100644 --- a/tests/modules/services/nix-gc/darwin/basic.nix +++ b/tests/modules/services/nix-gc/darwin/basic.nix @@ -1,7 +1,7 @@ { nix.gc = { automatic = true; - frequency = "monthly"; + dates = "monthly"; options = "--delete-older-than 30d"; }; diff --git a/tests/modules/services/nix-gc/darwin/changed-options.nix b/tests/modules/services/nix-gc/darwin/changed-options.nix new file mode 100644 index 000000000..0024558a6 --- /dev/null +++ b/tests/modules/services/nix-gc/darwin/changed-options.nix @@ -0,0 +1,21 @@ +{ lib, options, ... }: + +{ + nix.gc = { + automatic = true; + frequency = "monthly"; + options = "--delete-older-than 30d"; + }; + + test.asserts.warnings.expected = [ + "The option `nix.gc.frequency' defined in ${lib.showFiles options.nix.gc.frequency.files} has been changed to `nix.gc.dates' that has a different type. Please read `nix.gc.dates' documentation and update your configuration accordingly." + ]; + + nmt.script = '' + serviceFile=LaunchAgents/org.nix-community.home.nix-gc.plist + + assertFileExists "$serviceFile" + + assertFileContent "$serviceFile" ${./expected-agent.plist} + ''; +} diff --git a/tests/modules/services/nix-gc/darwin/darwin-nix-gc-interval-assertion.nix b/tests/modules/services/nix-gc/darwin/darwin-nix-gc-interval-assertion.nix index 6dbf70e83..f3bedb68d 100644 --- a/tests/modules/services/nix-gc/darwin/darwin-nix-gc-interval-assertion.nix +++ b/tests/modules/services/nix-gc/darwin/darwin-nix-gc-interval-assertion.nix @@ -1,10 +1,10 @@ { nix.gc = { automatic = true; - frequency = "00:02:03"; + dates = "00:02:03"; }; test.asserts.assertions.expected = [ - "On Darwin nix.gc.frequency must be one of: hourly, daily, weekly, monthly, semiannually, annually." + "On Darwin nix.gc.dates.* must be one of: hourly, daily, weekly, monthly, semiannually, annually." ]; } diff --git a/tests/modules/services/nix-gc/darwin/default.nix b/tests/modules/services/nix-gc/darwin/default.nix index 070820b37..5959ba6c2 100644 --- a/tests/modules/services/nix-gc/darwin/default.nix +++ b/tests/modules/services/nix-gc/darwin/default.nix @@ -1,4 +1,5 @@ { nix-gc = ./basic.nix; nix-gc-interval-assertion = ./darwin-nix-gc-interval-assertion.nix; + nix-gc-changed-options = ./changed-options.nix; } diff --git a/tests/modules/services/nix-gc/linux/basic.nix b/tests/modules/services/nix-gc/linux/basic.nix index a6b06f246..d7762dcbf 100644 --- a/tests/modules/services/nix-gc/linux/basic.nix +++ b/tests/modules/services/nix-gc/linux/basic.nix @@ -1,7 +1,7 @@ { nix.gc = { automatic = true; - frequency = "monthly"; + dates = [ "monthly" ]; randomizedDelaySec = "42min"; options = "--delete-older-than 30d --max-freed $((64 * 1024**3))"; }; diff --git a/tests/modules/services/nix-gc/linux/changed-options.nix b/tests/modules/services/nix-gc/linux/changed-options.nix new file mode 100644 index 000000000..e186b002d --- /dev/null +++ b/tests/modules/services/nix-gc/linux/changed-options.nix @@ -0,0 +1,43 @@ +{ lib, options, ... }: + +{ + nix.gc = { + automatic = true; + frequency = "monthly"; + randomizedDelaySec = "42min"; + options = "--delete-older-than 30d --max-freed $((64 * 1024**3))"; + }; + + test.asserts.warnings.expected = [ + "The option `nix.gc.frequency' defined in ${lib.showFiles options.nix.gc.frequency.files} has been changed to `nix.gc.dates' that has a different type. Please read `nix.gc.dates' documentation and update your configuration accordingly." + ]; + + nmt.script = '' + serviceFile=home-files/.config/systemd/user/nix-gc.service + + assertFileExists $serviceFile + + serviceFile=$(normalizeStorePaths $serviceFile) + + assertFileContent $serviceFile ${./expected.service} + + timerFile=home-files/.config/systemd/user/nix-gc.timer + + assertFileExists $timerFile + + timerFile=$(normalizeStorePaths $timerFile) + + assertFileContent $timerFile ${./expected.timer} + + nixgcScriptFile=$(grep -o \ + '/nix/store/.*-nix-gc' \ + $TESTED/home-files/.config/systemd/user/nix-gc.service + ) + + assertFileExists $nixgcScriptFile + + nixgcScriptFile=$(normalizeStorePaths $nixgcScriptFile) + + assertFileContent $nixgcScriptFile ${./nix-gc-script-expected} + ''; +} diff --git a/tests/modules/services/nix-gc/linux/default.nix b/tests/modules/services/nix-gc/linux/default.nix index 922d6a399..fefcac737 100644 --- a/tests/modules/services/nix-gc/linux/default.nix +++ b/tests/modules/services/nix-gc/linux/default.nix @@ -1,3 +1,4 @@ { nix-gc = ./basic.nix; + nix-gc-changed-options = ./changed-options.nix; }