{ pkgs, nixvirt }: rec {
  inherit (pkgs) lib;
  inherit (nixvirt) xml;

  genericVM = { name, uuid, arch, volume, cdroms, ... }: {
    inherit name uuid;
    type = "kvm";
    memory = { count = 4194304; unit = "KiB"; };
    vcpu = { placement = "static"; count = 4; };
    os = {
      inherit arch;
      type = "hvm";
      machine = "pc-q35-8.1";
      boot = [{ dev = "cdrom"; }]; #{ dev = "hd"; }];
      bootmenu.enable = false;
      smbios.mode = "sysinfo";
    };
    sysinfo = {
      type = "smbios";
      bios.entry = [
        { name = "vendor"; value = "Dell"; }
      ];
      system.entry = [
        { name = "manufacturer"; value = "Dell"; }
        { name = "product"; value = "Dell"; }
        { name = "version"; value = "9.5.4"; }
      ];
    };
    features = {
      acpi = {};
      apic = {};
      hyperv = {
        mode = "custom";
        relaxed.state = true;
        vapic.state = true;
        spinlocks.state = true;
        spinlocks.retries = 8191;
      };
      kvm.hidden.state = true;
      vmport.state = false;
    };
    cpu = {
      mode = "host-passthrough";
      check = "none";
      migratable = true;
      topology = {
        sockets = 1;
        dies = 1;
        cores = 4;
        threads = 1;
      };
      feature = [
        { policy = "disable"; name = "hypervisor"; }
      ];
    };
    clock = {
      offset = "localtime";
      timer = [
        { name = "rtc"; tickpolicy = "catchup"; }
        { name = "pit"; tickpolicy = "delay"; }
        { name = "hpet"; present = false; }
        { name = "hypervclock"; present = true; }
      ];
    };
    on_poweroff = "destroy";
    on_reboot = "restart";
    on_crash = "restart";
    pm.suspend-to-mem.enabled = false;
    pm.suspend-to-disk.enabled = false;
    devices = let
      pci_address = bus: slot: function: {
        type = "pci";
        domain = 0;
        bus = bus;
        slot = slot;
        inherit function;
      };
      usb_address = port: {
        type = "usb";
        bus = 0;
        inherit port;
      };
      drive_address = unit: {
        type = "drive";
        controller = 0;
        bus = 0;
        target = 0;
        inherit unit;
      };
      mksourcetype = with builtins;
        src:
        if isAttrs src && src ? "volume" then "volume"
        else "file";
      mksource = with builtins;
        src:
        if isString src || isPath src then { file = src; }
        else src;
    in {
      emulator = "/run/current-system/sw/bin/qemu-system-x86_64";
      disk = [{
        type = mksourcetype volume;
        device = "disk";
        driver = { name = "qemu"; type = "qcow2"; discard = "unmap"; }; #cache = "writeback"; };
        source = mksource volume;
        target = { bus = volume.bus; dev = "sda"; };
        address = drive_address volume.index;
        serial = "SEAG445692";
        vendor = if volume.bus == "scsi" then "Seagate" else null;
        product = if volume.bus == "scsi" then "HDD" else null;
      }] ++ (map (x: {
        type = "file";
        device = "cdrom";
        driver = { name = "qemu"; type = "raw"; };
        target = { bus = x.bus; dev = x.dev; };
        readonly = true;
        address = drive_address x.index;
        source = {
          inherit (x) file;
          # startupPolicy = "mandatory";
        };
        # inherit (x) serial vendor product;
        serial = "WD-WMAP9A966149";
        vendor = if x.bus == "scsi" then "WD" else null;
        product = if x.bus == "scsi" then "DVDROM" else null;
      }) (cdroms));
      controller = [
        {
          type = "usb";
          index = 0;
          # model = "qemu-xhci";
          model = "ich9-ehci1";
          # ports = 15;
          address = pci_address 0 29 7;
        }
        {
          type = "scsi";
          index = 0;
          model= "lsisas1068";
          address = pci_address 16 1 0;
        }
        # {
        #   type = "sata";
        #   index = 0;
        #   address = pci_address 0 31 2;
        # }
        {
          type = "pci";
          index = 0;
          model = "pcie-root";
        }
        {
          type = "virtio-serial";
          index = 0;
          address = pci_address 1 0 0;
        }
        # {
        #   type = "ccid";
        #   index = 0;
        #   address = usb_address 1;
        # }
        {
          type = "pci";
          index = 1;
          model = "pcie-root-port";
          address = pci_address 0 2 0 // { multifunction = true; };
        }
        {
          type = "pci";
          index = 16;
          model = "pcie-to-pci-bridge";
          address = pci_address 3 0 0;
        }
        {
          type = "pci";
          index = 2;
          model = "pcie-root-port";
          address = pci_address 0 2 1;
        }
        {
          type = "pci";
          index = 3;
          model = "pcie-root-port";
          address = pci_address 0 2 2;
        }
        {
          type = "pci";
          index = 4;
          model = "pcie-root-port";
          address = pci_address 0 2 3;
        }
        {
          type = "pci";
          index = 5;
          model = "pcie-root-port";
          address = pci_address 0 2 4;
        }
        {
          type = "pci";
          index = 6;
          model = "pcie-root-port";
          address = pci_address 0 2 5;
        }
        {
          type = "pci";
          index = 7;
          model = "pcie-root-port";
          address = pci_address 0 2 6;
        }
        {
          type = "pci";
          index = 8;
          model = "pcie-root-port";
          address = pci_address 0 2 7;
        }
        {
          type = "pci";
          index = 9;
          model = "pcie-root-port";
          address = pci_address 0 3 0 // { multifunction = true; };
        }
        {
          type = "pci";
          index = 10;
          model = "pcie-root-port";
          address = pci_address 0 3 1;
        }
        {
          type = "pci";
          index = 11;
          model = "pcie-root-port";
          address = pci_address 0 3 2;
        }
        {
          type = "pci";
          index = 12;
          model = "pcie-root-port";
          address = pci_address 0 3 3;
        }
        {
          type = "pci";
          index = 13;
          model = "pcie-root-port";
          address = pci_address 0 3 4;
        }
        {
          type = "pci";
          index = 14;
          model = "pcie-root-port";
          address = pci_address 0 3 5;
        }
        {
          type = "pci";
          index = 15;
          model = "pcie-root-port";
          address = pci_address 0 3 6;
        }
      ];
      interface = {
        type = "network";
        mac = { address = "52:54:00:10:c4:28"; };
        source = { network = "default"; };
        model = { type = "e1000e"; };
        address = pci_address 4 0 0;
      };
      # smartcard = {
      #   mode = "passthrough";
      #   type = "spicevmc";
      #   address = {
      #     type = "ccid";
      #     controller = 0;
      #     slot = 0;
      #   };
      # };
      serial = {
        type = "pty";
        target = {
          type = "isa-serial";
          port = 0;
          model = { name = "isa-serial"; };
        };
      };
      console = {
        type = "pty";
        target = { type = "serial"; port = 0; };
      };
      channel = [
        {
          type = "spicevmc";
          target = {
            type = "virtio";
            name = "com.redhat.spice.0";
          };
          address = {
            type = "virtio-serial";
            controller = 0;
            bus = 0;
            port = 1;
          };
        }
      ];
      input = [
        {
          type = "tablet";
          bus = "usb";
          address = usb_address 2;
        }
        {
          type = "mouse";
          bus = "ps2";
        }
        {
          type = "keyboard";
          bus = "ps2";
        }
      ];
      # tpm = {
      #   model = "tpm-crb";
      #   backend = {
      #     type = "emulator";
      #     version = "2.0";
      #   };
      # };
      graphics = {
        type = "spice";
        autoport = true;
        listen = { type = "address"; address = "127.0.0.1"; };
        image = { compression = false; };
        gl = { enable = false; };
      };
      sound = {
        model = "ich9";
        address = pci_address 0 27 0;
      };
      audio = {
        id = 1;
        type = "spice";
      };
      video = {
        model = {
          type = "qxl";
          ram = 65536;
          vram = 65536;
          vgamem = 16384;
          heads = 1;
          primary = true;
          acceleration = { accel3d = false; };
        };
        address = pci_address 0 1 0;
      };
      # redirdev = [
      #   {
      #     bus = "usb";
      #     type = "spicevmc";
      #     address = usb_address 3;
      #   }
      #   {
      #     bus = "usb";
      #     type = "spicevmc";
      #     address = usb_address 4;
      #   }
      #   {
      #     bus = "usb";
      #     type = "spicevmc";
      #     address = usb_address 5;
      #   }
      #   {
      #     bus = "usb";
      #     type = "spicevmc";
      #     address = usb_address 6;
      #   }
      # ];
      watchdog = {
        model = "itco";
        action = "reset";
      };
    };
  };
}