summaryrefslogtreecommitdiffstats
path: root/nixos/tests/nixos-rebuild-store-path.nix
blob: 5d8fc0fff71cf31bcc020116290f578170c36282 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
{ hostPkgs, ... }:
{
  name = "nixos-rebuild-store-path";

  # TODO: remove overlay from  nixos/modules/profiles/installation-device.nix
  #        make it a _small package instead, then remove pkgsReadOnly = false;.
  node.pkgsReadOnly = false;

  nodes = {
    machine =
      { lib, pkgs, ... }:
      {
        imports = [
          ../modules/profiles/installation-device.nix
          ../modules/profiles/base.nix
        ];

        nix.settings = {
          substituters = lib.mkForce [ ];
          hashed-mirrors = null;
          connect-timeout = 1;
        };
        nix.enable = true; # disabled by default. See all-tests.nix / tag(no-nix-by-default)

        system.includeBuildDependencies = true;

        system.extraDependencies = [
          # Not part of the initial build apparently?
          pkgs.grub2
        ];

        system.switch.enable = true;

        virtualisation = {
          cores = 2;
          memorySize = 4096;
        };
      };
  };

  testScript =
    let
      configFile =
        hostname:
        hostPkgs.writeText "configuration.nix" # nix
          ''
            { lib, pkgs, ... }: {
              imports = [
                ./hardware-configuration.nix
                <nixpkgs/nixos/modules/testing/test-instrumentation.nix>
              ];

              boot.loader.grub = {
                enable = true;
                device = "/dev/vda";
                forceInstall = true;
              };

              documentation.enable = false;

              networking.hostName = "${hostname}";
            }
          '';

    in
    # python
    ''
      machine.start()
      machine.succeed("udevadm settle")
      machine.wait_for_unit("multi-user.target")

      machine.succeed("nixos-generate-config")

      with subtest("Build configuration without switching"):
          machine.copy_from_host(
              "${configFile "store-path-test"}",
              "/etc/nixos/configuration.nix",
          )
          store_path = machine.succeed("nix-build '<nixpkgs/nixos>' -A system --no-out-link").strip()
          machine.succeed(f"test -f {store_path}/nixos-version")

      with subtest("Switch using --store-path"):
          machine.succeed(f"nixos-rebuild switch --store-path {store_path}")
          hostname = machine.succeed("cat /etc/hostname").strip()
          assert hostname == "store-path-test", f"Expected hostname 'store-path-test', got '{hostname}'"

      with subtest("Test using --store-path"):
          machine.copy_from_host(
              "${configFile "store-path-test-2"}",
              "/etc/nixos/configuration.nix",
          )
          store_path_2 = machine.succeed("nix-build '<nixpkgs/nixos>' -A system --no-out-link").strip()
          machine.succeed(f"nixos-rebuild test --store-path {store_path_2}")
          hostname = machine.succeed("cat /etc/hostname").strip()
          assert hostname == "store-path-test-2", f"Expected hostname 'store-path-test-2', got '{hostname}'"

      with subtest("Ensure --store-path rejects invalid combinations"):
          machine.fail(f"nixos-rebuild switch --store-path {store_path} --rollback")
          machine.fail(f"nixos-rebuild switch --store-path {store_path} --flake .")
          machine.fail(f"nixos-rebuild build --store-path {store_path}")
    '';
}