1{ config, lib, ... }:
2
3with lib;
4
5let
6 cfg = config.nix.gc;
7in
8
9{
10
11 ###### interface
12
13 options = {
14
15 nix.gc = {
16
17 automatic = mkOption {
18 default = false;
19 type = types.bool;
20 description = lib.mdDoc "Automatically run the garbage collector at a specific time.";
21 };
22
23 dates = mkOption {
24 type = types.str;
25 default = "03:15";
26 example = "weekly";
27 description = lib.mdDoc ''
28 How often or when garbage collection is performed. For most desktop and server systems
29 a sufficient garbage collection is once a week.
30
31 The format is described in
32 {manpage}`systemd.time(7)`.
33 '';
34 };
35
36 randomizedDelaySec = mkOption {
37 default = "0";
38 type = types.str;
39 example = "45min";
40 description = lib.mdDoc ''
41 Add a randomized delay before each garbage collection.
42 The delay will be chosen between zero and this value.
43 This value must be a time span in the format specified by
44 {manpage}`systemd.time(7)`
45 '';
46 };
47
48 persistent = mkOption {
49 default = true;
50 type = types.bool;
51 example = false;
52 description = lib.mdDoc ''
53 Takes a boolean argument. If true, the time when the service
54 unit was last triggered is stored on disk. When the timer is
55 activated, the service unit is triggered immediately if it
56 would have been triggered at least once during the time when
57 the timer was inactive. Such triggering is nonetheless
58 subject to the delay imposed by RandomizedDelaySec=. This is
59 useful to catch up on missed runs of the service when the
60 system was powered down.
61 '';
62 };
63
64 options = mkOption {
65 default = "";
66 example = "--max-freed $((64 * 1024**3))";
67 type = types.str;
68 description = lib.mdDoc ''
69 Options given to {file}`nix-collect-garbage` when the
70 garbage collector is run automatically.
71 '';
72 };
73
74 };
75
76 };
77
78
79 ###### implementation
80
81 config = {
82 assertions = [
83 {
84 assertion = cfg.automatic -> config.nix.enable;
85 message = ''nix.gc.automatic requires nix.enable'';
86 }
87 ];
88
89 systemd.services.nix-gc = lib.mkIf config.nix.enable {
90 description = "Nix Garbage Collector";
91 script = "exec ${config.nix.package.out}/bin/nix-collect-garbage ${cfg.options}";
92 startAt = optional cfg.automatic cfg.dates;
93 };
94
95 systemd.timers.nix-gc = lib.mkIf cfg.automatic {
96 timerConfig = {
97 RandomizedDelaySec = cfg.randomizedDelaySec;
98 Persistent = cfg.persistent;
99 };
100 };
101
102 };
103
104}