{ lib, pkgs, ... }:
let
  hashDisk = disk: "os-${builtins.substring 0 5 (builtins.hashString "sha256" disk)}";
  # os = "/dev/disk/by-id/mmc-SD64G_0x8336354b";
  vdev = [
    "/dev/disk/by-id/ata-ST20000NM002H-3KV133_ZYDBVV7Z"
    "/dev/disk/by-id/ata-ST20000NM002H-3KV133_ZYDBSJRE"
  ];
  configTxt = pkgs.writeText "config.txt" ''
    [pi4]
    kernel=u-boot-rpi4.bin
    enable_gic=1

    # Otherwise the resolution will be weird in most cases, compared to
    # what the pi3 firmware does by default.
    disable_overscan=1

    # Supported in newer board revisions
    arm_boost=1

    [all]
    # Boot in 64-bit mode.
    arm_64bit=1

    # U-Boot needs this to work, regardless of whether UART is actually used or not.
    # Look in arch/arm/mach-bcm283x/Kconfig in the U-Boot tree to see if this is still
    # a requirement in the future.
    enable_uart=1

    # Prevent the firmware from smashing the framebuffer setup done by the mainline kernel
    # when attempting to show low-voltage or overtemperature warnings.
    avoid_warnings=1
  '';
in
{

  boot.loader = {
    systemd-boot = {
      enable = true;
    };
    efi = {
      canTouchEfiVariables = true;
    };
  };
  # boot.tmp.useTmpfs = true;

  disko.devices = {
    disk = {
      # "os-${hashDisk os}" = {
      #   type = "disk";
      #   device = os;
      #   content = {
      #     type = "gpt";
      #     partitions = {
      #       firmware = {
      #         size = "60M";
      #         priority = 1;
      #         type = "0700";
      #         content = {
      #           type = "filesystem";
      #           format = "vfat";
      #           mountpoint = "/firmware";
      #           postMountHook = toString (
      #             pkgs.writeScript "postMountHook.sh" ''
      #               (cd ${pkgs.raspberrypifw}/share/raspberrypi/boot && cp bootcode.bin fixup*.dat start*.elf *.dtb /mnt/firmware/)
      #               cp ${pkgs.ubootRaspberryPi4_64bit}/u-boot.bin /mnt/firmware/u-boot-rpi4.bin
      #               cp ${configTxt} /mnt/firmware/config.txt
      #             ''
      #           );
      #         };
      #       };
      #       ESP = {
      #         size = "2G";
      #         type = "EF00";
      #         content = {
      #           type = "filesystem";
      #           format = "vfat";
      #           mountpoint = "/boot";
      #           mountOptions = [ "umask=0077" ];
      #         };
      #       };
      #       root = {
      #         name = "root";
      #         end = "-0";
      #         content = {
      #           type = "filesystem";
      #           format = "f2fs";
      #           mountpoint = "/";
      #           extraArgs = [
      #             "-O"
      #             "extra_attr,inode_checksum,sb_checksum,compression"
      #           ];
      #           mountOptions = [ "compress_algorithm=zstd:6,compress_chksum,atgc,gc_merge,lazytime,nodiscard" ];
      #         };
      #       };
      #     };
      #   };
      # };
    }
    // (lib.listToAttrs (
      map (disk: {
        name = "data-${hashDisk disk}";
        value = {
          type = "disk";
          device = disk;
          content = {
            type = "zfs";
            pool = "zdata";
          };
        };
      }) vdev
    ));
    zpool = {
      zdata = {
        type = "zpool";
        options.ashift = "12";
        rootFsOptions = {
          mountpoint = "none";
          compression = "lz4";
          acltype = "posixacl";
          xattr = "sa";
          "com.sun:auto-snapshot" = "true";
        };
        mode = {
          topology = {
            type = "topology";
            vdev = [
              {
                mode = "mirror";
                members = vdev;
              }
            ];
          };
        };
        datasets = {
          "nas" = {
            type = "zfs_fs";
            mountpoint = "/mnt/hdd";
            mountOptions = [ "nofail" ];
          };
          "service-data" = {
            type = "zfs_fs";
            mountpoint = "/var/lib";
            mountOptions = [ "nofail" ];
          };
        };
      };
    };
  };
}
