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

How to use the overlay for Rust Language Server? #20

Closed
srid opened this issue Apr 7, 2021 · 11 comments
Closed

How to use the overlay for Rust Language Server? #20

srid opened this issue Apr 7, 2021 · 11 comments

Comments

@srid
Copy link

srid commented Apr 7, 2021

I use VSCode, and it fails to start the Rust Language Server for my project using this overlay. From IDE logs,

Error: Command failed: rustup toolchain list
/bin/sh: rustup: command not found

What's the idiomatic way to configure this overlay in my flake.nix[1] so that when VSCode is launched in nix develop to the point that the Rust IDE extension works?

Thanks!

[1] You can find mine here: https://github.com/srid/bouncy/blob/afec3ca7c8771c035adfcbf32031f31f1bf568a4/flake.nix

@srid srid changed the title For using with Rust Language Server How to use the overlay for Rust Language Server? Apr 7, 2021
@srid
Copy link
Author

srid commented Apr 7, 2021

I ended up switching to a different language server, and had to specify a manual list of extensions to include rust-analyzer: srid/rust-nix-template@b767bad

@srid srid closed this as completed Apr 7, 2021
@oxalica
Copy link
Owner

oxalica commented Apr 7, 2021

There are already rust-analyzer and vscode-extensions.matklad.rust-analyzer available in nixpkgs, with support of customizing rust library src path.

@srid
Copy link
Author

srid commented Apr 7, 2021

I prefer to install VSCode extensions outside of NixOS (I use Settings Sync). As for rust-analyzer - if I'm using this overlay, shouldn't I aim to stick to Rust packages from this overlay as much as possible rather than nixpkgs upstream?

@henry-hz
Copy link

henry-hz commented Dec 21, 2021

@oxalica , hi there, I am also having difficult to use language server with neovim, just installed rust-analyzer and coc-rust-analyzer.... it complains that the rust-src is not installed.

[coc.nvim] rust-analyzer failed to start, run ':CocCommand rust-analyzer.reloadWorkspace' to reload       

@oxalica
Copy link
Owner

oxalica commented Dec 21, 2021

@oxalica , hi there, I am also having difficult to use language server with neovim, just installed rust-analyzer and coc-rust-analyzer....

[coc.nvim] rust-analyzer failed to start, run ':CocCommand rust-analyzer.reloadWorkspace' to reload       

rust-analyzer is already in nixpkgs, you could get it without using this overlay. Just add environment.systemPackages = [ pkgs.rust-analyzer ]; in your system config, or using other approach like home-manager to make it available in your PATH should be enough.

@henry-hz
Copy link

henry-hz commented Dec 21, 2021

Thanks @oxalica , I added, and the rls started, but complained about the rust-src. I am trying to figure out how to add the rust-src in my home-manager. Anyway I need to use your overlay to be updated to rustc 1.57, and I am avoiding to use rustup. I saw in the README the possibility below, but need help to understand how to add to the home-manager...

rust-bin.stable.latest.default.override {
  extensions = [ "rust-src" ];
}

@m1cr0man
Copy link

I figured this out but I'm not sure if I'm doing this right. I noticed rust-analyzer-preview defined here and rust-src declared below and realised maybe I can add it as an extension in the override. So I tried this:

mkShell {
  buildInputs = [
    (rust-bin.stable.latest.default.override {
        extensions = [ "rust-src" "rust-analyzer-preview" ];
    })
  ];
}

But that gave me this error:

error: Component resolution failed for rust-default-1.57.0
       Host component `rust-analyzer-preview` doesn't support `x86_64-unknown-linux-gnu`
       note: profile components: rustc cargo rust-std rust-mingw rust-docs rustfmt-preview clippy-preview
       note: selected targets: x86_64-unknown-linux-gnu
       note: selected extensions: rust-src rustfmt rust-analyzer-preview
       note: components available for x86_64-unknown-linux-gnu: cargo clippy clippy-preview llvm-tools-preview reproducible-artifacts rls rls-preview rust rust-analysis rust-docs rust-src rust-std rustc rustc-dev rustc-docs rustfmt rustfmt-preview
       note: check here to see all targets and which components are available on each targets:
             https://rust-lang.github.io/rustup-components-history

So I went to the mentioned URL and noticed that the nightlies definitely do have rust-analyzer-preview so I changed my rust-bin to rust-bin.nightly."2021-12-22".default and hey presto it compiled!

All that said and done, I tried simply adding pkgs.rust-analyzer to my buildInputs as recommended by @oxalica and sure enough that works just fine, but as a newbie Rust dev simply trying to get a working env set up on Nix, I'm not sure what the consequences are of this. Will this end up with my rust-analyzer using a different source/toolchain than the rest of my Rust-related binaries? @henry-hz what problems did you encounter specifically? I don't get any complaints.

