{ flakeInputs, lib, pkgs, ... }:

with lib;
with flakeInputs.nixVirt.lib;

{
  config = {
    virtualisation.libvirt = {
      enable = true;
      connections."qemu:///session" = {
        domains = [
          { definition = domain.writeXML (updateManyAttrsByPath [
            ] (recursiveUpdate (domain.templates.windows {
              name = "lmmirzm-vmrz01";
              uuid = "9e1dab2e-7986-4cb3-88af-6fad8969e15f";
              memory = { count = 16; unit = "GiB"; };
              storage_vol = "/home/gkleen/.local/share/libvirt/images/lmmirzm-vmrz01.qcow2";
              nvram_path = "/home/gkleen/.local/share/libvirt/lmmirzm-vmrz01.nvram";
              virtio_drive = true;
              virtio_video = true;
              install_virtio = false;
            }) {
              qemu-commandline.env = [
                { name = "SPICE_DEBUG_ALLOW_MC"; value = "1"; }
              ];
              vcpu.count = 4;
              cpu = {
                mode = "host-model";
                feature = [
                  { name = "vmx"; policy = "require"; }
                ];
              };
              os.bootmenu.enable = true;
              devices.graphics = {
                listen.type = "address";
                gl.enable = false;
              };
              devices.video.model.acceleration.accel3d = false;
              devices.interface = {
                model.type = "virtio";
                # model.type = "e1000e";
                type = "bridge";
                mac.address = "52:54:00:b9:f3:ed";
                source.bridge = "rz-0971";
              };
              devices.channel = [
                {
                  type = "unix";
                  target = { type = "virtio"; name = "org.qemu.guest_agent.0"; };
                }
                {
                  type = "spicevmc";
                  target = { type = "virtio"; name = "com.redhat.spice.0"; };
                }
                {
                  type = "spiceport";
                  target = { type = "virtio"; name = "org.spice-space.webdav.0"; };
                  source.channel = "org.spice-space.webdav.0";
                }
              ];
              devices.tpm.model = "tpm-tis";
            }));
          }
          { definition = domain.writeXML (recursiveUpdate (domain.templates.linux {
              name = "vmrz02";
              uuid = "daefc4b0-c48d-4b9d-a85d-7bd56eb068d0";
              memory = { count = 8; unit = "GiB"; };
              storage_vol = "/home/gkleen/.local/share/libvirt/images/vmrz02.qcow2";
              virtio_video = true;
            }) {
              os = {
                loader =
                  {
                    readonly = true;
                    type = "pflash";
                    path = "${pkgs.OVMFFull.fd}/FV/OVMF_CODE.ms.fd";
                  };
                nvram =
                  {
                    template = "${pkgs.OVMFFull.fd}/FV/OVMF_VARS.ms.fd";
                    path = "/home/gkleen/.local/share/libvirt/vmrz02.nvram";
                  };
                bootmenu.enable = true;
              };
              qemu-commandline.env = [
                { name = "SPICE_DEBUG_ALLOW_MC"; value = "1"; }
              ];
              vcpu.count = 4;
              cpu = {
                mode = "host-model";
                feature = [
                  { name = "vmx"; policy = "require"; }
                ];
              };
              devices.graphics = {
                listen.type = "address";
                gl.enable = false;
              };
              devices.video.model.acceleration.accel3d = false;
              devices.interface = [
                {
                  model.type = "virtio";
                  type = "bridge";
                  mac.address = "52:54:00:ec:d8:9d";
                  source.bridge = "rz-0971";
                }
                {
                  model.type = "virtio";
                  type = "bridge";
                  mac.address = "52:54:00:5e:f5:38";
                  source.bridge = "rz-2403";
                }
              ];
              devices.channel = [
                {
                  type = "unix";
                  target = { type = "virtio"; name = "org.qemu.guest_agent.0"; };
                }
                {
                  type = "spicevmc";
                  target = { type = "virtio"; name = "com.redhat.spice.0"; };
                }
                {
                  type = "spiceport";
                  target = { type = "virtio"; name = "org.spice-space.webdav.0"; };
                  source.channel = "org.spice-space.webdav.0";
                }
              ];
              devices.tpm = {
                model = "tpm-tis";
                backend =
                  {
                    type = "emulator";
                    version = "2.0";
                  };
              };
            });
          }
        ];
        pools = [
          { definition = pool.writeXML {
              type = "dir";
              name = "default";
              uuid = "ad899c92-02e3-45f9-bf49-195467aba2e2";
              target = {
                path = "/home/gkleen/.local/share/libvirt/images";
              };
            };
            active = true;
            volumes = [
              { definition = volume.writeXML {
                  type = "file";
                  name = "lmmirzm-vmrz01.qcow2";
                  capacity = { count = 40; unit = "GB"; };
                  target = {
                    path = "/home/gkleen/.local/share/libvirt/images/lmmirzm-vmrz01.qcow2";
                    format.type = "qcow2";
                    features.lazy_refcounts = {};
                  };
                };
              }
              { definition = volume.writeXML {
                  type = "file";
                  name = "vmrz02.qcow2";
                  capacity = { count = 256; unit = "GB"; };
                  target = {
                    path = "/home/gkleen/.local/share/libvirt/images/vmrz02.qcow2";
                    format.type = "qcow2";
                    features.lazy_refcounts = {};
                  };
                };
              }
            ];
          }
        ];
      };
    };
  };
}