Skip to content

Commit

Permalink
nixos/nvidia: add nvidia_dc_565 drivers
Browse files Browse the repository at this point in the history
Also deprecate dc_520 and use nixpkgs-fmt on module and driver files
  • Loading branch information
edwtjo committed Dec 8, 2024
1 parent 0bc7caf commit 085b521
Show file tree
Hide file tree
Showing 5 changed files with 1,069 additions and 832 deletions.
64 changes: 37 additions & 27 deletions nixos/modules/hardware/video/nvidia.nix
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,23 @@ in
information, see the NVIDIA docs, on Chapter 23. Dynamic Boost on Linux
'';

modesetting.enable = lib.mkEnableOption ''
kernel modesetting when using the NVIDIA proprietary driver.
Enabling this fixes screen tearing when using Optimus via PRIME (see
{option}`hardware.nvidia.prime.sync.enable`. This is not enabled
by default because it is not officially supported by NVIDIA and would not
work with SLI.
Enabling this and using version 545 or newer of the proprietary NVIDIA
driver causes it to provide its own framebuffer device, which can cause
Wayland compositors to work when they otherwise wouldn't.
'' // {
default = lib.versionAtLeast cfg.package.version "535";
defaultText = lib.literalExpression "lib.versionAtLeast cfg.package.version \"535\"";
};
modesetting.enable =
lib.mkEnableOption ''
kernel modesetting when using the NVIDIA proprietary driver.
Enabling this fixes screen tearing when using Optimus via PRIME (see
{option}`hardware.nvidia.prime.sync.enable`. This is not enabled
by default because it is not officially supported by NVIDIA and would not
work with SLI.
Enabling this and using version 545 or newer of the proprietary NVIDIA
driver causes it to provide its own framebuffer device, which can cause
Wayland compositors to work when they otherwise wouldn't.
''
// {
default = lib.versionAtLeast cfg.package.version "535";
defaultText = lib.literalExpression "lib.versionAtLeast cfg.package.version \"535\"";
};

prime.nvidiaBusId = lib.mkOption {
type = busIDType;
Expand Down Expand Up @@ -266,14 +268,16 @@ in
'';
};

gsp.enable = lib.mkEnableOption ''
the GPU System Processor (GSP) on the video card
'' // {
default = useOpenModules || lib.versionAtLeast nvidia_x11.version "555";
defaultText = lib.literalExpression ''
config.hardware.nvidia.open == true || lib.versionAtLeast config.hardware.nvidia.package.version "555"
'';
};
gsp.enable =
lib.mkEnableOption ''
the GPU System Processor (GSP) on the video card
''
// {
default = useOpenModules || lib.versionAtLeast nvidia_x11.version "555";
defaultText = lib.literalExpression ''
config.hardware.nvidia.open == true || lib.versionAtLeast config.hardware.nvidia.package.version "555"
'';
};
};
};

