From 6c25032fcdb3cb342adae64bfcc6ef902284320e Mon Sep 17 00:00:00 2001 From: Florian Warzecha Date: Fri, 10 May 2024 23:29:17 +0200 Subject: [PATCH] refactor(rust): try to better compose cross toolchains --- rust/flake.nix | 105 +++++++++++++++++++++++++++---------------------- 1 file changed, 58 insertions(+), 47 deletions(-) diff --git a/rust/flake.nix b/rust/flake.nix index b33f129..6e11f07 100644 --- a/rust/flake.nix +++ b/rust/flake.nix @@ -35,50 +35,62 @@ overlays = [ (final: prev: { ${packageName} = self.packages.${system}.${packageName}; }) ]; }; - fenix-pkgs = fenix.packages.${system}; - fenix-channel = fenix-pkgs.toolchainOf { - channel = "nightly"; - date = - builtins.replaceStrings [ "nightly-" ] [ "" ] - (builtins.fromTOML (builtins.readFile ./rust-toolchain.toml)).toolchain.channel; - sha256 = "sha256-SzEeSoO54GiBQ2kfANPhWrt0EDRxqEvhIbTt2uJt/TQ="; - }; + fenixPkgsFor = pkgs: fenix.packages.${pkgs.system}; + fenixChannelFor = + pkgs: + (fenixPkgsFor pkgs).toolchainOf { + channel = "nightly"; + date = + builtins.replaceStrings [ "nightly-" ] [ "" ] + (builtins.fromTOML (builtins.readFile ./rust-toolchain.toml)).toolchain.channel; + sha256 = "sha256-SzEeSoO54GiBQ2kfANPhWrt0EDRxqEvhIbTt2uJt/TQ="; + }; - makeCrossPackage = - packageName: pkgsCross: + toolchainFor = + pkgs: let - pkgs = builtins.throw "defining cross pkg, accessing pkgs is a bug"; + fenix-pkgs = fenixPkgsFor pkgs; + in + with fenix-pkgs; + combine [ + minimal.cargo + minimal.rustc + targets.${pkgs.rust.lib.toRustTarget pkgs.stdenv.targetPlatform}.latest.rust-std + ]; + + rustPlatformFor = + pkgs: + let + toolchain = toolchainFor pkgs; + in + pkgs.makeRustPlatform { + cargo = toolchain; + rustc = toolchain; + }; + + crossPackageFor = + pkgs: + let + rustPlatform = rustPlatformFor pkgs; in { - "${packageName}-cross-${pkgsCross.stdenv.hostPlatform.config}${ - if pkgsCross.stdenv.hostPlatform.isStatic then "-static" else "" - }" = - let - toolchain = - with fenix-pkgs; - combine [ - minimal.cargo - minimal.rustc - targets.${pkgsCross.rust.lib.toRustTarget pkgsCross.stdenv.targetPlatform}.latest.rust-std - ]; - in - pkgsCross.callPackage (./. + "/nix/packages/${packageName}.nix") { - inherit cargoMeta; - flake-self = self; - rustPlatform = pkgsCross.makeRustPlatform { - cargo = toolchain; - rustc = toolchain; - }; - }; + "${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 function { inherit system pkgs - fenix-pkgs - fenix-channel - makeCrossPackage + fenixPkgsFor + fenixChannelFor + toolchainFor + rustPlatformFor + crossPackageFor ; } ); @@ -89,11 +101,14 @@ packages = forSystems ( { pkgs, - fenix-channel, + fenixChannelFor, system, - makeCrossPackage, + crossPackageFor, ... }: + let + fenix-channel = fenixChannelFor pkgs; + in { ${packageName} = pkgs.callPackage (./. + "/nix/packages/${packageName}.nix") { inherit cargoMeta; @@ -105,21 +120,17 @@ }; default = self.packages.${system}.${packageName}; } - // makeCrossPackage packageName pkgs.pkgsCross.musl64.pkgsStatic - // makeCrossPackage packageName pkgs.pkgsCross.musl32.pkgsStatic - // makeCrossPackage packageName pkgs.pkgsCross.aarch64-multiplatform-musl.pkgsStatic - // makeCrossPackage packageName pkgs.pkgsCross.armv7l-hf-multiplatform.pkgsStatic - // makeCrossPackage packageName pkgs.pkgsCross.mingwW64.pkgsStatic + // crossPackageFor pkgs.pkgsCross.musl64.pkgsStatic + // crossPackageFor pkgs.pkgsCross.musl32.pkgsStatic + // crossPackageFor pkgs.pkgsCross.aarch64-multiplatform-musl.pkgsStatic + // crossPackageFor pkgs.pkgsCross.armv7l-hf-multiplatform.pkgsStatic + // crossPackageFor pkgs.pkgsCross.mingwW64.pkgsStatic ); devShells = forSystems ( - { - pkgs, - fenix-pkgs, - fenix-channel, - ... - }: + { pkgs, fenixChannelFor, ... }: let + fenix-channel = fenixChannelFor pkgs; fenixRustToolchain = fenix-channel.withComponents [ "cargo" "clippy-preview"