refactor(rust): try to better compose cross toolchains

This commit is contained in:
Florian Warzecha 2024-05-10 23:29:17 +02:00
parent 8142658f81
commit 6c25032fcd
Signed by untrusted user: liketechnik
GPG key ID: 4BE8C7D97F910C60

View file

@ -35,8 +35,10 @@
overlays = [ (final: prev: { ${packageName} = self.packages.${system}.${packageName}; }) ]; overlays = [ (final: prev: { ${packageName} = self.packages.${system}.${packageName}; }) ];
}; };
fenix-pkgs = fenix.packages.${system}; fenixPkgsFor = pkgs: fenix.packages.${pkgs.system};
fenix-channel = fenix-pkgs.toolchainOf { fenixChannelFor =
pkgs:
(fenixPkgsFor pkgs).toolchainOf {
channel = "nightly"; channel = "nightly";
date = date =
builtins.replaceStrings [ "nightly-" ] [ "" ] builtins.replaceStrings [ "nightly-" ] [ "" ]
@ -44,31 +46,39 @@
sha256 = "sha256-SzEeSoO54GiBQ2kfANPhWrt0EDRxqEvhIbTt2uJt/TQ="; sha256 = "sha256-SzEeSoO54GiBQ2kfANPhWrt0EDRxqEvhIbTt2uJt/TQ=";
}; };
makeCrossPackage = toolchainFor =
packageName: pkgsCross: pkgs:
let let
pkgs = builtins.throw "defining cross pkg, accessing pkgs is a bug"; fenix-pkgs = fenixPkgsFor pkgs;
in in
{
"${packageName}-cross-${pkgsCross.stdenv.hostPlatform.config}${
if pkgsCross.stdenv.hostPlatform.isStatic then "-static" else ""
}" =
let
toolchain =
with fenix-pkgs; with fenix-pkgs;
combine [ combine [
minimal.cargo minimal.cargo
minimal.rustc minimal.rustc
targets.${pkgsCross.rust.lib.toRustTarget pkgsCross.stdenv.targetPlatform}.latest.rust-std targets.${pkgs.rust.lib.toRustTarget pkgs.stdenv.targetPlatform}.latest.rust-std
]; ];
rustPlatformFor =
pkgs:
let
toolchain = toolchainFor pkgs;
in in
pkgsCross.callPackage (./. + "/nix/packages/${packageName}.nix") { pkgs.makeRustPlatform {
inherit cargoMeta;
flake-self = self;
rustPlatform = pkgsCross.makeRustPlatform {
cargo = toolchain; cargo = toolchain;
rustc = toolchain; rustc = toolchain;
}; };
crossPackageFor =
pkgs:
let
rustPlatform = rustPlatformFor pkgs;
in
{
"${packageName}-cross-${pkgs.stdenv.hostPlatform.config}${
if pkgs.stdenv.hostPlatform.isStatic then "-static" else ""
}" = pkgs.callPackage (./. + "/nix/packages/${packageName}.nix") {
inherit cargoMeta rustPlatform;
flake-self = self;
}; };
}; };
in in
@ -76,9 +86,11 @@
inherit inherit
system system
pkgs pkgs
fenix-pkgs fenixPkgsFor
fenix-channel fenixChannelFor
makeCrossPackage toolchainFor
rustPlatformFor
crossPackageFor
; ;
} }
); );
@ -89,11 +101,14 @@
packages = forSystems ( packages = forSystems (
{ {
pkgs, pkgs,
fenix-channel, fenixChannelFor,
system, system,
makeCrossPackage, crossPackageFor,
... ...
}: }:
let
fenix-channel = fenixChannelFor pkgs;
in
{ {
${packageName} = pkgs.callPackage (./. + "/nix/packages/${packageName}.nix") { ${packageName} = pkgs.callPackage (./. + "/nix/packages/${packageName}.nix") {
inherit cargoMeta; inherit cargoMeta;
@ -105,21 +120,17 @@
}; };
default = self.packages.${system}.${packageName}; default = self.packages.${system}.${packageName};
} }
// makeCrossPackage packageName pkgs.pkgsCross.musl64.pkgsStatic // crossPackageFor pkgs.pkgsCross.musl64.pkgsStatic
// makeCrossPackage packageName pkgs.pkgsCross.musl32.pkgsStatic // crossPackageFor pkgs.pkgsCross.musl32.pkgsStatic
// makeCrossPackage packageName pkgs.pkgsCross.aarch64-multiplatform-musl.pkgsStatic // crossPackageFor pkgs.pkgsCross.aarch64-multiplatform-musl.pkgsStatic
// makeCrossPackage packageName pkgs.pkgsCross.armv7l-hf-multiplatform.pkgsStatic // crossPackageFor pkgs.pkgsCross.armv7l-hf-multiplatform.pkgsStatic
// makeCrossPackage packageName pkgs.pkgsCross.mingwW64.pkgsStatic // crossPackageFor pkgs.pkgsCross.mingwW64.pkgsStatic
); );
devShells = forSystems ( devShells = forSystems (
{ { pkgs, fenixChannelFor, ... }:
pkgs,
fenix-pkgs,
fenix-channel,
...
}:
let let
fenix-channel = fenixChannelFor pkgs;
fenixRustToolchain = fenix-channel.withComponents [ fenixRustToolchain = fenix-channel.withComponents [
"cargo" "cargo"
"clippy-preview" "clippy-preview"