Expand All @@ -292,7 +296,7 @@ in
message = "You cannot configure both X11 and Data Center drivers at the same time.";
}
{
assertion = cfg.open != null;
assertion = cfg.open != null || cfg.datacenter.enable;
message = ''
You must configure `hardware.nvidia.open` on NVIDIA driver versions >= 560.
It is suggested to use the open source kernel modules on Turing or later GPUs (RTX series, GTX 16xx), and the closed source modules otherwise.
Expand All @@ -315,7 +319,9 @@ in
softdep nvidia post: nvidia-uvm
'';
};
systemd.tmpfiles.rules = lib.mkIf config.virtualisation.docker.enableNvidia [ "L+ /run/nvidia-docker/bin - - - - ${nvidia_x11.bin}/origBin" ];
systemd.tmpfiles.rules = lib.mkIf config.virtualisation.docker.enableNvidia [
"L+ /run/nvidia-docker/bin - - - - ${nvidia_x11.bin}/origBin"
];
services.udev.extraRules = ''
# Create /dev/nvidia-uvm when the nvidia-uvm module is loaded.
KERNEL=="nvidia", RUN+="${pkgs.runtimeShell} -c 'mknod -m 666 /dev/nvidiactl c 195 255'"
Expand Down Expand Up @@ -616,7 +622,9 @@ in
# If requested enable modesetting via kernel parameters.
kernelParams =
lib.optional (offloadCfg.enable || cfg.modesetting.enable) "nvidia-drm.modeset=1"
++ lib.optional ((offloadCfg.enable || cfg.modesetting.enable) && lib.versionAtLeast nvidia_x11.version "545") "nvidia-drm.fbdev=1"
++ lib.optional (
(offloadCfg.enable || cfg.modesetting.enable) && lib.versionAtLeast nvidia_x11.version "545"
) "nvidia-drm.fbdev=1"
++ lib.optional cfg.powerManagement.enable "nvidia.NVreg_PreserveVideoMemoryAllocations=1"
++ lib.optional useOpenModules "nvidia.NVreg_OpenRmEnableUnsupportedGpus=1"
++ lib.optional (config.boot.kernelPackages.kernel.kernelAtLeast "6.2" && !ibtSupport) "ibt=off";
Expand Down Expand Up @@ -677,7 +685,9 @@ in
TOPOLOGY_FILE_PATH = "${nvidia_x11.fabricmanager}/share/nvidia-fabricmanager/nvidia/nvswitch";
DATABASE_PATH = "${nvidia_x11.fabricmanager}/share/nvidia-fabricmanager/nvidia/nvswitch";
};
nv-fab-conf = settingsFormat.generate "fabricmanager.conf" (fabricManagerConfDefaults // cfg.datacenter.settings);
nv-fab-conf = settingsFormat.generate "fabricmanager.conf" (
fabricManagerConfDefaults // cfg.datacenter.settings
);
in
"${lib.getExe nvidia_x11.fabricmanager} -c ${nv-fab-conf}";
LimitCORE = "infinity";
Expand Down
107 changes: 75 additions & 32 deletions pkgs/os-specific/linux/nvidia-x11/builder.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ unpackManually() {


unpackFile() {
sh $src -x || unpackManually
if [ -x $src ];then
sh $src -x || unpackManually
else
tar xvf $src
fi
}


Expand All @@ -22,7 +26,6 @@ buildPhase() {
cd kernel
unset src # used by the nv makefile
make $makeFlags -j $NIX_BUILD_CORES module

cd ..
fi

Expand All @@ -33,21 +36,60 @@ buildPhase() {
installPhase() {
runHook preInstall

# build layout stuff which differs between run and redist-archives
if [ -d 32 ];then
bld_lib32_dir="32"
elif [ -d lib32 ];then
bld_lib32_dir="lib32"
else
bld_lib32_dir="."
fi
if [ -d lib ];then
bld_lib_dir="lib"
else
bld_lib_dir="."
fi
if [ -d wine ];then
bld_dll_dir="wine"
else
bld_dll_dir="."
fi
if [ -d etc ];then
bld_etc_dir="etc"
else
bld_etc_dir="."
fi
if [ -d share ];then
bld_shr_dir="share"
else
bld_shr_dir="."
fi
if [ -d man/man1 ];then
bld_man_dir="man/man1"
else
bld_man_dir="."
fi
if [ -d sbin ];then
bld_sbn_dir="sbin"
else
bld_sbn_dir="."
fi

# Install libGL and friends.

# since version 391, 32bit libraries are bundled in the 32/ sub-directory
if [ "$i686bundled" = "1" ]; then
mkdir -p "$lib32/lib"
cp -prd 32/*.so.* "$lib32/lib/"
if [ -d 32/tls ]; then
cp -prd 32/tls "$lib32/lib/"
cp -prd $bld_lib32_dir/*.so.* "$lib32/lib/"
if [ -d $bld_lib32_dir/tls ]; then
cp -prd $bld_lib32_dir/tls "$lib32/lib/"
fi
fi

mkdir -p "$out/lib"
cp -prd *.so.* "$out/lib/"
if [ -d tls ]; then
cp -prd tls "$out/lib/"
cp -prd $bld_lib_dir/*.so.* "$out/lib/"
if [ -d $bld_lib_dir/tls ]; then
cp -prd $bld_lib_dir/tls "$out/lib/"
fi

# Install systemd power management executables
Expand All @@ -67,6 +109,7 @@ installPhase() {
install -Dm755 nvidia.fixed $out/lib/systemd/system-sleep/nvidia
fi


for i in $lib32 $out; do
rm -f $i/lib/lib{glx,nvidia-wfb}.so.* # handled separately
rm -f $i/lib/libnvidia-gtk* # built from source
Expand All @@ -88,16 +131,16 @@ installPhase() {
# Be careful not to modify any original files because this runs twice.

# OpenCL
sed -E "s#(libnvidia-opencl)#$i/lib/\\1#" nvidia.icd > nvidia.icd.fixed
sed -E "s#(libnvidia-opencl)#$i/lib/\\1#" $bld_etc_dir/nvidia.icd > nvidia.icd.fixed
install -Dm644 nvidia.icd.fixed $i/etc/OpenCL/vendors/nvidia.icd

# Vulkan
if [ -e nvidia_icd.json.template ] || [ -e nvidia_icd.json ]; then
if [ -e nvidia_icd.json.template ]; then
if [ -e $bld_etc_dir/nvidia_icd.json.template ] || [ -e $bld_etc_dir/nvidia_icd.json ]; then
if [ -e $bld_etc_dir/nvidia_icd.json.template ]; then
# template patching for version < 435
sed "s#__NV_VK_ICD__#$i/lib/libGLX_nvidia.so#" nvidia_icd.json.template > nvidia_icd.json.fixed
sed "s#__NV_VK_ICD__#$i/lib/libGLX_nvidia.so#" $bld_etc_dir/nvidia_icd.json.template > nvidia_icd.json.fixed
else
sed -E "s#(libGLX_nvidia)#$i/lib/\\1#" nvidia_icd.json > nvidia_icd.json.fixed
sed -E "s#(libGLX_nvidia)#$i/lib/\\1#" $bld_etc_dir/nvidia_icd.json > nvidia_icd.json.fixed
fi

# nvidia currently only supports x86_64 and i686
Expand All @@ -108,17 +151,17 @@ installPhase() {
fi
fi

if [ -e nvidia_layers.json ]; then
sed -E "s#(libGLX_nvidia)#$i/lib/\\1#" nvidia_layers.json > nvidia_layers.json.fixed
if [ -e $bld_etc_dir/nvidia_layers.json ]; then
sed -E "s#(libGLX_nvidia)#$i/lib/\\1#" $bld_etc_dir/nvidia_layers.json > nvidia_layers.json.fixed
install -Dm644 nvidia_layers.json.fixed $i/share/vulkan/implicit_layer.d/nvidia_layers.json
fi

# EGL
if [ "$useGLVND" = "1" ]; then
mkdir -p "$i/share/egl/egl_external_platform.d"
for icdname in $(find . -name '*_nvidia*.json')
for icdname in $(find $bld_etc_dir -name '*_nvidia*.json')
do
cat "$icdname" | jq ".ICD.library_path |= \"$i/lib/\(.)\"" | tee "$i/share/egl/egl_external_platform.d/$icdname"
cat "$icdname" | jq ".ICD.library_path |= \"$i/lib/\(.)\"" | tee "$i/share/egl/egl_external_platform.d/$(basename $icdname)"
done

# glvnd icd
Expand All @@ -132,27 +175,27 @@ installPhase() {
fi

# Install libraries needed by Proton to support DLSS
if [ -e nvngx.dll ] && [ -e _nvngx.dll ]; then
install -Dm644 -t $i/lib/nvidia/wine/ nvngx.dll _nvngx.dll
if [ -e $bld_dll_dir/nvngx.dll ] && [ -e _$bld_dll_dir/nvngx.dll ]; then
install -Dm644 -t $i/lib/nvidia/wine/ $bld_dll_dir/nvngx.dll $bld_dll_dir/_nvngx.dll
fi
done


# OptiX tries loading `$ORIGIN/nvoptix.bin` first
if [ -e nvoptix.bin ]; then
install -Dm444 -t $out/lib/ nvoptix.bin
install -Dm444 -t $out/lib/ $bld_shr_dir/nvoptix.bin
fi

if [ -n "$bin" ]; then
# Install the X drivers.
mkdir -p $bin/lib/xorg/modules
if [ -f libnvidia-wfb.so ]; then
cp -p libnvidia-wfb.* $bin/lib/xorg/modules/
if [ -f $bld_lib_dir/libnvidia-wfb.so ]; then
cp -p $bld_lib_dir/libnvidia-wfb.* $bin/lib/xorg/modules/
fi
mkdir -p $bin/lib/xorg/modules/drivers
cp -p nvidia_drv.so $bin/lib/xorg/modules/drivers
cp -p $bld_lib_dir/nvidia_drv.so $bin/lib/xorg/modules/drivers
mkdir -p $bin/lib/xorg/modules/extensions
cp -p libglx*.so* $bin/lib/xorg/modules/extensions
cp -p $bld_lib_dir/libglx*.so* $bin/lib/xorg/modules/extensions

# Install the kernel module.
mkdir -p $bin/lib/modules/$kernelVersion/misc
Expand All @@ -164,8 +207,8 @@ installPhase() {
# Install application profiles.
if [ "$useProfiles" = "1" ]; then
mkdir -p $bin/share/nvidia
cp nvidia-application-profiles-*-rc $bin/share/nvidia/nvidia-application-profiles-rc
cp nvidia-application-profiles-*-key-documentation $bin/share/nvidia/nvidia-application-profiles-key-documentation
cp $bld_etc_dir/nvidia-application-profiles-*-rc $bin/share/nvidia/nvidia-application-profiles-rc
cp $bld_etc_dir/nvidia-application-profiles-*-key-documentation $bin/share/nvidia/nvidia-application-profiles-key-documentation
fi
fi

Expand Down Expand Up @@ -217,26 +260,26 @@ installPhase() {
if [ -n "$bin" ]; then
# Install /share files.
mkdir -p $bin/share/man/man1
cp -p *.1.gz $bin/share/man/man1
cp -p $bld_man_dir/*.1.gz $bin/share/man/man1
rm -f $bin/share/man/man1/{nvidia-xconfig,nvidia-settings,nvidia-persistenced}.1.gz
if [ -e "nvidia-dbus.conf" ]; then
install -Dm644 nvidia-dbus.conf $bin/share/dbus-1/system.d/nvidia-dbus.conf
fi

# Install the programs.
for i in nvidia-cuda-mps-control nvidia-cuda-mps-server nvidia-smi nvidia-debugdump nvidia-powerd; do
if [ -e "$i" ]; then
install -Dm755 $i $bin/bin/$i
if [ -e "$bld_sbn_dir/$i" ]; then
install -Dm755 $bld_sbn_dir/$i $bin/bin/$i
# unmodified binary backup for mounting in containers
install -Dm755 $i $bin/origBin/$i
install -Dm755 $bld_sbn_dir/$i $bin/origBin/$i
patchelf --interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" \
--set-rpath $out/lib:$libPath $bin/bin/$i
fi
done
substituteInPlace nvidia-bug-report.sh \
substituteInPlace $bld_sbn_dir/nvidia-bug-report.sh \
--replace /bin/grep grep \
--replace /bin/ls ls
install -Dm755 nvidia-bug-report.sh $bin/bin/nvidia-bug-report.sh
install -Dm755 $bld_sbn_dir/nvidia-bug-report.sh $bin/bin/nvidia-bug-report.sh
fi

runHook postInstall
Expand Down
Loading

0 comments on commit 085b521

Please sign in to comment.