Unrelatable error when working with patchelf

{ lib
, stdenv
, autoPatchelfHook
, udev
, cups
, ...
}:

let self = stdenv.mkDerivation rec {
  pname = "fudo_native_app_linux";
  version = "none";

  src = ./.;  # This is an unpacked https://fudo-downloads.s3-sa-east-1.amazonaws.com/fudo_native_app_linux.tar.gz

  nativeBuildInputs = [
    autoPatchelfHook
  ];

  buildInputs = [
    stdenv.cc.cc.lib
    udev
    cups
  ];

  installPhase = ''
    mkdir -p $out/bin

    cp $src/fudo_native_extension $out/bin/
    cp $src/*.node                $out/bin/
  '';

  passthru = let
    path = "${self}/bin/fudo_native_extension";
    chromeManifest =  builtins.fromJSON (
      lib.fileContents ./do.fu.native_extension_chrome.json);
    firefoxManifest =  builtins.fromJSON (
      lib.fileContents ./do.fu.native_extension_firefox.json);
  in {
    chromeNativeMessagingHostManifest = chromeManifest // { inherit path; };
    firefoxNativeMessagingHostManifest = firefoxManifest // { inherit path; };
  };

  meta = with lib; {
    description = "The native browser extension for fu.do Restaurant System";
    homepage = "https://fu.do/";
    license = licenses.unfree;
    maintainers = [ maintainers.blaggacao ];
    platforms = platforms.linux;
  };
}; in self

When running the resulting binary ($out/bin/fudo_native_extension), I get:

Pkg: Error reading from file.

$ tree $out
└── bin
    β”œβ”€β”€ bindings.node
    β”œβ”€β”€ fudo_native_extension
    β”œβ”€β”€ node_printer.node
    └── usb_bindings.node

Neither google nor grep vs nixpkgs knows anything about this error.

What cloud I do next?

I tried your derivation on my machine and I think I have found the problem though I don’t know how to solve it at the moment.

I tried running $out/bin/fudo_native_extension with strace (I just used the command strace $out/bin/fudo_native_extension) and that told me that the program opens itself and tries to read some bytes at the end of the executable that fails with EOF. I then looked at the size of the downloaded version of fudo_native_extension and the one in $out/bin/fudo_native_extension and the one made with the derivation is smaller.

From looking at the end of fudo_native_extension it looks to be a packaged node app with the JavaScript appended to the end of the executable and it seems that patchelf is stripping that data.

I fiddled around with patchelf manually and now get the same error as here:
https://github.com/vercel/pkg/issues/321

And that has a link to how the same issue was solved for now-cli
https://github.com/NixOS/nixpkgs/pull/48193/files#diff-329ce6280c48eac47275b02077a2fc62R25

First you need to add dontStrip = true; to your derivation since that is what is deleting the JavaScript sources and then you need to somehow fix the offset like it was fixed for now-cli

1 Like

Wow! I’m blown away by your analysis & walk-through. Thank you! I’ll try to conquer the task from here on.