Got windows cross-compilation working

The resulting binary is still untested, but it _does_ compile!

This required re-jiggering basically everything about the nix flake,
including replacing the toolchain management to fenix.
This commit is contained in:
Brian Picciano 2023-11-12 10:57:02 +01:00
parent 4ded1c4ab4
commit ada434032b
3 changed files with 156 additions and 164 deletions

View File

@ -1,26 +1,27 @@
{ {
"nodes": { "nodes": {
"flake-utils": { "fenix": {
"inputs": { "inputs": {
"systems": "systems" "nixpkgs": "nixpkgs",
"rust-analyzer-src": "rust-analyzer-src"
}, },
"locked": { "locked": {
"lastModified": 1681202837, "lastModified": 1699770036,
"narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=", "narHash": "sha256-bZmI7ytPAYLpyFNgj5xirDkKuAniOkj1xHdv5aIJ5GM=",
"owner": "numtide", "owner": "nix-community",
"repo": "flake-utils", "repo": "fenix",
"rev": "cfacdce06f30d2b68473a46042957675eebb3401", "rev": "81ab0b4f7ae9ebb57daa0edf119c4891806e4d3a",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "numtide", "owner": "nix-community",
"repo": "flake-utils", "repo": "fenix",
"type": "github" "type": "github"
} }
}, },
"naersk": { "naersk": {
"inputs": { "inputs": {
"nixpkgs": "nixpkgs" "nixpkgs": "nixpkgs_2"
}, },
"locked": { "locked": {
"lastModified": 1690373729, "lastModified": 1690373729,
@ -38,6 +39,22 @@
} }
}, },
"nixpkgs": { "nixpkgs": {
"locked": {
"lastModified": 1699099776,
"narHash": "sha256-X09iKJ27mGsGambGfkKzqvw5esP1L/Rf8H3u3fCqIiU=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "85f1ba3e51676fa8cc604a3d863d729026a6b8eb",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": { "locked": {
"lastModified": 1691003216, "lastModified": 1691003216,
"narHash": "sha256-Qq/MPkhS12Bl0X060pPvX3v9ac3f2rRQfHjjozPh/Qs=", "narHash": "sha256-Qq/MPkhS12Bl0X060pPvX3v9ac3f2rRQfHjjozPh/Qs=",
@ -51,7 +68,7 @@
"type": "indirect" "type": "indirect"
} }
}, },
"nixpkgs_2": { "nixpkgs_3": {
"locked": { "locked": {
"lastModified": 1688392541, "lastModified": 1688392541,
"narHash": "sha256-lHrKvEkCPTUO+7tPfjIcb7Trk6k31rz18vkyqmkeJfY=", "narHash": "sha256-lHrKvEkCPTUO+7tPfjIcb7Trk6k31rz18vkyqmkeJfY=",
@ -67,94 +84,27 @@
"type": "github" "type": "github"
} }
}, },
"nixpkgs_3": {
"locked": {
"lastModified": 1681358109,
"narHash": "sha256-eKyxW4OohHQx9Urxi7TQlFBTDWII+F+x2hklDOQPB50=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "96ba1c52e54e74c3197f4d43026b3f3d92e83ff9",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": { "root": {
"inputs": { "inputs": {
"fenix": "fenix",
"naersk": "naersk", "naersk": "naersk",
"nixpkgs": "nixpkgs_2",
"rust-overlay": "rust-overlay",
"utils": "utils"
}
},
"rust-overlay": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs_3" "nixpkgs": "nixpkgs_3"
},
"locked": {
"lastModified": 1691029059,
"narHash": "sha256-QwVeE9YTgH3LmL7yw2V/hgswL6yorIvYSp4YGI8lZYM=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "99df4908445be37ddb2d332580365fce512a7dcf",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
} }
}, },
"systems": { "rust-analyzer-src": {
"flake": false,
"locked": { "locked": {
"lastModified": 1681028828, "lastModified": 1699715108,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", "narHash": "sha256-yPozsobJU55gj+szgo4Lpcg1lHvGQYAT6Y4MrC80mWE=",
"owner": "nix-systems", "owner": "rust-lang",
"repo": "default", "repo": "rust-analyzer",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", "rev": "5fcf5289e726785d20d3aa4d13d90a43ed248e83",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "nix-systems", "owner": "rust-lang",
"repo": "default", "ref": "nightly",
"type": "github" "repo": "rust-analyzer",
}
},
"systems_2": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
},
"utils": {
"inputs": {
"systems": "systems_2"
},
"locked": {
"lastModified": 1689068808,
"narHash": "sha256-6ixXo3wt24N/melDWjq70UuHQLxGV8jZvooRanIHXw0=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "919d646de7be200f3bf08cb76ae1f09402b6f9b4",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github" "type": "github"
} }
} }

189
flake.nix
View File

@ -1,102 +1,143 @@
{ {
inputs = { inputs = {
fenix.url = "github:nix-community/fenix";
naersk.url = "github:nix-community/naersk/master"; naersk.url = "github:nix-community/naersk/master";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.11"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.11";
utils.url = "github:numtide/flake-utils";
rust-overlay.url = "github:oxalica/rust-overlay";
}; };
outputs = { self, nixpkgs, utils, naersk, rust-overlay }: outputs = { self, nixpkgs, naersk, fenix }:
let let
buildTargetsBySystem = { buildTargets = {
"x86_64-linux" = "x86_64-unknown-linux-musl"; "x86_64-linux" = {
"i686-linux" = "i686-unknown-linux-musl"; crossSystemConfig = "x86_64-unknown-linux-musl";
"aarch64-linux" = "aarch64-unknown-linux-musl"; rustTarget = "x86_64-unknown-linux-musl";
};
"i686-linux" = {
crossSystemConfig = "i686-unknown-linux-musl";
rustTarget = "i686-unknown-linux-musl";
};
"aarch64-linux" = {
crossSystemConfig = "aarch64-unknown-linux-musl";
rustTarget = "aarch64-unknown-linux-musl";
};
"x86_64-windows" = {
crossSystemConfig = "x86_64-w64-mingw32";
rustTarget = "x86_64-pc-windows-gnu";
makeBuildPackageAttrs = pkgsCross: {
depsBuildBuild = [
pkgsCross.stdenv.cc
pkgsCross.windows.pthreads
];
};
};
}; };
supportedSystems = builtins.attrNames buildTargetsBySystem; # eachSystem [system] (system: ...)
#
# Returns an attrset with a key for every system in the given array, with
# the key's value being the result of calling the callback with that key.
eachSystem = supportedSystems: callback: builtins.foldl'
(overall: system: overall // { ${system} = callback system; })
{}
supportedSystems;
eachCrossSystem = callback: (builtins.foldl' # eachCrossSystem [system] (buildSystem: targetSystem: ...)
(overall: buildSystem: overall // { #
${buildSystem} = (builtins.foldl' # Returns an attrset with a key "$buildSystem.cross-$targetSystem" for
(inner: hostSystem: inner // { # every combination of the elements of the array of system strings. The
"cross-${hostSystem}" = callback buildSystem hostSystem; # value of the attrs will be the result of calling the callback with each
# combination.
#
# There will also be keys "$system.default", which are aliases of
# "$system.cross-$system" for every system.
#
eachCrossSystem = supportedSystems: callback:
eachSystem supportedSystems (buildSystem: builtins.foldl'
(inner: targetSystem: inner // {
"cross-${targetSystem}" = callback buildSystem targetSystem;
}) })
{ default = callback buildSystem buildSystem; } { default = callback buildSystem buildSystem; }
supportedSystems supportedSystems
); );
})
{}
supportedSystems
);
mkPkgs = buildSystem: hostSystem: import nixpkgs ({ mkPkgs = buildSystem: targetSystem: import nixpkgs ({
system = buildSystem; system = buildSystem;
overlays = [ } // (if targetSystem == null then {} else {
(import rust-overlay)
(final: prev: {
rust-toolchain = prev.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
})
];
} // (if buildSystem == hostSystem then {} else {
# The nixpkgs cache doesn't have any packages where cross-compiling has # The nixpkgs cache doesn't have any packages where cross-compiling has
# been enabled, even if the target platform is actually the same as the # been enabled, even if the target platform is actually the same as the
# build platform (and therefore it's not really cross-compiling). So we # build platform (and therefore it's not really cross-compiling). So we
# only set up the cross-compiling config if the target platform is # only set up the cross-compiling config if the target platform is
# different. # different.
crossSystem.config = hostSystem; crossSystem.config = buildTargets.${targetSystem}.crossSystemConfig;
})); }));
mkRustEnv = crossPkgs: hostSystem: { in {
OPENSSL_STATIC = "1"; packages = eachCrossSystem
OPENSSL_LIB_DIR = "${crossPkgs.pkgsStatic.openssl.out}/lib"; (builtins.attrNames buildTargets)
OPENSSL_INCLUDE_DIR = "${crossPkgs.pkgsStatic.openssl.dev}/include"; (buildSystem: targetSystem: let
CARGO_BUILD_TARGET = buildTargetsBySystem.${hostSystem}; pkgs = mkPkgs buildSystem null;
CARGO_BUILD_RUSTFLAGS = [ pkgsCross = mkPkgs buildSystem targetSystem;
"-C" "target-feature=+crt-static" rustTarget = buildTargets.${targetSystem}.rustTarget;
"-C" "linker=${crossPkgs.stdenv.cc}/bin/${crossPkgs.stdenv.cc.targetPrefix}cc"
"-C" "link-arg=-static"
];
};
in # TODO I'd prefer to use the toolchain file
{ # https://github.com/nix-community/fenix/issues/123
packages = eachCrossSystem (buildSystem: hostSystem: let #toolchain = fenix.packages.${buildSystem}.fromToolchainFile {
pkgs = mkPkgs buildSystem buildSystem; # file = ./rust-toolchain.toml;
crossPkgs = mkPkgs buildSystem hostSystem; # sha256 = "sha256-LU4C/i+maIOqBZagUaXpFyWZyOVfQ3Ah5/JTz7v6CG4=";
naersk-lib = pkgs.callPackage naersk { #};
cargo = pkgs.rust-toolchain;
rustc = pkgs.rust-toolchain;
};
rustEnv = mkRustEnv crossPkgs hostSystem;
in
naersk-lib.buildPackage ({
src = ./.;
doCheck = false;
nativeBuildInputs = [ crossPkgs.pkgsStatic.stdenv.cc ];
} // rustEnv)
);
devShells = eachCrossSystem (buildSystem: hostSystem: let fenixPkgs = fenix.packages.${buildSystem};
pkgs = mkPkgs buildSystem buildSystem;
crossPkgs = mkPkgs buildSystem hostSystem; mkToolchain = fenixPkgs: fenixPkgs.toolchainOf {
rustEnv = mkRustEnv crossPkgs hostSystem; channel = "nightly";
in date = "2023-07-23";
pkgs.mkShell ({ sha256 = "sha256-LU4C/i+maIOqBZagUaXpFyWZyOVfQ3Ah5/JTz7v6CG4=";
nativeBuildInputs = [ };
crossPkgs.stdenv.cc
crossPkgs.openssl toolchain = fenixPkgs.combine [
pkgs.nmap # ncat (mkToolchain fenixPkgs).rustc
pkgs.rust-toolchain (mkToolchain fenixPkgs).cargo
(mkToolchain fenixPkgs.targets.${rustTarget}).rust-std
]; ];
shellHook = ''
export CARGO_HOME=$(pwd)/.cargo
if [ -f "config.yml" ]; then buildPackageAttrs = if
export DOMANI_CONFIG_PATH=config-dev.yml builtins.hasAttr "makeBuildPackageAttrs" buildTargets.${targetSystem}
fi then
''; buildTargets.${targetSystem}.makeBuildPackageAttrs pkgsCross
} // rustEnv)); else
{};
naersk-lib = pkgs.callPackage naersk {
cargo = toolchain;
rustc = toolchain;
};
in
naersk-lib.buildPackage (buildPackageAttrs // rec {
src = ./.;
strictDeps = true;
doCheck = false;
OPENSSL_STATIC = "1";
OPENSSL_LIB_DIR = "${pkgsCross.pkgsStatic.openssl.out}/lib";
OPENSSL_INCLUDE_DIR = "${pkgsCross.pkgsStatic.openssl.dev}/include";
# Required because ring crate is special. This also seems to have
# fixed some issues with the x86_64-windows cross-compile :shrug:
TARGET_CC="${pkgsCross.stdenv.cc}/bin/${pkgsCross.stdenv.cc.targetPrefix}cc";
CARGO_BUILD_TARGET = rustTarget;
CARGO_BUILD_RUSTFLAGS = [
"-C" "target-feature=+crt-static"
"-C" "link-arg=-static"
# https://github.com/rust-lang/cargo/issues/4133
"-C" "linker=${TARGET_CC}"
];
})
);
}; };
} }

View File

@ -4,5 +4,6 @@ components = [ "rustfmt", "rustc-dev", "clippy", "cargo" ]
targets = [ targets = [
"x86_64-unknown-linux-musl", "x86_64-unknown-linux-musl",
"i686-unknown-linux-musl", "i686-unknown-linux-musl",
"aarch64-unknown-linux-musl" "aarch64-unknown-linux-musl",
"x86_64-pc-windows-gnu"
] ]