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

fix toRustTarget for windows #134946

Merged
merged 2 commits into from
Aug 20, 2021
Merged

fix toRustTarget for windows #134946

merged 2 commits into from
Aug 20, 2021

Conversation

dermetfan
Copy link
Member

@dermetfan dermetfan commented Aug 20, 2021

Motivation for this change

Rust cross compilation to windows broke in 9171853 because the vendor component in the target triplet/"quadlet" was changed from pc to w64. Rust still uses pc so this PR just adds this replacement to the toRustTarget function in the spirit of #66613, which seems to have first introduced the idea of target component replacement.

You can test this using the following derivation, which should not evaluate before this change and successfully build after it.

The expected error is:

$ nix-build test.nix
error: attribute '*' missing

       at /nix/store/vgmjaczw53f7iknzwjhjr7fq1dlld37i-source/rust-overlay.nix:67:80:

           66|       pkg = pkgs.${pkgname};
           67|       srcInfo = pkg.target.${super.rust.toRustTarget stdenv.targetPlatform} or pkg.target."*";
             |                                                                                ^
           68|       extensions = srcInfo.extensions or [];

The repro snippet:
(I'm using the nixpkgs-mozilla overlay because building the rust tools in nixpkgs OOMs on my machine)

let
  ## TOGGLE THIS ##
  withFix = false;

  nixpkgs = builtins.fetchGit (if withFix then {
    url = "https://github.com/dermetfan/nixpkgs";
    ref = "rust-target-windows";
  } else {
    url = "https://github.com/NixOS/nixpkgs";
    rev = "456481aa3dd633038c86635f3bcf83d339254119";
  });

  pkgs = (import nixpkgs { system = builtins.currentSystem; })
    .pkgsCross.mingwW64;

  nixpkgs-mozilla = import "${builtins.fetchGit "https://github.com/mozilla/nixpkgs-mozilla"}/package-set.nix"
    { pkgs = pkgs.pkgsBuildHost; };

  # Only rustPlatform needs to target the host platform
  # because it produces the actual derivation
  # and the rest is build-time dependencies.
  crossPackage = pkgs.pkgsBuildHost.callPackage package {
    rustPlatform = let
      rustStable = (nixpkgs-mozilla.rustChannelOf {
        channel = "1.52.0";
        sha256 = "0qzaq3hsxh7skxjix4d4k38rv0cxwwnvi32arg08p11cxvpsmikx";
      }).rust.override {
        targets = [
          "x86_64-pc-windows-msvc"
        ];
      };
    in pkgs.makeRustPlatform {
      rustc = rustStable;
      cargo = rustStable;
    };
  };

  package =
    { lib, rustPlatform, runCommandNoCCLocal }:

    rustPlatform.buildRustPackage rec {
      pname = "pr-test";
      version = "0.0.0";

      src = let
        nv = ''
          name = "${pname}"
          version = "${version}"
        '';
      in runCommandNoCCLocal "gen-src" {} ''
        mkdir -p $out/src
        cd $out

        cat > Cargo.toml <<'EOF'
        [package]
        ${nv}
        EOF

        cat > Cargo.lock <<'EOF'
        [[package]]
        ${nv}
        EOF

        cat > src/main.rs <<'EOF'
        fn main() { println!("Hello, world!"); }
        EOF
      '';

      cargoHash = "sha256-g/msjJNAr+c3Av1GZUNZw40yb/9msHfkKPDPijn7P+k=";

      meta.platforms = lib.platforms.windows;
    };
in crossPackage

There is probably more work ahead to make cross compilation work the way it should, see #68804.

I also removed the unused dependency on remarshal.

Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • Tested compilation of all packages that depend on this change using nixpkgs-review rev HEAD
  • Tested execution of all binary files (usually in ./result/bin/)
  • Fits CONTRIBUTING.md.

@dermetfan dermetfan changed the title Rust target windows fix toRustTarget for windows Aug 20, 2021
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 0 This PR does not cause any packages to rebuild on Linux labels Aug 20, 2021
Copy link

@nrdxp nrdxp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

pkgs/development/compilers/rust/default.nix Show resolved Hide resolved
@nrdxp nrdxp merged commit 2b7e9a6 into NixOS:master Aug 20, 2021
@dermetfan dermetfan deleted the rust-target-windows branch August 20, 2021 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: rust 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 0 This PR does not cause any packages to rebuild on Linux
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants