# nix run github:nix-community/disko -- --mode zap_create_mount ./disko-config.nix
# nixos-generate-config --no-filesystems --root /mnt
# vim /mnt/configuration.nix
# nixos-install
{ config, lib, ... }:
let
  raid_disks = {
    "TOSHIBA_1360A003FVGG" = "/dev/disk/by-id/ata-TOSHIBA_MG08ACA16TE_1360A003FVGG";
    "TOSHIBA_1360A00BFVGG" = "/dev/disk/by-id/ata-TOSHIBA_MG08ACA16TE_1360A00BFVGG";
    "TOSHIBA_1360A00VFVGG" = "/dev/disk/by-id/ata-TOSHIBA_MG08ACA16TE_1360A00VFVGG";
    "TOSHIBA_41R0A0EBF57H" = "/dev/disk/by-id/ata-TOSHIBA_MG08ACA16TE_41R0A0EBF57H";
    "TOSHIBA_5120A03WF57H" = "/dev/disk/by-id/ata-TOSHIBA_MG08ACA16TE_5120A03WF57H";
  };
in
{
  # ZFS already has its own scheduler. Without this my(@Artturin) computer froze for a second when i nix build something.
  # copied from : https://github.com/numtide/srvos/blob/main/nixos/common/zfs.nix
  services.udev.extraRules = lib.optionalString (config.boot.zfs.enabled) ''
    ACTION=="add|change", KERNEL=="sd[a-z]*[0-9]*|mmcblk[0-9]*p[0-9]*|nvme[0-9]*n[0-9]*p[0-9]*", ENV{ID_FS_TYPE}=="zfs_member", ATTR{../queue/scheduler}="none"
  '';

  disko.devices = {
    disk =
      {
        root = {
          type = "disk";
          device = "/dev/nvme0n1";
          content = {
            type = "gpt";
            partitions = {
              boot = {
                priority = 1;
                size = "500M";
                content = {
                  type = "filesystem";
                  format = "vfat";
                  mountpoint = "/boot";
                  mountOptions = [
                    "defaults"
                  ];
                };
              };
              root = {
                priority = 100;
                size = "100%";
                content = {
                  type = "luks";
                  name = "root";
                  content = {
                    type = "zfs";
                    pool = "zroot";
                  };
                };
              };
            };
          };
        };
      }
      // builtins.mapAttrs (name: device_path: {
        type = "disk";
        device = device_path;
        content = {
          type = "gpt";
          partitions = {
            zfs = {
              size = "100%";
              content = {
                type = "luks";
                name = "raid_${name}";
                content = {
                  type = "zfs";
                  pool = "zraid";
                };
              };
            };
          };
        };
      }) raid_disks;

    zpool = {

      zroot = {
        type = "zpool";
        rootFsOptions = {
          mountpoint = "none";
          canmount = "off";
          compression = "lz4";
        };
        datasets = {
          "root" = {
            type = "zfs_fs";
            mountpoint = "/";
            options = {
              mountpoint = "legacy";
              compression = "lz4";
            };
          };
          "services2" = {
            type = "zfs_fs";
            mountpoint = "/srv2";
            options = {
              mountpoint = "legacy";
              compression = "lz4";
              "com.sun:auto-snapshot:daily" = toString true;
            };
          };
          "paperless" = {
            type = "zfs_fs";
            mountpoint = "/var/lib/paperless";
            options = {
              mountpoint = "legacy";
              compression = "lz4";
              "com.sun:auto-snapshot:daily" = toString true;
            };
          };
          "postgresql" = {
            type = "zfs_fs";
            mountpoint = "/var/lib/postgresql";
            options = {
              mountpoint = "legacy";
              compression = "lz4";
              "com.sun:auto-snapshot:daily" = toString true;
            };
          };
        };
      };

      # `zpool import -f zraid` once on the first boot and reboot
      # todo: rename to tank next time (this is the standard)
      zraid = {
        type = "zpool";
        mode = "raidz2";
        rootFsOptions = {
          mountpoint = "none";
          canmount = "off";
        };
        datasets = {
          "media" = {
            type = "zfs_fs";
            mountpoint = "/media";
            options = {
              mountpoint = "legacy";
              compression = "lz4";
              "com.sun:auto-snapshot:daily" = toString true;
              "com.sun:auto-snapshot:weekly" = toString true;
              "com.sun:auto-snapshot:monthly" = toString true;
            };
          };
          "legacy" = {
            type = "zfs_fs";
            mountpoint = "/legacy";
            options = {
              mountpoint = "legacy";
              compression = "lz4";
              "com.sun:auto-snapshot:monthly" = toString true;
            };
          };
          "borg" = {
            type = "zfs_fs";
            mountpoint = "/borg";
            options = {
              mountpoint = "legacy";
              compression = "lz4";
              "com.sun:auto-snapshot:daily" = toString true;
              "com.sun:auto-snapshot:weekly" = toString true;
              "com.sun:auto-snapshot:monthly" = toString true;
            };
          };
          "syncthing" = {
            type = "zfs_fs";
            mountpoint = "/syncthing";
            options = {
              mountpoint = "legacy";
              compression = "lz4";
              "com.sun:auto-snapshot:daily" = toString true;
              "com.sun:auto-snapshot:weekly" = toString true;
              "com.sun:auto-snapshot:monthly" = toString true;
            };
          };
          "mirror" = {
            type = "zfs_fs";
            mountpoint = "/mirror";
            options = {
              mountpoint = "legacy";
              compression = "lz4";
              "com.sun:auto-snapshot:daily" = toString true;
              "com.sun:auto-snapshot:weekly" = toString true;
              "com.sun:auto-snapshot:montly" = toString true;
            };
          };
          "services" = {
            type = "zfs_fs";
            mountpoint = "/srv";
            options = {
              mountpoint = "legacy";
              compression = "lz4";
              "com.sun:auto-snapshot:daily" = toString true;
              "com.sun:auto-snapshot:weekly" = toString true;
              "com.sun:auto-snapshot:montly" = toString true;
            };
          };
          "container" = {
            type = "zfs_fs";
            mountpoint = "/var/lib/containers/storage"; # needed for podman
            options = {
              mountpoint = "legacy";
              compression = "lz4";
              acltype = "posixacl"; # needed for podman
            };
          };
          "robi" = {
            type = "zfs_fs";
            mountpoint = "/robi";
            options = {
              mountpoint = "legacy";
              compression = "lz4";
              "com.sun:auto-snapshot:daily" = toString true;
              "com.sun:auto-snapshot:weekly" = toString true;
            };
          };
          "robi/media" = {
            type = "zfs_fs";
            mountpoint = "/robi/media";
            options = {
              mountpoint = "legacy";
              compression = "lz4";
            };
          };
          "robi/torrent" = {
            type = "zfs_fs";
            mountpoint = "/robi/torrent";
            options = {
              mountpoint = "legacy";
              compression = "lz4";
            };
          };
        };
      };

    };
  };
}