Skip to content

Commit 0350732

Browse files
authored
Merge pull request #1500 from eyJhb/rust-versions
rust: replace fenix with rust-overlay allow specifying rust version
2 parents 472fe80 + 741eae8 commit 0350732

File tree

5 files changed

+109
-23
lines changed

5 files changed

+109
-23
lines changed

docs/reference/options.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15301,7 +15301,7 @@ string
1530115301

1530215302

1530315303

15304-
List of extra [targets](https://github.com/nix-community/fenix\#supported-platforms-and-targets)
15304+
List of extra [targets](https://doc.rust-lang.org/nightly/rustc/platform-support.html)
1530515305
to install. Defaults to only the native target.
1530615306

1530715307

@@ -15445,6 +15445,28 @@ null or package
1544515445

1544615446

1544715447

15448+
## languages.rust.version
15449+
15450+
15451+
15452+
Which version of rust to use, this value could be ` latest `,` 1.81.0 `, ` 2021-01-01 `.
15453+
Only works when languages.rust.channel is NOT nixpkgs.
15454+
15455+
15456+
15457+
*Type:*
15458+
string
15459+
15460+
15461+
15462+
*Default:*
15463+
` "latest" `
15464+
15465+
*Declared by:*
15466+
- [https://github.com/cachix/devenv/blob/main/src/modules/languages/rust.nix](https://github.com/cachix/devenv/blob/main/src/modules/languages/rust.nix)
15467+
15468+
15469+
1544815470
## languages.scala.enable
1544915471

1545015472

docs/supported-languages/rust.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ string
103103

104104

105105

106-
List of extra [targets](https://github\.com/nix-community/fenix\#supported-platforms-and-targets)
106+
List of extra [targets](https://doc\.rust-lang\.org/nightly/rustc/platform-support\.html)
107107
to install\. Defaults to only the native target\.
108108

109109

@@ -223,3 +223,22 @@ null or package
223223

224224
*Default:*
225225
` pkgs.rustfmt `
226+
227+
228+
229+
## languages\.rust\.version
230+
231+
232+
233+
Which version of rust to use, this value could be ` latest `,` 1.81.0 `, ` 2021-01-01 `\.
234+
Only works when languages\.rust\.channel is NOT nixpkgs\.
235+
236+
237+
238+
*Type:*
239+
string
240+
241+
242+
243+
*Default:*
244+
` "latest" `
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
inputs:
2-
fenix:
3-
url: github:nix-community/fenix
2+
rust-overlay:
3+
url: github:oxalica/rust-overlay
44
inputs:
55
nixpkgs:
66
follows: nixpkgs

examples/rust/devenv.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
inputs:
2-
fenix:
3-
url: github:nix-community/fenix
2+
rust-overlay:
3+
url: github:oxalica/rust-overlay
44
inputs:
55
nixpkgs:
66
follows: nixpkgs

src/modules/languages/rust.nix

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,44 @@
33
let
44
cfg = config.languages.rust;
55

6-
fenix = config.lib.getInput {
7-
name = "fenix";
8-
url = "github:nix-community/fenix";
9-
attribute = "languages.rust.version";
6+
rust-overlay = config.lib.getInput {
7+
name = "rust-overlay";
8+
url = "github:oxalica/rust-overlay";
9+
attribute = "languages.rust.input";
1010
follows = [ "nixpkgs" ];
1111
};
12+
13+
# https://github.com/nix-community/fenix/blob/cdfd7bf3e3edaf9e3f6d1e397d3ee601e513613c/lib/combine.nix
14+
combine = name: paths:
15+
pkgs.symlinkJoin {
16+
inherit name paths;
17+
postBuild = ''
18+
for file in $(find $out/bin -xtype f -maxdepth 1); do
19+
install -m755 $(realpath "$file") $out/bin
20+
21+
if [[ $file =~ /rustfmt$ ]]; then
22+
continue
23+
fi
24+
25+
${lib.optionalString pkgs.stdenv.isLinux ''
26+
if isELF "$file"; then
27+
patchelf --set-rpath $out/lib "$file" || true
28+
fi
29+
''}
30+
31+
${lib.optionalString pkgs.stdenv.isDarwin ''
32+
install_name_tool -add_rpath $out/lib "$file" || true
33+
''}
34+
done
35+
36+
for file in $(find $out/lib -name "librustc_driver-*"); do
37+
install $(realpath "$file") "$file"
38+
done
39+
'';
40+
};
1241
in
1342
{
1443
imports = [
15-
(lib.mkRenamedOptionModule [ "languages" "rust" "version" ] [ "languages" "rust" "channel" ])
1644
(lib.mkRenamedOptionModule [ "languages" "rust" "packages" ] [ "languages" "rust" "toolchain" ])
1745
];
1846

@@ -34,8 +62,8 @@ in
3462
default = [ ];
3563
defaultText = lib.literalExpression ''[ ]'';
3664
description = ''
37-
List of extra [targets](https://github.com/nix-community/fenix#supported-platforms-and-targets)
38-
to install. Defaults to only the native target.
65+
List of extra [targets](https://doc.rust-lang.org/nightly/rustc/platform-support.html)
66+
to install. Defaults to only the native target.
3967
'';
4068
};
4169

@@ -46,6 +74,16 @@ in
4674
description = "The rustup toolchain to install.";
4775
};
4876

77+
version = lib.mkOption {
78+
type = lib.types.str;
79+
default = "latest";
80+
defaultText = lib.literalExpression ''"latest"'';
81+
description = ''
82+
Which version of rust to use, this value could be `latest`,`1.81.0`, `2021-01-01`.
83+
Only works when languages.rust.channel is NOT nixpkgs.
84+
'';
85+
};
86+
4987
rustflags = lib.mkOption {
5088
type = lib.types.str;
5189
default = "";
@@ -101,6 +139,17 @@ in
101139
The nixpkgs channel does not support cross-compiling with targets.
102140
Use the stable, beta, or nightly channels instead. For example:
103141
142+
languages.rust.channel = "stable";
143+
'';
144+
}
145+
{
146+
assertion = cfg.channel == "nixpkgs" -> (cfg.version == "latest");
147+
message = ''
148+
Cannot use `languages.rust.channel = "nixpkgs"` with `languages.rust.version`.
149+
150+
The nixpkgs channel does not contain all versions required, and is
151+
therefore not supported to be used together.
152+
104153
languages.rust.channel = "stable";
105154
'';
106155
}
@@ -154,21 +203,17 @@ in
154203

155204
(lib.mkIf (cfg.channel != "nixpkgs") (
156205
let
157-
rustPackages = fenix.packages.${pkgs.stdenv.system};
158-
fenixChannel =
159-
if cfg.channel == "nightly"
160-
then "latest"
161-
else cfg.channel;
162-
toolchain = rustPackages.${fenixChannel};
206+
toolchain = (rust-overlay.lib.mkRustBin { } pkgs.buildPackages)."${cfg.channel}"."${cfg.version}";
207+
filteredToolchain = (lib.filterAttrs (n: _: builtins.elem n toolchain._manifest.profiles.complete) toolchain);
163208
in
164209
{
165210
languages.rust.toolchain =
166-
(builtins.mapAttrs (_: pkgs.lib.mkDefault) toolchain);
211+
(builtins.mapAttrs (_: pkgs.lib.mkDefault) filteredToolchain);
167212

168213
packages = [
169-
(rustPackages.combine (
170-
(map (c: toolchain.${c}) cfg.components) ++
171-
(map (t: rustPackages.targets.${t}.${fenixChannel}.rust-std) cfg.targets)
214+
(combine "rust-mixed" (
215+
(map (c: cfg.toolchain.${c}) (cfg.components ++ [ "rust-std" ])) ++
216+
(map (t: toolchain._components.${t}.rust-std) cfg.targets)
172217
))
173218
];
174219
}

0 commit comments

Comments
 (0)