···11{
22 # General callPackage-supplied arguments
33 autoAddOpenGLRunpathHook,
44+ autoAddCudaCompatRunpathHook,
45 autoPatchelfHook,
56 backendStdenv,
67 fetchurl,
···126127 # Check e.g. with `patchelf --print-rpath path/to/my/binary
127128 autoAddOpenGLRunpathHook
128129 markForCudatoolkitRootHook
130130+ ]
131131+ # autoAddCudaCompatRunpathHook depends on cuda_compat and would cause
132132+ # infinite recursion if applied to `cuda_compat` itself (beside the fact
133133+ # that it doesn't make sense in the first place)
134134+ ++ lib.optionals (pname != "cuda_compat" && flags.isJetsonBuild) [
135135+ # autoAddCudaCompatRunpathHook must appear AFTER autoAddOpenGLRunpathHook.
136136+ # See its documentation in ./setup-hooks/extension.nix.
137137+ autoAddCudaCompatRunpathHook
129138 ];
130139131140 buildInputs =
···11+# shellcheck shell=bash
22+# Patch all dynamically linked, ELF files with the CUDA driver (libcuda.so)
33+# coming from the cuda_compat package by adding it to the RUNPATH.
44+echo "Sourcing auto-add-cuda-compat-runpath-hook"
55+66+elfHasDynamicSection() {
77+ patchelf --print-rpath "$1" >& /dev/null
88+}
99+1010+autoAddCudaCompatRunpathPhase() (
1111+ local outputPaths
1212+ mapfile -t outputPaths < <(for o in $(getAllOutputNames); do echo "${!o}"; done)
1313+ find "${outputPaths[@]}" -type f -executable -print0 | while IFS= read -rd "" f; do
1414+ if isELF "$f"; then
1515+ # patchelf returns an error on statically linked ELF files
1616+ if elfHasDynamicSection "$f" ; then
1717+ echo "autoAddCudaCompatRunpathHook: patching $f"
1818+ local origRpath="$(patchelf --print-rpath "$f")"
1919+ patchelf --set-rpath "@libcudaPath@:$origRpath" "$f"
2020+ elif (( "${NIX_DEBUG:-0}" >= 1 )) ; then
2121+ echo "autoAddCudaCompatRunpathHook: skipping a statically-linked ELF file $f"
2222+ fi
2323+ fi
2424+ done
2525+)
2626+2727+postFixupHooks+=(autoAddCudaCompatRunpathPhase)
···4444 ./auto-add-opengl-runpath-hook.sh
4545 )
4646 {};
4747+4848+ # autoAddCudaCompatRunpathHook hook must be added AFTER `setupCudaHook`. Both
4949+ # hooks prepend a path with `libcuda.so` to the `DT_RUNPATH` section of
5050+ # patched elf files, but `cuda_compat` path must take precedence (otherwise,
5151+ # it doesn't have any effect) and thus appear first. Meaning this hook must be
5252+ # executed last.
5353+ autoAddCudaCompatRunpathHook =
5454+ final.callPackage
5555+ (
5656+ {makeSetupHook, cuda_compat}:
5757+ makeSetupHook
5858+ {
5959+ name = "auto-add-cuda-compat-runpath-hook";
6060+ substitutions = {
6161+ libcudaPath = "${cuda_compat}/compat";
6262+ };
6363+ }
6464+ ./auto-add-cuda-compat-runpath.sh
6565+ )
6666+ {};
4767}