How to use nix with overlay

I have never used nix before, but, the rust package on nix stable seemed to be outdated. After a bit of research, I learned that people use GitHub - oxalica/rust-overlay: Pure and reproducible nix overlay of binary distributed rust toolchains with nix, I’ve experiment with it for a while but can’t make it work? Can anyone help? Is this possible? Thanks!

Hi @Sunny - I am not too familiar with that particular binary, but this is the dev.nix for our Rust CLI template:

# To learn more about how to use Nix to configure your environment
# see: https://developers.google.com/idx/guides/customize-idx-env
{ pkgs, ... }: {
  # Which nixpkgs channel to use.
  channel = "stable-23.11"; # or "unstable"
  # Use https://search.nixos.org/packages to find packages
  packages = [
    pkgs.cargo
    pkgs.rustc
    pkgs.rustfmt
    pkgs.stdenv.cc
  ];
  # Sets environment variables in the workspace
  env = {
    RUST_SRC_PATH = "${pkgs.rustPlatform.rustLibSrc}";
  };
  idx = {
    # Search for the extensions you want on https://open-vsx.org/ and use "publisher.id"
    extensions = [
      "rust-lang.rust-analyzer"
      "tamasfe.even-better-toml"
      "serayuzgur.crates"
      "vadimcn.vscode-lldb"
    ];
    # Enable previews and customize configuration
    previews = {};
  };
}

In case you had not already seen this, does this provide any help to unblock you?

If you had already seen this, is the Rust package referenced here the one that is out-of-date?

Cheers,
Kirupa

Sadly, the rust package in the stable Nix release is slightly out of date, which is what lead to the question.

1 Like

Ah! Do the suggestions listed here help you out?

https://www.reddit.com/r/rust/comments/173hkrf/best_way_to_use_latest_rust_toolchain_on_nixos/

Sadly, I couldn’t get any of them to work due to my lack of knowledge about Nix, and also, most of them want to edit a system file in Nix OS which is probably impossible on IDX, or flake I have no idea how to setup.

This is actually quite annoying, the stable channel have out-of-date package that only update once every 6 month. Alternatively you can go to the unstable channel that have a … unstable rust release (which have issue that is causing it to be unusable for me)

We have plans to import developer experience for such use cases, as well as make it build fast, but for now you can try the following and see if it works for you(note it will take some time to build the environment):

{ pkgs, ... }: 
let 
  fenix = import (fetchTarball "https://github.com/nix-community/fenix/archive/9af557bccdfa8fb6a425661c33dbae46afef0afa.tar.gz") { };
  rust = fenix.stable; # feel free to use beta or latest(nightly) instead
in {
  channel = "stable-23.11"; # or "unstable"

  packages = [
    rust.minimalToolchain
    pkgs.stdenv.cc
  ];
  env = {
    RUST_SRC_PATH = "${rust.rust-src}";
  };
  idx = {
    extensions = [
      "rust-lang.rust-analyzer"
      "tamasfe.even-better-toml"
      "serayuzgur.crates"
      "vadimcn.vscode-lldb"
    ];
}

I see, thanks for your help! I look forward to the future solution that will make similar use cases easier to set up, but it is nice to know that workaround exists. Thanks!

And also, as a side notes: Can we get some nice and detailed examples on how to use Nix in Project IDX? First of all, I would assume that a majority of users (or it’s just a skill issue on my part) won’t be too experienced with Nix in general. Secondly, the system Project IDX doesn’t seem to be Flake, Nixos or Nix package manager, making it hard to configure Project IDX.

The existing docs successfully document what is added to Nix in IDX, but it does somewhat assume all user are knowledgeable in Nix, but then again, most Nix documentation doesn’t seems to work on Project IDX as most of those documentation seemed to written for: flake, nixos or package manager (or even overlay).

Sorry if this is too much text or it is too much to ask, I suppose it is somewhat my fault for not fully understanding how to use Nix

1 Like

This is a totally valid ask! We are still in the process of evolving our Nix support, but we will look into documenting more of the Nix behaviors.

As an interim step, if we made all of our templates (and by extension idx.nix files) public, would that help in highlighting how we are using Nix ourselves to configure the dev environment?

Yes, that would, it would also nice if rust example like @Vova, is also included. Because that particular example, I now understand Nix more than ever. This is because in Fenix
and a lot of other similar Nix documentation, they have this particular code.

let
  fenix = import (fetchTarball "https://github.com/nix-community/fenix/archive/main.tar.gz") { };
in
fenix.minimal.toolchain

I never understand how to use this code until I saw @Vova 's example, so I would say adding example of existing template plus a few extra edge cases would be amazing for people who aren’t familiar with nix, but are willing to learn. Thanks!

Ok - we’ll see what we can do here! :slight_smile:

Can you share the link to the Rust example that Vova shared? I’m not able to find it at quick glance.

Vova’s example:

{ pkgs, ... }: 
let 
  fenix = import (fetchTarball "https://github.com/nix-community/fenix/archive/9af557bccdfa8fb6a425661c33dbae46afef0afa.tar.gz") { };
  rust = fenix.stable; # feel free to use beta or latest(nightly) instead
in {
  channel = "stable-23.11"; # or "unstable"

  packages = [
    rust.minimalToolchain
    pkgs.stdenv.cc
  ];
  env = {
    RUST_SRC_PATH = "${rust.rust-src}";
  };
  idx = {
    extensions = [
      "rust-lang.rust-analyzer"
      "tamasfe.even-better-toml"
      "serayuzgur.crates"
      "vadimcn.vscode-lldb"
    ];
}

Link to Fenix’s example: GitHub - nix-community/fenix: Rust toolchains and rust-analyzer nightly for Nix [maintainer=@figsoda]

let
  fenix = import (fetchTarball "https://github.com/nix-community/fenix/archive/main.tar.gz") { };
in
fenix.minimal.toolchain

You can see the resemblance in the 2 example, that’s why I found it useful.