How to set `runtimeDependencies` for shared libraries?

I’m attempting to package a python library that is distributed with .so files that dlopen other .so libraries at runtime and are not auto-patched by autoPatchelfHook. Following the docs, I was under the impression that I could add

runtimeDependencies = [ cudatoolkit_11.lib ];

to my derivation to get these dependencies into rpath regardless. Unfortunately this doesn’t work and it appears as though runtimeDependencies is only respected when patching binaries (source).

So I’m at a loss now… What is the nix-y way of patching these kinds of libraries? And how can I do this from within buildPythonPackage in particular?

I’m also curious what phase autoPatchelfHook runs in, because it seems to be after postFixup which was surprising to me considering the comment here. How can I make sure to run a phase after autoPatchelfHook?

EDIT: It appears that autoPatchelfHook does indeed run after postFixup (source). Phases are extremely confusing.

Don’t .so files typically have their execute bit set? If yours don’t you could try to chmod +x them and see if autoPatchelfHook picks them up.

I haven’t had much luck with autoPatchelfHook in my experience, I usually just call patchelf manually.

Also, I haven’t used this myself, but others have had luck with GitHub - Mic92/nix-ld: Run unpatched dynamic binaries on NixOS

I found that doing some manual patchelf’ing in the preInstallCheck phase works. I’m not sure that it’s the cleanest way to go about things, but it does work.

In case it can help someone, I got bitten by a similar issue as runtimeDependencies was not applied automatically. I actually realized that while some file were binary blob (these ones were patched correctly) the binaries that got compiled by the derivation are not fixed (which make sense…). To force to patch them, you can use something like:

    NIX_LDFLAGS = "-rpath ${lib.makeLibraryPath [ libtiff ]} $NIX_LDFLAGS";

(you can also set this variable in a phase using export + removing space around =)
but note that alone it is not enough, as nix automatically optimize it later and removes the just added rpath (maybe that could explain your issue?) So avoid this unecessary optimization, just do:

dontPatchELF = true;

To debug, you may like:

$ readelf -d $out/lib/yourfile
 0x000000000000001d (RUNPATH)            Bibliothèque runpath:[/nix/store/df8cvw3adyk0ip2vx6x6i0vdjyv2yinz-blablb/lib:/nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib:/nix/store/mdck89nsfisflwjv6xv8ydj7dj0sj2pn-gcc-11.3.0-lib/lib]

and check what is in the RUNPATH line.