Skip to content

Commit

Permalink
Add fallback for rust without profile support
Browse files Browse the repository at this point in the history
  • Loading branch information
oxalica committed Apr 6, 2021
1 parent d88d58c commit d063083
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
4 changes: 4 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 18 additions & 5 deletions rust-overlay.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit d063083

Please sign in to comment.