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

let
  make_machine_config = name: {
    enable = lib.mkOption {
      type = lib.types.bool;
      default = false;
      example = true;
      description = "Whether we want to use the ${name} machine during distributed builds.";
    };

    additional_config = lib.mkOption {
      type = lib.types.attrs;
      default = { };
      example = lib.literalExpression {
        speedFactor = 2;
      };
      description = "Additional config values for the buildMachines entry. For example, speedFactor.";
    };
  };
in
{
  imports = [ ];

  options.me = {
    distributed_build.enable = lib.mkOption {
      type = lib.types.bool;
      default = false;
      example = true;
      description = "Whether we want to use multiple machines to perform a nixos-rebuild.";
    };

    distributed_build.machines.hydra = make_machine_config "hydra";
    distributed_build.machines.quark = make_machine_config "quark";
  };

  config = lib.mkIf config.me.distributed_build.enable (
    lib.mkMerge [
      {
        nix.distributedBuilds = true;
      }
      (lib.mkIf config.me.distributed_build.machines.hydra.enable {
        nix.buildMachines = [
          (
            {
              hostName = "hydra";
              sshUser = "nixworker";
              # sshKey = "";
              # publicHostKey = "";
              systems = [
                "x86_64-linux"
                # "aarch64-linux"
              ];
              maxJobs = 1;
              supportedFeatures = [
                # "nixos-test"
                "benchmark"
                "big-parallel"
                # "kvm"
                "gccarch-x86-64-v3"
                "gccarch-x86-64-v4"
                "gccarch-znver4"
              ];
            }
            // config.me.distributed_build.machines.hydra.additional_config
          )
        ];
      })
      (lib.mkIf config.me.distributed_build.machines.quark.enable {
        nix.buildMachines = [
          (
            {
              hostName = "quark";
              sshUser = "nixworker";
              sshKey = "/persist/manual/ssh/root/keys/id_ed25519";
              # From: base64 -w0 /persist/ssh/ssh_host_ed25519_key.pub
              publicHostKey = "c3NoLWVkMjU1MTkgQUFBQUMzTnphQzFsWkRJMU5URTVBQUFBSUx0alplYlVYTkRkU3Y1enVGbjM3eFNMZUN3S2hPKzFMdWovM2FYNFJRTEEgcm9vdEBxdWFyawo=";
              systems = [
                "x86_64-linux"
                # "aarch64-linux"
              ];
              maxJobs = 1;
              supportedFeatures = [
                # "nixos-test"
                "benchmark"
                "big-parallel"
                # "kvm"
                "gccarch-x86-64-v3"
                "gccarch-x86-64-v4"
                "gccarch-znver4"
                "gccarch-znver5"
              ];
            }
            // config.me.distributed_build.machines.quark.additional_config
          )
        ];
      })
    ]
  );
}