我最近切换到 NixOS 并使用 Nix 包管理器安装了 Rust 工具链(rustup、cargo、rustc)。
但是,当我尝试打开预先配置的 Neovim 设置时,出现以下 LSP 错误:
Client rust-analyzer quit with exit code 1 and signal 0. Check log for errors: /home/user/.local/state/nvim/lsp.log
查看 lsp.log 文件,我看到以下错误消息:
[ERROR] .../vim/lsp/rpc.lua:420 "rpc" "/run/current-system/sw/bin/rust-analyzer" "stderr" "error: Unknown binary 'rust-analyzer' in official toolchain 'stable-x86_64-unknown-linux-gnu'.\n"
起初,我以为这可能是 Neovim 的问题,与 Nix 管理软件包的方式有关。因此,我尝试直接从终端运行 rust-analyzer,但仍然出现同样的错误:
$ rust-analyzer
error: Unknown binary 'rust-analyzer' in official toolchain 'stable-x86_64-unknown-linux-gnu'.
我意识到 rust-analyzer 可能没有随 Nix 的 rustup 包自动安装,因此我也使用 Nix 包管理器明确安装了 rust-analyzer。
尽管如此,锈蚀分析仪的行为仍然相同。
运行 which rust-analyzer 正确显示路径:
$ which rust-analyzer
/run/current-system/sw/bin/rust-analyzer
但直接使用绝对路径运行它仍然会产生相同的“未知二进制”错误:
$ /run/current-system/sw/bin/rust-analyzer
error: Unknown binary 'rust-analyzer' in official toolchain 'stable-x86_64-unknown-linux-gnu'.
此外,我重新安装了所有 Rust 软件包,包括 rustfmt 和 rustc,但现在尝试使用它们时(无论是直接在终端中还是通过 Neovim 集成)仍然遇到类似的问题。具体来说,我收到错误,提示 rustfmt 和 rustc 无法选择要运行的版本,因为没有明确指定版本,并且没有配置默认版本。我还使用 which 命令查找了这些二进制文件的位置,即使确认了正确的路径,运行它们仍然会导致同样的错误。
$ rustfmt
error: rustup could not choose a version of rustfmt to run, because one wasn't specified explicitly, and no default is configured.
help: run 'rustup default stable' to download the latest stable release of Rust and set it as your default toolchain.
$ rustc
error: rustup could not choose a version of rustc to run, because one wasn't specified explicitly, and no default is configured.
help: run 'rustup default stable' to download the latest stable release of Rust and set it as your default toolchain.
这让我想到几个问题:
考虑到 rust-analyzer 持续存在的“未知二进制”错误以及 rustfmt/rustc 的新错误,我是否应该通过 rustup(例如 rustup default stable、rustup component add rust-analyzer)而不是使用 Nix 包管理器来安装这些组件?通过 rustup 安装组件是否在某种程度上与 NixOS 的声明性和可重复性相矛盾?NixOS 的依赖项最好由 Nix 本身管理。
我是否错过了配置步骤,或者误解了 rustup(通过 Nix 安装)如何与 NixOS 上的 rust-analyzer、rustfmt 和 rustc(也通过 Nix 安装)等组件交互?
主要原因:整个问题的发生是因为我试图将Rust 工具( 、 、 、 )与同样通过 Nix 安装的工具( 、 、、 )
rustup
一起使用(尽管是通过 Nix 安装的)。结论是:在 NixOS 上,如果您直接使用 Nix 管理 Rust 工具链,通常不应安装。rustc
cargo
rust-analyzer
rustfmt
rustup
解释:
Nix/NixOS 已经完成了传统系统上可以完成的“工作”
rustup
,但是是以声明的方式与 Nix 包系统集成的。rustup
旨在管理其自己的目录 (~/.rustup
) 中的不同 Rust 版本和组件,充当诸如、等命令的代理。rustc
cargo
当我
rustup
通过 Nix 安装时,它被放置在系统路径中,但它仍然试图像 一样运行rustup
,寻找由自己管理的工具链并期望像rustup default stable
或 这样的命令rustup component add
。