Skip to content

Commit a936535

Browse files
committed
Add assertions and warnings to modular services
1 parent 1c8e965 commit a936535

File tree

5 files changed

+121
-8
lines changed

5 files changed

+121
-8
lines changed

nixos/modules/misc/assertions.nix

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,7 @@
3232
};
3333

3434
};
35-
# impl of assertions is in <nixpkgs/nixos/modules/system/activation/top-level.nix>
35+
# impl of assertions is in
36+
# - <nixpkgs/nixos/modules/system/activation/top-level.nix>
37+
# - <nixpkgs/nixos/modules/system/service/portable/lib.nix>
3638
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{ lib, ... }:
2+
let
3+
inherit (lib) concatLists mapAttrsToList showOption;
4+
in
5+
rec {
6+
flattenMapServicesConfigToList = f: loc: config:
7+
f loc config
8+
++ concatLists
9+
(mapAttrsToList
10+
(k: v:
11+
flattenMapServicesConfigToList
12+
f
13+
(loc ++ ["services" k])
14+
v)
15+
config.services);
16+
17+
getWarnings =
18+
flattenMapServicesConfigToList
19+
(loc: config:
20+
map (msg: "in ${showOption loc}: ${msg}") config.warnings);
21+
22+
getAssertions =
23+
flattenMapServicesConfigToList
24+
(loc: config:
25+
map
26+
(ass: {
27+
message = "in ${showOption loc}: ${ass.message}";
28+
assertion = ass.assertion;
29+
})
30+
config.assertions);
31+
}

nixos/modules/system/service/portable/service.nix

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ let
1616
};
1717
in
1818
{
19+
# https://nixos.org/manual/nixos/unstable/#modular-services
20+
_class = "service";
21+
imports = [
22+
../../../misc/assertions.nix
23+
];
1924
options = {
2025
services = mkOption {
2126
type = types.attrsOf (types.submoduleWith {

nixos/modules/system/service/portable/test.nix

+65-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ let
55

66
inherit (lib) mkOption types;
77

8+
portable-lib = import ./lib.nix { inherit lib; };
9+
810
dummyPkg =
911
name:
1012
derivation {
@@ -21,6 +23,12 @@ let
2123
executable = "/usr/bin/echo"; # *giggles*
2224
args = [ "hello" ];
2325
};
26+
assertions = [
27+
{ assertion = false; message = "you can't enable this for that reason"; }
28+
];
29+
warnings = [
30+
"The `foo' service is deprecated and will go away soon!"
31+
];
2432
};
2533
service2 = {
2634
process = {
@@ -32,8 +40,20 @@ let
3240
};
3341
service3 = {
3442
process = {
35-
executable = dummyPkg "cowsay-ng" // { meta.mainProgram = "cowsay"; };
36-
args = [ "!" ];
43+
executable = "/bin/false";
44+
args = [];
45+
};
46+
services.exclacow = {
47+
process = {
48+
executable = dummyPkg "cowsay-ng" // { meta.mainProgram = "cowsay"; };
49+
args = [ "!" ];
50+
};
51+
assertions = [
52+
{ assertion = false; message = "you can't enable this for such reason"; }
53+
];
54+
warnings = [
55+
"The `bar' service is deprecated and will go away soon!"
56+
];
3757
};
3858
};
3959
};
@@ -65,24 +85,64 @@ let
6585
args = [ "hello" ];
6686
};
6787
services = { };
88+
assertions = [
89+
{ assertion = false; message = "you can't enable this for that reason"; }
90+
];
91+
warnings = [
92+
"The `foo' service is deprecated and will go away soon!"
93+
];
6894
};
6995
service2 = {
7096
process = {
7197
executable = "${dummyPkg "cowsay.sh"}";
7298
args = [ "world" ];
7399
};
74100
services = { };
101+
assertions = [ ];
102+
warnings = [ ];
75103
};
76104
service3 = {
77105
process = {
78-
executable = "${dummyPkg "cowsay-ng"}/bin/cowsay";
79-
args = [ "!" ];
106+
executable = "/bin/false";
107+
args = [];
80108
};
81-
services = { };
109+
services.exclacow = {
110+
process = {
111+
executable = "${dummyPkg "cowsay-ng"}/bin/cowsay";
112+
args = [ "!" ];
113+
};
114+
services = { };
115+
assertions = [
116+
{ assertion = false; message = "you can't enable this for such reason"; }
117+
];
118+
warnings = [ "The `bar' service is deprecated and will go away soon!" ];
119+
};
120+
assertions = [ ];
121+
warnings = [ ];
82122
};
83123
};
84124
};
85125

126+
assert
127+
portable-lib.getWarnings ["service1"] exampleEval.config.services.service1 == [
128+
"in service1: The `foo' service is deprecated and will go away soon!"
129+
];
130+
131+
assert
132+
portable-lib.getAssertions ["service1"] exampleEval.config.services.service1 == [
133+
{ message = "in service1: you can't enable this for that reason"; assertion = false; }
134+
];
135+
136+
assert
137+
portable-lib.getWarnings ["service3"] exampleEval.config.services.service3 == [
138+
"in service3.services.exclacow: The `bar' service is deprecated and will go away soon!"
139+
];
140+
assert
141+
portable-lib.getAssertions ["service3"] exampleEval.config.services.service3 == [
142+
{ message = "in service3.services.exclacow: you can't enable this for such reason"; assertion = false; }
143+
];
144+
145+
86146
"ok";
87147

88148
in

nixos/modules/system/service/systemd/system.nix

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
{ lib, config, pkgs, ... }:
1+
{ lib, config, options, pkgs, ... }:
22

33
let
4-
inherit (lib) concatMapAttrs mkOption types;
4+
inherit (lib) concatMapAttrs mkOption types concatLists mapAttrsToList;
5+
6+
portable-lib = import ../portable/lib.nix { inherit lib; };
57

68
dash = before: after:
79
if after == ""
@@ -48,6 +50,19 @@ in
4850

4951
# Second half of the magic: siphon units that were defined in isolation to the system
5052
config = {
53+
54+
assertions = concatLists (
55+
mapAttrsToList
56+
(name: cfg: portable-lib.getAssertions (options.system.services.loc ++ [ name ]) cfg)
57+
config.system.services
58+
);
59+
60+
warnings = concatLists (
61+
mapAttrsToList
62+
(name: cfg: portable-lib.getWarnings (options.system.services.loc ++ [ name ]) cfg)
63+
config.system.services
64+
);
65+
5166
systemd.services =
5267
concatMapAttrs
5368
(serviceName: topLevelService:

0 commit comments

Comments
 (0)