Cross-compiling rust project for windows

Continuation from my previous post: I want to cross-compile a windows binary from my NixOS system. I’ve spent this morning looking at tutorials and previous help posts, but haven’t ended up with anything that works yet. Here’s the repo for the project: https://gitgud.io/neropatti/peanuts

I’m trying to compile with the command cargo build --target x86_64-pc-windows-gnu using this shell.nix:

{ pkgs ? import <nixpkgs> {} }:
  pkgs.mkShell {
    # nativeBuildInputs is usually what you want -- tools you need to run
    nativeBuildInputs = with pkgs.buildPackages; [
        gcc
        #python3
        rust-analyzer
        cargo
        rustfmt
    ];
    LD_LIBRARY_PATH = with pkgs; lib.makeLibraryPath [
      libGL
      libxkbcommon
      wayland
      vulkan-loader
    ];
}

And got this output:

error[E0463]: can't find crate for `core`
  |
  = note: the `x86_64-pc-windows-gnu` target may not be installed
  = help: consider downloading the target with `rustup target add x86_64-pc-windows-gnu`

For more information about this error, try `rustc --explain E0463`.
error: could not compile `equivalent` (lib) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `cfg-if` (lib) due to 1 previous error
error: could not compile `windows-link` (lib) due to 1 previous error
error[E0463]: can't find crate for `std`
  |
  = note: the `x86_64-pc-windows-gnu` target may not be installed
  = help: consider downloading the target with `rustup target add x86_64-pc-windows-gnu`

error: could not compile `bitflags` (lib) due to 1 previous error
error: could not compile `foldhash` (lib) due to 1 previous error
error: could not compile `log` (lib) due to 1 previous error
error: could not compile `allocator-api2` (lib) due to 1 previous error
error: could not compile `unicode-width` (lib) due to 1 previous error
error: could not compile `libm` (lib) due to 1 previous error

I tried using rustup instead and installed the windows toolchain with rustup target add x86_64-pc-windows-gnu, but then got hit with this:

error: error calling dlltool 'x86_64-w64-mingw32-dlltool': No such file or directory (os error 2)

error: could not compile `windows-threading` (lib) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `libloading` (lib) due to 1 previous error
error: could not compile `windows-result` (lib) due to 1 previous error
error: could not compile `windows-strings` (lib) due to 1 previous error

I’m assuming I need to somehow provide mingw (if I choose to use rustup), but I have no idea how. I’m also curious how I would provide the cross-compilation toolchain for cargo without using rustup by specifying things in the shell.nix instead!

Yeah, the default Rust packages only provide for the host’s target. To cross compile, you have to set up the target’s std, linker, and wrap cargo. Without using rustup, something like this should work:

{ pkgs ? import <nixpkgs> {} }:
  pkgs.mkShell {
    # nativeBuildInputs is usually what you want -- tools you need to run
    nativeBuildInputs = with pkgs.buildPackages; [
        gcc
        #python3
        rust-analyzer
+        # Provides windows-gnu std
+        pkgs.pkgsCross.mingwW64.buildPackages.rustc
+        # Wraps host cargo to add path for windows-gnu rustc
+        pkgs.pkgsCross.mingwW64.buildPackages.cargo
-        cargo
        rustfmt
    ];
+    buildInputs = [
+        # Cross compiler/linker
+        pkgs.pkgsCross.mingwW64.buildPackages.gcc
+    ];
+    shellHook = ''
+        export CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER=x86_64-w64-mingw32-gcc
+    '';
+    # Windows std links against libpthread.a, however:
+    # Don't put the mingw pthreads package into buildInputs, because then the
+    # host's native linker will find it and fail
+    CARGO_TARGET_X86_64_PC_WINDOWS_GNU_RUSTFLAGS = "-C link-arg=-L${pkgs.pkgsCross.mingwW64.windows.pthreads}/lib";
    LD_LIBRARY_PATH = with pkgs; lib.makeLibraryPath [
      libGL # Didn't need this to get past the previous error, but this might be needed! Idk!
      libxkbcommon
      wayland
      vulkan-loader
    ];
}

That basically “combines” your native rustc and cargo with the stuff needed for Windows builds. I don’t really understand it well enough to explain how it works, but if you’re feeling brave, you can read the source:

As for using rustup, this should work:

{ pkgs ? import <nixpkgs> {} }:
  pkgs.mkShell {
    # nativeBuildInputs is usually what you want -- tools you need to run
    nativeBuildInputs = with pkgs.buildPackages; [
        gcc
        #python3
        rust-analyzer
-        cargo
        rustfmt
    ];
+    buildInputs = [
+        pkgs.pkgsCross.mingwW64.buildPackages.gcc
+    ];
+    shellHook = ''
+        export CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER=x86_64-w64-mingw32-gcc
+    '';
+    CARGO_TARGET_X86_64_PC_WINDOWS_GNU_RUSTFLAGS = "-C link-arg=-L${pkgs.pkgsCross.mingwW64.windows.pthreads}/lib";
    LD_LIBRARY_PATH = with pkgs; lib.makeLibraryPath [
      libGL # Didn't need this to get past the previous error, but this might be needed! Idk!
      libxkbcommon
      wayland
      vulkan-loader
    ];
}

cargo needs to be removed from nativeBuildInputs or it shadows the cargo added by rustup. Otherwise, the setup is very similar.