Repository navigation
Description
Describe the bug
I am encountering an issue when cross-compiling with a Windows library installed, libpthread in this example. GCC is incorrectly picking up the Windows library instead of the system one.
I came across this in the context of a Rust project with a build.rs file. First, the build file has to be compiled for and executed on the host platform with GCC. Afterward, the rest of the project can be compiled for the target with MinGW.
NIX_LDFLAGS contains the Windows library when it's installed, which causes GCC to error because it doesn't obviously understand the format. If I manually remove it from the environment, then MinGW doesn't know where to pick up the library and the build fails.
Steps To Reproduce
Steps to reproduce the behavior:
- Clone https://github.com/kognise/crosscomp-mcve
- Run
nix-shell --pure - Run
cargo clean && cargo build --target x86_64-pc-windows-gnu
Expected behavior
I'm relatively new to Nix, so I honestly don't know what the right solution to this is.
The expected behavior from a developer's perspective is simply for GCC to find the Linux libpthread and for MinGW to find the Windows version so that everything can compile nicely.
Screenshots
Additional context
I would just like to mention that this isn't a problem with Rust exclusively, or the overlay I'm using. This is just the simplest way I've been able to reproduce it, and how I came across the issue during real-world development.
Notify maintainers
Not sure who I should specifically ping for this, so apologies if I incorrectly mentioned you.
@matthewbauer (from MinGW derivation history)
@madjar @cstrahan @globin @Havvy (from rustc maintainers, probably didn't need to ping all of you so sorry)
Metadata
- system:
"x86_64-linux" - host os:
Linux 5.10.67, NixOS, 21.05.3367.fd8a7fd07da (Okapi) - multi-user?:
yes - sandbox:
yes - version:
nix-env (Nix) 2.3.15 - channels(root):
"nixos-21.05.3367.fd8a7fd07da" - channels(kognise):
"nixos-21.05-21.05.3367.fd8a7fd07da" - nixpkgs:
/nix/var/nix/profiles/per-user/root/channels/nixos
Maintainer information:
# a list of nixpkgs attributes affected by the problem
attribute:
# a list of nixos modules affected by the problem
module:

Activity
nixos-discourse commentedon Sep 29, 2021
This issue has been mentioned on NixOS Discourse. There might be relevant details there:
https://discourse.nixos.org/t/cross-compile-rust-for-windows/15259/3
lammermann commentedon Oct 6, 2021
I had a similar problem in one of my projects. The advice in this link helped me nix-community/naersk#181 (comment) . Hope it works for you too.
stale commentedon Apr 17, 2022
I marked this as stale due to inactivity. → More info
zopsicle commentedon Jan 17, 2023
A way to work around this is to not put
windows.pthreadsin the build inputs of the shell, but to setCARGO_TARGET_X86_64_PC_WINDOWS_GNU_RUSTFLAGS. This will tell Cargo to tell rustc about the directory containinglibpthread.afor Windows, but only when actually building a crate for Windows.nixos-discourse commentedon Aug 17, 2023
This issue has been mentioned on NixOS Discourse. There might be relevant details there:
https://discourse.nixos.org/t/cross-compiling-to-mingw-missing-libraries/31849/2
RossSmyth commentedon Feb 15, 2025
This is still a really annoying issue :(
The way I solved it is by doing
RUSTFLAGS = libs.optional stdenv.hostPlatform.isMinGW "-L native=${windows.pthreads}/lib";This line
nixpkgs/pkgs/build-support/rust/build-rust-package/default.nix
Line 171 in 8bb3716
Doesn't work. I do not know why.
pinage404 commentedon Feb 15, 2025
i have been able to build Rust targeting Windows with this
https://gitlab.com/pinage404/git-gamble/-/blob/ac671d9fa2bd880e2adb596718facdb5f0519cb3/nix/packages/container-image-rust-windows/default.nix#L27
RossSmyth commentedon Feb 25, 2025
Yeah that should be equivalent, but really one shouldn't have to do these workarounds. They are pervasive if you look around nixpkgs and github just to get around this issue.
Sewer56 commentedon Oct 20, 2025
It would be ideal if such workaround could be avoided- that is true, unfortunately.
I guess it's just another case of
Or however you want to phrase it.
RossSmyth commentedon Feb 26, 2026
If anyone see this issue again, please, please let us know! We are trying to figure out what is going on here!
env.RUSTFLAGS= "-Clink-arg=-v -Wlinker-messages"and upload the full build log, nix source code, and cli command used.