Executables with shared libraries fail to run with err "no such file or directory"

$ type -a gcc
gcc is /home/cql/.nix-profile/bin/gcc
1 Like

It could be useful to run a nested shell in this case:

$ LD_DEBUG=libs zsh
$ ./main

It will show you the libraries each command will try to load.

heres everything after pressing enter on ./main

zsh: no such file or directory: ./main
     19838:     find library=libc.so.6 [0]; searching
     19838:      search path=/nix/store/apab5i73dqa09wx0q27b6fbhd1r18ihl-glibc-2.39-31/lib/glibc-hwcaps/x86-64-v3:/nix/store/apab5i73dqa09wx0q27b6fbhd1r18ihl-glibc-2.39-31/lib/glibc-hwcaps/x86-64-v2:/nix/store/apab5i73dqa09wx0q27b6fbhd1r18ihl-glibc-2.39-31/lib          (system search path)
     19838:       trying file=/nix/store/apab5i73dqa09wx0q27b6fbhd1r18ihl-glibc-2.39-31/lib/glibc-hwcaps/x86-64-v3/libc.so.6
     19838:       trying file=/nix/store/apab5i73dqa09wx0q27b6fbhd1r18ihl-glibc-2.39-31/lib/glibc-hwcaps/x86-64-v2/libc.so.6
     19838:       trying file=/nix/store/apab5i73dqa09wx0q27b6fbhd1r18ihl-glibc-2.39-31/lib/libc.so.6
     19838:     find library=libgcc_s.so.1 [0]; searching
     19838:      search path=/nix/store/apab5i73dqa09wx0q27b6fbhd1r18ihl-glibc-2.39-31/lib              (system search path)
     19838:       trying file=/nix/store/apab5i73dqa09wx0q27b6fbhd1r18ihl-glibc-2.39-31/lib/libgcc_s.so.1
     19838:      search path=/nix/store/p3ffjixpnfgkqh20nsrc13vrj3yfi0nj-gcc-13.2.0-lib/lib/glibc-hwcaps/x86-64-v3:/nix/store/p3ffjixpnfgkqh20nsrc13vrj3yfi0nj-gcc-13.2.0-lib/lib/glibc-hwcaps/x86-64-v2:/nix/store/p3ffjixpnfgkqh20nsrc13vrj3yfi0nj-gcc-13.2.0-lib/lib               (RUNPATH from file /home/cql/.nix-profile/bin/starship)
     19838:       trying file=/nix/store/p3ffjixpnfgkqh20nsrc13vrj3yfi0nj-gcc-13.2.0-lib/lib/glibc-hwcaps/x86-64-v3/libgcc_s.so.1
     19838:       trying file=/nix/store/p3ffjixpnfgkqh20nsrc13vrj3yfi0nj-gcc-13.2.0-lib/lib/glibc-hwcaps/x86-64-v2/libgcc_s.so.1
     19838:       trying file=/nix/store/p3ffjixpnfgkqh20nsrc13vrj3yfi0nj-gcc-13.2.0-lib/lib/libgcc_s.so.1
     19838:     find library=libm.so.6 [0]; searching
     19838:      search path=/nix/store/apab5i73dqa09wx0q27b6fbhd1r18ihl-glibc-2.39-31/lib              (system search path)
     19838:       trying file=/nix/store/apab5i73dqa09wx0q27b6fbhd1r18ihl-glibc-2.39-31/lib/libm.so.6
     19838:     calling init: /nix/store/apab5i73dqa09wx0q27b6fbhd1r18ihl-glibc-2.39-31/lib/ld-linux-x86-64.so.2
     19838:     calling init: /nix/store/apab5i73dqa09wx0q27b6fbhd1r18ihl-glibc-2.39-31/lib/libc.so.6

Alright, so I found an interesting project nix-alien which allows you to run unpatched binaries.

Inside the project directory, run:

$ nix --extra-experimental-features "nix-command flakes" run github:thiagokokada/nix-alien -- ./main

If that worked, the following command will show you which libs are needed:

$ nix --extra-experimental-features "nix-command flakes" run github:thiagokokada/nix-alien#nix-alien-find-libs -- ./main

I tried this on a random non-NixOS program and it worked nicely.

If after all of this and it still doesn’t run, I think this might be an issue with the project itself or something else like a permission error…

