No worries, not a dumb question at all, took me a week to figure it out on my own so no sweat.
The short version is you need to ldd and patchelf --print-needed the executables you are pulling down for the toolchain.
The nix linker is Black Magic. I am going to show you the manual method so the autoPatchelfHook makes sense, as it does the same thing its just an automated process you use during build instead of having to hunt down missing libs in bins.
This is a long one so get some coffee or a coke before you start reading.
Starting point:
We are going to need a few things, notably nix-index and nix-locate. You can do this as a nix-shell but if dev is your thing then youâll want it for later as the db can take a while to populate.
Edit your /etc/nixos/configuration.nix file and add the pkg nix-index and nix-locate to system packages, rebuild and run:
nix-index
This will take a minute, so do this now while you are reading.
Now we need to know what libs the bin needs to do stuff. Im going to use my aces project from earlier as an example.
My project was an already packaged program native to linux, just needed to be ported so the process should mirror pretty close to what you are tryn for.
Here, im downloading a fresh copy of the bins from the official website for the purpose of demonstration.
[jason@Beast:~/tmp]$ tar -xf wt_launcher_linux_1.0.3.22.tar.gz
[jason@Beast:~/tmp]$ cd WarThunder/
[jason@Beast:~/tmp/WarThunder]$ ls
bpreport ca-bundle.crt gaijin_selfupdater launcher launcher.ico launcherr.dat libsciter-gtk.so libsteam_api.so package.blk yupartner.blk
[jason@Beast:~/tmp/WarThunder]$ ./launcher
./launcher: error while loading shared libraries: libglib-2.0.so.0: cannot open shared object file: No such file or directory
[jason@Beast:~/tmp/WarThunder]$
No worky, as expected. Its not been stripped or linked yet so lets start/
First, we query the bin for what it wants:
[jason@Beast:~/tmp/WarThunder]$ patchelf --print-interpreter launcher
/lib64/ld-linux-x86-64.so.2
[jason@Beast:~/tmp/WarThunder]$ patchelf --print-needed launcher
libXrandr.so.2
librt.so.1
libpthread.so.0
libdl.so.2
libX11.so.6
libgtk-3.so.0
libglib-2.0.so.0
libgobject-2.0.so.0
libgio-2.0.so.0
libgdk-3.so.0
libstdc++.so.6
libm.so.6
libgcc_s.so.1
libc.so.6
[jason@Beast:~/tmp/WarThunder]$
This lists all the libs needed for execution.
Now we need to see which are linked and which arent, we need to use ldd.
[jason@Beast:~/tmp/WarThunder]$ ldd launcher
linux-vdso.so.1 (0x00007ffe8d9fa000)
libXrandr.so.2 => not found
librt.so.1 => /nix/store/wn7v2vhyyyi6clcyn0s9ixvl7d4d87ic-glibc-2.40-36/lib/librt.so.1 (0x000070754dcdf000)
libpthread.so.0 => /nix/store/wn7v2vhyyyi6clcyn0s9ixvl7d4d87ic-glibc-2.40-36/lib/libpthread.so.0 (0x000070754dcda000)
libdl.so.2 => /nix/store/wn7v2vhyyyi6clcyn0s9ixvl7d4d87ic-glibc-2.40-36/lib/libdl.so.2 (0x000070754dcd5000)
libX11.so.6 => not found
libgtk-3.so.0 => not found
libglib-2.0.so.0 => not found
libgobject-2.0.so.0 => not found
libgio-2.0.so.0 => not found
libgdk-3.so.0 => not found
libstdc++.so.6 => not found
libm.so.6 => /nix/store/wn7v2vhyyyi6clcyn0s9ixvl7d4d87ic-glibc-2.40-36/lib/libm.so.6 (0x000070754dbea000)
libgcc_s.so.1 => /nix/store/2d5spnl8j5r4n1s4bj1zmra7mwx0f1n8-xgcc-13.3.0-libgcc/lib/libgcc_s.so.1 (0x000070754dbc5000)
libc.so.6 => /nix/store/wn7v2vhyyyi6clcyn0s9ixvl7d4d87ic-glibc-2.40-36/lib/libc.so.6 (0x000070754d9cc000)
/lib64/ld-linux-x86-64.so.2 => /nix/store/wn7v2vhyyyi6clcyn0s9ixvl7d4d87ic-glibc-2.40-36/lib64/ld-linux-x86-64.so.2 (0x000070754dce6000)
[jason@Beast:~/tmp/WarThunder]$
Now we know which libs are missing. Lets link them.
Before we do, we need to build lib paths in the shell for them and thats where we will start to use nix-index and nix repl.
Youâve probably not been introduced yet to nix repl, so the brief explanation is nix repl is a shell you can launch that will accept commands like a nix shell, you can build an entire drv and install it only using nix repl. Its a powerful tool thats woven into the very core of nix so its worth taking some time to get familiar with it.
Nix repl can also be the most infuriating piece of dog shit software written since windows ME but i digress.
running nix repl will just drop you in a shell with no variables or pkgs imported so you will need to import the pkg repo directly or you can get a copy of the repo as a git and execute nix repl from that directory to import all of them that way. Your pick on that.
But for the sake of simplicity, you can just run (as normal user, no need for root)
[jason@Beast:~/tmp/WarThunder]$ nix repl
Nix 2.24.11
Type :? for help.
nix-repl> :l <nixpkgs>
Added 22928 variables.
nix-repl>
By this point, your nix-index should be done.
Take all the libs you found in print-missing statement from the elf interpreter and do this with them.
Im going to use the lib libXrandr.so.2 for this example.
Here we are going to use nix-locate to find what pkg contains this library.
[jason@Beast:~/tmp/WarThunder]$ nix-locate --top-level -w lib/libXrandr.so.2
xorg.libXrandr.out 0 s /nix/store/k4wbhldwg6lfsiaxmp6fjzma8qdh3vm5-libXrandr-1.5.4/lib/libXrandr.so.2
[jason@Beast:~/tmp/WarThunder]$
Coo, the pkg xorg.libXrandr contains the lib we need. So lets build a path for it, back to nix repl.
nix-repl> with pkgs; lib.makeLibraryPath [ xorg.libXrandr ]
"/nix/store/k4wbhldwg6lfsiaxmp6fjzma8qdh3vm5-libXrandr-1.5.4/lib"
nix-repl>
Thats our new library path, now we can take and link that path to the bin using patchelf on the terminal.
First, we need to spawn a nix-shell with a standard dev env, then set the link.
[jason@Beast:~/tmp/WarThunder]$ nix-shell -p stdenv.cc.cc stdenv.cc.cc.lib
bash: .: /run/current-system/sw/etc/bash_completion.d/bash-completion: is a directory
[nix-shell:~/tmp/WarThunder]$ patchelf --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" --set-rpath /nix/store/k4wbhldwg6lfsiaxmp6fjzma8qdh3vm5-libXrandr-1.5.4/lib launcher
[nix-shell:~/tmp/WarThunder]$
Now lets check with the interpreter again and see if this lib is still listed as missing.
[nix-shell:~/tmp/WarThunder]$ ldd launcher
linux-vdso.so.1 (0x00007ffd2e9f4000)
libXrandr.so.2 => /nix/store/k4wbhldwg6lfsiaxmp6fjzma8qdh3vm5-libXrandr-1.5.4/lib/libXrandr.so.2 (0x000070cd8a983000)
librt.so.1 => /nix/store/nqb2ns2d1lahnd5ncwmn6k84qfd7vx2k-glibc-2.40-36/lib/librt.so.1 (0x000070cd8a97e000)
libpthread.so.0 => /nix/store/nqb2ns2d1lahnd5ncwmn6k84qfd7vx2k-glibc-2.40-36/lib/libpthread.so.0 (0x000070cd8a979000)
libdl.so.2 => /nix/store/nqb2ns2d1lahnd5ncwmn6k84qfd7vx2k-glibc-2.40-36/lib/libdl.so.2 (0x000070cd8a974000)
libX11.so.6 => not found
libgtk-3.so.0 => not found
libglib-2.0.so.0 => not found
libgobject-2.0.so.0 => not found
libgio-2.0.so.0 => not found
libgdk-3.so.0 => not found
libstdc++.so.6 => not found
libm.so.6 => /nix/store/nqb2ns2d1lahnd5ncwmn6k84qfd7vx2k-glibc-2.40-36/lib/libm.so.6 (0x000070cd8a889000)
libgcc_s.so.1 => /nix/store/2d5spnl8j5r4n1s4bj1zmra7mwx0f1n8-xgcc-13.3.0-libgcc/lib/libgcc_s.so.1 (0x000070cd8a864000)
libc.so.6 => /nix/store/nqb2ns2d1lahnd5ncwmn6k84qfd7vx2k-glibc-2.40-36/lib/libc.so.6 (0x000070cd8a66b000)
libXext.so.6 => /nix/store/hi1mbz7s24l63w1m9sy9ynl8yjvb2f46-libXext-1.3.6/lib/libXext.so.6 (0x000070cd8a654000)
libXrender.so.1 => /nix/store/bnfmimcfan36gg0g488c5hksg8nyr16y-libXrender-0.9.11/lib/libXrender.so.1 (0x000070cd8a647000)
libX11.so.6 => /nix/store/zcq4irdcgn3ljqdnlpm2zjp7f1kw9jvm-libX11-1.8.10/lib/libX11.so.6 (0x000070cd8a4fb000)
/nix/store/nqb2ns2d1lahnd5ncwmn6k84qfd7vx2k-glibc-2.40-36/lib/ld-linux-x86-64.so.2 => /nix/store/nqb2ns2d1lahnd5ncwmn6k84qfd7vx2k-glibc-2.40-36/lib64/ld-linux-x86-64.so.2 (0x000070cd8a992000)
libxcb.so.1 => /nix/store/c25p9xs9n6grwx4i4l4kmz09scgcav4b-libxcb-1.17.0/lib/libxcb.so.1 (0x000070cd8a4cf000)
libXau.so.6 => /nix/store/6a49zj2wva8nxw7sidw9j9bp2nifscbw-libXau-1.0.11/lib/libXau.so.6 (0x000070cd8a4ca000)
libXdmcp.so.6 => /nix/store/1x0fg2bhf14lk83458c4iir92hc1njjh-libXdmcp-1.1.5/lib/libXdmcp.so.6 (0x000070cd8a4c0000)
[nix-shell:~/tmp/WarThunder]$
And we see, libXrandr is now linked for execution.
This is the long version of how patchelf works to ânixifyâ bins.