Preventing "No such file or directory" error for compiled binary

Many of us are familiar with the “No such file or directory” error that comes up when you try to run a downloaded binary on NixOS. For the binary in my case it is because the path to the interpreter /lib64/ld-linux-x86-64.so.2 does not exist on NixOS.

I am producing the binary, so my questions is: can I specify the interpreter in a way that will work on most Linux operating systems and NixOS?

For my use case I can not use static linking.

You cannot use dynamic linking on NixOS. There is no static path to an interpreter and no dynamic libraries it could find.

You must statically link at least up to the point where you create a user namespace with dynamic linker, libraries etc. in it a la AppImage.

I’m not sure quite what you’re aiming for here. Why is it so important to use the exact same binary on nixos and non-nixos? what are the exact parameters of the non-nixos case? I assume that in the non-nixos case you’re also not running it from the nix store, so there are actually probably a lot of differences between the nixos version and the non-nixos version that are important. I doubt using the same binary for both is feasible anyway, interpreter aside. If you are fine with running it from the nix store, then the interpreter will be part of the closure and will be deployed with the program, so it will work fine on non-nixos too.