1 Like

oh my god, it works, tysm!
also running the second command doesnt output anything… oddly.

1 Like

When you run the first command, does it show you a list of libraries and asks you to choose or does it just launch the program?

no it just launches the program

*i wrote dependency instead of library

Try running the next command:

$ nix --extra-experimental-features "nix-command flakes" run github:thiagokokada/nix-alien#nix-alien-ld -- ./main

It will create a cached shell in '~/.cache/nix-alien and give you its path. When you open it, you should see the libraries it loads in NIX_LD_LIBRARY_PATH:

{ pkgs ? import
    (builtins.fetchTarball {
      name = "nixpkgs-unstable-20240509145238";
      url = "https://github.com/NixOS/nixpkgs/archive/f1010e0469db743d14519a1efd37e23f8513d714.tar.gz";
      sha256 = "sha256-doPgfj+7FFe9rfzWo1siAV2mVCasW+Bh8I1cToAXEE4=";
    { }

  inherit (pkgs) lib stdenv;
  NIX_LD_LIBRARY_PATH = with pkgs; lib.makeLibraryPath [
  NIX_LD = lib.fileContents "${stdenv.cc}/nix-support/dynamic-linker";
pkgs.writeShellScriptBin "anki-panky" ''
  export NIX_LD='${NIX_LD}'
  /home/user/Downloads/anki-panky "$@"

For this example, it’s libz and gmp.

nope, totally blank.

  inherit (pkgs) lib stdenv;
  NIX_LD_LIBRARY_PATH = with pkgs; lib.makeLibraryPath [
  NIX_LD = lib.fileContents "${stdenv.cc}/nix-support/dynamic-linker";

How odd… Well, since we’ve gotten this far, we might as well see what is being loaded with LD_DEBUG=libs when it does work:

$ LD_DEBUG=libs nix --extra-experimental-features "nix-command flakes" run github:thiagokokada/nix-alien -- ./main

The log is too long to post here so i created a pastebin.

Thanks, if you could do this as well, what does the following give you?

$ nix-shell -p patchelf --run "patchelf --print-needed ./main"

i added glibc under nix-ld.libraries and it started working, bruh.

1 Like

I was gonna say, those three libraries appear in the following derivations:

$ nix-locate libc.so.6 | grep glibc
(zsnes.out)                                   2,550,384 x /nix/store/80h8wkvzf60012mnkik703ifzc1w1kyl-glibc-2.39-31/lib/libc.so.6
(hare.out)                                    2,122,232 x /nix/store/s5wxag65m62a2bg2lg82jb3nqc42wyi6-glibc-aarch64-unknown-linux-gnu-2.39-31/lib/libc.so.6
(hare.out)                                    1,759,184 x /nix/store/brnz0ga886s09gkmjd0nwhvirjwaph7w-glibc-riscv64-unknown-linux-gnu-2.39-31/lib/libc.so.6
iconv.out                                     2,285,600 x /nix/store/apab5i73dqa09wx0q27b6fbhd1r18ihl-glibc-2.39-31/lib/libc.so.6
glibc_multi.out                                       0 s /nix/store/s4gcddkm2kvb02qnrm2rq22lj8yynqhw-glibc-multi-2.39-31/lib/libc.so.6
glibc_memusage.debug                                  0 s /nix/store/ssai8vvm9hvhjc7i6acpma6fv66dz19a-glibc-gd-2.39-31-debug/lib/debug/libc.so.6
iconv.debug                                           0 s /nix/store/0k9qgiqxjlhh71pj7a2czz33ir3nv720-glibc-2.39-31-debug/lib/debug/libc.so.6
glibc_memusage.out                            2,285,600 x /nix/store/bimkvwhg1wrg4fmzf6aqhy68m8b12q9a-glibc-gd-2.39-31/lib/libc.so.6

I think just installing glibc_multi will just solve the issue.

1 Like

Is this what you had in mind?

programs.nix-ld.libraries = with pkgs; [

I meant just as a normal package, if only to test, but if that doesn’t work then using nix-ld would indeed be the way to go.

1 Like

Thank you, this solution works perfectly. I will now mark this as resolved.

1 Like

Which one solved it, installing as a package or nix-ld?

I installed it as a global package in configuration.nix
environment.systemPackages = with pkgs; [ glibc_multi ];

1 Like