-
Notifications
You must be signed in to change notification settings - Fork 29
/
default.nix
172 lines (143 loc) · 6.2 KB
/
default.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
{ pkgs, lib, config, ... }:
let
cfg = config.hardware.nvidia.vgpu;
mdevctl = pkgs.callPackage ./mdevctl {};
pythonPackages = pkgs.python38Packages;
frida = pythonPackages.callPackage ./frida {};
vgpuVersion = "460.32.04";
gridVersion = "460.32.03";
guestVersion = "461.33";
combinedZipName = "NVIDIA-GRID-Linux-KVM-${vgpuVersion}-${gridVersion}-${guestVersion}.zip";
requireFile = { name, ... }@args: pkgs.requireFile (rec {
inherit name;
url = "https://www.nvidia.com/object/vGPU-software-driver.html";
message = ''
Unfortunately, we cannot download file ${name} automatically.
This file can be extracted from ${combinedZipName}.
Please go to ${url} to download it yourself, and add it to the Nix store
using either
nix-store --add-fixed sha256 ${name}
or
nix-prefetch-url --type sha256 file:///path/to/${name}
'';
} // args);
nvidia-vgpu-kvm-src = pkgs.runCommand "nvidia-${vgpuVersion}-vgpu-kvm-src" {
src = requireFile {
name = "NVIDIA-Linux-x86_64-${vgpuVersion}-vgpu-kvm.run";
sha256 = "00ay1f434dbls6p0kaawzc6ziwlp9dnkg114ipg9xx8xi4360zzl";
};
} ''
mkdir $out
cd $out
# From unpackManually() in builder.sh of nvidia-x11 from nixpkgs
skip=$(sed 's/^skip=//; t; d' $src)
tail -n +$skip $src | xz -d | tar xvf -
'';
vgpu_unlock = pkgs.stdenv.mkDerivation {
name = "nvidia-vgpu-unlock";
version = "unstable-2021-04-22";
src = pkgs.fetchFromGitHub {
owner = "DualCoder";
repo = "vgpu_unlock";
rev = "1888236c75d8eac673695be8b000f0b065111c51";
sha256 = "0s8bmscb8irj1sggfg1fhacqd1lh59l326bnrk4a2g4qngsbkix3";
};
buildInputs = [ (pythonPackages.python.withPackages (p: [ frida ])) ];
postPatch = ''
substituteInPlace vgpu_unlock \
--replace /bin/bash ${pkgs.bash}/bin/bash
'';
installPhase = "install -Dm755 vgpu_unlock $out/bin/vgpu_unlock";
};
in
{
options = {
hardware.nvidia.vgpu = {
enable = lib.mkEnableOption "vGPU support";
unlock.enable = lib.mkOption {
default = false;
type = lib.types.bool;
description = "Unlock vGPU functionality for consumer grade GPUs";
};
};
};
config = lib.mkIf cfg.enable {
hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.stable.overrideAttrs (
{ patches ? [], postUnpack ? "", postPatch ? "", preFixup ? "", ... }@attrs: {
name = "nvidia-x11-${vgpuVersion}-${gridVersion}-${config.boot.kernelPackages.kernel.version}";
version = "${vgpuVersion}";
src = requireFile {
name = "NVIDIA-Linux-x86_64-${gridVersion}-grid.run";
sha256 = "0smvmxalxv7v12m0hvd5nx16jmcc7018s8kac3ycmxam8l0k9mw9";
};
patches = patches ++ [
./nvidia-vgpu-merge.patch
] ++ lib.optional cfg.unlock.enable
(pkgs.substituteAll {
src = ./nvidia-vgpu-unlock.patch;
vgpu_unlock = vgpu_unlock.src;
});
postUnpack = postUnpack + ''
# More merging, besides patch above
cp -r ${nvidia-vgpu-kvm-src}/init-scripts .
cp ${nvidia-vgpu-kvm-src}/kernel/common/inc/nv-vgpu-vfio-interface.h kernel/common/inc//nv-vgpu-vfio-interface.h
cp ${nvidia-vgpu-kvm-src}/kernel/nvidia/nv-vgpu-vfio-interface.c kernel/nvidia/nv-vgpu-vfio-interface.c
echo "NVIDIA_SOURCES += nvidia/nv-vgpu-vfio-interface.c" >> kernel/nvidia/nvidia-sources.Kbuild
cp -r ${nvidia-vgpu-kvm-src}/kernel/nvidia-vgpu-vfio kernel/nvidia-vgpu-vfio
for i in libnvidia-vgpu.so.${vgpuVersion} libnvidia-vgxcfg.so.${vgpuVersion} nvidia-vgpu-mgr nvidia-vgpud vgpuConfig.xml sriov-manage; do
cp ${nvidia-vgpu-kvm-src}/$i $i
done
chmod -R u+rw .
'';
postPatch = postPatch + ''
# Move path for vgpuConfig.xml into /etc
sed -i 's|/usr/share/nvidia/vgpu|/etc/nvidia-vgpu-xxxxx|' nvidia-vgpud
substituteInPlace sriov-manage \
--replace lspci ${pkgs.pciutils}/bin/lspci \
--replace setpci ${pkgs.pciutils}/bin/setpci
'';
# HACK: Using preFixup instead of postInstall since nvidia-x11 builder.sh doesn't support hooks
preFixup = preFixup + ''
for i in libnvidia-vgpu.so.${vgpuVersion} libnvidia-vgxcfg.so.${vgpuVersion}; do
install -Dm755 "$i" "$out/lib/$i"
done
patchelf --set-rpath ${pkgs.stdenv.cc.cc.lib}/lib $out/lib/libnvidia-vgpu.so.${vgpuVersion}
install -Dm644 vgpuConfig.xml $out/vgpuConfig.xml
for i in nvidia-vgpud nvidia-vgpu-mgr; do
install -Dm755 "$i" "$bin/bin/$i"
# stdenv.cc.cc.lib is for libstdc++.so needed by nvidia-vgpud
patchelf --interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" \
--set-rpath $out/lib "$bin/bin/$i"
done
install -Dm755 sriov-manage $bin/bin/sriov-manage
'';
});
systemd.services.nvidia-vgpud = {
description = "NVIDIA vGPU Daemon";
wants = [ "syslog.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "forking";
ExecStart = "${lib.optionalString cfg.unlock.enable "${vgpu_unlock}/bin/vgpu_unlock "}${lib.getBin config.hardware.nvidia.package}/bin/nvidia-vgpud";
ExecStopPost = "${pkgs.coreutils}/bin/rm -rf /var/run/nvidia-vgpud";
Environment = [ "__RM_NO_VERSION_CHECK=1" ]; # Avoids issue with API version incompatibility when merging host/client drivers
};
};
systemd.services.nvidia-vgpu-mgr = {
description = "NVIDIA vGPU Manager Daemon";
wants = [ "syslog.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "forking";
KillMode = "process";
ExecStart = "${lib.optionalString cfg.unlock.enable "${vgpu_unlock}/bin/vgpu_unlock "}${lib.getBin config.hardware.nvidia.package}/bin/nvidia-vgpu-mgr";
ExecStopPost = "${pkgs.coreutils}/bin/rm -rf /var/run/nvidia-vgpu-mgr";
Environment = [ "__RM_NO_VERSION_CHECK=1" ];
};
};
environment.etc."nvidia-vgpu-xxxxx/vgpuConfig.xml".source = config.hardware.nvidia.package + /vgpuConfig.xml;
boot.kernelModules = [ "nvidia-vgpu-vfio" ];
environment.systemPackages = [ mdevctl ];
services.udev.packages = [ mdevctl ];
};
}