-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
nix-gc.nix
149 lines (135 loc) · 3.81 KB
/
nix-gc.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.nix.gc;
darwinIntervals =
[ "hourly" "daily" "weekly" "monthly" "semiannually" "annually" ];
mkCalendarInterval = frequency:
let
freq = {
"hourly" = [{ Minute = 0; }];
"daily" = [{
Hour = 0;
Minute = 0;
}];
"weekly" = [{
Weekday = 1;
Hour = 0;
Minute = 0;
}];
"monthly" = [{
Day = 1;
Hour = 0;
Minute = 0;
}];
"semiannually" = [
{
Month = 1;
Day = 1;
Hour = 0;
Minute = 0;
}
{
Month = 7;
Day = 1;
Hour = 0;
Minute = 0;
}
];
"annually" = [{
Month = 1;
Day = 1;
Hour = 0;
Minute = 0;
}];
};
in freq.${frequency};
nixPackage = if config.nix.enable && config.nix.package != null then
config.nix.package
else
pkgs.nix;
in {
meta.maintainers = [ maintainers.shivaraj-bh ];
options = {
nix.gc = {
automatic = mkOption {
type = types.bool;
default = false;
description = ''
Automatically run the garbage collector at a specific time.
Note: This will only garbage collect the current user's profiles.
'';
};
frequency = mkOption {
type = types.str;
default = "weekly";
example = "03:15";
description = ''
When to run the Nix garbage collector.
On Linux this is a string as defined by {manpage}`systemd.time(7)`.
On Darwin it must be one of: ${toString darwinIntervals}, which are
implemented as defined in the manual page above.
'';
};
options = mkOption {
type = types.nullOr types.str;
default = null;
example = "--max-freed $((64 * 1024**3))";
description = ''
Options given to {file}`nix-collect-garbage` when the
garbage collector is run automatically.
'';
};
persistent = mkOption {
type = types.bool;
default = true;
example = false;
description = ''
If true, the time when the service unit was last triggered is
stored on disk. When the timer is activated, the service unit is
triggered immediately if it would have been triggered at least once
during the time when the timer was inactive.
'';
};
};
};
config = lib.mkIf cfg.automatic (mkMerge [
(mkIf pkgs.stdenv.isLinux {
systemd.user.services.nix-gc = {
Unit = { Description = "Nix Garbage Collector"; };
Service = {
Type = "oneshot";
ExecStart = toString (pkgs.writeShellScript "nix-gc"
"exec ${nixPackage}/bin/nix-collect-garbage ${
lib.optionalString (cfg.options != null) cfg.options
}");
};
};
systemd.user.timers.nix-gc = {
Unit = { Description = "Nix Garbage Collector"; };
Timer = {
OnCalendar = "${cfg.frequency}";
Persistent = cfg.persistent;
Unit = "nix-gc.service";
};
Install = { WantedBy = [ "timers.target" ]; };
};
})
(mkIf pkgs.stdenv.isDarwin {
assertions = [{
assertion = elem cfg.frequency darwinIntervals;
message = "On Darwin nix.gc.frequency must be one of: ${
toString darwinIntervals
}.";
}];
launchd.agents.nix-gc = {
enable = true;
config = {
ProgramArguments = [ "${nixPackage}/bin/nix-collect-garbage" ]
++ lib.optional (cfg.options != null) cfg.options;
StartCalendarInterval = mkCalendarInterval cfg.frequency;
};
};
})
]);
}