Cross-compile Rust for Windows

I’m trying to cross-compile some Rust code from NixOS to Windows with MinGW.

I have a cursed-feeling issue:

  • When I attempted to build my Rust project, it was linked with x86_64-w64-mingw32-gcc as expected. I got the error cannot find -l:libpthread.a, so I added the library to my buildPackages.
  • After adding the library, for some reason Rust switches to cc for linking. This time, the error I get is libpthread.a: error adding symbols: file format not recognized because it’s using the wrong linker.

I think it’s an issue with pkg-config, but not sure what. I tried adding pkg-config itself to my buildPackages but it fails to build.

Here’s my shell.nix after adding the library:

let
  unstable = import (fetchTarball https://nixos.org/channels/nixos-unstable/nixexprs.tar.xz);

  pkgs = unstable {
    crossSystem = { config = "x86_64-w64-mingw32"; };
    overlays = [
      (import (builtins.fetchTarball "https://github.com/oxalica/rust-overlay/archive/master.tar.gz"))
    ];
  };

  rust = pkgs.buildPackages.rust-bin.stable.latest.default.override {
    extensions = [ "rust-src" "rust-analysis" ];
    targets = [ "x86_64-pc-windows-gnu" ];
  };
in pkgs.mkShell { 
  nativeBuildInputs = with pkgs; [
    rust
  ];

  buildInputs = with pkgs; [
    windows.mingw_w64_pthreads
    windows.pthreads
  ];
}

To reproduce it you’ll also need a dependency like crc32fast = "1.2.0".

I’m not sure what next steps to take. Thanks!

I did some more experimenting. It looks like it isn’t the dependency’s fault either, simply creating a build.rs and adding fn main() {} triggers the issue.

1 Like

https://github.com/NixOS/nixpkgs/issues/139966