nvf/docs/manual/configuring/languages/lsp.md
NotAShelf 3182811b8d
docs/manual: describe registring custom LSPs in the language section
Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I776aa1283b0d89016a4eb0de2b67e2f86a6a6964
2026-01-27 11:52:53 +03:00

2.3 KiB

LSP Custom Packages/Command

One of the strengths of nvf is convenient aliases to quickly configure LSP servers through the Nix module system. By default the LSP packages for relevant language modules will be pulled into the closure. If this is not desirable, you may provide a custom LSP package (e.g., a Bash script that calls a command) or a list of strings to be interpreted as the command to launch the language server. By using a list of strings, you can use this to skip automatic installation of a language server, and instead use the one found in your $PATH during runtime, for example:

vim.languages.java = {
  lsp = {
    enable = true;

    # This expects 'jdt-language-server' to be in your PATH or in
    # 'vim.extraPackages.' There are no additional checks performed to see
    # if the command provided is valid.
    package = ["jdt-language-server" "-data" "~/.cache/jdtls/workspace"];
  };
}

Custom LSP Servers

Neovim 0.11, in an effort to improve the out-of-the-box experience of Neovim, has introduced a new vim.lsp API that can be used to register custom LSP servers with ease. In nvf, this translates to the custom vim.lsp API that can be used to register servers that are not present in existing language modules.

The {option}vim.lsp.servers submodule can be used to modify existing LSP definitions OR register your own custom LSPs respectively. For example, if you'd like to avoid having NVF pull the LSP packages you may modify the start command to use a string, which will cause the LSP API to discover LSP servers from {env}PATH. For example:

{lib, ...}: { 
  vim.lsp.servers = {
    # Get `basedpyright-langserver` from PATH, e.g., a dev shell.
    basedpyright.cmd = lib.mkForce ["basedpyright-langserver" "--stdio"];

    # Define a custom LSP entry using `vim.lsp.servers`:
    ty = {
      cmd = lib.mkDefault [(lib.getExe pkgs.ty) "server"];
      filetypes = ["python"];
      root_markers = [
        ".git"
        "pyproject.toml"
        "setup.cfg"
        "requirements.txt"
        "Pipfile"
        "pyrightconfig.json"
      ];

    # If your LSP accepts custom settings. See `:help lsp-config` for more details
    # on available fields. This is a freeform field.
    settings.ty = { /* ... */ };
  };
}