adhoc executable patching on nixos

[ 2020-03-16 ¦ 2020-03-14 ]

AKA 'one weird trick' for getting that ELF executable someone just handed you working soon™ (don't depend on this often)

Create a shell.nix file that looks like:

  pkgs = import <nixpkgs> {};
in pkgs.mkShell {
  nativeBuildInputs =
    (with pkgs; [

      # example libraries you might want:

Launch the nix-shell, patch the linker so you can call the executable and see what's up:

$ nix-shell shell.nix
$ patchelf --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" /path/to/executable

Strategies to find out what you might need:

$ ldd /path/to/executable
$ strace /path/to/executable

Update the shell.nix file with the dependencies/linked libraries your executable might need, and then patch the executable from the derived nix-shell based on that:

$ nix-shell ./shell.nix
$ # you may want to peek at new_rpath here or mangle NIX_LDFLAGS yourself
$ new_rpath=$(echo "$NIX_LDFLAGS" | tr ' ' $'\n' | grep "^-L" | sed -E 's/^-L/:/' | tr -d $'\n')
$ patchelf --set-rpath "$new_rpath" /path/to/executable

Then you should be able to run the executable by calling it.

[ Index ¦ Root ¦ Source ]