From 1c2fccef832e5fbf2df4162f742f76a153aa5443 Mon Sep 17 00:00:00 2001 From: Damien Cassou Date: Sat, 22 Feb 2025 15:03:39 +0100 Subject: [PATCH] services.nix-gc: extract darwin agent interval code to new library file --- modules/lib/darwin.nix | 76 +++++++++++++++++++++++++++++++++++++ modules/lib/default.nix | 1 + modules/services/nix-gc.nix | 69 ++------------------------------- 3 files changed, 80 insertions(+), 66 deletions(-) create mode 100644 modules/lib/darwin.nix diff --git a/modules/lib/darwin.nix b/modules/lib/darwin.nix new file mode 100644 index 000000000..070cf2153 --- /dev/null +++ b/modules/lib/darwin.nix @@ -0,0 +1,76 @@ +{ lib }: +let + intervals = [ + "hourly" + "daily" + "weekly" + "monthly" + "semiannually" + "annually" + ]; + + mkCalendarInterval = + frequency: + let + freq = { + "hourly" = [ { Minute = 0; } ]; + "daily" = [ + { + Hour = 0; + Minute = 0; + } + ]; + "weekly" = [ + { + Weekday = 1; + Hour = 0; + Minute = 0; + } + ]; + "monthly" = [ + { + Day = 1; + Hour = 0; + Minute = 0; + } + ]; + "semiannually" = [ + { + Month = 1; + Day = 1; + Hour = 0; + Minute = 0; + } + { + Month = 7; + Day = 1; + Hour = 0; + Minute = 0; + } + ]; + "annually" = [ + { + Month = 1; + Day = 1; + Hour = 0; + Minute = 0; + } + ]; + }; + in + freq.${frequency} or null; + + intervalsString = lib.concatStringsSep ", " intervals; + + assertInterval = option: interval: pkgs: { + assertion = (!pkgs.stdenv.isDarwin) || (lib.elem interval intervals); + message = "On Darwin ${option} must be one of: ${intervalsString}."; + }; + + intervalDocumentation = '' + On Darwin it must be one of: ${intervalsString}, which are implemented as defined in {manpage}`systemd.time(7)`. + ''; +in +{ + inherit mkCalendarInterval assertInterval intervalDocumentation; +} diff --git a/modules/lib/default.nix b/modules/lib/default.nix index 27fc51833..7adc71987 100644 --- a/modules/lib/default.nix +++ b/modules/lib/default.nix @@ -6,6 +6,7 @@ rec { assertions = import ./assertions.nix { inherit lib; }; booleans = import ./booleans.nix { inherit lib; }; + darwin = import ./darwin.nix { inherit lib; }; deprecations = import ./deprecations.nix { inherit lib; }; generators = import ./generators.nix { inherit lib; }; gvariant = import ./gvariant.nix { inherit lib; }; diff --git a/modules/services/nix-gc.nix b/modules/services/nix-gc.nix index a49220990..74831e13b 100644 --- a/modules/services/nix-gc.nix +++ b/modules/services/nix-gc.nix @@ -8,65 +8,6 @@ let inherit (lib) mkOption types; cfg = config.nix.gc; - darwinIntervals = [ - "hourly" - "daily" - "weekly" - "monthly" - "semiannually" - "annually" - ]; - - mkCalendarInterval = - frequency: - let - freq = { - "hourly" = [ { Minute = 0; } ]; - "daily" = [ - { - Hour = 0; - Minute = 0; - } - ]; - "weekly" = [ - { - Weekday = 1; - Hour = 0; - Minute = 0; - } - ]; - "monthly" = [ - { - Day = 1; - Hour = 0; - Minute = 0; - } - ]; - "semiannually" = [ - { - Month = 1; - Day = 1; - Hour = 0; - Minute = 0; - } - { - Month = 7; - Day = 1; - Hour = 0; - Minute = 0; - } - ]; - "annually" = [ - { - Month = 1; - Day = 1; - Hour = 0; - Minute = 0; - } - ]; - }; - in - freq.${frequency} or null; nixPackage = if config.nix.enable && config.nix.package != null then config.nix.package else pkgs.nix; @@ -95,8 +36,7 @@ in On Linux this is a string as defined by {manpage}`systemd.time(7)`. - On Darwin it must be one of: ${lib.concatStringsSep ", " darwinIntervals}, which are - implemented as defined in the manual page above. + $lib.hm.darwin.{intervalDocumentation} ''; }; @@ -170,10 +110,7 @@ in (lib.mkIf pkgs.stdenv.isDarwin { assertions = [ - { - assertion = lib.elem cfg.frequency darwinIntervals; - message = "On Darwin nix.gc.frequency must be one of: ${lib.concatStringsSep ", " darwinIntervals}."; - } + (lib.hm.darwin.assertInterval "nix.gc.frequency" cfg.frequency pkgs) ]; launchd.agents.nix-gc = { @@ -182,7 +119,7 @@ in ProgramArguments = [ "${nixPackage}/bin/nix-collect-garbage" ] ++ lib.optional (cfg.options != null) cfg.options; - StartCalendarInterval = mkCalendarInterval cfg.frequency; + StartCalendarInterval = lib.hm.darwin.mkCalendarInterval cfg.frequency; }; }; })