Skip to content

Add ability to force overwrite LIBCLANG_PATH for build from nix #407

@skibon02

Description

@skibon02

Bug description

I was getting myself familiar with nix, and faced some issues while compiling esp-idf rust project.

Since using rustup is not a good thing in nix (everything should be declarative and separate tools managing packets or versions are generally discouraged), I've created a flake locally in my project dir, which would prepare clean environment with nightly toolchain ready (cargo and rustc). Therefore I don't have rustup and espup in my build environment.

I've created empty test project and minimal nix configuration, and I am getting compilation error:

  thread 'main' (1217628) panicked at /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bindgen-0.71.1/lib.rs:604:27:
  Unable to find libclang: "couldn't find any valid shared libraries matching: ['libclang.so', 'libclang-*.so', 'libclang.so.*', 'libclang-*.so.*'], set the `LIBCLANG_PATH` environment variable to a path where one of these files can be found (invalid: [])"

After a few hours trying to fix this, trying different configurations and setting env variables, I came up with a solution, but it requires changes in esp-idf-sys crate:

  1. clone esp-idf-sys locally and make changes to function setup_clang_env in build::common:
pub fn setup_clang_env(path: Option<&Path>) -> Result<()> {
    // If the user already set LIBCLANG_PATH, respect it and skip auto-detection
    if std::env::var_os("LIBCLANG_PATH").is_some() {
        return Ok(());
    }

    // ...
  1. modify flake.nix

From

    devShells.x86_64-linux.default =
      let pkgs = nixpkgs.legacyPackages.x86_64-linux;
      in pkgs.mkShell {
        buildInputs = [ pkgs.rustup pkgs.ldproxy ];
        shellHook = "rustup show";
      };

to

    devShells.x86_64-linux.default =
      let pkgs = nixpkgs.legacyPackages.x86_64-linux;
      in pkgs.mkShell {
        buildInputs = [ pkgs.rustup pkgs.ldproxy ];
        packages = [
            pkgs.llvmPackages.libclang
        ];
        env.LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
      };

And after this project builds successfully.

  • Would you like to work on a fix? y

To Reproduce

  1. Clone https://github.com/skibon02/esp-nix-issue
  2. Install nix package manager
  3. In project directory run nix develop and cargo build -r

Expected behavior

Build without errors

Environment

  • Crate (esp-idf-sys) version: 0.37.1
  • ESP-IDF branch or tag: 5.4.3
  • Target device (MCU): esp32c6
  • OS: Linux Mint 22.2, Nix package manager with environment from flake

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions