Skip to content

Commit 75d8380

Browse files
nix-gc: rename frequency to dates
1 parent 3ec1cd9 commit 75d8380

File tree

8 files changed

+87
-10
lines changed

8 files changed

+87
-10
lines changed

modules/services/nix-gc.nix

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
...
66
}:
77
let
8-
inherit (lib) mkOption types;
8+
inherit (lib) mkChangedOptionModule mkOption types;
99

1010
cfg = config.nix.gc;
1111

@@ -15,6 +15,12 @@ in
1515
{
1616
meta.maintainers = [ lib.maintainers.shivaraj-bh ];
1717

18+
imports = [
19+
(mkChangedOptionModule [ "nix" "gc" "frequency" ] [ "nix" "gc" "dates" ] (
20+
config: lib.toList (lib.getAttrFromPath [ "nix" "gc" "frequency" ] config)
21+
))
22+
];
23+
1824
options = {
1925
nix.gc = {
2026
automatic = mkOption {
@@ -27,8 +33,9 @@ in
2733
'';
2834
};
2935

30-
frequency = mkOption {
31-
type = types.str;
36+
dates = mkOption {
37+
type = with types; either singleLineStr (listOf str);
38+
apply = lib.toList;
3239
default = "weekly";
3340
example = "03:15";
3441
description = ''
@@ -97,7 +104,7 @@ in
97104
Description = "Nix Garbage Collector";
98105
};
99106
Timer = {
100-
OnCalendar = "${cfg.frequency}";
107+
OnCalendar = cfg.dates;
101108
RandomizedDelaySec = cfg.randomizedDelaySec;
102109
Persistent = cfg.persistent;
103110
Unit = "nix-gc.service";
@@ -110,7 +117,11 @@ in
110117

111118
(lib.mkIf pkgs.stdenv.isDarwin {
112119
assertions = [
113-
(lib.hm.darwin.assertInterval "nix.gc.frequency" cfg.frequency pkgs)
120+
{
121+
assertion = (lib.length cfg.dates) == 1;
122+
message = "On Darwin, `nix.gc.dates` must contain a single element.";
123+
}
124+
(lib.hm.darwin.assertInterval "nix.gc.dates.*" (lib.elemAt cfg.dates 0) pkgs)
114125
];
115126

116127
launchd.agents.nix-gc = {
@@ -120,7 +131,7 @@ in
120131
"${nixPackage}/bin/nix-collect-garbage"
121132
]
122133
++ lib.optional (cfg.options != null) cfg.options;
123-
StartCalendarInterval = lib.hm.darwin.mkCalendarInterval cfg.frequency;
134+
StartCalendarInterval = lib.hm.darwin.mkCalendarInterval (lib.elemAt cfg.dates 0);
124135
};
125136
};
126137
})

tests/modules/services/nix-gc/darwin/basic.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
nix.gc = {
33
automatic = true;
4-
frequency = "monthly";
4+
dates = "monthly";
55
options = "--delete-older-than 30d";
66
};
77

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{ lib, options, ... }:
2+
3+
{
4+
nix.gc = {
5+
automatic = true;
6+
frequency = "monthly";
7+
options = "--delete-older-than 30d";
8+
};
9+
10+
test.asserts.warnings.expected = [
11+
"The option `nix.gc.frequency' defined in ${lib.showFiles options.nix.gc.frequency.files} has been changed to `nix.gc.dates' that has a different type. Please read `nix.gc.dates' documentation and update your configuration accordingly."
12+
];
13+
14+
nmt.script = ''
15+
serviceFile=LaunchAgents/org.nix-community.home.nix-gc.plist
16+
17+
assertFileExists "$serviceFile"
18+
19+
assertFileContent "$serviceFile" ${./expected-agent.plist}
20+
'';
21+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
nix.gc = {
33
automatic = true;
4-
frequency = "00:02:03";
4+
dates = "00:02:03";
55
};
66

77
test.asserts.assertions.expected = [
8-
"On Darwin nix.gc.frequency must be one of: hourly, daily, weekly, monthly, semiannually, annually."
8+
"On Darwin nix.gc.dates must be one of: hourly, daily, weekly, monthly, semiannually, annually."
99
];
1010
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
nix-gc = ./basic.nix;
33
nix-gc-interval-assertion = ./darwin-nix-gc-interval-assertion.nix;
4+
nix-gc-changed-options = ./changed-options.nix;
45
}

tests/modules/services/nix-gc/linux/basic.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
nix.gc = {
33
automatic = true;
4-
frequency = "monthly";
4+
dates = [ "monthly" ];
55
randomizedDelaySec = "42min";
66
options = "--delete-older-than 30d --max-freed $((64 * 1024**3))";
77
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{ lib, options, ... }:
2+
3+
{
4+
nix.gc = {
5+
automatic = true;
6+
frequency = "monthly";
7+
randomizedDelaySec = "42min";
8+
options = "--delete-older-than 30d --max-freed $((64 * 1024**3))";
9+
};
10+
11+
test.asserts.warnings.expected = [
12+
"The option `nix.gc.frequency' defined in ${lib.showFiles options.nix.gc.frequency.files} has been changed to `nix.gc.dates' that has a different type. Please read `nix.gc.dates' documentation and update your configuration accordingly."
13+
];
14+
15+
nmt.script = ''
16+
serviceFile=home-files/.config/systemd/user/nix-gc.service
17+
18+
assertFileExists $serviceFile
19+
20+
serviceFile=$(normalizeStorePaths $serviceFile)
21+
22+
assertFileContent $serviceFile ${./expected.service}
23+
24+
timerFile=home-files/.config/systemd/user/nix-gc.timer
25+
26+
assertFileExists $timerFile
27+
28+
timerFile=$(normalizeStorePaths $timerFile)
29+
30+
assertFileContent $timerFile ${./expected.timer}
31+
32+
nixgcScriptFile=$(grep -o \
33+
'/nix/store/.*-nix-gc' \
34+
$TESTED/home-files/.config/systemd/user/nix-gc.service
35+
)
36+
37+
assertFileExists $nixgcScriptFile
38+
39+
nixgcScriptFile=$(normalizeStorePaths $nixgcScriptFile)
40+
41+
assertFileContent $nixgcScriptFile ${./nix-gc-script-expected}
42+
'';
43+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
22
nix-gc = ./basic.nix;
3+
nix-gc-changed-options = ./changed-options.nix;
34
}

0 commit comments

Comments
 (0)