modules/lsp/server: declare package defaults

Convert the `attrsOf (servers.nix)` option to a freeform submodule.

Declare a `servers.nix` option for each lsp server listed in
`lsp-packages.nix` that has a known nixpkgs package.
This commit is contained in:
Matt Sturgeon 2025-04-29 02:44:19 +01:00
parent 276abde288
commit e34eaf8395
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299
3 changed files with 98 additions and 29 deletions

View file

@ -2,6 +2,7 @@
lib,
config,
options,
pkgs,
...
}:
let
@ -9,6 +10,55 @@ let
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 = {
@ -25,7 +75,11 @@ in
};
servers = lib.mkOption {
type = types.attrsOf (types.submodule ./server.nix);
type = types.submodule {
freeformType = types.attrsOf (mkServerType { });
options = builtins.mapAttrs mkServerOption serverPackages;
};
description = ''
LSP servers to enable and/or configure.
@ -35,7 +89,7 @@ in
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
[`plugins.lspconfig`]: ../../plugins/lspconfig/index.md
'';
default = { };
example = {