1 import ./make-test-python.nix ({ pkgs, ... }:
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 ];
20 containerSystem = (import ../lib/eval-config.nix {
21 inherit (pkgs) system;
22 modules = [ container ];
23 }).config.system.build.toplevel;
25 containerName = "container";
26 containerRoot = "/var/lib/machines/${containerName}";
30 name = "systemd-machinectl";
32 nodes.machine = { lib, ... }: {
33 # use networkd to obtain systemd network setup
34 networking.useNetworkd = true;
35 networking.useDHCP = false;
37 # do not try to access cache.nixos.org
38 nix.settings.substituters = lib.mkForce [ ];
40 # auto-start container
41 systemd.targets.machines.wants = [ "systemd-nspawn@${containerName}.service" ];
43 virtualisation.additionalPaths = [ containerSystem ];
45 # not needed, but we want to test the nspawn file generation
46 systemd.nspawn.${containerName} = { };
48 systemd.services."systemd-nspawn@${containerName}" = {
49 serviceConfig.Environment = [
50 # Disable tmpfs for /tmp
51 "SYSTEMD_NSPAWN_TMPFS_TMP=0"
53 overrideStrategy = "asDropin";
59 machine.wait_for_unit("default.target");
62 machine.succeed("mkdir -p ${containerRoot}");
63 # Workaround for nixos-install
64 machine.succeed("chmod o+rx /var/lib/machines");
65 machine.succeed("nixos-install --root ${containerRoot} --system ${containerSystem} --no-channel-copy --no-root-passwd");
67 # Allow systemd-nspawn to apply user namespace on immutable files
68 machine.succeed("chattr -i ${containerRoot}/var/empty");
70 # Test machinectl start
71 machine.succeed("machinectl start ${containerName}");
72 machine.wait_until_succeeds("systemctl -M ${containerName} is-active default.target");
74 # Test nss_mymachines without nscd
75 machine.succeed('LD_LIBRARY_PATH="/run/current-system/sw/lib" getent -s hosts:mymachines hosts ${containerName}');
77 # Test nss_mymachines via nscd
78 machine.succeed("getent hosts ${containerName}");
80 # Test systemd-nspawn network configuration
81 machine.succeed("ping -n -c 1 ${containerName}");
83 # Test systemd-nspawn uses a user namespace
84 machine.succeed("test $(machinectl status ${containerName} | grep 'UID Shift: ' | wc -l) = 1")
86 # Test systemd-nspawn reboot
87 machine.succeed("machinectl shell ${containerName} /run/current-system/sw/bin/reboot");
88 machine.wait_until_succeeds("systemctl -M ${containerName} is-active default.target");
90 # Test machinectl reboot
91 machine.succeed("machinectl reboot ${containerName}");
92 machine.wait_until_succeeds("systemctl -M ${containerName} is-active default.target");
97 machine.wait_for_unit("default.target");
100 machine.succeed("machinectl show ${containerName}")
102 # Test machinectl stop
103 machine.succeed("machinectl stop ${containerName}");
104 machine.wait_until_succeeds("test $(systemctl is-active systemd-nspawn@${containerName}) = inactive");
106 # Test tmpfs for /tmp
107 machine.fail("mountpoint /tmp");
109 # Show to to delete the container
110 machine.succeed("chattr -i ${containerRoot}/var/empty");
111 machine.succeed("rm -rf ${containerRoot}");