Packaging: link to libs provided by same package

First of all: I don’t have much experience building cpp, using cmake and Im not even sure Im using the right terms here.

Second of all: I’ve tried to google and read manual, but due to lack of expertise was not able to find a solution, and I’m sorry if this is a very commonly asked, or very obvious question.

Now to the problem: I’m trying to practice nix packaging. I’ve picked some cpp repo that has cmake config, and has relatively clear list of dependencies.

I’ve managed to make a nix derivation that compiles it.
But it seems that as part of installing process, this repo installs some libs, and the main binary executable depends on them. I can see with ldd that those libraries are not resolved for the produced binary, and the built .so files are not present in lib dir in the store.

so my question is - how do I approach debugging this? I need to find missing .so files that were built, and make executable link (is it the correct term?) with them at runtime.

the case i have there is a binary executable produced, and it expects to find it’s own libs, that are built in /share/xstudio/lib directory

what i’m currently doing is viciously patchelfing every .so file with something like

    # patch executable
    patchelf \
      --add-rpath '$ORIGIN/../share/xstudio/lib' \
    # patch libs
    for f in $out/share/xstudio/lib/*.so; do
      patchelf --add-rpath '$ORIGIN' $f

does this seem like a valid way to go?