Skip to content

Commit

Permalink
fix: StartCalendarInterval type, nix.gc.interval
Browse files Browse the repository at this point in the history
Stricter launchd -> StartCalendarInterval type which now verifies that
the ints passed to `Minute`, `Hour`, etc. are within range. The type now
allows `null`, a `StartCalendarInterval` submodule, or a non-empty list
of `StartCalendarInterval` submodules. The example and default now
type-check successfully.

Set `nix.gc.interval.type` to launchd's `StartCalendarInterval.type`
sans `null`. It now accepts a list as well. The example and default now
type-check successfully.

Improve wording/documentation of `nix.gc.interval` ("time interval" can
be misleading as it's actually a "calendar" interval, i.e. `{ Hour = 3;
Minute = 15;}` actually runs daily, not every 3.25 hours).
  • Loading branch information
tmillr authored and steveej committed Aug 21, 2023
1 parent 511177f commit 72d647c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 44 deletions.
85 changes: 44 additions & 41 deletions modules/launchd/launchd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -350,49 +350,52 @@ with lib;
up. If multiple intervals transpire before the computer is woken, those events will be coalesced into
one event upon wake from sleep.
'';
type = types.nullOr (types.listOf (types.submodule {
options = {
Minute = mkOption {
type = types.nullOr types.int;
default = null;
description = lib.mdDoc ''
The minute on which this job will be run.
'';
};

Hour = mkOption {
type = types.nullOr types.int;
default = null;
description = lib.mdDoc ''
The hour on which this job will be run.
'';
};

Day = mkOption {
type = types.nullOr types.int;
default = null;
description = lib.mdDoc ''
The day on which this job will be run.
'';
};

Weekday = mkOption {
type = types.nullOr types.int;
default = null;
description = lib.mdDoc ''
The weekday on which this job will be run (0 and 7 are Sunday).
'';
};

Month = mkOption {
type = types.nullOr types.int;
default = null;
description = lib.mdDoc ''
The month on which this job will be run.
'';
type = let
submod = types.submodule {
options = {
Minute = mkOption {
type = types.nullOr (types.ints.between 0 59);
default = null;
description = lib.mdDoc ''
The minute on which this job will be run.
'';
};

Hour = mkOption {
type = types.nullOr (types.ints.between 0 23);
default = null;
description = lib.mdDoc ''
The hour on which this job will be run.
'';
};

Day = mkOption {
type = types.nullOr (types.ints.between 1 31);
default = null;
description = lib.mdDoc ''
The day on which this job will be run.
'';
};

Weekday = mkOption {
type = types.nullOr (types.ints.between 0 7);
default = null;
description = lib.mdDoc ''
The weekday on which this job will be run (0 and 7 are Sunday).
'';
};

Month = mkOption {
type = types.nullOr (types.ints.between 1 12);
default = null;
description = lib.mdDoc ''
The month on which this job will be run.
'';
};
};
};
}));
in
types.nullOr (types.either submod (types.addCheck (types.listOf submod) (x: x != [])));
};

StandardInPath = mkOption {
Expand Down
10 changes: 7 additions & 3 deletions modules/services/nix-gc/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ with lib;

let
cfg = config.nix.gc;
launchdConfig = import ../../launchd/launchd.nix {inherit config lib;};
in

{
Expand Down Expand Up @@ -35,9 +36,12 @@ in
};

interval = mkOption {
type = types.attrs;
type = let
t = launchdConfig.options.StartCalendarInterval.type;
in
types.addCheck (t // {description = lib.removePrefix "null or" t.description;}) (x: x != null);
default = { Hour = 3; Minute = 15; };
description = lib.mdDoc "The time interval at which the garbage collector will run.";
description = lib.mdDoc "The calendar interval at which the garbage collector will run.";
};

options = mkOption {
Expand All @@ -63,7 +67,7 @@ in
command = "${config.nix.package}/bin/nix-collect-garbage ${cfg.options}";
environment.NIX_REMOTE = optionalString config.nix.useDaemon "daemon";
serviceConfig.RunAtLoad = false;
serviceConfig.StartCalendarInterval = [ cfg.interval ];
serviceConfig.StartCalendarInterval = cfg.interval;
serviceConfig.UserName = cfg.user;
};

Expand Down

0 comments on commit 72d647c

Please sign in to comment.