1 import ./make-test-python.nix ({ pkgs, ... }:
4 container = { config, ... }: {
5 # We re-use the NixOS container option ...
6 boot.isContainer = true;
7 # ... and revert unwanted defaults
8 networking.useHostResolvConf = false;
10 # use networkd to obtain systemd network setup
11 networking.useNetworkd = true;
12 networking.useDHCP = false;
14 # systemd-nspawn expects /sbin/init
15 boot.loader.initScript.enable = true;
17 imports = [ ../modules/profiles/minimal.nix ];
19 system.stateVersion = config.system.nixos.release;
22 containerSystem = (import ../lib/eval-config.nix {
23 inherit (pkgs) system;
24 modules = [ container ];
25 }).config.system.build.toplevel;
27 containerName = "container";
28 containerRoot = "/var/lib/machines/${containerName}";
30 containerTarball = pkgs.callPackage ../lib/make-system-tarball.nix {
33 object = containerSystem;
34 symlink = "/nix/var/nix/profiles/system";
40 source = containerSystem + "/etc/os-release";
41 target = "/etc/os-release";
44 source = containerSystem + "/init";
45 target = "/sbin/init";
51 name = "systemd-machinectl";
53 nodes.machine = { lib, ... }: {
54 # use networkd to obtain systemd network setup
55 networking.useNetworkd = true;
56 networking.useDHCP = false;
58 # do not try to access cache.nixos.org
59 nix.settings.substituters = lib.mkForce [ ];
61 # auto-start container
62 systemd.targets.machines.wants = [ "systemd-nspawn@${containerName}.service" ];
64 virtualisation.additionalPaths = [ containerSystem containerTarball ];
66 systemd.tmpfiles.rules = [
67 "d /var/lib/machines/shared-decl 0755 root root - -"
69 systemd.nspawn.shared-decl = {
72 Parameters = "${containerSystem}/init";
75 BindReadOnly = "/nix/store";
79 systemd.nspawn.${containerName} = {
81 # workaround to fix kernel namespaces; needed for Nix sandbox
82 # https://github.com/systemd/systemd/issues/27994#issuecomment-1704005670
83 Bind = "/proc:/run/proc";
87 systemd.services."systemd-nspawn@${containerName}" = {
88 serviceConfig.Environment = [
89 # Disable tmpfs for /tmp
90 "SYSTEMD_NSPAWN_TMPFS_TMP=0"
92 # force unified cgroup delegation, which would be the default
93 # if systemd could check the capabilities of the installed systemd.
94 # see also: https://github.com/NixOS/nixpkgs/pull/198526
95 "SYSTEMD_NSPAWN_UNIFIED_HIERARCHY=1"
97 overrideStrategy = "asDropin";
100 # open DHCP for container
101 networking.firewall.extraCommands = ''
102 ${pkgs.iptables}/bin/iptables -A nixos-fw -i ve-+ -p udp -m udp --dport 67 -j nixos-fw-accept
108 machine.wait_for_unit("default.target");
110 # Test machinectl start stop of shared-decl
111 machine.succeed("machinectl start shared-decl");
112 machine.wait_until_succeeds("systemctl -M shared-decl is-active default.target");
113 machine.succeed("machinectl stop shared-decl");
115 # create containers root
116 machine.succeed("mkdir -p ${containerRoot}");
118 # start container with shared nix store by using same arguments as for systemd-nspawn@.service
119 machine.succeed("systemd-run systemd-nspawn --machine=${containerName} --network-veth -U --bind-ro=/nix/store ${containerSystem}/init")
120 machine.wait_until_succeeds("systemctl -M ${containerName} is-active default.target");
122 # Test machinectl stop
123 machine.succeed("machinectl stop ${containerName}");
126 # Workaround for nixos-install
127 machine.succeed("chmod o+rx /var/lib/machines");
128 machine.succeed("nixos-install --root ${containerRoot} --system ${containerSystem} --no-channel-copy --no-root-passwd");
130 # Allow systemd-nspawn to apply user namespace on immutable files
131 machine.succeed("chattr -i ${containerRoot}/var/empty");
133 # Test machinectl start
134 machine.succeed("machinectl start ${containerName}");
135 machine.wait_until_succeeds("systemctl -M ${containerName} is-active default.target");
137 # Test systemd-nspawn configured unified cgroup delegation
139 # https://github.com/systemd/systemd/blob/main/docs/CGROUP_DELEGATION.md#three-different-tree-setups-
140 machine.succeed('systemd-run --pty --wait -M ${containerName} /run/current-system/sw/bin/stat --format="%T" --file-system /sys/fs/cgroup > fstype')
141 machine.succeed('test $(tr -d "\\r" < fstype) = cgroup2fs')
143 # Test if systemd-nspawn provides a working environment for nix to build derivations
144 # https://nixos.org/guides/nix-pills/07-working-derivation
145 machine.succeed('systemd-run --pty --wait -M ${containerName} /run/current-system/sw/bin/nix-instantiate --expr \'derivation { name = "myname"; builder = "/bin/sh"; args = [ "-c" "echo foo > $out" ]; system = "${pkgs.system}"; }\' --add-root /tmp/drv')
146 machine.succeed('systemd-run --pty --wait -M ${containerName} /run/current-system/sw/bin/nix-store --option substitute false --realize /tmp/drv')
148 # Test nss_mymachines without nscd
149 machine.succeed('LD_LIBRARY_PATH="/run/current-system/sw/lib" getent -s hosts:mymachines hosts ${containerName}');
151 # Test nss_mymachines via nscd
152 machine.succeed("getent hosts ${containerName}");
154 # Test systemd-nspawn network configuration to container
155 machine.succeed("networkctl --json=short status ve-${containerName} | ${pkgs.jq}/bin/jq -e '.OperationalState == \"routable\"'");
157 # Test systemd-nspawn network configuration to host
158 machine.succeed("machinectl shell ${containerName} /run/current-system/sw/bin/networkctl --json=short status host0 | ${pkgs.jq}/bin/jq -r '.OperationalState == \"routable\"'");
160 # Test systemd-nspawn network configuration
161 machine.succeed("ping -n -c 1 ${containerName}");
163 # Test systemd-nspawn uses a user namespace
164 machine.succeed("test $(machinectl status ${containerName} | grep 'UID Shift: ' | wc -l) = 1")
166 # Test systemd-nspawn reboot
167 machine.succeed("machinectl shell ${containerName} /run/current-system/sw/bin/reboot");
168 machine.wait_until_succeeds("systemctl -M ${containerName} is-active default.target");
170 # Test machinectl reboot
171 machine.succeed("machinectl reboot ${containerName}");
172 machine.wait_until_succeeds("systemctl -M ${containerName} is-active default.target");
177 machine.wait_for_unit("default.target");
180 machine.succeed("machinectl show ${containerName}")
182 # Test machinectl stop
183 machine.succeed("machinectl stop ${containerName}");
184 machine.wait_until_succeeds("test $(systemctl is-active systemd-nspawn@${containerName}) = inactive");
186 # Test tmpfs for /tmp
187 machine.fail("mountpoint /tmp");
189 # Show to to delete the container
190 machine.succeed("chattr -i ${containerRoot}/var/empty");
191 machine.succeed("rm -rf ${containerRoot}");
193 # Test import tarball, start, stop and remove
194 machine.succeed("machinectl import-tar ${containerTarball}/tarball/*.tar* ${containerName}");
195 machine.succeed("machinectl start ${containerName}");
196 machine.wait_until_succeeds("systemctl -M ${containerName} is-active default.target");
197 machine.succeed("machinectl stop ${containerName}");
198 machine.wait_until_succeeds("test $(systemctl is-active systemd-nspawn@${containerName}) = inactive");
199 machine.succeed("machinectl remove ${containerName}");