Debug/error message strings causing dependencies for static binary

I’m building a static Rust binary using naersk, and when including rust-std as a component (for rust-analyzer support) it causes some panic messages to include the /nix/store path for rustc, which I think is causing Nix to erroneously identify rustc as a runtime dependency of the binary.

That then causes the docker image to contain rustc and all of its dependencies.

What is the best way to avoid this?

I’m thinking I’ll just patch the binary with a horrible sed hack, but I’m not sure how to do that from a nix flake using naersk.

Thanks!

For anyone else experiencing this issue, here is a hackaround:

          packages.x86_64-unknown-linux-musl = naersk-lib.buildPackage {
            src = ./.;
            nativeBuildInputs = with pkgs; [ pkgsStatic.stdenv.cc ];
            CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl";
            CARGO_BUILD_RUSTFLAGS = "-C target-feature=+crt-static";
            CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER = with pkgs.pkgsStatic.stdenv;
              "${cc}/bin/${cc.targetPrefix}gcc";
            postInstall = ''
  sed -i "s|${toolchain}|${nixpkgs.lib.strings.toUpper "${toolchain}"}|g" $out/bin/diskre
'';
          };

The postInstall patches the binary with sed, by changing all strings referencing the toolchain to the upper-case version. This causes nix to no longer identify the toolchain as a dependency without breaking the ELF.

This does of course not work in the general case, your binary should be static and you have to confirm that these references to the toolchain are only present in debug/panic messages.