{ config, nixpkgs-xr, pkgs, lib, ... }:
let
  passthrough = pkgs.writeShellApplication {
    name = "passthrough.sh";
    runtimeInputs = lib.attrVals [ "coreutils" "kmod" "libvirt" "procps" ] pkgs;
    text = ''
      GUEST_NAME="$1"
      HOOK_NAME="$2"
      STATE_NAME="$3"

      set -x

      # For the windows 10 VM
      if [[ "$GUEST_NAME" == "win10" ]]; then

        # Prepare hook
        if [[ "$HOOK_NAME/$STATE_NAME" == "prepare/begin" ]]; then

          # Remove Hyprland
          #pkill Hyprland

          # Unbind VTconsoles
          echo 0 > /sys/class/vtconsole/vtcon0/bind
          echo 0 > /sys/class/vtconsole/vtcon1/bind

          # Unbind EFI-Framebuffer
          #echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind

          # Unbind the GPU from display driver
          virsh nodedev-detach pci_0000_03_00_0
          virsh nodedev-detach pci_0000_03_00_1
          virsh nodedev-detach pci_0000_03_00_2

          # Unload Modules
          modprobe -r amdgpu

          # We love race conditions
          sleep 2

          # Load VFIO Kernel Module
          modprobe vfio vfio_pci vfio_iommu_type1

          # Performance Governer
          for file in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do echo "performance" > "$file"; done

          # Isolating CPU
          systemctl set-property --runtime -- user.slice AllowedCPUs=16-19
          systemctl set-property --runtime -- system.slice AllowedCPUs=16-19
          systemctl set-property --runtime -- init.scope AllowedCPUs=16-19

        elif [[ "$HOOK_NAME/$STATE_NAME" == "release/end" ]]; then

          modprobe -r vfio_iommu_type1 vfio_pci vfio

          # Re-Bind GPU to Host
          virsh nodedev-reattach pci_0000_03_00_0
          virsh nodedev-reattach pci_0000_03_00_1
          virsh nodedev-reattach pci_0000_03_00_2

          # Rebind VT consoles
          echo 1 > /sys/class/vtconsole/vtcon0/bind
          echo 1 > /sys/class/vtconsole/vtcon1/bind

          sleep 2

          # Bind Framebuffer
          #echo "efi-framebuffer.0" > /sys/bus/platform/drivers/efi-framebuffer/bind

          # Reload Modules
          modprobe amdgpu

          # Back to On-Demand Scheduler
          for file in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do echo "ondemand" > "$file"; done

          systemctl set-property --runtime -- user.slice AllowedCPUs=0-19
          systemctl set-property --runtime -- system.slice AllowedCPUs=0-19
          systemctl set-property --runtime -- init.scope AllowedCPUs=0-19

        fi

      fi

    '';
  };
in {
  imports =
    [
      ./hardware-configuration.nix
      ../default.nix
    ];
    virtualisation.libvirtd.hooks.qemu = {
      passthrough = "${passthrough}/bin/passthrough.sh";
    };

  stitchyconf = {
    artPkgs.enable = true;
  };

  virtualisation.libvirtd = {
    enable = true;
    onBoot = "ignore";
  };
  hardware = {
    opentabletdriver.enable = true;
    steam-hardware.enable = true;
  };

  networking.networkmanager.enable = true;
  services.syncthing = {
    enable = true;
    user = "stitchynyan";
    openDefaultPorts = true;
    databaseDir = "/home/stitchynyan/.local/share/syncthing";
    dataDir = "/home/stitchynyan";
  };

  environment.systemPackages = [
  pkgs.wlx-overlay-s ];

  networking.hostName = "malachite";

  time.timeZone = "America/Los_Angeles";

  # Read the Docs before Changing
  system.stateVersion = "23.05"; # Did you read the comment?
}