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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
{
pkgs,
evalSystem,
runTest,
callTest,
}:
let
sharedDir = "/tmp/modular-service-compliance";
inherit (pkgs) lib coreutils;
evalSystemServices =
services:
evalSystem (
{ ... }:
{
system.services = services;
system.stateVersion = "25.05";
fileSystems."/" = {
device = "/test/dummy";
fsType = "auto";
};
boot.loader.grub.enable = false;
}
);
in
let
suite = pkgs.testers.modularServiceCompliance {
inherit sharedDir;
namePrefix = "system-services-compliance";
evalConfig =
{ services }:
let
machine = evalSystemServices services;
in
{
config = machine.config.system.services;
checkDrv = machine.config.system.build.toplevel;
};
callReload = path: "systemctl reload ${lib.concatStringsSep "-" path}.service";
mkTest =
{
name,
services,
testExe,
}:
runTest {
_class = "nixosTest";
inherit name;
nodes.machine.system.services = services;
testScript = ''
machine.wait_for_unit("multi-user.target")
machine.succeed("${testExe}")
'';
meta.maintainers = with pkgs.lib.maintainers; [ roberth ];
};
};
# systemd-specific eval assertions. serviceConfig.Type/ExecReload only exist on
# the resolved host units, so a fresh eval is used per case.
systemdEvalTests =
let
defaultUnits =
(evalSystemServices {
service.process.argv = [ "${coreutils}/bin/true" ];
}).config.systemd.services;
notifyUnits =
(evalSystemServices {
service = {
process.argv = [ "${coreutils}/bin/true" ];
notificationProtocol.systemd = true;
};
}).config.systemd.services;
reloadUnits =
(evalSystemServices {
service = {
process.argv = [ "${coreutils}/bin/true" ];
process.reloadSignal = "HUP";
};
}).config.systemd.services;
# A service setting `ExecReload` through the systemd escape hatch, without
# any `process.reloadCommand` of its own.
ownExecReloadUnits =
(evalSystemServices {
service = {
process.argv = [ "${coreutils}/bin/true" ];
systemd.service.serviceConfig.ExecReload = "${coreutils}/bin/kill -USR2 $MAINPID";
};
}).config.systemd.services;
in
{
testDefaultType = {
expr = defaultUnits.service.serviceConfig.Type;
expected = "simple";
};
testNotifyType = {
expr = notifyUnits.service.serviceConfig.Type;
expected = "notify";
};
testReloadExecReload = {
expr = reloadUnits.service.serviceConfig.ExecReload;
expected = "${coreutils}/bin/kill -HUP $MAINPID";
};
# Without a reload command, the framework must not define `ExecReload` at
# all, so that it neither conflicts with a service-provided definition nor
# renders a bare `ExecReload=` line.
testNoReloadExecReloadUnset = {
expr = defaultUnits.service.serviceConfig ? ExecReload;
expected = false;
};
testServiceOwnExecReload = {
expr = ownExecReloadUnits.service.serviceConfig.ExecReload;
expected = "${coreutils}/bin/kill -USR2 $MAINPID";
};
};
systemdEval = pkgs.stdenvNoCC.mkDerivation (finalAttrs: {
__structuredAttrs = true;
name = "system-services-compliance-systemd-eval-report";
passthru = {
tests = systemdEvalTests;
failures = lib.runTests finalAttrs.finalPackage.tests;
};
testResults = lib.mapAttrs (_: test: test.expr == test.expected) finalAttrs.finalPackage.tests;
buildCommand = ''
touch $out
for testName in "''${!testResults[@]}"; do
if [[ -n "''${testResults[$testName]}" ]]; then
echo "PASS $testName"
else
echo "FAIL $testName"
fi
done
''
+ lib.optionalString (lib.any (v: !v) (lib.attrValues finalAttrs.testResults)) ''
{
echo ""
echo "systemd-specific eval-level compliance failures:"
for testName in "''${!testResults[@]}"; do
if [[ -z "''${testResults[$testName]}" ]]; then
echo "- $testName"
fi
done
} >&2
exit 1
'';
});
in
# Please the callTest pattern.
#
# runTest results already go through findTests/callTest.
# For plain derivations like `eval`, we apply callTest directly.
pkgs.lib.mapAttrs (
_: v:
if v ? test then
v
else
callTest {
test = v;
driver = v;
}
) (suite // { systemd-eval = systemdEval; })
|