Skip to content

fix toRustTarget for windows#134946

Merged
nrdxp merged 2 commits intoNixOS:masterfrom
dermetfan:rust-target-windows
Aug 20, 2021
Merged

fix toRustTarget for windows#134946
nrdxp merged 2 commits intoNixOS:masterfrom
dermetfan:rust-target-windows

Conversation

@dermetfan
Copy link
Copy Markdown
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.

@github-actions github-actions bot added the 6.topic: rust General-purpose programming language emphasizing performance, type safety, and concurrency. label Aug 20, 2021
@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
Copy Markdown

@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.

👍

@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 General-purpose programming language emphasizing performance, type safety, and concurrency. 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