|
| 1 | +{ options, config, lib, pkgs, ... }: |
| 2 | + |
| 3 | +let |
| 4 | + inherit (lib.attrsets) attrValues; |
| 5 | + inherit (lib.modules) mkIf; |
| 6 | + virtCfg = config.modules.virtualisation; |
| 7 | +in { |
| 8 | + options.modules.virtualisation.kvm = let inherit (lib.options) mkEnableOption; |
| 9 | + in { |
| 10 | + enable = mkEnableOption "Kernel-based Virutal Machine."; |
| 11 | + vendor = lib.types.enum [ "amd" "intel" ] "amd"; |
| 12 | + }; |
| 13 | + |
| 14 | + config = mkIf virtCfg.kvm.enable { |
| 15 | + virtCfg.looking-glass.enable = true; |
| 16 | + |
| 17 | + user.packages = attrValues { inherit (pkgs) virt-manager; }; |
| 18 | + |
| 19 | + boot = { |
| 20 | + kernelModules = |
| 21 | + # https://wiki.archlinux.org/title/PCI_passthrough_via_OVMF#modprobe.d |
| 22 | + [ "kvm-${virtCfg.vendor}" "vfio" "vfio_iommu_type1" "vfio_pci" ]; |
| 23 | + kernelParams = [ |
| 24 | + "${virtCfg.vendor}_iommu=on" |
| 25 | + "${virtCfg.vendor}_iommu=pt" |
| 26 | + "kvm.ignore_msrs=1" # https://pve.proxmox.com/wiki/PCI_Passthrough#Tips |
| 27 | + ]; |
| 28 | + extraModprobeConfig = '' |
| 29 | + softdep drm pre: vfio vfio-pci |
| 30 | + ''; |
| 31 | + }; |
| 32 | + |
| 33 | + virtualisation = { |
| 34 | + libvirtd = { |
| 35 | + enable = true; |
| 36 | + onBoot = "ignore"; |
| 37 | + onShutdown = "shutdown"; |
| 38 | + qemu = { |
| 39 | + ovmf = true; |
| 40 | + swtpm = true; |
| 41 | + runAsRoot = false; |
| 42 | + }; |
| 43 | + }; |
| 44 | + }; |
| 45 | + |
| 46 | + user.extraGroups = [ "libvirtd" ]; |
| 47 | + |
| 48 | + systemd.user.services.scream-ivshmem = { |
| 49 | + after = |
| 50 | + [ "libvirtd.service" "pipewire.service" "pipewire-pulse.service" ]; |
| 51 | + wantedBy = [ "graphical-session.target" ]; |
| 52 | + partOf = [ "graphical-session.target" ]; |
| 53 | + serviceConfig = { |
| 54 | + Type = "simple"; |
| 55 | + Restart = "on-failure"; |
| 56 | + ExecStart = "${lib.getExe pkgs.scream} -m /dev/shm/scream -o pulse"; |
| 57 | + }; |
| 58 | + }; |
| 59 | + }; |
| 60 | +} |
0 commit comments