diff --git a/modules/services/misc/nix-gc.nix b/modules/services/misc/nix-gc.nix index 280450446016..27bcfafa20f8 100644 --- a/modules/services/misc/nix-gc.nix +++ b/modules/services/misc/nix-gc.nix @@ -11,36 +11,38 @@ in ###### interface options = { + nix.gc = { automatic = mkOption { default = false; type = types.bool; - description = " - Automatically run the garbage collector at specified dates. - "; + description = "Automatically run the garbage collector at a specific time."; }; dates = mkOption { - default = "15 03 * * *"; - type = types.string; - description = " - Run the garbage collector at specified dates to avoid full - hard-drives. - "; + default = "00:43"; + type = types.uniq types.string; + description = '' + Specification (in the format described by + systemd.time + 5) of the time at + which the garbage collector will run. + ''; }; options = mkOption { default = ""; example = "--max-freed $((64 * 1024**3))"; - type = types.string; - description = " + type = types.uniq types.string; + description = '' Options given to nix-collect-garbage when the garbage collector is run automatically. - "; + ''; }; }; + }; @@ -48,10 +50,11 @@ in config = { - services.cron.systemCronJobs = mkIf cfg.automatic (singleton - "${cfg.dates} root ${config.systemd.package}/bin/systemctl start nix-gc.service"); + #systemd.timers.nix-gc.enable = cfg.automatic; + systemd.timers.nix-gc.enable = true; + systemd.timers.nix-gc.timerConfig.OnCalendar = cfg.dates; - systemd.services."nix-gc" = + systemd.services.nix-gc = { description = "Nix Garbage Collector"; path = [ config.environment.nix ]; script = "exec nix-collect-garbage ${cfg.options}"; diff --git a/modules/system/boot/systemd-unit-options.nix b/modules/system/boot/systemd-unit-options.nix index 513e9e857085..8f61399465b4 100644 --- a/modules/system/boot/systemd-unit-options.nix +++ b/modules/system/boot/systemd-unit-options.nix @@ -223,6 +223,26 @@ rec { }; + + timerOptions = unitOptions // { + + timerConfig = mkOption { + default = {}; + example = { OnCalendar = "Sun 14:00:00"; Unit = "foo.service"; }; + type = types.attrs; + description = '' + Each attribute in this set specifies an option in the + [Timer] section of the unit. See + systemd.timer + 5 and + systemd.time + 5 for details. + ''; + }; + + }; + + mountOptions = unitOptions // { what = mkOption { diff --git a/modules/system/boot/systemd.nix b/modules/system/boot/systemd.nix index dabb6f6661b8..f99eca91ad59 100644 --- a/modules/system/boot/systemd.nix +++ b/modules/system/boot/systemd.nix @@ -278,6 +278,18 @@ let ''; }; + timerToUnit = name: def: + { inherit (def) wantedBy enable; + text = + '' + [Unit] + ${attrsToSection def.unitConfig} + + [Timer] + ${attrsToSection def.timerConfig} + ''; + }; + mountToUnit = name: def: { inherit (def) wantedBy enable; text = @@ -410,6 +422,13 @@ in description = "Definition of systemd socket units."; }; + systemd.timers = mkOption { + default = {}; + type = types.attrsOf types.optionSet; + options = [ timerOptions unitConfig ]; + description = "Definition of systemd timer units."; + }; + systemd.mounts = mkOption { default = []; type = types.listOf types.optionSet; @@ -552,6 +571,7 @@ in mapAttrs' (n: v: nameValuePair "${n}.target" (targetToUnit n v)) cfg.targets // mapAttrs' (n: v: nameValuePair "${n}.service" (serviceToUnit n v)) cfg.services // mapAttrs' (n: v: nameValuePair "${n}.socket" (socketToUnit n v)) cfg.sockets + // mapAttrs' (n: v: nameValuePair "${n}.timer" (timerToUnit n v)) cfg.timers // listToAttrs (map (v: let n = escapeSystemdPath v.where; in nameValuePair "${n}.mount" (mountToUnit n v)) cfg.mounts);