Skip to content

Commit 03fc6bd

Browse files
committed
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.
1 parent cd3cbb1 commit 03fc6bd

File tree

3 files changed

+86
-29
lines changed

3 files changed

+86
-29
lines changed

modules/lsp/default.nix

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,53 @@
22
lib,
33
config,
44
options,
5+
pkgs,
56
...
67
}:
78
let
89
inherit (lib) types;
910
inherit (lib.nixvim) toLuaObject;
1011

1112
cfg = config.lsp;
13+
14+
mkServerType = args: types.submodule (mkServerModule args);
15+
mkServerModule = args: lib.modules.importApply ./server.nix ({ inherit pkgs; } // args);
16+
17+
mkServerOption =
18+
name: args:
19+
let
20+
homepage = lib.pipe options.lsp.servers [
21+
# Get suboptions of `lsp.servers`
22+
(opt: opt.type.getSubOptions opt.loc)
23+
# Get suboptions of `lsp.servers.<name>`
24+
(opts: opts.${name}.type.getSubOptions opts.${name}.loc)
25+
# Get package option's homepage
26+
(opts: opts.package.default.meta.homepage or null)
27+
];
28+
29+
nameLink = if homepage == null then name else "[${name}](${homepage})";
30+
in
31+
lib.mkOption {
32+
type = mkServerType args;
33+
description = ''
34+
The ${nameLink} language server.
35+
'';
36+
default = { };
37+
};
38+
39+
# Combine `packages` and `customCmd` sets from `lsp-packages.nix`
40+
# We use this set to generate the package-option defaults
41+
serverPackages =
42+
let
43+
inherit (import ../../plugins/lsp/lsp-packages.nix)
44+
packages
45+
customCmd
46+
;
47+
in
48+
builtins.mapAttrs (name: v: {
49+
inherit name;
50+
package = v.package or v;
51+
}) (packages // customCmd);
1252
in
1353
{
1454
options.lsp = {
@@ -25,7 +65,11 @@ in
2565
};
2666

2767
servers = lib.mkOption {
28-
type = types.attrsOf (types.submodule ./server.nix);
68+
type = types.submodule {
69+
freeformType = types.attrsOf (mkServerType { });
70+
options = builtins.mapAttrs mkServerOption serverPackages;
71+
};
72+
2973
description = ''
3074
LSP servers to enable and/or configure.
3175
@@ -35,7 +79,7 @@ in
3579
This can be installed using [`${options.plugins.lspconfig.enable}`][`plugins.lspconfig`].
3680
3781
[nvim-lspconfig]: ${options.plugins.lspconfig.package.default.meta.homepage}
38-
[`plugins.lspconfig`]: ../plugins/lspconfig/index.md
82+
[`plugins.lspconfig`]: ../../plugins/lspconfig/index.md
3983
'';
4084
default = { };
4185
example = {

modules/lsp/server.nix

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,72 @@
1+
# Usage: lib.importApply ./server.nix { /*args*/ }
2+
{
3+
name ? null,
4+
package ? null,
5+
config ? null,
6+
pkgs ? { },
7+
}@args:
18
{ lib, name, ... }:
29
let
310
inherit (lib) types;
11+
displayName = args.name or "the language server";
12+
packageName = package.name or (lib.strings.removePrefix "the " displayName);
413
in
514
{
615
options = {
7-
enable = lib.mkEnableOption "the language server";
16+
enable = lib.mkEnableOption displayName;
817

918
name = lib.mkOption {
1019
type = types.maybeRaw types.str;
1120
description = ''
12-
The name of the language server, supplied to functions like `vim.lsp.enable()`.
21+
The name to use for ${displayName}.
22+
Supplied to functions like `vim.lsp.enable()`.
1323
'';
14-
default = name;
15-
defaultText = lib.literalMD "the attribute name";
24+
# Use the supplied attr name, or fallback to the name module-arg
25+
default = args.name or name;
26+
defaultText = args.name or (lib.literalMD "the attribute name");
1627
};
1728

1829
activate = lib.mkOption {
1930
type = types.bool;
2031
description = ''
21-
Whether to call `vim.lsp.enable()` for this server.
32+
Whether to call `vim.lsp.enable()` for ${displayName}.
2233
'';
2334
default = true;
2435
example = false;
2536
};
2637

27-
package = lib.mkOption {
28-
type = with types; nullOr package;
29-
default = null;
30-
description = ''
31-
Package to use for this language server.
38+
package = lib.mkPackageOption pkgs packageName {
39+
nullable = true;
40+
default = package.default or package;
41+
example = package.example or null;
42+
extraDescription = ''
43+
${package.extraDescription or ""}
3244
33-
Alternatively, the language server should be available on your `$PATH`.
45+
Alternatively, ${displayName} should be installed on your `$PATH`.
3446
'';
3547
};
3648

3749
config = lib.mkOption {
3850
type = with types; attrsOf anything;
3951
description = ''
40-
Configurations for each language server.
52+
Configurations for ${displayName}. ${config.extraDescription or ""}
4153
'';
4254
default = { };
43-
example = {
44-
cmd = [
45-
"clangd"
46-
"--background-index"
47-
];
48-
root_markers = [
49-
"compile_commands.json"
50-
"compile_flags.txt"
51-
];
52-
filetypes = [
53-
"c"
54-
"cpp"
55-
];
56-
};
55+
example =
56+
config.example or {
57+
cmd = [
58+
"clangd"
59+
"--background-index"
60+
];
61+
root_markers = [
62+
"compile_commands.json"
63+
"compile_flags.txt"
64+
];
65+
filetypes = [
66+
"c"
67+
"cpp"
68+
];
69+
};
5770
};
5871
};
5972
}

plugins/by-name/lspconfig/default.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ lib.nixvim.plugins.mkNeovimPlugin {
3535
> setup that relate to neovim's builtin LSP and are now being moved to the
3636
> new [`lsp`] module.
3737
38-
[`lsp`]: ../../lsp/servers.md
38+
[`lsp`]: ../../lsp/servers/index.md
3939
[`plugins.lsp`]: ../lsp/index.md
4040
[nvim-lspconfig]: ${opts.package.default.meta.homepage}
4141
'';

0 commit comments

Comments
 (0)