|
| 1 | +{ |
| 2 | + testers, |
| 3 | + writeText, |
| 4 | + ... |
| 5 | +}: |
| 6 | +testers.runNixOSTest { |
| 7 | + name = "nh-nixos-test"; |
| 8 | + nodes.machine = { |
| 9 | + lib, |
| 10 | + pkgs, |
| 11 | + ... |
| 12 | + }: { |
| 13 | + imports = [ |
| 14 | + ../vm.nix |
| 15 | + ]; |
| 16 | + |
| 17 | + nix.settings = { |
| 18 | + substituters = lib.mkForce []; |
| 19 | + hashed-mirrors = null; |
| 20 | + connect-timeout = 1; |
| 21 | + }; |
| 22 | + |
| 23 | + # Indicate parent config |
| 24 | + environment.systemPackages = [ |
| 25 | + (pkgs.writeShellScriptBin "parent" "") |
| 26 | + ]; |
| 27 | + |
| 28 | + programs.nh = { |
| 29 | + enable = true; |
| 30 | + flake = "/etc/nixos"; |
| 31 | + }; |
| 32 | + }; |
| 33 | + |
| 34 | + testScript = {nodes, ...}: let |
| 35 | + newConfig = |
| 36 | + writeText "configuration.nix" # nix |
| 37 | + |
| 38 | + '' |
| 39 | + { lib, pkgs, ... }: { |
| 40 | + imports = [ |
| 41 | + ./hardware-configuration.nix |
| 42 | + <nixpkgs/nixos/modules/testing/test-instrumentation.nix> |
| 43 | + ]; |
| 44 | +
|
| 45 | + boot.loader.grub = { |
| 46 | + enable = true; |
| 47 | + device = "/dev/vda"; |
| 48 | + forceInstall = true; |
| 49 | + }; |
| 50 | +
|
| 51 | + documentation.enable = false; |
| 52 | +
|
| 53 | + environment.systemPackages = [ |
| 54 | + (pkgs.writeShellScriptBin "parent" "") |
| 55 | + ]; |
| 56 | +
|
| 57 | +
|
| 58 | + specialisation.foo = { |
| 59 | + inheritParentConfig = true; |
| 60 | +
|
| 61 | + configuration = {...}: { |
| 62 | + environment.etc."specialisation".text = "foo"; |
| 63 | + }; |
| 64 | + }; |
| 65 | +
|
| 66 | + specialisation.bar = { |
| 67 | + inheritParentConfig = true; |
| 68 | +
|
| 69 | + configuration = {...}: { |
| 70 | + environment.etc."specialisation".text = "bar"; |
| 71 | + }; |
| 72 | + }; |
| 73 | + } |
| 74 | + ''; |
| 75 | + in |
| 76 | + # python |
| 77 | + '' |
| 78 | + machine.start() |
| 79 | + machine.succeed("udevadm settle") |
| 80 | + machine.wait_for_unit("multi-user.target") |
| 81 | +
|
| 82 | + machine.succeed("nixos-generate-config --flake") |
| 83 | + machine.copy_from_host("${newConfig}", "/etc/nixos/configuration.nix") |
| 84 | +
|
| 85 | + with subtest("Switch to the base system"): |
| 86 | + machine.succeed("nh os switch --no-nom") |
| 87 | + machine.succeed("parent") |
| 88 | + machine.fail("cat /etc/specialisation/text | grep 'foo'") |
| 89 | + machine.fail("cat /etc/specialisation/text | grep 'bar'") |
| 90 | +
|
| 91 | + with subtest("Switch to the foo system"): |
| 92 | + machine.succeed("nh os switch --no-nom --specialisation foo") |
| 93 | + machine.succeed("parent") |
| 94 | + machine.succeed("cat /etc/specialisation/text | grep 'foo'") |
| 95 | + machine.fail("cat /etc/specialisation/text | grep 'bar'") |
| 96 | +
|
| 97 | + with subtest("Switch to the bar system"): |
| 98 | + machine.succeed("nh os switch --no-nom --specialisation bar") |
| 99 | + machine.succeed("parent") |
| 100 | + machine.fail("cat /etc/specialisation/text | grep 'foo'") |
| 101 | + machine.succeed("cat /etc/specialisation/text | grep 'bar'") |
| 102 | +
|
| 103 | + with subtest("Switch into specialization using `nh os test`"): |
| 104 | + machine.succeed("nh os test --specialisation foo") |
| 105 | + machine.succeed("parent") |
| 106 | + machine.succeed("foo") |
| 107 | + machine.fail("bar") |
| 108 | + ''; |
| 109 | +} |
0 commit comments