2022-09-19 13:33:32 +02:00
|
|
|
import ./make-test-python.nix ({ pkgs, ... }:
|
2022-02-17 22:07:05 +01:00
|
|
|
let
|
|
|
|
|
|
|
|
container = {
|
|
|
|
# We re-use the NixOS container option ...
|
|
|
|
boot.isContainer = true;
|
|
|
|
# ... and revert unwanted defaults
|
|
|
|
networking.useHostResolvConf = false;
|
|
|
|
|
|
|
|
# use networkd to obtain systemd network setup
|
|
|
|
networking.useNetworkd = true;
|
|
|
|
networking.useDHCP = false;
|
|
|
|
|
|
|
|
# systemd-nspawn expects /sbin/init
|
|
|
|
boot.loader.initScript.enable = true;
|
|
|
|
|
|
|
|
imports = [ ../modules/profiles/minimal.nix ];
|
|
|
|
};
|
|
|
|
|
|
|
|
containerSystem = (import ../lib/eval-config.nix {
|
2022-09-19 13:33:32 +02:00
|
|
|
inherit (pkgs) system;
|
2022-02-17 22:07:05 +01:00
|
|
|
modules = [ container ];
|
|
|
|
}).config.system.build.toplevel;
|
|
|
|
|
|
|
|
containerName = "container";
|
|
|
|
containerRoot = "/var/lib/machines/${containerName}";
|
|
|
|
|
|
|
|
in
|
|
|
|
{
|
|
|
|
name = "systemd-machinectl";
|
|
|
|
|
2022-03-21 00:15:30 +01:00
|
|
|
nodes.machine = { lib, ... }: {
|
2022-02-17 22:07:05 +01:00
|
|
|
# use networkd to obtain systemd network setup
|
|
|
|
networking.useNetworkd = true;
|
|
|
|
networking.useDHCP = false;
|
|
|
|
|
|
|
|
# do not try to access cache.nixos.org
|
|
|
|
nix.settings.substituters = lib.mkForce [ ];
|
|
|
|
|
2022-07-31 17:40:21 +02:00
|
|
|
# auto-start container
|
|
|
|
systemd.targets.machines.wants = [ "systemd-nspawn@${containerName}.service" ];
|
|
|
|
|
2022-02-17 22:07:05 +01:00
|
|
|
virtualisation.additionalPaths = [ containerSystem ];
|
2022-10-23 15:57:55 +02:00
|
|
|
|
|
|
|
# not needed, but we want to test the nspawn file generation
|
|
|
|
systemd.nspawn.${containerName} = { };
|
2022-02-17 22:07:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
testScript = ''
|
|
|
|
start_all()
|
|
|
|
machine.wait_for_unit("default.target");
|
|
|
|
|
|
|
|
# Install container
|
|
|
|
machine.succeed("mkdir -p ${containerRoot}");
|
|
|
|
# Workaround for nixos-install
|
|
|
|
machine.succeed("chmod o+rx /var/lib/machines");
|
|
|
|
machine.succeed("nixos-install --root ${containerRoot} --system ${containerSystem} --no-channel-copy --no-root-passwd");
|
|
|
|
|
|
|
|
# Allow systemd-nspawn to apply user namespace on immutable files
|
|
|
|
machine.succeed("chattr -i ${containerRoot}/var/empty");
|
|
|
|
|
|
|
|
# Test machinectl start
|
|
|
|
machine.succeed("machinectl start ${containerName}");
|
|
|
|
machine.wait_until_succeeds("systemctl -M ${containerName} is-active default.target");
|
|
|
|
|
2022-07-31 17:40:21 +02:00
|
|
|
# Test nss_mymachines without nscd
|
|
|
|
machine.succeed('LD_LIBRARY_PATH="/run/current-system/sw/lib" getent -s hosts:mymachines hosts ${containerName}');
|
|
|
|
|
2022-08-16 12:07:48 +02:00
|
|
|
# Test nss_mymachines via nscd
|
|
|
|
machine.succeed("getent hosts ${containerName}");
|
2022-07-31 17:40:21 +02:00
|
|
|
|
2022-02-17 22:07:05 +01:00
|
|
|
# Test systemd-nspawn network configuration
|
2022-08-16 12:07:48 +02:00
|
|
|
machine.succeed("ping -n -c 1 ${containerName}");
|
2022-02-17 22:07:05 +01:00
|
|
|
|
|
|
|
# Test systemd-nspawn uses a user namespace
|
2022-07-30 22:32:12 +02:00
|
|
|
machine.succeed("test $(machinectl status ${containerName} | grep 'UID Shift: ' | wc -l) = 1")
|
2022-02-17 22:07:05 +01:00
|
|
|
|
|
|
|
# Test systemd-nspawn reboot
|
|
|
|
machine.succeed("machinectl shell ${containerName} /run/current-system/sw/bin/reboot");
|
|
|
|
machine.wait_until_succeeds("systemctl -M ${containerName} is-active default.target");
|
|
|
|
|
|
|
|
# Test machinectl reboot
|
|
|
|
machine.succeed("machinectl reboot ${containerName}");
|
|
|
|
machine.wait_until_succeeds("systemctl -M ${containerName} is-active default.target");
|
|
|
|
|
2022-07-31 17:40:21 +02:00
|
|
|
# Restart machine
|
|
|
|
machine.shutdown()
|
|
|
|
machine.start()
|
|
|
|
machine.wait_for_unit("default.target");
|
|
|
|
|
|
|
|
# Test auto-start
|
|
|
|
machine.succeed("machinectl show ${containerName}")
|
|
|
|
|
2022-02-17 22:07:05 +01:00
|
|
|
# Test machinectl stop
|
|
|
|
machine.succeed("machinectl stop ${containerName}");
|
2022-07-30 22:32:12 +02:00
|
|
|
machine.wait_until_succeeds("test $(systemctl is-active systemd-nspawn@${containerName}) = inactive");
|
2022-02-17 22:07:05 +01:00
|
|
|
|
|
|
|
# Show to to delete the container
|
|
|
|
machine.succeed("chattr -i ${containerRoot}/var/empty");
|
|
|
|
machine.succeed("rm -rf ${containerRoot}");
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
)
|