flake: impl builder interface lib.mkRustBin

This allows construction of `rust-bin` attrset on an existing `pkgs`,
instead of staging another overlayed nixpkgs. This can reduce redundant
evaluation of nixpkgs for flake-heavy users who also don't like or
cannot (due to flake dependency hell) manually instantiate nixpkgs.
This commit is contained in:
oxalica
2024-06-03 02:32:34 -04:00
parent 92089b75db
commit 305d3b088e
3 changed files with 45 additions and 13 deletions

View File

@@ -24,7 +24,7 @@ let
in {
rust-bin = (prev.rust-bin or { }) // {
# The overridable dist url for fetching.
distRoot = "https://static.rust-lang.org/dist";
distRoot = import ./lib/dist-root.nix;
} // import ./lib/rust-bin.nix {
inherit lib manifests;
inherit (final.rust) toRustTarget;

View File

@@ -10,11 +10,10 @@
};
outputs = { self, nixpkgs, flake-utils }: let
inherit (nixpkgs.lib)
inherit (nixpkgs) lib;
inherit (lib)
elem filterAttrs head mapAttrs' optionalAttrs replaceStrings;
overlay = import ./.;
allSystems = [
"aarch64-darwin"
"aarch64-linux"
@@ -30,13 +29,43 @@
"x86_64-linux"
];
overlay = import ./.;
defaultDistRoot = import ./lib/dist-root.nix;
mkManifests = distRoot: import ./lib/manifests.nix { inherit lib distRoot; };
# Builder to construct `rust-bin` interface on an existing `pkgs`.
# This would be immutable, non-intrusive and (hopefully) can benefit from
# flake eval-cache.
#
# Note that this does not contain compatible attrs for mozilla-overlay.
mkRustBin =
{ distRoot ? defaultDistRoot }:
pkgs:
lib.fix (rust-bin: import ./lib/rust-bin.nix {
inherit lib pkgs;
inherit (pkgs.rust) toRustTarget;
inherit (rust-bin) nightly;
manifests = mkManifests distRoot;
});
in {
lib = {
# Internal use only!
_internal = {
defaultManifests = mkManifests defaultDistRoot;
};
inherit mkRustBin;
};
overlays = {
default = overlay;
rust-overlay = overlay;
};
} // flake-utils.lib.eachSystem allSystems (system: let
pkgs = import nixpkgs { inherit system; overlays = [ overlay ]; };
pkgs = nixpkgs.legacyPackages.${system};
rust-bin = mkRustBin {} pkgs;
in {
# TODO: Flake outputs except `overlay[s]` are not stabilized yet.
@@ -54,25 +83,27 @@
then "rust"
else "rust_${replaceStrings ["."] ["_"] version}";
value = select version comps;
}) pkgs.rust-bin.stable //
}) rust-bin.stable //
mapAttrs' (version: comps: {
name = if version == "latest"
then "rust-nightly"
else "rust-nightly_${version}";
value = select version comps;
}) pkgs.rust-bin.nightly //
}) rust-bin.nightly //
mapAttrs' (version: comps: {
name = if version == "latest"
then "rust-beta"
else "rust-beta_${version}";
value = select version comps;
}) pkgs.rust-bin.beta;
}) rust-bin.beta;
result' = filterAttrs (name: drv: drv != null) result;
in result' // { default = result'.rust; };
checks = let
inherit (pkgs) rust-bin rustChannelOf;
inherit (pkgs.rust-bin) fromRustupToolchain fromRustupToolchainFile stable beta nightly;
inherit (rust-bin) fromRustupToolchain fromRustupToolchainFile stable beta nightly;
pkgs-compat = import nixpkgs { inherit system; overlays = [ overlay ]; };
inherit (pkgs-compat) latest rustChannelOf;
rustHostPlatform = pkgs.rust.toRustTarget pkgs.hostPlatform;
@@ -100,9 +131,9 @@
rename-unavailable = assertEq (stable."1.30.0" ? rustfmt) false;
rename-available = assertEq stable."1.48.0".rustfmt stable."1.48.0".rustfmt-preview;
latest-stable-legacy = assertEq pkgs.latest.rustChannels.stable.rustc stable.latest.rustc;
latest-beta-legacy = assertEq pkgs.latest.rustChannels.beta.rustc beta.latest.rustc;
latest-nightly-legacy = assertEq pkgs.latest.rustChannels.nightly.rustc nightly.latest.rustc;
latest-stable-legacy = assertEq latest.rustChannels.stable.rustc stable.latest.rustc;
latest-beta-legacy = assertEq latest.rustChannels.beta.rustc beta.latest.rustc;
latest-nightly-legacy = assertEq latest.rustChannels.nightly.rustc nightly.latest.rustc;
rust-channel-of-stable = assertEq (rustChannelOf { channel = "stable"; }).rustc stable.latest.rustc;
rust-channel-of-beta = assertEq (rustChannelOf { channel = "beta"; }).rustc beta.latest.rustc;

1
lib/dist-root.nix Normal file
View File

@@ -0,0 +1 @@
"https://static.rust-lang.org/dist"