From d06308345e1e56e4afb517921e45f981c951658e Mon Sep 17 00:00:00 2001 From: oxalica Date: Tue, 6 Apr 2021 19:35:24 +0800 Subject: [PATCH] Add fallback for rust without profile support --- flake.nix | 4 ++++ rust-overlay.nix | 23 ++++++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/flake.nix b/flake.nix index a8d4e82..118245b 100644 --- a/flake.nix +++ b/flake.nix @@ -143,6 +143,10 @@ targets = [ "wasm32-unknown-unknown" "aarch64-unknown-linux-gnu" ]; }); + rustup-toolchain-profile-missing = assertEq (builtins.tryEval (fromRustupToolchain { channel = "1.51.0"; profile = "non_existent"; })).success false; + rustup-toolchain-profile-too-early = assertEq (builtins.tryEval (fromRustupToolchain { channel = "1.29.0"; profile = "minimal"; })).success false; + rustup-toolchain-profile-fallback = assertEq (fromRustupToolchain { channel = "1.29.0"; }) stable."1.29.0".rust; + rustup-toolchain-file-toml = assertEq (fromRustupToolchainFile ./tests/rust-toolchain-toml) (nightly."2021-03-25".default.override { diff --git a/rust-overlay.nix b/rust-overlay.nix index 338dead..41e7045 100644 --- a/rust-overlay.nix +++ b/rust-overlay.nix @@ -48,11 +48,25 @@ let # Select a toolchain and aggregate components by rustup's `rust-toolchain` file format. # See: https://rust-lang.github.io/rustup/concepts/profiles.html # Or see source: https://github.com/rust-lang/rustup/blob/84974df1387812269c7b29fa5f3bb1c6480a6500/doc/src/overrides.md#the-toolchain-file - fromRustupToolchain = { path ? null, channel ? null, components ? [], targets ? [], profile ? "default" }: + fromRustupToolchain = { path ? null, channel ? null, profile ? null, components ? [], targets ? [] }: if path != null then throw "`path` is not supported, please directly add it to your PATH instead" else if channel == null then throw "`channel` is required" else - (toolchainFromManifest (selectManifest { inherit channel; }))._profiles.${profile}.override { + let + toolchain = toolchainFromManifest (selectManifest { inherit channel; }); + profile' = if profile == null then "default" else profile; + pkg = + if toolchain._profiles != {} then + toolchain._profiles.${profile'} or (throw '' + Rust ${toolchain._version} doesn't have profile `${profile'}`. + Available profiles are: ${self.lib.concatStringsSep ", " (builtins.attrNames toolchain._profiles)} + '') + # Fallback to package `rust` when profiles are not supported and not specified. + else if profile == null then + toolchain.rust + else + throw "Cannot select profile `${profile'}` since rust ${toolchain._version} is too early to support profiles"; + in pkg.override { extensions = components; inherit targets; }; @@ -468,9 +482,8 @@ let # Internal use. _components = componentSet; - _profiles = if profiles == {} - then throw "Rust ${manifest.version} doesn't support profiles" - else profiles; + _profiles = profiles; + _version = manifest.version; }; # Same as `toolchainFromManifest` but read from a manifest file.