NixOS + Rust `/usr/bin/clang` not found

Hello, so far enjoyed NixOS, seeking for help:

Recently I tried to setup Rust development environment in my NixOS and when compiling the project with cargo run, I get error: linker `/usr/bin/clang` not found in my nix-shell.

which clang returns the correct path of clang in nix store.

I wonder why rust, or some rust modules are using the absolute path /usr/bin/clang to find clang in my case. And if it’s rust or some rust modules’ problem what is the workaround. (like adding a link to clang in /usr/bin manually, or some nix functions to create it for me?)

BTW, I tried rustup and moz overlay for rust installation, both have this problem.

I don’t think Rust would default to “clang” at all, but rather to cc / ld, which will often point to GCC instead. However, we do not know what project you are compiling. If a particular crate’s build scripts expect /usr/bin/clang, you will unfortunately need to adjust that crate.

1 Like

There are a number of issues on nixpkgs’ github repo linked to this seems in connection mostly to building to webassembly and/or building on macOS, some fixed and some still open. Have you had a look at them?

Any chance you could share the repository you’re working on? I’ve run into this with amethyst before, usually the project’s build.rs will be what’s doing things like hard-coding paths to binaries and libraries.

1 Like

I use Rust on NixOS, maybe I can help you. Can you copy the dependencies of your project?

Thanks for the help!!

shell.nix

let
  moz_overlay = import (builtins.fetchTarball https://github.com/mozilla/nixpkgs-mozilla/archive/master.tar.gz);
  nixpkgs = import <nixpkgs> { overlays = [ moz_overlay ]; };
in
  with nixpkgs;
  stdenv.mkDerivation {
    name = "moz_overlay_shell";
    buildInputs = [
      nixpkgs.latest.rustChannels.nightly.rust
      clang llvm llvmPackages.libclang lld # lld is recommended from bevy
      pkgconfig udev alsaLib lutris # from this line and below are dependencies of bevy
      x11 xorg.libXcursor xorg.libXrandr xorg.libXi
      vulkan-tools vulkan-headers vulkan-loader vulkan-validation-layers
    ];
    shellHook = ''
      export LIBCLANG_PATH="${pkgs.llvmPackages.libclang}/lib"; // I searched this discourse and added this but still failing.
    '';
  }

Cargo.toml

[package]
name = "my_bevy_game"
version = "0.1.0"
edition = "2018"

[dependencies]
bevy = { version = "0.5.0"}

The error:

error: linker `/usr/bin/clang` not found
  |
  = note: No such file or directory (os error 2)

error: could not compile `serde_derive` due to previous error

(Sometimes it’s libm)

The weird thing is that once ```/usr/bin/clang` not found`` happens once, it will happen all the time no matter how I change the dependencies. (I changed it to no dependencies the error still happens)

It doesn’t looks like particular problem in some crates though, cargo build --verbose has

     Running `rustc --crate-name build_script_build --edition=2018 /home/lliu/.cargo/registry/src/github.com-1ecc6299db9ec823/libm-0.2.1/build.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 --cfg 'feature="default"' -C metadata=1f5ec41f42fa5445 -C extra-filename=-1f5ec41f42fa5445 --out-dir /home/lliu/personal/my_bevy_game/target/debug/build/libm-1f5ec41f42fa5445 -C linker=/usr/bin/clang -L dependency=/home/lliu/personal/my_bevy_game/target/debug/deps --cap-lints allow -Clink-arg=-fuse-ld=lld -Zshare-generics=y`

-C linker=/usr/bin/clang is in every line.

Alright, I found the reason, it’s my own bad.

The bevy tutorial recommend a rust config and it’s setting the linker path.

I’m good to go on now, thanks for all the help!

2 Likes