Talk:Rust

From NixOS Wiki
Revision as of 21:34, 10 November 2023 by imported>Cessationoftime (→‎Intellij Idea and Rust, launching from terminal.: new section)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

How to integrate with intellij-rust?

intellij-rust is able to detect the toolchain but not the standard library, and the "Download via Rustup" "link" doesn't seem to do anything. If anyone's been able to set up IDEA to work with Rust on Nix any tips would be really helpful.

I've tried a couple suggestions:

  • rustup run stable idea-ultimate
  • Adding RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}"; to mkShell in shell.nix

L0b0 (talk) 01:37, 5 September 2021 (UTC)Reply

Intellij Idea and Rust, launching from terminal.

I seem to be having success by using nix develop, flakes and launching Intellij from the terminal. Launching this way lets me capture the environment variables of my development environment. I used to get errors when doing this but I found the key is to export an XDG environment variable in the shellHook. I also use a shellAlias to launch idea with the command "@idea". This requires a .XDG folder be added to the development environment for the XDG_DATA_HOME. There are three other XDG variables that may need to be similarly exported too and folders created. But this one is the only one I have identified that is definitely problematic.

 environment.shellAliases = { 
   "@idea" = "idea-community </dev/null &>/dev/null &; echo 'Launching Intellij idea-community'"; # launch idea from the command line. This let's it access the environment variables in the 'nix develop' shell environment.
   };

I combine that with:

allows one to automatically enter nix develop shell:

 direnv = {
   enable = true;
   nix-direnv.enable = true;
 };

a .envrc file with the following two lines:

 strict_env
 use flake

I then modify the basic rust-overlay flake found here: https://github.com/oxalica/rust-overlay#use-in-devshell-for-nix-develop

The key is to add:

 shellHook = ''
   export XDG_DATA_HOME="$PWD/.XDG/share"
 '';

The XDG_DATA_HOME variable export avoids the following error message from Intellij Idea:

 /nix/store/hl5v6vjsf3yhi9a2fyf5vnq06w6c257r-rust-default-1.64.0/bin/cargo check --message-format json --workspace --all-targets -Z unstable-options --keep-going
 error: failed to run `rustc` to learn about target-specific information
 error: failed to run `rustc` to learn about target-specific information
 Caused by:
 could not execute process `/home/username/.local/share/JetBrains/IdeaIC2023.1/intellij-rust/bin/linux/x86-64/intellij-rust-native-helper /nix/store/hl5v6vjsf3yhi9a2fyf5vnq06w6c257r-rust-default-1.64.0/bin/rustc - --crate-name ___ --print=file-names --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=cfg` (never executed)
 Caused by:
  No such file or directory (os error 2)

My unredacted flake.nix that seems to be working with Intellij is here: https://gist.github.com/cessationoftime/e4e988059727ba664f8f105bd5526c0c