1
0
Fork 0
mirror of https://github.com/LnL7/nix-darwin.git synced 2025-03-16 05:18:17 +00:00
nix-darwin/modules/services/nix-optimise/default.nix
Tyler Miller 861af0fc94
fix(launchd): improve StartCalendarInterval
Stricter launchd -> StartCalendarInterval type:

- Verify that the integers passed to `Minute`, `Hour`, etc. are within
  range.

- When provided, the value for StartCalendarInterval must be a non-empty
  list of calendar intervals and must not contain duplicates entries
  (throw an error otherwise).

- For increased flexibility and backwards-compatibility, allow an
  attrset to be passed as well (which will be type-checked and is
  functionally equivalent to passing a singleton list). Allowing an
  attrset or list is precisely in-line with what `launchd.plist(5)`
  accepts for StartCalendarInterval.

Migrate `nix.gc.interval` and `nix.optimise.interval` over to use this
new type, and update their defaults to run weekly instead of daily.

Create `modules/launchd/types.nix` file for easier/modular use of
launchd types needed in multiple files.

Documentation:

- Update and improve wording/documentation of launchd's
  `StartCalendarInterval`.

- Improve wording/documentation of `nix.gc.interval` and
  `nix.optimise.interval` ("time interval" can be misleading as it's
  actually a "calendar interval"; e.g. `{ Hour = 3; Minute = 15;}`
  runs daily, not every 3.25 hours).
2024-06-09 11:20:15 -07:00

77 lines
1.8 KiB
Nix

# Based off:
# https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/misc/nix-optimise.nix
# When making changes please try to keep it in sync.
{ config, lib, ... }:
let
inherit (lib)
mkIf
mkOption
mkRemovedOptionModule
optionalString
types
;
cfg = config.nix.optimise;
launchdTypes = import ../../launchd/types.nix { inherit config lib; };
in
{
imports = [
(mkRemovedOptionModule [ "nix" "optimise" "dates" ] "Use `nix.optimise.interval` instead.")
];
###### interface
options = {
nix.optimise = {
automatic = mkOption {
type = types.bool;
default = false;
description = "Automatically run the nix store optimiser at a specific time.";
};
# Not in NixOS module
user = mkOption {
type = types.nullOr types.str;
default = null;
description = "User that runs the store optimisation.";
};
interval = mkOption {
type = launchdTypes.StartCalendarInterval;
default = [{ Weekday = 7; Hour = 4; Minute = 15; }];
description = ''
The calendar interval at which the optimiser will run.
See the {option}`serviceConfig.StartCalendarInterval` option of
the {option}`launchd` module for more info.
'';
};
};
};
###### implementation
config = mkIf cfg.automatic {
launchd.daemons.nix-optimise = {
environment.NIX_REMOTE = optionalString config.nix.useDaemon "daemon";
serviceConfig = {
ProgramArguments = [
"/bin/sh" "-c"
"/bin/wait4path ${config.nix.package} && exec ${config.nix.package}/bin/nix-store --optimise"
];
RunAtLoad = false;
StartCalendarInterval = cfg.interval;
UserName = cfg.user;
};
};
};
}