15 automatic = lib.mkOption {
17 type = lib.types.bool;
18 description = "Automatically run the garbage collector at a specific time.";
21 dates = lib.mkOption {
22 type = lib.types.singleLineStr;
26 How often or when garbage collection is performed. For most desktop and server systems
27 a sufficient garbage collection is once a week.
29 The format is described in
30 {manpage}`systemd.time(7)`.
34 randomizedDelaySec = lib.mkOption {
36 type = lib.types.singleLineStr;
39 Add a randomized delay before each garbage collection.
40 The delay will be chosen between zero and this value.
41 This value must be a time span in the format specified by
42 {manpage}`systemd.time(7)`
46 persistent = lib.mkOption {
48 type = lib.types.bool;
51 Takes a boolean argument. If true, the time when the service
52 unit was last triggered is stored on disk. When the timer is
53 activated, the service unit is triggered immediately if it
54 would have been triggered at least once during the time when
55 the timer was inactive. Such triggering is nonetheless
56 subject to the delay imposed by RandomizedDelaySec=. This is
57 useful to catch up on missed runs of the service when the
58 system was powered down.
62 options = lib.mkOption {
64 example = "--max-freed $((64 * 1024**3))";
65 type = lib.types.singleLineStr;
67 Options given to [`nix-collect-garbage`](https://nixos.org/manual/nix/stable/command-ref/nix-collect-garbage) when the garbage collector is run automatically.
81 assertion = cfg.automatic -> config.nix.enable;
82 message = ''nix.gc.automatic requires nix.enable'';
86 systemd.services.nix-gc = lib.mkIf config.nix.enable {
87 description = "Nix Garbage Collector";
88 script = "exec ${config.nix.package.out}/bin/nix-collect-garbage ${cfg.options}";
89 serviceConfig.Type = "oneshot";
90 startAt = lib.optional cfg.automatic cfg.dates;
93 systemd.timers.nix-gc = lib.mkIf cfg.automatic {
95 RandomizedDelaySec = cfg.randomizedDelaySec;
96 Persistent = cfg.persistent;