Help UEFI booting RPi4 with custom image

I have an RPi4 which has an SD card configured with EDK2 (via this repo) in order to provide support for UEFI booting an external USB drive. I’ve done this successfully using Flatcar Linux but wanted to switch to NixOS. This is the current state of my flake:

{
  description = "Build image";
  inputs.nixpkgs.url = "github:nixos/nixpkgs";
  outputs = { self, nixpkgs }: rec {
    nixosConfigurations.rpi = nixpkgs.lib.nixosSystem rec {
      system = "aarch64-linux";
      modules = [
        "${nixpkgs}/nixos/modules/installer/sd-card/sd-image-aarch64.nix"
        ({ pkgs, ... }: {
          services.pcscd.enable = true;
          services.udev.packages = [ pkgs.yubikey-personalization ];

          environment.systemPackages = [
            pkgs.gnupg
            pkgs.pinentry-curses
            pkgs.pinentry-qt
            pkgs.paperkey
            pkgs.wget
          ];

          programs = {
            ssh.startAgent = false;
            gnupg.agent = {
              enable = true;
              enableSSHSupport = true;
            };
          };
        })
      ];
    };
    images.rpi = nixosConfigurations.rpi.config.system.build.sdImage;
  };
}

It’s mostly just services and programs I need to be available at runtime as this RPi4 is never connected to a network. Where I’m currently stuck is how to generate a raw image file that can support the UEFI booting method mentioned above. Most of the generic sd-card/** images available in nixpkgs seem to be geared towards packing everything into the image, including uboot as a bootloader. The problem is I’m still very new to Nix and am struggling to figure out how to put together my own configuration to meet my requirements.

Any tips or guidance would be greatly appreciated.

Thanks!

FWIW, the pi can boot from USB on its own now; no need for EDK2.

But when you use EFI, you can use the isoImage instead from nixpkgs/nixos/modules/installer/cd-dvd/installation-cd-minimal.nix to create a standard EFI bootable live CD image. Then you can use that to actually install NixOS like you would on a normal system. That’s how I installed NixOS on my RPi CM4 with EDK2 (though FYI on that board I needed a kernel patch for the PCIe to work; no idea if that’s the case for the regular RPi4’s PCIe-based USB 3).

Beware: For me, after EDK2 loads grub, grub takes ages to boot the kernel, and I couldn’t get systemd-boot to work at all.

Thanks, I didn’t realize there was support for direct USB booting, that seems like a much easier approach.

For the ISO, I was hoping to create a system that just boots into a shell with the applications I need already available. I’m hoping to avoid having to run any sort of installer. Is this possible with NixOS?

The Pi’s firmware has to be up to date for USB booting to work. There’s a number of guides online about it.

Well if you don’t use EDK2, then the SD image should work fine IIRC (on a regular RPi4; doesn’t work for the CM4 because quirks galore).

If you still want to use EDK2 (you probably don’t), the iso image can be tailored to contain whatever configurations you want, like system packages and whatnot. It’s just a nixos module. But it’s a live CD so it’s not persistent like the SD image. I’m sure a persistent EFI image could be crafted but I don’t know if anyone’s done it.

Thanks for the detailed response. I ended up getting it to work with direct booting. One other issue I’ve run into is I need to modify config.txt to disable wifi/bluetooth, but my initial searching around hasn’t led to many promising results and what appears to be a lot of outdated information.

Do you happen to know how to control what gets set there? It looks like there’s a static blob that just gets put in there in the image I’m using and there doesn’t seem to be an easy way to modify it.