1 import ./make-test-python.nix ({ pkgs, ... }: {
4 nodes.machine = { config, lib, ... }: {
5 imports = [ common/user-account.nix common/x11.nix ];
7 virtualisation.emptyDiskImages = [ 512 512 ];
9 environment.systemPackages = [ pkgs.cryptsetup ];
11 virtualisation.fileSystems = {
12 "/test-x-initrd-mount" = {
17 options = [ "x-initrd.mount" ];
21 systemd.extraConfig = "DefaultEnvironment=\"XXX_SYSTEM=foo\"";
22 systemd.user.extraConfig = "DefaultEnvironment=\"XXX_USER=bar\"";
23 services.journald.extraConfig = "Storage=volatile";
24 test-support.displayManager.auto.user = "alice";
26 systemd.shutdown.test = pkgs.writeScript "test.shutdown" ''
27 #!${pkgs.runtimeShell}
28 PATH=${lib.makeBinPath (with pkgs; [ util-linux coreutils ])}
29 mount -t 9p shared -o trans=virtio,version=9p2000.L /tmp/shared
30 touch /tmp/shared/shutdown-test
34 systemd.services.oncalendar-test = {
35 description = "calendar test";
36 # Japan does not have DST which makes the test a little bit simpler
37 startAt = "Wed 10:00 Asia/Tokyo";
41 systemd.services.testDependency1 = {
42 description = "Test Dependency 1";
43 wantedBy = [ config.systemd.services."testservice1".name ];
44 serviceConfig.Type = "oneshot";
50 systemd.services.testservice1 = {
51 description = "Test Service 1";
52 wantedBy = [ config.systemd.targets.multi-user.name ];
53 serviceConfig.Type = "oneshot";
55 if [ "$XXX_SYSTEM" = foo ]; then
56 touch /system_conf_read
61 systemd.user.services.testservice2 = {
62 description = "Test Service 2";
63 wantedBy = [ "default.target" ];
64 serviceConfig.Type = "oneshot";
66 if [ "$XXX_USER" = bar ]; then
67 touch "$HOME/user_conf_read"
73 device = "/dev/watchdog";
79 environment.etc."systemd/system-preset/10-testservice.preset".text = ''
80 disable ${config.systemd.services.testservice1.name}
84 testScript = { nodes, ... }: ''
88 machine.start(allow_reboot=True)
90 # Will not succeed unless ConditionFirstBoot=yes
91 machine.wait_for_unit("first-boot-complete.target")
93 # Make sure, a subsequent boot isn't a ConditionFirstBoot=yes.
96 state = machine.get_unit_info("first-boot-complete.target")['ActiveState']
97 assert state == 'inactive', "Detected first boot despite first-boot-completed.target was already reached on a previous boot."
99 # wait for user services
100 machine.wait_for_unit("default.target", "alice")
102 with subtest("systemctl edit suggests --runtime"):
103 # --runtime is suggested when using `systemctl edit`
104 ret, out = machine.execute("systemctl edit testservice1.service 2>&1")
106 assert out.rstrip("\n") == "The unit-directory '/etc/systemd/system' is read-only on NixOS, so it's not possible to edit system-units directly. Use 'systemctl edit --runtime' instead."
107 # editing w/o `--runtime` is possible for user-services, however
108 # it's not possible because we're not in a tty when grepping
109 # (i.e. hacky way to ensure that the error from above doesn't appear here).
110 _, out = machine.execute("systemctl --user edit testservice2.service 2>&1")
111 assert out.rstrip("\n") == "Cannot edit units if not on a tty."
113 # Regression test for https://github.com/NixOS/nixpkgs/issues/105049
114 with subtest("systemd reads timezone database in /etc/zoneinfo"):
115 timer = machine.succeed("TZ=UTC systemctl show --property=TimersCalendar oncalendar-test.timer")
116 assert re.search("next_elapse=Wed ....-..-.. 01:00:00 UTC", timer), f"got {timer.strip()}"
118 # Regression test for https://github.com/NixOS/nixpkgs/issues/35415
119 with subtest("configuration files are recognized by systemd"):
120 machine.succeed("test -e /system_conf_read")
121 machine.succeed("test -e /home/alice/user_conf_read")
122 machine.succeed("test -z $(ls -1 /var/log/journal)")
124 with subtest("regression test for https://bugs.freedesktop.org/show_bug.cgi?id=77507"):
125 retcode, output = machine.execute("systemctl status testservice1.service")
126 assert retcode in [0, 3] # https://bugs.freedesktop.org/show_bug.cgi?id=77507
128 # Regression test for https://github.com/NixOS/nixpkgs/issues/35268
129 with subtest("file system with x-initrd.mount is not unmounted"):
130 machine.succeed("mountpoint -q /test-x-initrd-mount")
133 subprocess.check_call(
139 "vm-state-machine/empty0.qcow2",
140 "x-initrd-mount.raw",
143 extinfo = subprocess.check_output(
145 "${pkgs.e2fsprogs}/bin/dumpe2fs",
146 "x-initrd-mount.raw",
150 re.search(r"^Filesystem state: *clean$", extinfo, re.MULTILINE) is not None
151 ), ("File system was not cleanly unmounted: " + extinfo)
153 # Regression test for https://github.com/NixOS/nixpkgs/pull/91232
154 with subtest("setting transient hostnames works"):
155 machine.succeed("hostnamectl set-hostname --transient machine-transient")
156 machine.fail("hostnamectl set-hostname machine-all")
158 with subtest("systemd-shutdown works"):
160 machine.wait_for_unit("multi-user.target")
161 machine.succeed("test -e /tmp/shared/shutdown-test")
163 # Test settings from /etc/sysctl.d/50-default.conf are applied
164 with subtest("systemd sysctl settings are applied"):
165 machine.wait_for_unit("multi-user.target")
166 assert "fq_codel" in machine.succeed("sysctl net.core.default_qdisc")
168 # Test systemd is configured to manage a watchdog
169 with subtest("systemd manages hardware watchdog"):
170 machine.wait_for_unit("multi-user.target")
172 # It seems that the device's path doesn't appear in 'systemctl show' so
173 # check it separately.
174 assert "WatchdogDevice=/dev/watchdog" in machine.succeed(
175 "cat /etc/systemd/system.conf"
178 output = machine.succeed("systemctl show | grep Watchdog")
179 # assert "RuntimeWatchdogUSec=30s" in output
180 # for some reason RuntimeWatchdogUSec, doesn't seem to be updated in here.
181 assert "RebootWatchdogUSec=10min" in output
182 assert "KExecWatchdogUSec=5min" in output
184 # Test systemd cryptsetup support
185 with subtest("systemd successfully reads /etc/crypttab and unlocks volumes"):
186 # create a luks volume and put a filesystem on it
188 "echo -n supersecret | cryptsetup luksFormat -q /dev/vdc -",
189 "echo -n supersecret | cryptsetup luksOpen --key-file - /dev/vdc foo",
190 "mkfs.ext3 /dev/mapper/foo",
193 # create a keyfile and /etc/crypttab
194 machine.succeed("echo -n supersecret > /var/lib/luks-keyfile")
195 machine.succeed("chmod 600 /var/lib/luks-keyfile")
196 machine.succeed("echo 'luks1 /dev/vdc /var/lib/luks-keyfile luks' > /etc/crypttab")
198 # after a reboot, systemd should unlock the volume and we should be able to mount it
200 machine.succeed("systemctl status systemd-cryptsetup@luks1.service")
201 machine.succeed("mkdir -p /tmp/luks1")
202 machine.succeed("mount /dev/mapper/luks1 /tmp/luks1")
205 output_ping = machine.succeed(
206 "systemd-run --wait -- ping -c 1 127.0.0.1 2>&1"
209 with subtest("systemd reports accounting data on system.slice"):
210 output = machine.succeed("systemctl status system.slice")
211 assert "CPU:" in output
212 assert "Memory:" in output
214 assert "IP:" in output
215 assert "0B in, 0B out" not in output
217 assert "IO:" in output
218 assert "0B read, 0B written" not in output
220 with subtest("systemd per-unit accounting works"):
221 assert "IP traffic received: 84B sent: 84B" in output_ping
223 with subtest("systemd environment is properly set"):
224 machine.systemctl("daemon-reexec") # Rewrites /proc/1/environ
225 machine.succeed("grep -q TZDIR=/etc/zoneinfo /proc/1/environ")
227 with subtest("systemd presets are ignored"):
228 machine.succeed("systemctl preset ${nodes.machine.systemd.services.testservice1.name}")
229 machine.succeed("test -e /etc/systemd/system/${nodes.machine.systemd.services.testservice1.name}")