{ config, lib, pkgs, ... }:

let
  inherit (lib) mkEnableOption mkIf mkMerge;

  cfg = config.dgn-hardware;

in {
  options.dgn-hardware = {
    enable = mkEnableOption "default hardware configuration." // {
      default = true;
    };

    useSystemd = mkEnableOption "sytemd boot and configuration." // {
      default = true;
    };

    useZfs = mkEnableOption "zfs configuration.";
  };

  config = mkIf cfg.enable (mkMerge [
    {
      hardware.enableRedistributableFirmware = true;
      hardware.cpu.intel.updateMicrocode = true;

      nixpkgs.hostPlatform = lib.mkDefault "x86_64-linux";

      boot = {
        initrd.availableKernelModules =
          [ "ata_piix" "uhci_hcd" "ehci_pci" "virtio_pci" "ahci" "virtio_blk" ];
        kernelModules = [ "kvm-intel" ];
        kernelParams = [
          "cgroup_enable=cpu"
          "cgroup_enable=cpuset"
          "cgroup_enable=memory"
          "cgroup_memory=1"
        ];
      };
    }

    (mkIf cfg.useSystemd {
      boot.loader = {
        systemd-boot.enable = true;
        efi.canTouchEfiVariables = true;
      };
    })

    (mkIf cfg.useZfs {
      boot = {
        initrd.availableKernelModules = [
          "ahci"
          "ehci_pci"
          "megaraid_sas"
          "usb_storage"
          "usbhid"
          "sd_mod"
          "sr_mod"
        ];
        kernelModules = [ "kvm-intel" ];
        supportedFilesystems = [ "zfs" ];

        zfs = {
          forceImportRoot = false;
          extraPools = [ "fast01" "work01" ];
          package = pkgs.zfs_2_1;
        };
      };
    })
  ]);
}