Skip to content

Commit

Permalink
rust: replaces fenix with rust-overlay now allows specifying version
Browse files Browse the repository at this point in the history
  • Loading branch information
eyJhb committed Oct 5, 2024
1 parent a8495ab commit 2d0eb48
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions src/modules/languages/rust.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
let
cfg = config.languages.rust;

fenix = config.lib.getInput {
name = "fenix";
url = "github:nix-community/fenix";
attribute = "languages.rust.version";
rust-overlay = config.lib.getInput {
name = "rust-overlay";
url = "github:oxalica/rust-overlay";
attribute = "languages.rust.input";
follows = [ "nixpkgs" ];
};
in
{
imports = [
(lib.mkRenamedOptionModule [ "languages" "rust" "version" ] [ "languages" "rust" "channel" ])
(lib.mkRenamedOptionModule [ "languages" "rust" "packages" ] [ "languages" "rust" "toolchain" ])
];

Expand All @@ -34,7 +33,7 @@ in
default = [ ];
defaultText = lib.literalExpression ''[ ]'';
description = ''
List of extra [targets](https://github.com/nix-community/fenix#supported-platforms-and-targets)
List of extra [targets](https://doc.rust-lang.org/nightly/rustc/platform-support.html)
to install. Defaults to only the native target.
'';
};
Expand All @@ -46,6 +45,16 @@ in
description = "The rustup toolchain to install.";
};

version = lib.mkOption {
type = lib.types.str;
default = "latest";
defaultText = lib.literalExpression ''"latest"'';
description = ''
Which version of rust to use, this value could be `latest`,`1.81.0`, `2021-01-01`.
Only works when languages.rust.channel is NOT nixpkgs.
'';
};

rustflags = lib.mkOption {
type = lib.types.str;
default = "";
Expand Down Expand Up @@ -101,6 +110,19 @@ in
The nixpkgs channel does not support cross-compiling with targets.
Use the stable, beta, or nightly channels instead. For example:
languages.rust.channel = "stable";
'';
}
{
assertion = cfg.channel == "nixpkgs" -> (cfg.version == "latest");
# assertion = !(cfg.channel == "nixpkgs" && cfg.version != "latest");
# assertion = false;
message = ''
Cannot use `languages.rust.channel = "nixpkgs"` with `languages.rust.version`.
The nixpkgs channel does not contain all versions required, and is
therefore not supported to be used together.
languages.rust.channel = "stable";
'';
}
Expand Down Expand Up @@ -153,23 +175,15 @@ in

(lib.mkIf (cfg.channel != "nixpkgs") (
let
rustPackages = fenix.packages.${pkgs.stdenv.system};
fenixChannel =
if cfg.channel == "nightly"
then "latest"
else cfg.channel;
toolchain = rustPackages.${fenixChannel};
toolchain = (rust-overlay.lib.mkRustBin {} pkgs)."${cfg.channel}"."${cfg.version}";
# filter toolchain, otherwise it contains a bunch of non-rust stuff (llvm-tools, etc.)
filteredToolchain = (lib.filterAttrs (n: _: builtins.elem n toolchain._manifest.profiles.complete) toolchain);
in
{
languages.rust.toolchain =
(builtins.mapAttrs (_: pkgs.lib.mkDefault) toolchain);
(builtins.mapAttrs (_: pkgs.lib.mkDefault) filteredToolchain);

packages = [
(rustPackages.combine (
(map (c: toolchain.${c}) cfg.components) ++
(map (t: rustPackages.targets.${t}.${fenixChannel}.rust-std) cfg.targets)
))
];
packages = builtins.attrValues (lib.filterAttrs (n: _: builtins.elem n cfg.components) cfg.toolchain);
}
))
]);
Expand Down

0 comments on commit 2d0eb48

Please sign in to comment.