1# Miscellaneous small tests that don't warrant their own VM run.
2
3import ./make-test-python.nix (
4 { lib, pkgs, ... }:
5 let
6 foo = pkgs.writeText "foo" "Hello World";
7 in
8 {
9 name = "misc";
10 meta.maintainers = with lib.maintainers; [ eelco ];
11
12 nodes.machine =
13 { lib, ... }:
14 {
15 swapDevices = lib.mkOverride 0 [
16 {
17 device = "/root/swapfile";
18 size = 128;
19 }
20 ];
21 environment.variables.EDITOR = lib.mkOverride 0 "emacs";
22 systemd.tmpfiles.rules = [ "d /tmp 1777 root root 10d" ];
23 systemd.tmpfiles.settings."10-test"."/tmp/somefile".d = { };
24 virtualisation.fileSystems = {
25 "/tmp2" = {
26 fsType = "tmpfs";
27 options = [
28 "mode=1777"
29 "noauto"
30 ];
31 };
32 # Tests https://discourse.nixos.org/t/how-to-make-a-derivations-executables-have-the-s-permission/8555
33 "/user-mount/point" = {
34 device = "/user-mount/source";
35 fsType = "none";
36 options = [
37 "bind"
38 "rw"
39 "user"
40 "noauto"
41 ];
42 };
43 "/user-mount/denied-point" = {
44 device = "/user-mount/denied-source";
45 fsType = "none";
46 options = [
47 "bind"
48 "rw"
49 "noauto"
50 ];
51 };
52 };
53 systemd.automounts = lib.singleton {
54 wantedBy = [ "multi-user.target" ];
55 where = "/tmp2";
56 };
57 users.users.sybil = {
58 isNormalUser = true;
59 group = "wheel";
60 };
61 users.users.alice = {
62 isNormalUser = true;
63 };
64 security.sudo = {
65 enable = true;
66 wheelNeedsPassword = false;
67 };
68 boot.kernel.sysctl."vm.swappiness" = 1;
69 boot.kernelParams = [ "vsyscall=emulate" ];
70 system.extraDependencies = [ foo ];
71 };
72
73 testScript = ''
74 with subtest("nixos-version"):
75 machine.succeed("[ `nixos-version | wc -w` = 2 ]")
76
77 with subtest("nixos-rebuild"):
78 assert "NixOS module" in machine.succeed("nixos-rebuild --help")
79
80 with subtest("Sanity check for uid/gid assignment"):
81 assert "4" == machine.succeed("id -u messagebus").strip()
82 assert "4" == machine.succeed("id -g messagebus").strip()
83 assert "users:x:100:" == machine.succeed("getent group users").strip()
84
85 with subtest("Regression test for GMP aborts on QEMU."):
86 machine.succeed("expr 1 + 2")
87
88 with subtest("the swap file got created"):
89 machine.wait_for_unit("root-swapfile.swap")
90 machine.succeed("ls -l /root/swapfile | grep 134217728")
91
92 with subtest("whether kernel.poweroff_cmd is set"):
93 machine.succeed('[ -x "$(cat /proc/sys/kernel/poweroff_cmd)" ]')
94
95 with subtest("whether the io cgroupv2 controller is properly enabled"):
96 machine.succeed("grep -q '\\bio\\b' /sys/fs/cgroup/cgroup.controllers")
97
98 with subtest("whether we have a reboot record in wtmp"):
99 machine.shutdown
100 machine.wait_for_unit("multi-user.target")
101 machine.succeed("last | grep reboot >&2")
102
103 with subtest("whether we can override environment variables"):
104 machine.succeed('[ "$EDITOR" = emacs ]')
105
106 with subtest("whether hostname (and by extension nss_myhostname) works"):
107 assert "machine" == machine.succeed("hostname").strip()
108 assert "machine" == machine.succeed("hostname -s").strip()
109
110 with subtest("whether systemd-udevd automatically loads modules for our hardware"):
111 machine.succeed("systemctl start systemd-udev-settle.service")
112 machine.wait_for_unit("systemd-udev-settle.service")
113 assert "mousedev" in machine.succeed("lsmod")
114
115 with subtest("whether systemd-tmpfiles-clean works"):
116 machine.succeed(
117 "touch /tmp/foo", "systemctl start systemd-tmpfiles-clean", "[ -e /tmp/foo ]"
118 )
119 # move into the future
120 machine.succeed(
121 'date -s "@$(($(date +%s) + 1000000))"',
122 "systemctl start systemd-tmpfiles-clean",
123 )
124 machine.fail("[ -e /tmp/foo ]")
125
126 with subtest("whether systemd-tmpfiles settings works"):
127 machine.succeed("[ -e /tmp/somefile ]")
128
129 with subtest("/etc/mtab"):
130 assert "/proc/mounts" == machine.succeed("readlink --no-newline /etc/mtab")
131
132 with subtest("whether automounting works"):
133 machine.fail("grep '/tmp2 tmpfs' /proc/mounts")
134 machine.succeed("touch /tmp2/x")
135 machine.succeed("grep '/tmp2 tmpfs' /proc/mounts")
136
137 with subtest(
138 "Whether mounting by a user is possible with the `user` option in fstab (#95444)"
139 ):
140 machine.succeed("mkdir -p /user-mount/source")
141 machine.succeed("touch /user-mount/source/file")
142 machine.succeed("chmod -R a+Xr /user-mount/source")
143 machine.succeed("mkdir /user-mount/point")
144 machine.succeed("chown alice:users /user-mount/point")
145 machine.succeed("su - alice -c 'mount /user-mount/point'")
146 machine.succeed("su - alice -c 'ls /user-mount/point/file'")
147 with subtest(
148 "Whether mounting by a user is denied without the `user` option in fstab"
149 ):
150 machine.succeed("mkdir -p /user-mount/denied-source")
151 machine.succeed("touch /user-mount/denied-source/file")
152 machine.succeed("chmod -R a+Xr /user-mount/denied-source")
153 machine.succeed("mkdir /user-mount/denied-point")
154 machine.succeed("chown alice:users /user-mount/denied-point")
155 machine.fail("su - alice -c 'mount /user-mount/denied-point'")
156
157 with subtest("shell-vars"):
158 machine.succeed('[ -n "$NIX_PATH" ]')
159
160 with subtest("Test sysctl"):
161 machine.wait_for_unit("systemd-sysctl.service")
162 assert "1" == machine.succeed("sysctl -ne vm.swappiness").strip()
163 machine.execute("sysctl vm.swappiness=60")
164 assert "60" == machine.succeed("sysctl -ne vm.swappiness").strip()
165
166 with subtest("Test boot parameters"):
167 assert "vsyscall=emulate" in machine.succeed("cat /proc/cmdline")
168 '';
169 }
170)