I am also not an expert but am learning as you are so will do what i can.
I suggest reading Where can I get libgthread-2.0.so.0 - #7 by ryan4yin and Reddit - Dive into anything
Both are very informative on the nature of dynamic linking in nix as it can be a clusterfuck.
Typically, you’d wrap your libraries up and link them with this line:
export LD_LIBRARY_PATH=”${pkgs.glib.out}/lib:${pkgs.gtk2.out}/lib:${pkgs.gdk-pixbuf.out}/
In the future, you can use nix-index to run down libraries.
Example.
install nix-index to your sys packages and rebuild, with your normal user run the command nix-index. It will take a few to complete, when its done, run this:
nix-locate --minimal --top-level lib/libgthread-2.so.0
and it should spit out:
[jason@Beast:~]$ nix-locate --minimal --top-level lib/libgthread-2
remarkable2-toolchain.out
remarkable-toolchain.out
hyperhdr.out
glib.out
Cool, now we know we can find the library libgthread-2.so.0 in the package remarkable2-toolchain.
Go to NixOS Search to search for it and others like it, makes browsing a bit easier if you are not already familiar with this.
Now, the linker. There (annoyingly) isnt a dedicated section in the wiki to describe, instead its nested in the packaging section. Packaging/Binaries - NixOS Wiki
You are sort of creating a derivation even if its just to execute it from shell so this applies in a few ways. Notably with library linking: This is quoteds from the source mentioned just prior:
All the libraries which are not yet found are the ones we need to add to the runtime search path of the executable (RPATH). Again we can use
patchelf
to do this. We will be usingnix-index
for finding the files we are looking for:
$ # we generate the database index of all files in our channel first
$ nix-index
+ querying available packages
+ generating index: 41977 paths found :: 15957 paths not in binary cache :: 00000 paths in queue
+ wrote index of 21,621,061 bytes
# we use the power of nix-locate to find the packages which contain the file:
$ nix-locate --minimal --top-level -w lib/libsane.so.1
saneBackends.out
$ nix-locate --minimal --top-level -w lib/libQt5Svg.so.5
remarkable2-toolchain.out
remarkable-toolchain.out
libsForQt5.qt5.qtsvg.out
onlyoffice-bin.out
$ nix-locate --minimal --top-level -w lib/libQt5PrintSupport.so.5
robo3t.out
libsForQt5.qt5.qtbase.out
$ nix-locate --minimal --top-level -w lib/libstdc++.so.6
# ...
# libsdtc++.so.6 is `special`, it resides in stdenv.cc.cc.lib (see other packages)
The next step is to create a library path for all these packages. We use nix-repl to resolve the paths:
$ nix repl '<nixpkgs>'
# .out can be omitted because this is the default output for all packages
# makeLibraryPath outputs the correct path for each package to use as rpath
nix-repl> with pkgs; lib.makeLibraryPath [ sane-backends libsForQt5.qt5.qtbase libsForQt5.qt5.qtsvg stdenv.cc.cc.lib ]
"/nix/store/7lbi3gn351j4hix3dqhis58adxbmvbxa-sane-backends-1.0.25/lib:/nix/store/0990ls1p2nnxq6605mr9lxpps8p7qvw7-qtbase-5.9.1/lib:/nix/store/qzhn2svk71886fz3a79vklps781ah0lb-qtsvg-5.9.1/lib:/nix/store/snc31f0alikhh3a835riyqhbsjm29vki-gcc-6.4.0-lib/lib"
Let’s try out the path we generated:
$ patchelf --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" --set-rpath /nix/store/7lbi3gn351j4hix3dqhis58adxbmvbxa-sane-backends-1.0.25/lib:/nix/store/0990ls1p2nnxq6605mr9lxpps8p7qvw7-qtbase-5.9.1/lib:/nix/store/qzhn2svk71886fz3a79vklps781ah0lb-qtsvg-5.9.1/lib:/nix/store/snc31f0alikhh3a835riyqhbsjm29vki-gcc-6.4.0-lib/lib masterpdfeditor4
$ ./masterpdfeditor4
# SUCCESS!!!
You can learn much of this by reading the source of other nix derivations and flakes, its all written uniformly in nix.