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