Proper way to run a language server in a nix-shell with neovim lspconfig

Howdy,

Using nix-shell with rust_analyzer

I’m programming in rust using rust_analyzer. In order for rust_analyzer to work, I need to pull in dependencies to my project through a nix-shell. Meaning, I need to manually run nix-shell and then neovim, which is annoying. Instead, I’m trying to run rust_analyzer directly through nix-shell like so in the following config:

I have

vim.lsp.config('rust_analyzer', {
  cmd = { "nix-shell /home/apple/repos/nixos/devshells/rust/shell.nix --run 'rust-analyzer'" },
  settings = {
      -- ...
    },
  },
})

This throws the following error in :LspLog

[ERROR][2025-06-16 19:42:30] .../lua/vim/lsp.lua:473	"cannot start rust_analyzer due to config error: ...ovim-unwrapped-0.11.1/share/nvim/runtime/lua/vim/lsp.lua:462: cmd: expected expected function or table with executable command, got table: 0x7fda9a2cf4b0. Info: nix-shell /home/apple/repos/nixos/devshells/rust/shell.nix --run 'rust-analyzer' is not executable"

What is the proper way to setup the lspconfig cmd property or is there a more idiomatic way to achieve what I’m doing.

Other Info

Note, I did consider simply aliasing nvim to run with nix-shell but the small delay entering neovim is distracting in my workflow, ontop of being unnecessary when I open non-rust files.

Here is my devshells/rust/shell.nix I’m using for completeness

{ pkgs ? import <nixpkgs> { } }:

with pkgs;

mkShell rec {
  nativeBuildInputs = [
    pkg-config
  ];
  buildInputs = [
    udev alsa-lib-with-plugins vulkan-loader
    xorg.libX11 xorg.libXcursor xorg.libXi xorg.libXrandr # To use the x11 feature
    libxkbcommon wayland # To use the wayland feature
  ];
  LD_LIBRARY_PATH = lib.makeLibraryPath buildInputs;
}

I’m using rust specifically for Bevy, and my shell.nix comes from as a template from their docs.

Thanks for the help hacking!

This will look for a binary literally named nix-shell /home/apple/repos/nixos/devshells/rust/shell.nix --run 'rust-analyzer', you have to split cmd (see NeoVim’s documentation):

{ "nix-shell", "/home/apple/repos/nixos/devshells/rust/shell.nix", "--run", "rust-analyzer" }

Aaaaaaah, I got it. Thank you! And for the doc page, I completely missed that.