#
# Looking glass for virtualization on machines that can do
# GPU passthrough.
#
# vim: et:ts=2:sw=2:
{
  pkgs,
  config,
  ...
}:
{

  #
  # Setup instructions.
  #

  # 1. Install looking glass in the guest VM.
  #    Be sure to get a matching version.
  # 2. If using an NVIDIA GPU, hook the encoder using the following patches:
  #    https://github.com/keylase/nvidia-patch/tree/master/win
  #    -> DLLs are included in ../../looking-glass-contrib.
  #    -> Rename %WINDIR%\system32\NvFBC64.dll -> %WINDIR%\system32\NvFBC64_.dll
  #    -> Copy the `nvfbcwrp64.dll` to %WINDIR%\system32\NvFBC64.dll.
  #    -> Rename %WINDIR%\SysWOW64\NvFBC.dll -> %WINDIR%\SysWow64\NvFBC_.dll
  #    -> Copy the `nvfbcwrp32.dll` to %WINDIR%\SysWOW64\NvFBC.dll.
  # 3. Install https://github.com/ge9/IddSampleDriver onto the machine.
  #    -> Be sure to install its root cert by running the included batch file.
  #    -> Preferably use the Device Manager or devcon to install this.
  #    -> A copy is included ../../looking-glass-contrib.
  # 4. Disable the e.g. QXL video device in the VM.
  # 5. Start the VM and the looking glass client.

  #
  # NixOS configuration.
  #

  # Set up KVMFR as the shared memory between us and the guest.
  boot.extraModulePackages = [ config.boot.kernelPackages.kvmfr ];
  boot.extraModprobeConfig = ''
    options kvmfr static_size_mb=128
  '';
  boot.kernelModules = [ "kvmfr" ];
  services.udev.extraRules = ''
    SUBSYSTEM=="kvmfr", OWNER="deprekated", GROUP="kvm", MODE="0660"
  '';

  virtualisation.libvirtd.qemu.verbatimConfig = ''
    namespaces = []
    cgroup_controllers = []
  '';

  # Provide the looking-glass client.
  environment.systemPackages = [
    pkgs.looking-glass-client
  ];

}
