typioca: 2.7.0 -> 2.8.0
[NixPkgs.git] / nixos / modules / virtualisation / kvmgt.nix
blob1e02636f81f415eaead8bc1769637c250ae3ff31
1 { config, lib, pkgs, ... }:
3 with lib;
5 let
6   cfg = config.virtualisation.kvmgt;
8   kernelPackages = config.boot.kernelPackages;
10   vgpuOptions = {
11     uuid = mkOption {
12       type = with types; listOf str;
13       description = lib.mdDoc "UUID(s) of VGPU device. You can generate one with `libossp_uuid`.";
14     };
15   };
17 in {
18   options = {
19     virtualisation.kvmgt = {
20       enable = mkEnableOption (lib.mdDoc ''
21         KVMGT (iGVT-g) VGPU support. Allows Qemu/KVM guests to share host's Intel integrated graphics card.
22         Currently only one graphical device can be shared. To allow users to access the device without root add them
23         to the kvm group: `users.extraUsers.<yourusername>.extraGroups = [ "kvm" ];`
24       '');
25       # multi GPU support is under the question
26       device = mkOption {
27         type = types.str;
28         default = "0000:00:02.0";
29         description = lib.mdDoc "PCI ID of graphics card. You can figure it with {command}`ls /sys/class/mdev_bus`.";
30       };
31       vgpus = mkOption {
32         default = {};
33         type = with types; attrsOf (submodule [ { options = vgpuOptions; } ]);
34         description = lib.mdDoc ''
35           Virtual GPUs to be used in Qemu. You can find devices via {command}`ls /sys/bus/pci/devices/*/mdev_supported_types`
36           and find info about device via {command}`cat /sys/bus/pci/devices/*/mdev_supported_types/i915-GVTg_V5_4/description`
37         '';
38         example = {
39           i915-GVTg_V5_8.uuid = [ "a297db4a-f4c2-11e6-90f6-d3b88d6c9525" ];
40         };
41       };
42     };
43   };
45   config = mkIf cfg.enable {
46     assertions = singleton {
47       assertion = versionAtLeast kernelPackages.kernel.version "4.16";
48       message = "KVMGT is not properly supported for kernels older than 4.16";
49     };
51     boot.kernelModules = [ "kvmgt" ];
52     boot.kernelParams = [ "i915.enable_gvt=1" ];
54     services.udev.extraRules = ''
55       SUBSYSTEM=="vfio", OWNER="root", GROUP="kvm"
56     '';
58     systemd = let
59       vgpus = listToAttrs (flatten (mapAttrsToList
60         (mdev: opt: map (id: nameValuePair "kvmgt-${id}" { inherit mdev; uuid = id; }) opt.uuid)
61         cfg.vgpus));
62     in {
63       paths = mapAttrs (_: opt:
64         {
65           description = "KVMGT VGPU ${opt.uuid} path";
66           wantedBy = [ "multi-user.target" ];
67           pathConfig = {
68             PathExists = "/sys/bus/pci/devices/${cfg.device}/mdev_supported_types/${opt.mdev}/create";
69           };
70         }) vgpus;
72       services = mapAttrs (_: opt:
73         {
74           description = "KVMGT VGPU ${opt.uuid}";
75           serviceConfig = {
76             Type = "oneshot";
77             RemainAfterExit = true;
78             ExecStart = "${pkgs.runtimeShell} -c 'echo ${opt.uuid} > /sys/bus/pci/devices/${cfg.device}/mdev_supported_types/${opt.mdev}/create'";
79             ExecStop = "${pkgs.runtimeShell} -c 'echo 1 > /sys/bus/pci/devices/${cfg.device}/${opt.uuid}/remove'";
80           };
81         }) vgpus;
82     };
83   };
85   meta.maintainers = with maintainers; [ patryk27 ];