Nixos-install fails to find /boot device with luks+btrfs SSD

I have a flake-based NixOS system running. I have created a new host entry ssd to facilitate creating bootable USB devices. Using disko (fraught with poor documentation perils) to create a luks+btrfs system:

nix --extra-experimental-features 'nix-command flakes' run github:nix-community/disko -- --mode zap_create_mount /home/dap/.nixos/nixos/ssd/disko-config.nix --arg disks '[ "/dev/sdg" ]'

This works fine:

sdg                                  8:96   1  57.8G  0 disk  
├─sdg1                               8:97   1   512M  0 part  /mnt/boot
└─sdg2                               8:98   1  57.3G  0 part  
  └─crypted                        254:6    0  57.3G  0 crypt /mnt/var/log
                                                              /mnt/persist
                                                              /mnt/nix
                                                              /mnt/home
                                                              /mnt

Next:

nixos-generate-config --no-filesystems --root /mnt

followed by:

nixos-install --no-root-password --flake ~dap/.nixos#ssd -v --cores 0

This fails with:

% sudo nixos-install --no-root-password --flake ~dap/.nixos#ssd -v --cores 0
warning: Git tree '/home/dap/.nixos' is dirty
building the flake in git+file:///home/dap/.nixos...
warning: Git tree '/home/dap/.nixos' is dirty
these 12 derivations will be built:
  /nix/store/f21v0vjn51x6f5rzymi5vcp4c2ajfzx6-system-path.drv
(... elided ...)
building '/nix/store/4ms17a4j5359ygbiap9qzsby0w8qkvi0-nixos-system-ssd-23.05.20231007.5a237ae.drv'...
installing the boot loader...
setting up /etc...
No filesystem found on "/dev/sdg1": No such device
Traceback (most recent call last):
  File "/nix/store/lv4wbkqnz0apyj3g24sqwrp3ivs5hlzh-systemd-boot", line 341, in <module>
    main()
  File "/nix/store/lv4wbkqnz0apyj3g24sqwrp3ivs5hlzh-systemd-boot", line 258, in main
    subprocess.check_call(["/nix/store/9gzw98jc64qkwd17a6qqm63w25zysi57-systemd-253.6/bin/bootctl", "--esp-path=/boot"] + bootctl_flags + ["install"])
  File "/nix/store/bc45k1n0pkrdkr3xa6w84w1xhkl1kkyp-python3-3.10.12/lib/python3.10/subprocess.py", line 369, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['/nix/store/9gzw98jc64qkwd17a6qqm63w25zysi57-systemd-253.6/bin/bootctl', '--esp-path=/boot', 'install']' returned non-zero exit status 1.
installation finished!

The ssd config pulls in the disko config:

 cat disko-config.nix
{ disks ? [ "/dev/disk/by-id/usb-Samsung_Portable_SSD_T5_12345681C1BA-0:0" ], ... }: {
  disko.devices = {
    disk = {
      vdb = {
        type = "disk";
        device = builtins.elemAt disks 0; # "/dev/disk/by-id/usb-Samsung_Portable_SSD_T5_12345681C1BA-0:0";
        content = {
          type = "gpt";
          partitions = {
            ESP = {
              size = "512M";
              type = "EF00";
              content = {
                type = "filesystem";
                format = "vfat";
                mountpoint = "/boot";
                mountOptions = [
                  "defaults"
                ];
              };
            };
            luks = {
              size = "100%";
              content = {
                type = "luks";
                name = "crypted";
                extraOpenArgs = [ "--allow-discards" ];
                # if you want to use the key for interactive login be sure there is no trailing newline
                # for example use `echo -n "password" > /tmp/secret.key`
                #passwordFile = "/tmp/secret.key"; # Interactive
                # settings.keyFile = "/tmp/secret.key";
                # additionalKeyFiles = [ "/tmp/additionalSecret.key" ];
                content = {
                  type = "btrfs";
                  extraArgs = [ "-f" ];
                  subvolumes = {
                    "@" = {
                      mountOptions = [ "compress=zstd" "noatime" ];
                    };
                    "@/root" = {
                      mountpoint = "/";
                      mountOptions = [ "compress=zstd" "noatime" ];
                    };
                    # Mountpoints inferred from subvolume name
                    "@/home" = {
                      mountpoint = "/home";
                      mountOptions = [ "compress=zstd" ];
                    };
                    "@/nix" = {
                      mountpoint = "/nix";
                      mountOptions = [ "compress=zstd" "noatime" ];
                    };
                    "@/persist" = {
                      mountpoint = "/persist";
                      mountOptions = [ "compress=zstd" "noatime" ];
                    };
                    "@/log" = {
                      mountpoint = "/var/log";
                      mountOptions = [ "compress=zstd" "noatime" ];
                    };
                  };
                };
              };
            };
          };
        };
      };
    };
  };
}

