summaryrefslogtreecommitdiffstats
path: root/nixos/tests/os-prober.nix
blob: c1cb71dc75e4ece054a1933dc0715fd05ef37623 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import ./make-test-python.nix (
  { pkgs, lib, ... }:
  let
    # A filesystem image with a (presumably) bootable debian
    debianImage = pkgs.vmTools.diskImageFuns.debian11i386 {
      # os-prober cannot detect systems installed on disks without a partition table
      # so we create the disk ourselves
      createRootFS = with pkgs; ''
        ${parted}/bin/parted --script /dev/vda mklabel msdos
        ${parted}/sbin/parted --script /dev/vda -- mkpart primary ext2 1M -1s
        mkdir /mnt
        ${e2fsprogs}/bin/mkfs.ext4 -O '^metadata_csum_seed' /dev/vda1
        ${util-linux}/bin/mount -t ext4 /dev/vda1 /mnt

        if test -e /mnt/.debug; then
          exec ${bash}/bin/sh
        fi
        touch /mnt/.debug

        mkdir /mnt/proc /mnt/dev /mnt/sys
      '';
      extraPackages = [
        # /etc/os-release
        "base-files"
        # make the disk bootable-looking
        "grub2"
        "linux-image-686"
      ];
      # install grub
      postInstall = ''
        ln -sf /proc/self/mounts > /etc/mtab
        PATH=/usr/bin:/bin:/usr/sbin:/sbin $chroot /mnt \
          grub-install /dev/vda --force
        PATH=/usr/bin:/bin:/usr/sbin:/sbin $chroot /mnt \
          update-grub
      '';
    };
    zfsSupport = false;

    # a part of the configuration of the test vm
    simpleConfig = {
      boot.loader.grub = {
        enable = true;
        useOSProber = true;
        device = "/dev/vda";
        # vda is a filesystem without partition table
        forceInstall = true;
      };
      nix.settings = {
        substituters = lib.mkForce [ ];
        hashed-mirrors = null;
        connect-timeout = 1;
      };
      # save some memory
      documentation.enable = false;
      # this option is only there to enable more direct copy paste from the installer test
      boot.supportedFilesystems.zfs = zfsSupport;
    };
    # /etc/nixos/configuration.nix for the vm
    configFile = pkgs.writeText "configuration.nix" ''
      {config, pkgs, lib, ...}: ({
      imports =
            [ ./hardware-configuration.nix
              <nixpkgs/nixos/modules/testing/test-instrumentation.nix>
            ];
      } // lib.importJSON ${pkgs.writeText "simpleConfig.json" (builtins.toJSON simpleConfig)})
    '';
  in
  {
    name = "os-prober";

    nodes.machine =
      { config, pkgs, ... }:
      (
        simpleConfig
        // {
          imports = [
            ../modules/profiles/installation-device.nix
            ../modules/profiles/base.nix
          ];
          virtualisation.memorySize = 1300;
          # To add the secondary disk:
          virtualisation.qemu.options = [
            "-drive index=2,file=${debianImage}/disk-image.qcow2,read-only,if=virtio"
          ];

          # The test cannot access the network, so any packages
          # nixos-rebuild needs must be included in the VM.
          system.extraDependencies = with pkgs; [
            os-prober

            # list copied from installer test nixos/tests/installer.nix
            stdenv

            bintools
            brotli
            brotli.dev
            brotli.lib
            desktop-file-utils
            docbook5
            docbook_xsl_ns
            hello
            kbd.dev
            kmod.dev
            libarchive.dev
            libcap-text-verifier
            libxml2.bin
            libxslt.bin
            nixos-artwork.wallpapers.simple-dark-gray-bottom
            nixos-rebuild-ng
            ntp
            perlPackages.ConfigIniFiles
            perlPackages.FileSlurp
            perlPackages.JSON
            perlPackages.ListCompare
            perlPackages.XMLLibXML
            # make-options-doc/default.nix
            (python3.withPackages (p: [ p.mistune ]))
            shared-mime-info
            sudo
            switch-to-configuration-ng
            texinfo
            unionfs-fuse
            lndir
            shellcheck-minimal

            # Only the out output is included here, which is what is
            # required to build the NixOS udev rules
            # See the comment in services/hardware/udev.nix
            systemdMinimal.out

            # add curl so that rather than seeing the test attempt to download
            # curl's tarball, we see what it's trying to download
            curl

            (pkgs.grub2.override { inherit zfsSupport; })
            (pkgs.grub2_efi.override { inherit zfsSupport; })
            pkgs.nixos-artwork.wallpapers.simple-dark-gray-bootloader
            pkgs.perlPackages.FileCopyRecursive
            pkgs.perlPackages.XMLSAX
            pkgs.perlPackages.XMLSAXBase
          ];
        }
      );

    testScript = ''
      machine.start()
      machine.succeed("udevadm settle")
      machine.wait_for_unit("multi-user.target")
      print(machine.succeed("lsblk"))

      # check that os-prober works standalone
      machine.succeed(
          "${pkgs.os-prober}/bin/os-prober | grep /dev/vdb1"
      )

      # rebuild and test that debian is available in the grub menu
      machine.succeed("nixos-generate-config")
      machine.copy_from_host(
          "${configFile}",
          "/etc/nixos/configuration.nix",
      )
      machine.succeed("nixos-rebuild boot --show-trace >&2")

      machine.succeed("egrep 'menuentry.*debian' /boot/grub/grub.cfg")
    '';
  }
)