I have the minimal reproduction here using the following flake definition:
{
inputs = {
nixpkgs.url = github:NixOS/nixpkgs/nixpkgs-unstable;
naersk = {
url = github:nmattia/naersk;
inputs.nixpkgs.follows = "nixpkgs";
};
fenix = {
url = github:nix-community/fenix;
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils.url = github:numtide/flake-utils;
};
outputs = { self, nixpkgs, naersk, fenix, flake-utils }:
flake-utils.lib.eachDefaultSystem (
system: let
pkgs = nixpkgs.legacyPackages.${system};
manifest = (pkgs.lib.importTOML ./Cargo.toml).package;
toolchain = fenix.packages.${system}.fromToolchainFile {
file = ./rust-toolchain.toml;
sha256 = "sha256-vMlz0zHduoXtrlu0Kj1jEp71tYFXyymACW8L4jzrzNA=";
};
naersk-lib = naersk.lib.${system}.override {
cargo = toolchain;
rustc = toolchain;
};
rev = self.rev or self.dirtyRev;
in
rec {
defaultPackage = packages.x86_64-unknown-linux-musl;
packages.docker = pkgs.dockerTools.buildImage {
name = manifest.name;
tag = "v" + manifest.version + "-g" + rev;
config = {
Cmd = [ "${defaultPackage}/bin/${manifest.default-run}" ];
WorkingDir = "/";
Volumes = { "/data" = { }; };
};
};
packages.x86_64-unknown-linux-gnu = naersk-lib.buildPackage {
src = ./.;
};
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";
};
devShell = pkgs.mkShell {
nativeBuildInputs = [ toolchain ];
};
}
);
}
When building without the flake it works, but with nix develop
I get the following error:
Compiling libc v0.2.169
error: failed to run custom build command for `libc v0.2.169`
Caused by:
process didn't exit successfully: `/home/sanoj/dev/buildscripttest/target/debug/build/libc-b19ddf8e2d903ce4/build-script-build` (exit status: 101)
--- stdout
cargo:rerun-if-changed=build.rs
--- stderr
thread 'main' panicked at /home/sanoj/.local/share/cargo/registry/src/index.crates.io-6f17d22bba15001f/libc-0.2.169/build.rs:143:9:
failed to run rustc: rustc: error while loading shared libraries: libLLVM.so.19.1-rust-1.84.1-stable: cannot open shared object file: No such file or directory
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
The libc crate has a build scipt that itself runs rustc
in order to determine the rust version, this is where the error is happening. But I don’t understand why it’s suddenly not finding libraries, rustc is obviously working when executed by cargo (otherwise the build script wouldn’t even run) but the second invocation fails with that dynamic linking error.
If I set LD_LIBRARY_PATH=/nix/store/smi722ljb0fz5ipnba20wdw5gm17ls7r-rustc-1.84.1-2025-01-30/lib
I get this error:
Compiling libc v0.2.169
error[E0463]: can't find crate for `core`
|
= note: the `x86_64-unknown-linux-gnu` target may not be installed
= help: consider downloading the target with `rustup target add x86_64-unknown-linux-gnu`
For more information about this error, try `rustc --explain E0463`.
error: could not compile `libc` (lib) due to 1 previous error
Which suggests to me that for some reason the build script is picking up the rustc -> rustup
link, how could this happen?