Skip to content

Commit

Permalink
flake: impl builder interface lib.mkRustBin
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
oxalica committed Jun 3, 2024
1 parent 92089b7 commit 305d3b0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
2 changes: 1 addition & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
55 changes: 43 additions & 12 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.

Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions lib/dist-root.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"https://static.rust-lang.org/dist"

0 comments on commit 305d3b0

Please sign in to comment.