I am using Nix Environment Selector to make sure the rust-analyzer plugin picks up these binaries.

@henry-hz
Copy link

@m1cr0man thanks for the follow-up, in fact, I feel that I lack enough nix knowledge to understand how to have the overlay working with neovim or vscode, but I just gave another step, the rust-src is declared, but still not on the right path, so I am still receiving :

rust-analyzer failed to load workspace: Failed to find sysroot for Cargo.toml
file /home/henry/code/r1/Cargo.tom Is rust-src installed?: can't load standard
library from sysroot  /nix/store/zzwjgnvpd84x3qzpz1gp4bsy27k9gyxn-rust-default-57.0
(discovered via `rustc --print sysroot`) 
try installing the Rust source the same way you installed rustc

My config below

All inside the ./config/nixpkgs

overlay.nix

[ (import (builtins.fetchTarball "https://github.com/oxalica/rust-overlay/archive/master.tar.gz")) ]

home.nix

{ builtins, config, pkgs, lib, nixpkgs, ... }:

let
  dag = config.lib.dag;
  pkg-list-opts = {
    pkgs = pkgs;
    # import from stable and master
    unstable = import (fetchTarball https://nixos.org/channels/nixos-unstable/nixexprs.tar.xz) {};
   };
  dev      = import ./env/dev.nix pkg-list-opts;
  sys      = import ./env/sys.nix pkg-list-opts;
  zsh_mod  = import ./app/zsh.nix pkgs;
  git_mod  = import ./app/git.nix pkgs;
  nvim_mod = import ./app/nvim.nix pkgs;
  tmux_mod = import ./app/tmux.nix pkgs;
in
{
  home.username = "henry";
  home.homeDirectory = "/home/henry";

  home.packages = dev ++ sys;

  programs.zsh = zsh_mod;
  programs.neovim = nvim_mod;
  programs.git = git_mod;
  programs.tmux = tmux_mod;

  home.stateVersion = "21.11";

  programs.home-manager.enable = true;
}

env/dev.nix

{ pkgs, unstable }:

let
  # rus = rust-bin.stable.latest.default.override {
  #   extensions = [ "rust-src" ];
  # };

  last_flutter = pkgs.fetchFromGitHub {
    owner = "flutter";
    repo = "flutter";
    rev = "2.8.1";
    sha256 = "0nzvshv3g559mqrlf4906c9iw4jw8j83dxjax275b2wi8ix0wgmj";
  };
in
  with pkgs; [

    # C
    gcc gnumake
    
    # haskell
    cabal-install cabal2nix ghc
    haskellPackages.haskell-language-server
    hlint

    # rust
    rust-bin.stable.latest.default
    unstable.rust-analyzer
    rust-bin.stable.latest.rust-src

  ]

@henry-hz
Copy link

@m1cr0man seems I just found a fix that should also work for you. I am getting the rust-analyzer from outside (unstable nix pkgs). The code completion is working, but nvim still shows RLS starting. The coc.nvim also still complains "Rustup not available", but the stuff seems to be working.

    # rust
    (rust-bin.stable.latest.default.override {
        extensions = [ "rust-src"];
    })
    unstable.rust-analyzer

@m1cr0man
Copy link

m1cr0man commented Jan 14, 2022

@henry-hz Thanks for sharing that! I forgot to follow up post-christmas until now 😅 Your final solution is very similar to what I ended up using too. I managed to create a nix-shell compatible file for use in VS Code, and the rust-analyzer plugin does not seem to be stuck in a loading loop for me fortunately. I also learned much more about flakes over christmas, and thus came up with this quite elegant way to download everything required (if you have the freedom to use nix develop + a flake.nix, do that instead).

let
  rustVersion = "1.57.0";
  defaultPkgs = import (builtins.getFlake "nixpkgs") {
    overlays = [ (builtins.getFlake "github:oxalica/rust-overlay").overlay ];
  };
in { pkgs ? defaultPkgs }:
with pkgs;
mkShell {
  buildInputs = [
    (rust-bin.stable."${rustVersion}".default.override {
        extensions = [ "rust-src" "rustfmt" ];
    })
    rust-analyzer
  ];
}

Note I'm using import (builtins.getFlake "nixpkgs") here, which means it only works in Nix 2.4+. I have pinned my nixpkgs at the system level so that this is stable. I hope you find some use in this too!

@henry-hz
Copy link

@m1cr0man indeed very elegant and useful ! thanks for sharing :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants