From a5b8caa346ba946749136c9896b40d25266958f0 Mon Sep 17 00:00:00 2001
From: Graham Bennett <graham@grahambennett.org>
Date: Sun, 3 Dec 2023 18:42:41 +0000
Subject: [PATCH] cudatoolkit: Replace vendored Qt plugins with symlinks

This resolves crashes in nsys-ui
---
 .../cuda-modules/cudatoolkit/default.nix      | 31 ++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/pkgs/development/cuda-modules/cudatoolkit/default.nix b/pkgs/development/cuda-modules/cudatoolkit/default.nix
index e0868e2b1c2cb..229602b6ddff5 100644
--- a/pkgs/development/cuda-modules/cudatoolkit/default.nix
+++ b/pkgs/development/cuda-modules/cudatoolkit/default.nix
@@ -258,7 +258,7 @@ backendStdenv.mkDerivation rec {
         rm $out/host-linux-x64/libstdc++.so*
       ''}
         ${
-          lib.optionalString (lib.versionAtLeast version "11.8")
+          lib.optionalString (lib.versionAtLeast version "11.8" && lib.versionOlder version "12")
             # error: auto-patchelf could not satisfy dependency libtiff.so.5 wanted by /nix/store/.......-cudatoolkit-12.0.1/host-linux-x64/Plugins/imageformats/libqtiff.so
             # we only ship libtiff.so.6, so let's use qt plugins built by Nix.
             # TODO: don't copy, come up with a symlink-based "merge"
@@ -266,6 +266,21 @@ backendStdenv.mkDerivation rec {
               rsync ${lib.getLib qt6Packages.qtimageformats}/lib/qt-6/plugins/ $out/host-linux-x64/Plugins/ -aP
             ''
         }
+        ${
+          lib.optionalString (lib.versionAtLeast version "12")
+            # Use Qt plugins built by Nix.
+            ''
+              for qtlib in $out/host-linux-x64/Plugins/*/libq*.so; do
+                qtdir=$(basename $(dirname $qtlib))
+                filename=$(basename $qtlib)
+                for qtpkgdir in ${lib.concatMapStringsSep " " (x: qt6Packages.${x}) ["qtbase" "qtimageformats" "qtsvg" "qtwayland"]}; do
+                  if [ -e $qtpkgdir/lib/qt-6/plugins/$qtdir/$filename ]; then
+                    ln -snf $qtpkgdir/lib/qt-6/plugins/$qtdir/$filename $qtlib
+                  fi
+                done
+              done
+            ''
+        }
 
       rm -f $out/tools/CUDA_Occupancy_Calculator.xls # FIXME: why?
 
@@ -336,6 +351,20 @@ backendStdenv.mkDerivation rec {
       wrapProgram "$out/bin/$b" \
         --set GDK_PIXBUF_MODULE_FILE "$GDK_PIXBUF_MODULE_FILE"
     done
+    ${
+      lib.optionalString (lib.versionAtLeast version "12")
+        # Check we don't have any lurking vendored qt libraries that weren't
+        # replaced during installPhase
+        ''
+          qtlibfiles=$(find $out -name "libq*.so" -type f)
+          if [ ! -z "$qtlibfiles" ]; then
+            echo "Found unexpected vendored Qt library files in $out" >&2
+            echo $qtlibfiles >&2
+            echo "These should be replaced with symlinks in installPhase" >&2
+            exit 1
+          fi
+        ''
+    }
   '';
 
   # cuda-gdb doesn't run correctly when not using sandboxing, so