I (just!) found out how to chroot into the nascent system.

% sudo nixos-enter --root /mnt
setting up /etc...

[root@ssd:/]# df
Filesystem          1K-blocks    Used Available Use% Mounted on
/dev/mapper/crypted  60110848 8566768  50117376  15% /
/dev/sdg1              523248       4    523244   1% /boot
/dev/mapper/crypted  60110848 8566768  50117376  15% /home
/dev/mapper/crypted  60110848 8566768  50117376  15% /nix
/dev/mapper/crypted  60110848 8566768  50117376  15% /persist
/dev/mapper/crypted  60110848 8566768  50117376  15% /var/log
devtmpfs              3276416       0   3276416   0% /dev
tmpfs                32764124     524  32763600   1% /dev/shm
tmpfs                16382064    9792  16372272   1% /etc/resolv.conf
tmpfs                16382064       0  16382064   0% /run
tmpfs                32764124     504  32763620   1% /run/wrappers

See if I can see wot’s wot:

[root@ssd:/]# ls /boot

[root@ssd:/]# bootctl --esp-path=/boot 'install'
No filesystem found on "/dev/sdg1": No such device

[root@ssd:/]# blkid /dev/sdg
/dev/sdg: PTUUID="5d7eb531-b790-49b7-bc49-26e120eab6b7" PTTYPE="gpt"

[root@ssd:/]# blkid /dev/sdg1
/dev/sdg1: PARTLABEL="disk-vdb-ESP" PARTUUID="1d855595-9c59-4861-a528-0c0b24f9ca3d"

[root@ssd:/]# df /boot
Filesystem     1K-blocks  Used Available Use% Mounted on
/dev/sdg1         523248     4    523244   1% /boot

The chrooted system can access sdg:

Device       Start       End   Sectors  Size Type
/dev/sdg1     2048   1050623   1048576  512M EFI System
/dev/sdg2  1050624 121305087 120254464 57.3G Linux filesystem

I can strace! The last few operations prior to failure were rummaging about in:

[root@ssd:/]# ls -l /sys/dev/block/8:96
lrwxrwxrwx 1 root root 0 Oct 27 17:00 /sys/dev/block/8:96 -> ../../devices/pci0000:00/0000:00:14.0/usb2/2-4/2-4.1/2-4.1.2/2-4.1.2:1.0/host12/target12:0:0/12:0:0:0/block/sdg

[root@ssd:/]# ls -l /sys/dev/block/8:96/dm/uuid
ls: cannot access '/sys/dev/block/8:96/dm/uuid': No such file or directory

[root@ssd:/]# ls -l /sys/dev/block/8:96/.
total 0
-r--r--r-- 1 root root 4096 Oct 27 17:08 alignment_offset
lrwxrwxrwx 1 root root    0 Oct 27 17:00 bdi -> ../../../../../../../../../../../../virtual/bdi/8:96
-r--r--r-- 1 root root 4096 Oct 27 17:14 capability
-r--r--r-- 1 root root 4096 Oct 27 17:00 dev
lrwxrwxrwx 1 root root    0 Oct 27 17:00 device -> ../../../12:0:0:0
-r--r--r-- 1 root root 4096 Oct 27 17:14 discard_alignment
-r--r--r-- 1 root root 4096 Oct 27 17:14 diskseq
-r--r--r-- 1 root root 4096 Oct 27 17:14 events
-r--r--r-- 1 root root 4096 Oct 27 17:14 events_async
-rw-r--r-- 1 root root 4096 Oct 27 17:14 events_poll_msecs
-r--r--r-- 1 root root 4096 Oct 27 17:14 ext_range
-r--r--r-- 1 root root 4096 Oct 27 17:00 hidden
drwxr-xr-x 2 root root    0 Oct 27 17:00 holders
-r--r--r-- 1 root root 4096 Oct 27 17:14 inflight
drwxr-xr-x 2 root root    0 Oct 27 17:00 integrity
drwxr-xr-x 3 root root    0 Oct 27 17:00 mq
drwxr-xr-x 2 root root    0 Oct 27 17:00 power
drwxr-xr-x 3 root root    0 Oct 27 17:00 queue
-r--r--r-- 1 root root 4096 Oct 27 17:14 range
-r--r--r-- 1 root root 4096 Oct 27 17:00 removable
-r--r--r-- 1 root root 4096 Oct 27 17:00 ro
drwxr-xr-x 5 root root    0 Oct 27 17:00 sdg1
drwxr-xr-x 5 root root    0 Oct 27 17:00 sdg2
-r--r--r-- 1 root root 4096 Oct 27 17:00 size
drwxr-xr-x 2 root root    0 Oct 27 17:00 slaves
-r--r--r-- 1 root root 4096 Oct 27 17:14 stat
lrwxrwxrwx 1 root root    0 Oct 27 17:00 subsystem -> ../../../../../../../../../../../../../class/block
drwxr-xr-x 2 root root    0 Oct 27 17:00 trace
-rw-r--r-- 1 root root 4096 Oct 27 17:08 uevent

