Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rust: replace fenix with rust-overlay allow specifying rust version #1500

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions docs/reference/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -9213,8 +9213,6 @@ list of package

## git-hooks.hooks.dune-fmt.stages



Confines the hook to run at a particular stage.


Expand Down Expand Up @@ -11311,8 +11309,6 @@ null or signed integer

## git-hooks.hooks.flynt.settings.no-multiline



Convert only single line expressions.


Expand Down Expand Up @@ -13442,8 +13438,6 @@ list of string

## git-hooks.hooks.isort.description



Description of the hook. Used for metadata purposes only.


Expand Down Expand Up @@ -15482,8 +15476,6 @@ internal name, same as ` id `

## git-hooks.hooks.lychee.pass_filenames



Whether to pass filenames as arguments to the entry point.


Expand Down Expand Up @@ -17591,8 +17583,6 @@ list of package

## git-hooks.hooks.mypy.fail_fast



if true pre-commit will stop running hooks if this hook fails.


Expand Down Expand Up @@ -19682,8 +19672,6 @@ list of (one of “commit-msg”, “post-checkout”, “post-commit”, “pos

## git-hooks.hooks.ormolu.types



List of file types to run on. See [Filtering files with types](https://pre-commit.com/\#filtering-files-with-types).


Expand Down Expand Up @@ -21801,8 +21789,6 @@ list of package

## git-hooks.hooks.prettier.fail_fast



if true pre-commit will stop running hooks if this hook fails.


Expand Down Expand Up @@ -23826,8 +23812,6 @@ submodule

## git-hooks.hooks.pyright.enable



Whether to enable this pre-commit hook.


Expand Down Expand Up @@ -34947,7 +34931,7 @@ string



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 Down Expand Up @@ -35091,6 +35075,28 @@ null or package



## languages.rust.version



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.



*Type:*
string



*Default:*
` "latest" `

*Declared by:*
- [https://github.com/cachix/devenv/blob/main/src/modules/languages/rust.nix](https://github.com/cachix/devenv/blob/main/src/modules/languages/rust.nix)



## languages.scala.enable


Expand Down
21 changes: 20 additions & 1 deletion docs/supported-languages/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ string



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 Down Expand Up @@ -223,3 +223,22 @@ null or package

*Default:*
` pkgs.rustfmt `



## languages\.rust\.version



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\.



*Type:*
string



*Default:*
` "latest" `
4 changes: 2 additions & 2 deletions examples/rust-wasm-cross/devenv.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
inputs:
fenix:
url: github:nix-community/fenix
rust-overlay:
url: github:oxalica/rust-overlay
inputs:
nixpkgs:
follows: nixpkgs
4 changes: 2 additions & 2 deletions examples/rust/devenv.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
inputs:
fenix:
url: github:nix-community/fenix
rust-overlay:
url: github:oxalica/rust-overlay
inputs:
nixpkgs:
follows: nixpkgs
79 changes: 62 additions & 17 deletions src/modules/languages/rust.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,44 @@
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" ];
};

# https://github.com/nix-community/fenix/blob/cdfd7bf3e3edaf9e3f6d1e397d3ee601e513613c/lib/combine.nix
combine = name: paths:
pkgs.symlinkJoin {
inherit name paths;
postBuild = ''
for file in $(find $out/bin -xtype f -maxdepth 1); do
install -m755 $(realpath "$file") $out/bin

if [[ $file =~ /rustfmt$ ]]; then
continue
fi

${lib.optionalString pkgs.stdenv.isLinux ''
if isELF "$file"; then
patchelf --set-rpath $out/lib "$file" || true
fi
''}

${lib.optionalString pkgs.stdenv.isDarwin ''
install_name_tool -add_rpath $out/lib "$file" || true
''}
done

for file in $(find $out/lib -name "librustc_driver-*"); do
install $(realpath "$file") "$file"
done
'';
};
in
{
imports = [
(lib.mkRenamedOptionModule [ "languages" "rust" "version" ] [ "languages" "rust" "channel" ])
(lib.mkRenamedOptionModule [ "languages" "rust" "packages" ] [ "languages" "rust" "toolchain" ])
];

Expand All @@ -34,8 +62,8 @@ in
default = [ ];
defaultText = lib.literalExpression ''[ ]'';
description = ''
List of extra [targets](https://github.com/nix-community/fenix#supported-platforms-and-targets)
to install. Defaults to only the native target.
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 +74,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 +139,17 @@ 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");
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 @@ -154,21 +203,17 @@ 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.buildPackages)."${cfg.channel}"."${cfg.version}";
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)
(combine "rust-mixed" (
(map (c: cfg.toolchain.${c}) (cfg.components ++ [ "rust-std" ])) ++
(map (t: toolchain._components.${t}.rust-std) cfg.targets)
))
];
}
Expand Down
6 changes: 3 additions & 3 deletions tests/rust/devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
languages.rust.enable = true;
languages.rust.mold.enable = false;
enterTest = ''
if [ -n "$RUSTFLAGS" ]; then
echo "RUSTFLAGS is set, but it should not be"
exit 1
if [ -n "''${RUSTFLAGS:-}" ]; then
echo "RUSTFLAGS is set, but it should not be"
exit 1
fi
'';
}