fenix icon indicating copy to clipboard operation
fenix copied to clipboard

Support for `+<toolchain>` rustup feature

Open eureka-cpu opened this issue 1 year ago • 7 comments

Adding this for posterity in case someone else finds themselves here.

My particular use case was for nightly formatting like so: cargo +nightly fmt in a script that formats all files.

I was able to work around this by installing the nightly toolchain and changing the above to rustup run nightly cargo fmt, then adding the unstable_features = true option to the rustfmt.toml.

To quote @figsoda on the topic:

+<toolchain> is a rustup feature and fenix doesn’t currently support it. You should be able to just remove the +nightly argument and the same command should work. You can make a wrapper for cargo (and possibly other components) that skips the first argument if it starts with +. I’m not sure that this will ever be implemented within fenix, if it gets implemented it will probably be a separate tool that wraps nix and uses fenix under the hood.

eureka-cpu avatar Jun 21 '23 17:06 eureka-cpu

Can you explain more why and where exactly do you need it?

Personally I always use nightly rustfmt with stable toolchain. In my own project I use combine, but for ad-hoc use I have a script:

> cat ~/bin/nix-rust-flake-shell
#!/usr/bin/env bash

nixpkgs=github:NixOS/nixpkgs/nixos-23.05

exec nix shell github:nix-community/fenix#complete.rustfmt github:nix-community/fenix#stable.rustc github:nix-community/fenix#stable.cargo  $nixpkgs#rust-analyzer $nixpkgs#pkg-config $nixpkgs#openssl
19:36:02 ~/lab/redb  master:origin/master [✘!?] is 📦 v1.1.0 via 🐍 v3.10.12 🦀 
> nix-rust-flake-shell
19:36:06 ~/lab/redb  master:origin/master [✘!?] is 📦 v1.1.0 via 🐍 v3.10.12 🦀v1.72.1 
> rustfmt --version
rustfmt 1.6.0-nightly (5ae769f 2023-09-26)
19:36:09 ~/lab/redb  master:origin/master [✘!?] is 📦 v1.1.0 via 🐍 v3.10.12 🦀v1.72.1 
> rustc --version
rustc 1.72.1 (d5c2e9c34 2023-09-13)

dpc avatar Sep 28 '23 02:09 dpc

I've use this +<toolchain> feature to test building against a specific MSRV in the past. @dpc do you suggest running a new nix shell with a specific version for this purpose?

DanGould avatar Jan 09 '24 21:01 DanGould

There are some cargo features and tools that can't be used without the +<toolchain> feature, but I've sort of come to accept that fenix likely won't support this so I've just used nix-shell -p rustup for the times I've needed to use those features. It's not ideal, but it works.

eureka-cpu avatar Jan 10 '24 05:01 eureka-cpu

+toolchain is a rustup binary wrapper functionality, and can't be done without it.

With fenix one can just instantiate multiple fenix instances with different toolchains (e.g. using toolchainOf). For more than just a one-off operation, this often requires wrapping a bunch of things in a function to parametrize it over the exact fenix toolchain.

I should add support for it to Flakebox ... :thinking:

dpc avatar Jan 10 '24 16:01 dpc

I'm happy to see this closed, but glad the conversation was had if not just for posterity. Thanks for the recommendation!

eureka-cpu avatar Jan 11 '24 07:01 eureka-cpu

I'm having issues with cargo fmt as well and I think it might be related to this. When I invoke rustfmt directly, I (correctly) get the nightly version, but cargo fmt uses stable version of rustfmt. How is this even possible, and what is the fix/workaround? Is it only possible to use rustfmt nightly (for cargo fmt) via rustup?

@figsoda

You should be able to just remove the +nightly argument and the same command should work.

This doesn't appear to be the case, unless I'm doing something wrong.

tmillr avatar Mar 17 '24 03:03 tmillr

I'm having issues with cargo fmt as well and I think it might be related to this. When I invoke rustfmt directly, I (correctly) get the nightly version, but cargo fmt uses stable version of rustfmt. How is this even possible, and what is the fix/workaround? Is it only possible to use rustfmt nightly (for cargo fmt) via rustup?

@tmillr You can use lib.hiPrio to override rustfmt. Here is a quick snippet that I use in my flake.nix:

flake-utils.lib.eachDefaultSystem (
      system: let
        <SNIP>
        rust = (
          pkgs.rust-bin.stable."1.76.0".default.override {
            extensions = [
              "rust-src"
              "rust-analyzer"
              "clippy"
            ];
            targets = ["x86_64-unknown-linux-gnu"];
          }
        );
      in
        with pkgs; {
          devShells.default = mkShell {
            buildInputs = [
              # We use the nightly formatter which is executed when running `cargo fmt`, without `+nightly` flag
              # which is a rustup binary wrapper functionality, which we don't use.
              (lib.hiPrio rust-bin.nightly."2024-04-01".rustfmt)
              rust
            ];
          };
        }
    );

With this you get the nightly version when you invoke it regularly like this:

$ cargo fmt --version
rustfmt 1.7.0-nightly (8058136 2024-03-31)

The full snippet can be found in trade_aggregation-rs. Actually I'm using the rust-overlay but I imagine it should work the same for fenix as well. Correct me if I'm wrong

MathisWellmann avatar Apr 04 '24 19:04 MathisWellmann