It read size and start and decided to abandon:

openat(AT_FDCWD, "/sys/dev/block/8:97", O_RDONLY|O_CLOEXEC) = 5
openat(5, "size", O_RDONLY|O_CLOEXEC)   = 6
fcntl(6, F_GETFL)                       = 0x8000 (flags O_RDONLY|O_LARGEFILE)
newfstatat(6, "", {st_mode=S_IFREG|0444, st_size=4096, ...}, AT_EMPTY_PATH) = 0
read(6, "1048576\n", 4096)              = 8
close(6)                                = 0
openat(5, "start", O_RDONLY|O_CLOEXEC)  = 6
fcntl(6, F_GETFL)                       = 0x8000 (flags O_RDONLY|O_LARGEFILE)
newfstatat(6, "", {st_mode=S_IFREG|0444, st_size=4096, ...}, AT_EMPTY_PATH) = 0
read(6, "2048\n", 4096)                 = 5
close(6)                                = 0
close(5)                                = 0
writev(2, [{iov_base="No filesystem found on \"/dev/sdg"..., iov_len=50}, {iov_base="\n", iov_len=1}], 2No filesystem found on "/dev/sdg1": No such device

Rather poor choice of error to claim.

Perhaps I need to retry with a larger EFI partition?

Actually, all I did was exit the chroot, and:

% sudo umount /mnt/boot       
┌──(dap 💀 z8)-[/mnt/…/nixos/.nixos/nixos/ssd]
└─% sudo  mkfs.fat -F32 /dev/sdg1
mkfs.fat 4.2 (2021-01-31)
┌──(dap 💀 z8)-[/mnt/…/nixos/.nixos/nixos/ssd]
└─% sudo mount /dev/sdg1 /mnt/boot
┌──(dap 💀 z8)-[/mnt/…/nixos/.nixos/nixos/ssd]
└─% sudo nixos-enter --root /mnt
setting up /etc...

[root@ssd:/]# df
Filesystem          1K-blocks    Used Available Use% Mounted on
/dev/mapper/crypted  60110848 8566768  50117376  15% /
/dev/mapper/crypted  60110848 8566768  50117376  15% /home
/dev/mapper/crypted  60110848 8566768  50117376  15% /nix
/dev/mapper/crypted  60110848 8566768  50117376  15% /persist
/dev/mapper/crypted  60110848 8566768  50117376  15% /var/log
/dev/sdg1              523248       4    523244   1% /boot
devtmpfs              3276416       0   3276416   0% /dev
tmpfs                32764124     524  32763600   1% /dev/shm
tmpfs                16382064    9792  16372272   1% /etc/resolv.conf
tmpfs                16382064       0  16382064   0% /run
tmpfs                32764124     504  32763620   1% /run/wrappers

[root@ssd:/]# ls -l /boot
total 0

[root@ssd:/]# bootctl install
Created "/boot/EFI".
Created "/boot/EFI/systemd".
Created "/boot/EFI/BOOT".
Created "/boot/loader".
Created "/boot/loader/entries".
Created "/boot/EFI/Linux".
Copied "/nix/store/9gzw98jc64qkwd17a6qqm63w25zysi57-systemd-253.6/lib/systemd/boot/efi/systemd-bootx64.efi" to "/boot/EFI/systemd/systemd-bootx64.efi".
Copied "/nix/store/9gzw98jc64qkwd17a6qqm63w25zysi57-systemd-253.6/lib/systemd/boot/efi/systemd-bootx64.efi" to "/boot/EFI/BOOT/BOOTX64.EFI".
Random seed file /boot/loader/random-seed successfully written (32 bytes).
Created EFI boot entry "Linux Boot Manager".

Thanks for all your help!