modules/lsp/servers: move to dedicated file/dir

Move the code related to the `lsp.servers` option into a dedicated module,
cleaning up `modules/lsp/default.nix`.
This commit is contained in:
Matt Sturgeon 2025-05-05 22:36:48 +01:00
parent 64cd675ece
commit 5308425718
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299
5 changed files with 175 additions and 166 deletions

View file

@ -0,0 +1,163 @@
{
lib,
config,
options,
pkgs,
...
}:
let
inherit (lib) types;
inherit (lib.nixvim) toLuaObject;
cfg = config.lsp;
# Import `server.nix` and apply args
# For convenience, we set a default here for args.pkgs
mkServerModule = args: lib.modules.importApply ./server.nix ({ inherit pkgs; } // args);
# Create a submodule type from `server.nix`
# Used as the type for both the freeform `lsp.servers.<name>`
# and the explicitly declared `lsp.servers.*` options
mkServerType = args: types.submodule (mkServerModule args);
# Create a server option
# Used below for the `lsp.servers.*` options
mkServerOption =
name: args:
let
homepage = lib.pipe options.lsp.servers [
# Get suboptions of `lsp.servers`
(opt: opt.type.getSubOptions opt.loc)
# Get suboptions of `lsp.servers.<name>`
(opts: opts.${name}.type.getSubOptions opts.${name}.loc)
# Get package option's homepage
(opts: opts.package.default.meta.homepage or null)
];
# If there's a known homepage for this language server,
# we'll link to it in the option description
nameLink = if homepage == null then name else "[${name}](${homepage})";
in
lib.mkOption {
type = mkServerType args;
description = ''
The ${nameLink} language server.
'';
default = { };
};
# Combine `packages` and `customCmd` sets from `lsp-packages.nix`
# We use this set to generate the package-option defaults
serverPackages =
let
inherit (import ../../../plugins/lsp/lsp-packages.nix)
packages
customCmd
;
in
builtins.mapAttrs (name: v: {
inherit name;
package = v.package or v;
}) (packages // customCmd);
in
{
options.lsp = {
servers = lib.mkOption {
type = types.submodule [
{
freeformType = types.attrsOf (mkServerType { });
}
{
options = builtins.mapAttrs mkServerOption serverPackages;
}
{
# `*` is effectively a meta server, where shared config & defaults can be set.
# It shouldn't have options like `activate` or `package` which relate to "real" servers.
# Therefore, we use a bespoke `global-server.nix`, which is inspired by the full `server.nix` module.
options."*" = lib.mkOption {
description = ''
Global configuration applied to all language servers.
'';
type = types.submodule ./global-server.nix;
default = { };
};
}
];
description = ''
LSP servers to enable and/or configure.
This option is implemented using neovim's `vim.lsp` lua API.
You may also want to use [nvim-lspconfig] to install _default configs_ for many language servers.
This can be installed using [`${options.plugins.lspconfig.enable}`][`plugins.lspconfig`].
[nvim-lspconfig]: ${options.plugins.lspconfig.package.default.meta.homepage}
[`plugins.lspconfig`]: ../../plugins/lspconfig/index.md
'';
default = { };
example = {
"*".settings = {
root_markers = [ ".git" ];
capabilities.textDocument.semanticTokens = {
multilineTokenSupport = true;
};
};
luals.enable = true;
clangd = {
enable = true;
settings = {
cmd = [
"clangd"
"--background-index"
];
root_markers = [
"compile_commands.json"
"compile_flags.txt"
];
filetypes = [
"c"
"cpp"
];
};
};
};
};
};
config =
let
enabledServers = lib.pipe cfg.servers [
builtins.attrValues
(builtins.filter (server: server.enable))
];
# Collect per-server warnings
serverWarnings = lib.pipe cfg.servers [
builtins.attrValues
(builtins.catAttrs "warnings")
builtins.concatLists
];
in
{
extraPackages = builtins.catAttrs "package" enabledServers;
lsp.luaConfig.content =
let
mkServerConfig =
server:
let
luaName = toLuaObject server.name;
luaSettings = toLuaObject server.settings;
in
[
(lib.mkIf (server.settings != { }) "vim.lsp.config(${luaName}, ${luaSettings})")
(lib.mkIf (server.activate or false) "vim.lsp.enable(${luaName})")
];
in
lib.mkMerge (builtins.concatMap mkServerConfig enabledServers);
# Propagate per-server warnings
warnings = lib.mkIf (serverWarnings != [ ]) serverWarnings;
};
}

View file

@ -0,0 +1,49 @@
{ lib, ... }:
let
inherit (lib) types;
in
{
options = {
enable = lib.mkOption {
type = types.bool;
description = "Whether to enable global defaults shared by all servers.";
default = true;
example = false;
};
name = lib.mkOption {
type = types.str;
description = ''
The name to use for global defaults shared by all servers.
Supplied to functions like `vim.lsp.config()`.
Will always be `"*"`.
'';
readOnly = true;
};
settings = lib.mkOption {
type = with types; attrsOf anything;
description = ''
Default configuration shared by all servers.
Will be merged by neovim using the behaviour of [`vim.tbl_deep_extend()`](https://neovim.io/doc/user/lua.html#vim.tbl_deep_extend()).
'';
default = { };
example = {
root_markers = [ ".git" ];
capabilities.textDocument.semanticTokens = {
multilineTokenSupport = true;
};
};
};
};
imports = [
./server-renames.nix
];
# Define the read-only `name` here, instead of via `default`, to avoid documenting it twice
config.name = "*";
}

View file

@ -0,0 +1,21 @@
{ lib, ... }:
{
# NOTE: we need a warnings option for `mkRenamedOptionModule` to warn about unexpected definitions
# This can be removed when all rename aliases are gone
options.warnings = lib.mkOption {
type = with lib.types; listOf str;
description = "Warnings to propagate to nixvim's `warnings` option.";
default = [ ];
internal = true;
visible = false;
};
imports = [
# TODO: rename added 2025-04-30 (during the 25.05 cycle)
# The previous name `config` was introduced 2025-04-28 (during the 25.05 cycle)
# Because the previous name `config` never made it into a stable release,
# we could consider dropping this alias sooner than normal.
(lib.mkRenamedOptionModule [ "config" ] [ "settings" ])
];
}

View file

@ -0,0 +1,84 @@
# Usage: lib.importApply ./server.nix { /*args*/ }
{
name ? "the language server",
package ? null,
settings ? null,
pkgs ? { },
}@args:
{
lib,
name,
config,
...
}:
let
inherit (lib) types;
displayName = args.name or "the language server";
packageName = package.name or (lib.strings.removePrefix "the " displayName);
in
{
options = {
enable = lib.mkEnableOption displayName;
name = lib.mkOption {
type = types.maybeRaw types.str;
description = ''
The name to use for ${displayName}.
Supplied to functions like `vim.lsp.enable()`.
'';
# Use the supplied attr name, or fallback to the name module-arg
default = args.name or name;
defaultText = args.name or (lib.literalMD "the attribute name");
};
activate = lib.mkOption {
type = types.bool;
description = ''
Whether to call `vim.lsp.enable()` for ${displayName}.
'';
default = config.name != "*";
defaultText = lib.literalMD ''
`true`, unless the server's `name` is `*`
'';
example = false;
};
package = lib.mkPackageOption pkgs packageName {
nullable = true;
default = package.default or package;
example = package.example or null;
extraDescription = ''
${package.extraDescription or ""}
Alternatively, ${displayName} should be installed on your `$PATH`.
'';
};
settings = lib.mkOption {
type = with types; attrsOf anything;
description = ''
Configurations for ${displayName}. ${settings.extraDescription or ""}
'';
default = { };
example =
settings.example or {
cmd = [
"clangd"
"--background-index"
];
root_markers = [
"compile_commands.json"
"compile_flags.txt"
];
filetypes = [
"c"
"cpp"
];
};
};
};
imports = [
./server-renames.nix
];
}