Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impossible to use clang to build rust packages #303796

Open
ninegua opened this issue Apr 13, 2024 · 12 comments
Open

Impossible to use clang to build rust packages #303796

ninegua opened this issue Apr 13, 2024 · 12 comments
Labels
6.topic: llvm/clang Issues related to llvmPackages, clangStdenv and related 6.topic: rust significant Novel ideas, large API changes, notable refactorings, issues with RFC potential, etc.

Comments

@ninegua
Copy link
Contributor

ninegua commented Apr 13, 2024

Can we just say that use setEnv to force set environment variables before the cargo command is a bad idea?

@setEnv@ cargo build -j $NIX_BUILD_CORES \

It forces HOST_CC to be set to gcc even when stdenv is switched to llvm, for example the code below would still run gcc when building native packages because of the HOST_CC setting, which would lead to linking failures:

makeRustPlatform {
        rustc = self.rust-stable;
        cargo = self.rust-stable;
        stdenv = self.llvmPackages.libcxxStdenv;
}

Note that this problem does not exist with Nixpkgs 23.05. It only becomes a problem after the setEnv approach is adopted in 23.11 and newer.

In general, we want environment variables to be configurable. But this setEnv thing is just very difficult to override. Would appreciate suggestions on how to override HOST_CC, or getting rid of setEnv completely. Thanks!

@lf- lf- added significant Novel ideas, large API changes, notable refactorings, issues with RFC potential, etc. 6.topic: llvm/clang Issues related to llvmPackages, clangStdenv and related labels May 26, 2024
@RossComputerGuy
Copy link
Member

Is this still a problem? Have you tried pkgsLLVM?

@ninegua
Copy link
Contributor Author

ninegua commented Oct 11, 2024

It is still a problem. It is pretty clear where the problem is, namely, setEnv is too rigid and can't be overridden.

@RossComputerGuy
Copy link
Member

Ok, how have you tried setting the CC? Have you tried overriding rustc's stdenv to clangStdenv?

@ninegua
Copy link
Contributor Author

ninegua commented Oct 11, 2024

Ok, how have you tried setting the CC? Have you tried overriding rustc's stdenv to clangStdenv?

If you read the source code of setEnv, it gets CC from environment, but it gets HOST_CC from pkgsBuildHost.stdenv.cc, which is not something a user can easily have control over.

@RossComputerGuy
Copy link
Member

I am reading the source code which is why I have been asking if you have tried changing the stdenv.

@ninegua
Copy link
Contributor Author

ninegua commented Oct 11, 2024

Yes, I tried to pass a different stdenv as argument to makeRustPlatform, but it wasn't effective.

@RossComputerGuy
Copy link
Member

makeRustPlatform isn't what needs to have stdenv overridden. pkgs/development/compilers/rust/default.nix imports pkgs/build-support/rust/lib/default.nix which the rust lib takes in stdenv. Which the rust default file is imported from pkgs/development/compilers/rust/1_80.nix.

@ninegua
Copy link
Contributor Author

ninegua commented Oct 11, 2024

I don't know, but using pkgsLLVM is too much of a heavy lifting to solve a local environment variable problem. But thanks for the suggestion though!

@RossComputerGuy
Copy link
Member

Yeah, these are two of the best (probably only) options. I suggest pkgsLLVM since it gives you an environment that is purely LLVM on target. Overriding a single package to use LLVM will result in an LLVM + GCC environment. If you have any issues with clangStdenv when doing this, I suggest giving pkgsLLVM a shot.

@emilazy
Copy link
Member

emilazy commented Oct 11, 2024

The Rust build infrastructure should be fixed, though.

@RossComputerGuy
Copy link
Member

@emilazy True, pkgs/development/compilers/rust/default.nix could be callPackaged and pkgs/development/compilers/rust/1_80.nix could just inherit it and pass through stdenv.

@reckenrode
Copy link
Contributor

Rust and setEnv is what motivated #346043. Prior to that PR, it was impossible to override the SDK version of Rust packages that needed to build non-Rust code (particularly some -sys crates) because setEnv pulled in other clangs with the wrong SDK version (e.g., #239384).

Some of my early attempts to work around it involved turning makeRustPlatform into a fixed-point and allowing rustPlatform to be overridden, but it uses pkgsBuildHost, pkgsBuildTarget, and pkgsTargetTarget depending on the cross-compilation scenario. Just overriding the stdenv may not be enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: llvm/clang Issues related to llvmPackages, clangStdenv and related 6.topic: rust significant Novel ideas, large API changes, notable refactorings, issues with RFC potential, etc.
Projects
None yet
Development

No branches or pull requests

6 participants