1
+ { pkgs , nixos-anywhere , kexec-installer , nix-vm-test , ... } :
2
+
3
+ let
4
+ # Create dummy store paths for testing
5
+ testPaths = pkgs . runCommand "test-store-paths" { } ''
6
+ mkdir -p $out
7
+ echo "echo 'Dummy disko script'" > $out/disko
8
+ echo "echo 'Dummy system closure'" > $out/system
9
+ chmod +x $out/disko $out/system
10
+ '' ;
11
+ in
12
+ nix-vm-test . ubuntu . "22_04" {
13
+ memorySize = 2048 ;
14
+
15
+ # Forward SSH port to allow connection from the host
16
+ virtualisation . forwardPorts = [
17
+ { from = "host" ; host . port = 2222 ; guest . port = 22 ; }
18
+ ] ;
19
+
20
+ # Make the SSH keys available in the VM
21
+ sharedDirs = {
22
+ sshKeys = {
23
+ source = "${ nixos-anywhere } /tests/modules/ssh-keys" ;
24
+ target = "/tmp/ssh-keys" ;
25
+ } ;
26
+ } ;
27
+
28
+ # The test script
29
+ testScript = ''
30
+ # Wait for the system to be fully booted
31
+ vm.wait_for_unit("multi-user.target")
32
+
33
+ # Unmask SSH service (which is masked by default in the test VM)
34
+ vm.succeed("systemctl unmask ssh.service")
35
+ vm.succeed("systemctl unmask ssh.socket")
36
+ vm.succeed("systemctl start ssh")
37
+
38
+ # Setup SSH with the existing keys
39
+ vm.succeed("mkdir -p /root/.ssh")
40
+ vm.succeed("cp /tmp/ssh-keys/ssh.pub /root/.ssh/authorized_keys")
41
+ vm.succeed("chmod 644 /root/.ssh/authorized_keys")
42
+
43
+ # Setup SSH for connection from host
44
+ vm.succeed("sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config")
45
+ vm.succeed("systemctl restart ssh")
46
+
47
+ # Wait for SSH to be available
48
+ vm.wait_for_open_port(22)
49
+ print("SSH server is ready")
50
+
51
+ # Run nixos-anywhere from the host against the VM
52
+ print("Starting kexec test phase...")
53
+
54
+ # Use Python's subprocess to run nixos-anywhere from the host
55
+ import subprocess
56
+
57
+ cmd = f"""${ nixos-anywhere } /bin/nixos-anywhere \\
58
+ -i ${ nixos-anywhere } /tests/modules/ssh-keys/ssh \\
59
+ --ssh-port 2222 \\
60
+ --phases kexec \\
61
+ --kexec ${ kexec-installer } \\
62
+ --store-paths ${ testPaths } /disko ${ testPaths } /system \\
63
+ --debug \\
64
+ root@localhost
65
+ """
66
+
67
+ print(f"Running command: {cmd}")
68
+ result = subprocess.run(cmd, shell=True, check=False)
69
+
70
+ if result.returncode == 0:
71
+ print("kexec phase completed successfully")
72
+ else:
73
+ print(f"nixos-anywhere failed with exit code {result.returncode}")
74
+ raise Exception("nixos-anywhere command failed")
75
+ '' ;
76
+ }
0 commit comments