Disko zfs cannot import 'zroot': no such pool available

NixOS Stage 1 is failing to boot with “cannot import ‘zroot’: no such pool available” when attempting to boot from a newly installed nixos (in a qemu VM), but when I boot from the ISO image again I’m able to successfully import the pool:

[nixos@nixos:~]$ sudo zpool import zroot

[nixos@nixos:~]$ sudo zpool list
NAME    SIZE  ALLOC   FREE  CKPOINT  EXPANDSZ   FRAG    CAP  DEDUP    HEALTH  ALTROOT
zroot   126G  6.51G   119G        -         -     0%     5%  1.00x    ONLINE  -

[nixos@nixos:~]$ sudo zfs list
NAME                  USED  AVAIL  REFER  MOUNTPOINT
zroot                6.51G   116G  6.51G  /zroot
zroot/alistair         24K   116G    24K  /zroot/alistair
zroot/alistaircache    24K   116G    24K  /zroot/alistaircache
zroot/home             24K   116G    24K  /zroot/home

Disko and grub definition:

{ config, pkgs, ... }:

{
  imports =
    [ ./nixosvm-hardware-configuration.nix
      "${builtins.fetchTarball "https://github.com/nix-community/disko/archive/master.tar.gz"}/module.nix"
      ./nixosvm-disko.nix
    ];

  boot.initrd.supportedFilesystems = [ "zfs" ];
  boot.supportedFilesystems = [ "zfs" ];
  boot.zfs.enableUnstable = true;
  services.zfs.autoScrub.enable = true;

  boot.loader.systemd-boot.enable = false;
  
  boot.loader.grub = {
    enable = true;
    efiSupport = true;
    efiInstallAsRemovable = true;
    devices = [ "nodev" ];
  };

...

nixosvm-disko.nix (adapted from https://github.com/nix-community/disko/blob/98ab91109716871f50ea8cb0e0ac7cc1e1e14714/example/zfs.nix):

{ disks ? [ "/dev/vda" ], swapsize ? "2GB", ... }:
{
  disko.devices = {
    disk = {
      vda = {
        type = "disk";
        device = builtins.elemAt disks 0;
        content = {
          type = "gpt";
          partitions = {
            ESP = {
              size = "1G";
              type = "EF00";
              content = {
                type = "filesystem";
                format = "vfat";
                mountpoint = "/boot";
              };
            };
            zfs = {
              size = "100%";
              content = {
                type = "zfs";
                pool = "zroot";
              };
            };
          };
        };
      };
    };
    zpool = {
      zroot = {
        type = "zpool";
        rootFsOptions = {
          compression = "zstd";
          "com.sun:auto-snapshot" = "false";
        };
        mountpoint = "/";

        datasets = {
          home = {
            type = "zfs_fs";
            mountpoint = "/home";
          };
          alistair = {
            type = "zfs_fs";
            mountpoint = "/home/alistair";
          };
          alistaircache = {
            type = "zfs_fs";
            mountpoint = "/home/alistair/.cache";
          };
        };
      };
    };
  };
}

This is in a flake with nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11".

I’ve tried about 20 variations (systemd vs. grub, generate the hardware config directly, efiInstallAsRemovable=false, etc.) without success.

Thanks in advance for any suggestions,
Alistair

When you boot the iso image and import the pool, you need to do a zfs export zpool before rebooting into your new installation since the iso and your installation have a different hostId.

@firecat53 thanks for your response and my apologies for a slow reponse, my time has been rather fragmented over the last week or so.

Great point, when I saw this my first thought was “of course!”, but after much more investigation this wasn’t the issue…

There appear to be two issues, the first is that attempting to install the OS on ZFS on a root dataset (as specified above) doesn’t work, while the following disko config does work (for me):

{ disks ? [ "/dev/vda" ], ... }:
{
  disko.devices = {
    disk = {
      vda = {
        type = "disk";
        device = builtins.elemAt disks 0;
        content = {
          type = "gpt";
          partitions = {
            ESP = {
              size = "1G";
              type = "EF00";
              content = {
                type = "filesystem";
                format = "vfat";
                mountpoint = "/boot";
              };
            };
            zfs = {
              size = "100%";
              content = {
                type = "zfs";
                pool = "zroot";
              };
            };
          };
        };
      };
    };
    zpool = {
      zroot = {
        type = "zpool";

        datasets = {
          root = {
            type = "zfs_fs";
            options.mountpoint = "legacy";
            mountpoint = "/";
            options."com.sun:auto-snapshot" = "false";
          };
          home = {
            type = "zfs_fs";
	    options.mountpoint = "legacy";
            mountpoint = "/home";
          };
          alistair = {
            type = "zfs_fs";
            options.mountpoint = "legacy";
            mountpoint = "/home/alistair";
          };
          alistaircache = {
            type = "zfs_fs";
            options.mountpoint = "legacy";
            mountpoint = "/home/alistair/.cache";
          };
        };
      };
    };
  };
}

(the difference being that the dataset mounted at root (/) is zpool/root instead of zpool/zroot).

The second issue was that I coudn’t get nixos-install to work with flakes (nixos-install --flake .#nixosvm). Installing using channels and then switching to flakes works fine.

Thanks again.