From 3b930bb653dd9ed7da5fe86c147bbc67492efe2c Mon Sep 17 00:00:00 2001 From: Damien Cassou Date: Wed, 14 May 2025 21:45:20 +0200 Subject: [PATCH] services.nix-gc: improve error message when nix.gc.frequency is invalid on darwin Previously, if an invalid value was passed, the build would fail with: error: attribute '"00:02:03"' missing at /nix/store/sz92b5gqi0ma61d18fwbihi8p37mkvir-source/modules/services/nix-gc.nix:69:5: 68| in 69| freq.${frequency}; | ^ 70| There was an assertion that should have prevented this from happening but the crash would happen before the assertion gets a chance to stop the build with a nice error message. This commit both gives the assertion a chance to trigger and improves the assertion's error message. --- modules/services/nix-gc.nix | 6 +++--- .../nix-gc-darwin/darwin-nix-gc-interval-assertion.nix | 10 ++++++++++ tests/modules/services/nix-gc-darwin/default.nix | 5 ++++- 3 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 tests/modules/services/nix-gc-darwin/darwin-nix-gc-interval-assertion.nix diff --git a/modules/services/nix-gc.nix b/modules/services/nix-gc.nix index d95d6e94c..a49220990 100644 --- a/modules/services/nix-gc.nix +++ b/modules/services/nix-gc.nix @@ -66,7 +66,7 @@ let ]; }; in - freq.${frequency}; + freq.${frequency} or null; nixPackage = if config.nix.enable && config.nix.package != null then config.nix.package else pkgs.nix; @@ -95,7 +95,7 @@ in On Linux this is a string as defined by {manpage}`systemd.time(7)`. - On Darwin it must be one of: ${toString darwinIntervals}, which are + On Darwin it must be one of: ${lib.concatStringsSep ", " darwinIntervals}, which are implemented as defined in the manual page above. ''; }; @@ -172,7 +172,7 @@ in assertions = [ { assertion = lib.elem cfg.frequency darwinIntervals; - message = "On Darwin nix.gc.frequency must be one of: ${toString darwinIntervals}."; + message = "On Darwin nix.gc.frequency must be one of: ${lib.concatStringsSep ", " darwinIntervals}."; } ]; 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 new file mode 100644 index 000000000..6dbf70e83 --- /dev/null +++ b/tests/modules/services/nix-gc-darwin/darwin-nix-gc-interval-assertion.nix @@ -0,0 +1,10 @@ +{ + nix.gc = { + automatic = true; + frequency = "00:02:03"; + }; + + test.asserts.assertions.expected = [ + "On Darwin nix.gc.frequency 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 f2fc20aa7..251e0d23a 100644 --- a/tests/modules/services/nix-gc-darwin/default.nix +++ b/tests/modules/services/nix-gc-darwin/default.nix @@ -1 +1,4 @@ -{ nix-gc = ./basic.nix; } +{ + nix-gc = ./basic.nix; + darwin-nix-gc-interval-assertion = ./darwin-nix-gc-interval-assertion.nix; +}