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

Issue cross-compiling to Windows from NixOS #52

Closed
kognise opened this issue Sep 28, 2021 · 10 comments
Closed

Issue cross-compiling to Windows from NixOS #52

kognise opened this issue Sep 28, 2021 · 10 comments

Comments

@kognise
Copy link

kognise commented Sep 28, 2021

I'm trying to cross-compile a Rust project to Windows with MinGW. I'm getting the ungoogleable error "rust is no available" and I'm not sure what next steps to take.

This is the smallest shell.nix that can reproduce the problem:

let
  pkgs = import <nixpkgs> {
    crossSystem = { config = "x86_64-w64-mingw32"; };
    overlays = [
      (import (builtins.fetchTarball "https://github.com/oxalica/rust-overlay/archive/master.tar.gz"))
    ];
  };
in pkgs.mkShell { 
  nativeBuildInputs = [
    pkgs.buildPackages.rust-bin.stable.latest.rust
  ];
}

And the error message:

error: while evaluating the attribute 'nativeBuildInputs' of the derivation 'nix-shell-x86_64-w64-mingw32' at /nix/store/0qm06jcncxkn0ds17pawl0kpbj1m0cyi-nixos-21.05.3314.83413f47809/nixos/pkgs/stdenv/generic/make-derivation.nix:201:11:
while evaluating the attribute 'paths' of the derivation 'rust-1.55.0' at /nix/store/0qm06jcncxkn0ds17pawl0kpbj1m0cyi-nixos-21.05.3314.83413f47809/nixos/pkgs/stdenv/generic/make-derivation.nix:201:11:
while evaluating 'getComponents' at /nix/store/dspngfrg375swrfj5sbcags7chd01bbc-source/rust-overlay.nix:176:81, called from /nix/store/dspngfrg375swrfj5sbcags7chd01bbc-source/rust-overlay.nix:426:26:
while evaluating 'flatten' at /nix/store/0qm06jcncxkn0ds17pawl0kpbj1m0cyi-nixos-21.05.3314.83413f47809/nixos/lib/lists.nix:137:13, called from /nix/store/dspngfrg375swrfj5sbcags7chd01bbc-source/rust-overlay.nix:183:19:
while evaluating 'getTargetPkgTuples' at /nix/store/dspngfrg375swrfj5sbcags7chd01bbc-source/rust-overlay.nix:131:64, called from /nix/store/dspngfrg375swrfj5sbcags7chd01bbc-source/rust-overlay.nix:183:28:
while evaluating 'getExtensions' at /nix/store/dspngfrg375swrfj5sbcags7chd01bbc-source/rust-overlay.nix:109:34, called from /nix/store/dspngfrg375swrfj5sbcags7chd01bbc-source/rust-overlay.nix:136:20:
while evaluating the attribute 'target.x86_64-w64-windows-gnu' at /nix/store/dspngfrg375swrfj5sbcags7chd01bbc-source/manifest.nix:67:9:
while evaluating the attribute 'target."*"' at /nix/store/dspngfrg375swrfj5sbcags7chd01bbc-source/manifest.nix:67:9:
rust is no available
@oxalica
Copy link
Owner

oxalica commented Sep 28, 2021

It's an issue on nixos 21.05 that rust.toRustTarget works improperly for mingw targets. It works on nixpkgs master and unstable.

The expr rust.toRustTarget pkgsCross.mingwW64.hostPlatform returns "x86_64-pc-windows-gnu" on nixpkgs master and unstable, but on stable 21.05, it returns incorrect "x86_64-w64-windows-gnu".

I opened a backport PR for 21.05. You can checkout and apply it on your nixpkgs. NixOS/nixpkgs#139697

@oxalica oxalica closed this as completed Sep 28, 2021
@oxalica
Copy link
Owner

oxalica commented Sep 28, 2021

Also note that the component rust is deprecated and may be removed in the future. You should already see an warning.
Please use profile name like default instead, which is pkgs.buildPackages.rust-bin.stable.latest.default.

@kognise
Copy link
Author

kognise commented Sep 28, 2021

Thank you-- after a bit more research I thought toRustTarget might've been the issue and saw the commit fixing it, but after switching channels the problem was still there. I may have somehow just switched incorrectly.

And yeah, I know rust is deprecated but it was the simplest option while debugging. Thank you for the backport PR, and I appreciate the help!

@kognise
Copy link
Author

kognise commented Sep 28, 2021

I'm sorry, I have a bit of a dumb question.

Apologies for bothering you, and feel entirely free not to answer as I'm aware this is extremely out of scope for this project!

Attempting to build for Windows gives the MinGW error that it cannot find -l:libpthread.a. So I added the respective packages, and while the library seems to be installed correctly, along with them comes Rust trying to use the wrong linker to build! The default cc obviously can't read the Windows format, so it errors.

What I don't understand is how it's even possible for a library to change the linker used.

My shell.nix:

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
  ];
}

@oxalica
Copy link
Owner

oxalica commented Sep 28, 2021

My shell.nix:

I use your shell.nix and cargo build --target x86_64-pc-windows-gnu successfully built the windows PE file for a simple hello world program.
Are you missing the target flag? Or maybe you specify the wrong linker in .cargo/config or $CARGO_HOME/config? It should work without any config.

Also if you specify the crossSystem for nixpkgs. You don't need to override targets with that target again. It will be automatically added.

@kognise
Copy link
Author

kognise commented Sep 28, 2021

I'm sorry, I forgot to include important information. Adding crc32fast = "1.2.0" to the dependencies triggers the issue when it builds. This is just a dependency of a different package I'm using but it's a smaller example.

And that's awesome, removed it!

@kognise
Copy link
Author

kognise commented Sep 28, 2021

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.

@kognise
Copy link
Author

kognise commented Sep 29, 2021

I believe I narrowed it down as much as I can.

The build.rs is compiled with gcc so it runs on the current platform, and the rest of the project is compiled with mingw.

The NIX_LDFLAGS contains the windows version of the pthreads library which gcc doesn't understand. If I manually remove it, then mingw doesn't understand the built-in linux version.

@oxalica
Copy link
Owner

oxalica commented Sep 29, 2021

The NIX_LDFLAGS contains the windows version of the pthreads library which gcc doesn't understand. If I manually remove it, then mingw doesn't understand the built-in linux version.

I think you could report this issue to nixpkgs.

@kognise
Copy link
Author

kognise commented Sep 29, 2021

That sounds fun! Thanks for the time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants