From b505961fd82cfbbf80e10a523ba4b48b9a31b9ac Mon Sep 17 00:00:00 2001 From: eyjhb Date: Fri, 15 Nov 2024 18:44:26 +0100 Subject: [PATCH 1/5] rust: replaces fenix with rust-overlay now allows specifying version --- src/modules/languages/rust.nix | 79 ++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 17 deletions(-) diff --git a/src/modules/languages/rust.nix b/src/modules/languages/rust.nix index 3aaff26f9..c850cfaa7 100644 --- a/src/modules/languages/rust.nix +++ b/src/modules/languages/rust.nix @@ -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" ]) ]; @@ -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. ''; }; @@ -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 = ""; @@ -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"; ''; } @@ -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) ++ + (map (t: toolchain._components.${t}.rust-std) cfg.targets) )) ]; } From e6693ae1971ba8b2a183c731bff481f1b40302c5 Mon Sep 17 00:00:00 2001 From: eyjhb Date: Sun, 6 Oct 2024 11:06:07 +0200 Subject: [PATCH 2/5] always add rust-std for the default target --- src/modules/languages/rust.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/languages/rust.nix b/src/modules/languages/rust.nix index c850cfaa7..15041ef42 100644 --- a/src/modules/languages/rust.nix +++ b/src/modules/languages/rust.nix @@ -212,7 +212,7 @@ in packages = [ (combine "rust-mixed" ( - (map (c: cfg.toolchain.${c}) cfg.components) ++ + (map (c: cfg.toolchain.${c}) (cfg.components ++ [ "rust-std" ])) ++ (map (t: toolchain._components.${t}.rust-std) cfg.targets) )) ]; From adcc72ccdb9b43991f1ed08ea122898877bca7f6 Mon Sep 17 00:00:00 2001 From: eyjhb Date: Fri, 15 Nov 2024 19:28:26 +0100 Subject: [PATCH 3/5] rust: fix tests --- examples/rust-wasm-cross/devenv.yaml | 4 ++-- examples/rust/devenv.yaml | 4 ++-- tests/rust/devenv.nix | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/rust-wasm-cross/devenv.yaml b/examples/rust-wasm-cross/devenv.yaml index aecda8b76..c08ccc882 100644 --- a/examples/rust-wasm-cross/devenv.yaml +++ b/examples/rust-wasm-cross/devenv.yaml @@ -1,6 +1,6 @@ inputs: - fenix: - url: github:nix-community/fenix + rust-overlay: + url: github:oxalica/rust-overlay inputs: nixpkgs: follows: nixpkgs diff --git a/examples/rust/devenv.yaml b/examples/rust/devenv.yaml index aecda8b76..c08ccc882 100644 --- a/examples/rust/devenv.yaml +++ b/examples/rust/devenv.yaml @@ -1,6 +1,6 @@ inputs: - fenix: - url: github:nix-community/fenix + rust-overlay: + url: github:oxalica/rust-overlay inputs: nixpkgs: follows: nixpkgs diff --git a/tests/rust/devenv.nix b/tests/rust/devenv.nix index fa7a5b7f6..cc282eacd 100644 --- a/tests/rust/devenv.nix +++ b/tests/rust/devenv.nix @@ -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 ''; } From 3ad29368df2e0e7d4140d0b96e8f44cf70e22b52 Mon Sep 17 00:00:00 2001 From: eyjhb Date: Fri, 15 Nov 2024 19:41:31 +0100 Subject: [PATCH 4/5] rust: updated docs --- docs/reference/options.md | 40 ++++++++++++++++++-------------- docs/supported-languages/rust.md | 21 ++++++++++++++++- 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/docs/reference/options.md b/docs/reference/options.md index a10955d57..a76bcc312 100644 --- a/docs/reference/options.md +++ b/docs/reference/options.md @@ -9213,8 +9213,6 @@ list of package ## git-hooks.hooks.dune-fmt.stages - - Confines the hook to run at a particular stage. @@ -11311,8 +11309,6 @@ null or signed integer ## git-hooks.hooks.flynt.settings.no-multiline - - Convert only single line expressions. @@ -13442,8 +13438,6 @@ list of string ## git-hooks.hooks.isort.description - - Description of the hook. Used for metadata purposes only. @@ -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. @@ -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. @@ -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). @@ -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. @@ -23826,8 +23812,6 @@ submodule ## git-hooks.hooks.pyright.enable - - Whether to enable this pre-commit hook. @@ -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. @@ -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 diff --git a/docs/supported-languages/rust.md b/docs/supported-languages/rust.md index 5db06a345..bc489054e 100644 --- a/docs/supported-languages/rust.md +++ b/docs/supported-languages/rust.md @@ -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\. @@ -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" ` From 99c5dddae3e212d2d7b51d98b038d8cb3af3a94d Mon Sep 17 00:00:00 2001 From: eyjhb Date: Sat, 16 Nov 2024 20:59:26 +0100 Subject: [PATCH 5/5] rust: format rust.nix correctly --- src/modules/languages/rust.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/languages/rust.nix b/src/modules/languages/rust.nix index 15041ef42..22182dae1 100644 --- a/src/modules/languages/rust.nix +++ b/src/modules/languages/rust.nix @@ -203,7 +203,7 @@ in (lib.mkIf (cfg.channel != "nixpkgs") ( let - toolchain = (rust-overlay.lib.mkRustBin {} pkgs.buildPackages)."${cfg.channel}"."${cfg.version}"; + toolchain = (rust-overlay.lib.mkRustBin { } pkgs.buildPackages)."${cfg.channel}"."${cfg.version}"; filteredToolchain = (lib.filterAttrs (n: _: builtins.elem n toolchain._manifest.profiles.complete) toolchain); in {