1{
2 hostPkgs,
3 lib,
4 withNg,
5 ...
6}:
7{
8 name = "nixos-rebuild-specialisations";
9
10 # TODO: remove overlay from nixos/modules/profiles/installation-device.nix
11 # make it a _small package instead, then remove pkgsReadOnly = false;.
12 node.pkgsReadOnly = false;
13
14 nodes = {
15 machine =
16 { lib, pkgs, ... }:
17 {
18 imports = [
19 ../modules/profiles/installation-device.nix
20 ../modules/profiles/base.nix
21 ];
22
23 nix.settings = {
24 substituters = lib.mkForce [ ];
25 hashed-mirrors = null;
26 connect-timeout = 1;
27 };
28
29 system.includeBuildDependencies = true;
30
31 system.extraDependencies = [
32 # Not part of the initial build apparently?
33 pkgs.grub2
34 ];
35
36 system.rebuild.enableNg = withNg;
37 system.switch.enable = true;
38
39 virtualisation = {
40 cores = 2;
41 memorySize = 4096;
42 };
43 };
44 };
45
46 testScript =
47 let
48 configFile =
49 hostPkgs.writeText "configuration.nix" # nix
50 ''
51 { lib, pkgs, ... }: {
52 imports = [
53 ./hardware-configuration.nix
54 <nixpkgs/nixos/modules/testing/test-instrumentation.nix>
55 ];
56
57 boot.loader.grub = {
58 enable = true;
59 device = "/dev/vda";
60 forceInstall = true;
61 };
62
63 documentation.enable = false;
64
65 environment.systemPackages = [
66 (pkgs.writeShellScriptBin "parent" "")
67 ];
68
69 system.rebuild.enableNg = ${lib.boolToString withNg};
70
71 specialisation.foo = {
72 inheritParentConfig = true;
73
74 configuration = { ... }: {
75 environment.systemPackages = [
76 (pkgs.writeShellScriptBin "foo" "")
77 ];
78 };
79 };
80
81 specialisation.bar = {
82 inheritParentConfig = true;
83
84 configuration = { ... }: {
85 environment.systemPackages = [
86 (pkgs.writeShellScriptBin "bar" "")
87 ];
88 };
89 };
90 }
91 '';
92
93 in
94 # python
95 ''
96 machine.start()
97 machine.succeed("udevadm settle")
98 machine.wait_for_unit("multi-user.target")
99
100 machine.succeed("nixos-generate-config")
101 machine.copy_from_host(
102 "${configFile}",
103 "/etc/nixos/configuration.nix",
104 )
105
106 with subtest("Switch to the base system"):
107 machine.succeed("nixos-rebuild switch")
108 machine.succeed("parent")
109 machine.fail("foo")
110 machine.fail("bar")
111
112 with subtest("Switch from base system into a specialization"):
113 machine.succeed("nixos-rebuild switch --specialisation foo")
114 machine.succeed("parent")
115 machine.succeed("foo")
116 machine.fail("bar")
117
118 with subtest("Switch from specialization into another specialization"):
119 machine.succeed("nixos-rebuild switch -c bar")
120 machine.succeed("parent")
121 machine.fail("foo")
122 machine.succeed("bar")
123
124 with subtest("Switch from specialization into the base system"):
125 machine.succeed("nixos-rebuild switch")
126 machine.succeed("parent")
127 machine.fail("foo")
128 machine.fail("bar")
129
130 with subtest("Switch into specialization using `nixos-rebuild test`"):
131 machine.succeed("nixos-rebuild test --specialisation foo")
132 machine.succeed("parent")
133 machine.succeed("foo")
134 machine.fail("bar")
135
136 with subtest("Make sure nonsense command combinations are forbidden"):
137 machine.fail("nixos-rebuild boot --specialisation foo")
138 machine.fail("nixos-rebuild boot -c foo")
139 '';
140}