Cross compiling to mingw, missing libraries

Hi, I’m trying to cross compile a program I’ve written in rust to windows using mingw.

I’m using rust-overlay and crane to build the package and it works fine for native target. For mingw, the only thing I change is the rust toolchain (so it has the x86_64-pc-windows-gnu target) and the following dependencies for mingw.

          depsBuildBuild = [
          CARGO_BUILD_TARGET = "x86_64-pc-windows-gnu";
          # Other build inputs and native build inputs for the derivation

Here is the error

# Many undefined symbols before...
          /nix/store/fm1xq4f3g48k1mmwl6qg9jc3ipgfsk1x-binutils-2.40/bin/ld: /nix/store/37bblyshfb9f35f1c036rvxawqr7wj24-pthreads-w32-x86_64-w64-mingw32-2.9.1/lib/libpthread.a(pthread.o):(.text+0xf68b): undefined reference to `__imp__endthreadex'
          /nix/store/fm1xq4f3g48k1mmwl6qg9jc3ipgfsk1x-binutils-2.40/bin/ld: BFD (GNU Binutils) 2.40 assertion fail reloc.c:8632
          /nix/store/37bblyshfb9f35f1c036rvxawqr7wj24-pthreads-w32-x86_64-w64-mingw32-2.9.1/lib/libpthread.a(pthread.o):(.pdata+0x0): dangerous relocation: /nix/store/ygi2qf01dyx5015lqlqshrdh9gqijkxa-gcc-wrapper-12.3.0/bin/ld: line 269:  1160 Segmentation fault      (core dumped) /nix/store/fm1xq4f3g48k1mmwl6qg9jc3ipgfsk1x-binutils-2.40/bin/ld ${extraBefore+"${extraBefore[@]}"} ${params+"${params[@]}"} ${extraAfter+"${extraAfter[@]}"}
          collect2: error: ld returned 139 exit status

  = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see

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

Am I using the wrong library for pthread?

After searching a bit, I’ve found the information here. It seems using CARGO_TARGET_X86_64_PC_WINDOWS_GNU_RUSTFLAGS instead of depsBuildBuild for pthreads was the solution. So instead we use

          depsBuildBuild = [
            "-L native=${}/lib";

          CARGO_BUILD_TARGET = "x86_64-pc-windows-gnu";

I’ll mark as resolved since the problem is fixed, but if anyone understands why it wasn’t working or the reasoning on why it works with the env variable but not the dependency, I would love to understand the underlying issue.