From dad9c990826e522976afca663de01ae75aa439ad Mon Sep 17 00:00:00 2001 From: Someone Serge Date: Thu, 21 Dec 2023 23:59:09 +0000 Subject: [PATCH 1/3] cudaPackages: replace the fhs paths in pkg-config files Cf. https://github.com/NixOS/nixpkgs/issues/224119 --- .../cuda-modules/cuda/overrides.nix | 5 ++- .../generic-builders/manifest.nix | 37 ++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/pkgs/development/cuda-modules/cuda/overrides.nix b/pkgs/development/cuda-modules/cuda/overrides.nix index 12e14ef9965b..bcb41f43e98e 100644 --- a/pkgs/development/cuda-modules/cuda/overrides.nix +++ b/pkgs/development/cuda-modules/cuda/overrides.nix @@ -115,7 +115,10 @@ attrsets.filterAttrs (attr: _: (builtins.hasAttr attr prev)) { moveToOutput "nvvm" "''${!outputBin}" ''; - meta = (oldAttrs.meta or {}) // { + # The nvcc and cicc binaries contain hard-coded references to /usr + allowFHSReferences = true; + + meta = (oldAttrs.meta or { }) // { mainProgram = "nvcc"; }; } diff --git a/pkgs/development/cuda-modules/generic-builders/manifest.nix b/pkgs/development/cuda-modules/generic-builders/manifest.nix index 71c914c8c8f2..6b29f55c3e31 100644 --- a/pkgs/development/cuda-modules/generic-builders/manifest.nix +++ b/pkgs/development/cuda-modules/generic-builders/manifest.nix @@ -94,7 +94,12 @@ backendStdenv.mkDerivation ( # Traversed in the order of the outputs speficied in outputs; # entries are skipped if they don't exist in outputs. outputToPatterns = { - bin = ["bin"]; + bin = [ "bin" ]; + dev = [ + "share/pkg-config" + "**/*.pc" + "**/*.cmake" + ]; lib = [ "lib" "lib64" @@ -116,6 +121,22 @@ backendStdenv.mkDerivation ( inherit (redistribRelease.${redistArch}) sha256; }; + postPatch = '' + if [[ -d pkg-config ]] ; then + mkdir -p share/pkg-config + mv pkg-config/* share/pkg-config/ + rmdir pkg-config + fi + + for pc in share/pkg-config/*.pc ; do + sed -i \ + -e "s|^cudaroot\s*=.*\$|cudaroot=''${!outputDev}|" \ + -e "s|^libdir\s*=.*/lib\$|libdir=''${!outputLib}/lib|" \ + -e "s|^includedir\s*=.*/include\$|includedir=''${!outputDev}/include|" \ + "$pc" + done + ''; + # We do need some other phases, like configurePhase, so the multiple-output setup hook works. dontBuild = true; @@ -197,6 +218,20 @@ backendStdenv.mkDerivation ( runHook postInstall ''; + doInstallCheck = true; + allowFHSReferences = false; + postInstallCheck = '' + echo "Executing postInstallCheck" + + if [[ -z "''${allowFHSReferences-}" ]] ; then + mapfile -t outputPaths < <(for o in $(getAllOutputNames); do echo "''${!o}"; done) + if grep --max-count=5 --recursive --exclude=LICENSE /usr/ "''${outputPaths[@]}" ; then + echo "Detected references to /usr" >&2 + exit 1 + fi + fi + ''; + # libcuda needs to be resolved during runtime # NOTE: Due to the use of __structuredAttrs, we can't use a list for autoPatchelfIgnoreMissingDeps, since it # will take only the first value. Instead, we produce a string with the values separated by spaces. From cfa07014b6af63298365329c8ffc2a132941d985 Mon Sep 17 00:00:00 2001 From: Someone Serge Date: Fri, 22 Dec 2023 00:33:55 +0000 Subject: [PATCH 2/3] cudaPackages.cuda_cudart: patch cuda-XX.Y.pc --- pkgs/development/cuda-modules/cuda/overrides.nix | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pkgs/development/cuda-modules/cuda/overrides.nix b/pkgs/development/cuda-modules/cuda/overrides.nix index bcb41f43e98e..b835b5350aee 100644 --- a/pkgs/development/cuda-modules/cuda/overrides.nix +++ b/pkgs/development/cuda-modules/cuda/overrides.nix @@ -1,4 +1,4 @@ -{cudaVersion, lib}: +{cudaVersion, lib, addDriverRunpath}: let inherit (lib) attrsets lists strings; # cudaVersionOlder : Version -> Boolean @@ -42,6 +42,20 @@ attrsets.filterAttrs (attr: _: (builtins.hasAttr attr prev)) { lists.optionals (cudaVersionAtLeast "12.0") [final.libnvjitlink.lib] ); + cuda_cudart = prev.cuda_cudart.overrideAttrs ( + prevAttrs: { + + # The libcuda stub's pkg-config doesn't follow the general pattern: + postPatch = prevAttrs.postPatch or "" + '' + while IFS= read -r -d $'\0' path ; do + sed -i \ + -e "s|^libdir\s*=.*/lib\$|libdir=''${!outputLib}/lib/stubs|" \ + -e "s|^Libs\s*:\(.*\)\$|Libs: \1 -Wl,-rpath,${addDriverRunpath.driverLink}/lib|" \ + "$path" + done < <(find -iname 'cuda-*.pc' -print0) + ''; + }); + cuda_compat = prev.cuda_compat.overrideAttrs ( prevAttrs: { env.autoPatchelfIgnoreMissingDeps = From 5d6136a53eea91bcc7f32c83eb203d1b96494f52 Mon Sep 17 00:00:00 2001 From: Someone Serge Date: Mon, 25 Dec 2023 00:38:48 +0000 Subject: [PATCH 3/3] cudaPackages: allow FHS references by default ...harden gradually instead --- pkgs/development/cuda-modules/cuda/overrides.nix | 1 + pkgs/development/cuda-modules/generic-builders/manifest.nix | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pkgs/development/cuda-modules/cuda/overrides.nix b/pkgs/development/cuda-modules/cuda/overrides.nix index b835b5350aee..225dada7c16b 100644 --- a/pkgs/development/cuda-modules/cuda/overrides.nix +++ b/pkgs/development/cuda-modules/cuda/overrides.nix @@ -44,6 +44,7 @@ attrsets.filterAttrs (attr: _: (builtins.hasAttr attr prev)) { cuda_cudart = prev.cuda_cudart.overrideAttrs ( prevAttrs: { + allowFHSReferences = false; # The libcuda stub's pkg-config doesn't follow the general pattern: postPatch = prevAttrs.postPatch or "" + '' diff --git a/pkgs/development/cuda-modules/generic-builders/manifest.nix b/pkgs/development/cuda-modules/generic-builders/manifest.nix index 6b29f55c3e31..67f6e93559c4 100644 --- a/pkgs/development/cuda-modules/generic-builders/manifest.nix +++ b/pkgs/development/cuda-modules/generic-builders/manifest.nix @@ -219,7 +219,7 @@ backendStdenv.mkDerivation ( ''; doInstallCheck = true; - allowFHSReferences = false; + allowFHSReferences = true; # TODO: Default to `false` postInstallCheck = '' echo "Executing postInstallCheck"