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