Btrfs raid with Disko

I wrote a helper function mkBtrfsRAID to make defining btrfs raid collections more ergonomic in disko. It has the “other” disks formatted with an empty partition table, so disko knows about them and asks for wipe confirmation on formatting. Doesn’t solve the abovementioned problem when the first disk that’s actually tried to be mounted fails or is missing though.

let
  # make a disko.devices.disk attrset that empty-format the given disks before anything else
  mkEmptyDisks = { before ? "", devices }:
    builtins.listToAttrs (builtins.genList (i:
      let dev = builtins.elemAt devices i;
      in {
        # disks are formatted alphabetically, these dummys must be formatted first, so we prefix with zeros
        name = "#before:${before}:${builtins.toString i}";
        value = {
          type = "disk";
          device = dev;
          content = {
            type = "gpt";
            partitions = { };
          };
        };
      }) (builtins.length devices));
  # make a disko.devices.disk attrset forming a btrfs raid from a list of devices
  # The *first* device in this list will be mounted (which will auto-mount the others)
  mkBtrfsRAID = { name, devices, raid ? "raid1", content ? { } }:
    {
      "${name}" = {
        type = "disk";
        # The first device in the list is used, but it could be any of those
        device = builtins.head devices;
        content = content // {
          type = "btrfs";
          extraArgs = (content.extraArgs or [ ])
            ++ [ "-f" "-d ${raid}" "-m ${raid}" ] ++
            # disko has no builtin concept of collections of btrfs disks,
            # so we sneak all the other parts of the RAID into the creation of the "first" one
            # Below, all of these other disks are first formatted empty
            # to ensure disko knows about them and asks the user for confirmation upon formatting
            builtins.tail devices;
        };
      };
    }
    # This will first empty-format all the other parts of the RAID
    # This ensures disko will ask for confirmation to empty those out
    // mkEmptyDisks {
      before = name;
      devices = builtins.tail devices;
    };
in {
  disko.devices = {
    disk = mkBtrfsRAID {
      name = "HDD-raid";
      raid = "raid1";
      devices = [
        # ⚠️  the *first* disk here will be mounted (which will auto-mount the others)
        "/dev/disk/by-id/ata-..._1234"
        "/dev/disk/by-id/ata-..._2345"
        "/dev/disk/by-id/ata-..._3456"
        "/dev/disk/by-id/ata-..._4567"
      ];
      content = {
        # mount the entire root subvolume for easy snapshotting "across" subvolumes
        mountpoint = "/mnt/.DATA";
        mountOptions = [ "compress=zstd" "noatime" ];
        subvolumes = {
          "services" = {
            mountpoint = "/var/lib";
            mountOptions = [ "compress=zstd" "noatime" ];
          };
          "data" = {
            mountpoint = "/data";
            mountOptions = [ "compress=zstd" "noatime" ];
          };
        };
      };
    };
  };
}