Skip to content

Commit e34eaf8

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 276abde commit e34eaf8

File tree

3 files changed

+98
-29
lines changed

3 files changed

+98
-29
lines changed

modules/lsp/default.nix

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,63 @@
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+
# Import `server.nix` and apply args
15+
# For convenience, we set a default here for args.pkgs
16+
mkServerModule = args: lib.modules.importApply ./server.nix ({ inherit pkgs; } // args);
17+
18+
# Create a submodule type from `server.nix`
19+
# Used as the type for both the freeform `lsp.servers.<name>`
20+
# and the explicitly declared `lsp.servers.*` options
21+
mkServerType = args: types.submodule (mkServerModule args);
22+
23+
# Create a server option
24+
# Used below for the `lsp.servers.*` options
25+
mkServerOption =
26+
name: args:
27+
let
28+
homepage = lib.pipe options.lsp.servers [
29+
# Get suboptions of `lsp.servers`
30+
(opt: opt.type.getSubOptions opt.loc)
31+
# Get suboptions of `lsp.servers.<name>`
32+
(opts: opts.${name}.type.getSubOptions opts.${name}.loc)
33+
# Get package option's homepage
34+
(opts: opts.package.default.meta.homepage or null)
35+
];
36+
37+
# If there's a known homepage for this language server,
38+
# we'll link to it in the option description
39+
nameLink = if homepage == null then name else "[${name}](${homepage})";
40+
in
41+
lib.mkOption {
42+
type = mkServerType args;
43+
description = ''
44+
The ${nameLink} language server.
45+
'';
46+
default = { };
47+
};
48+
49+
# Combine `packages` and `customCmd` sets from `lsp-packages.nix`
50+
# We use this set to generate the package-option defaults
51+
serverPackages =
52+
let
53+
inherit (import ../../plugins/lsp/lsp-packages.nix)
54+
packages
55+
customCmd
56+
;
57+
in
58+
builtins.mapAttrs (name: v: {
59+
inherit name;
60+
package = v.package or v;
61+
}) (packages // customCmd);
1262
in
1363
{
1464
options.lsp = {
@@ -25,7 +75,11 @@ in
2575
};
2676

2777
servers = lib.mkOption {
28-
type = types.attrsOf (types.submodule ./server.nix);
78+
type = types.submodule {
79+
freeformType = types.attrsOf (mkServerType { });
80+
options = builtins.mapAttrs mkServerOption serverPackages;
81+
};
82+
2983
description = ''
3084
LSP servers to enable and/or configure.
3185
@@ -35,7 +89,7 @@ in
3589
This can be installed using [`${options.plugins.lspconfig.enable}`][`plugins.lspconfig`].
3690
3791
[nvim-lspconfig]: ${options.plugins.lspconfig.package.default.meta.homepage}
38-
[`plugins.lspconfig`]: ../plugins/lspconfig/index.md
92+
[`plugins.lspconfig`]: ../../plugins/lspconfig/index.md
3993
'';
4094
default = { };
4195
example = {

modules/lsp/server.nix

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,74 @@
1+
# Usage: lib.importApply ./server.nix { /*args*/ }
2+
{
3+
name ? null,
4+
package ? null,
5+
# Avoid naming conflict with the `config` module arg
6+
# TODO: consider renaming the `config` option to something like `settings`?
7+
configOption ? null,
8+
pkgs ? { },
9+
}@args:
110
{ lib, name, ... }:
211
let
312
inherit (lib) types;
13+
displayName = args.name or "the language server";
14+
packageName = package.name or (lib.strings.removePrefix "the " displayName);
415
in
516
{
617
options = {
7-
enable = lib.mkEnableOption "the language server";
18+
enable = lib.mkEnableOption displayName;
819

920
name = lib.mkOption {
1021
type = types.maybeRaw types.str;
1122
description = ''
12-
The name of the language server, supplied to functions like `vim.lsp.enable()`.
23+
The name to use for ${displayName}.
24+
Supplied to functions like `vim.lsp.enable()`.
1325
'';
14-
default = name;
15-
defaultText = lib.literalMD "the attribute name";
26+
# Use the supplied attr name, or fallback to the name module-arg
27+
default = args.name or name;
28+
defaultText = args.name or (lib.literalMD "the attribute name");
1629
};
1730

1831
activate = lib.mkOption {
1932
type = types.bool;
2033
description = ''
21-
Whether to call `vim.lsp.enable()` for this server.
34+
Whether to call `vim.lsp.enable()` for ${displayName}.
2235
'';
2336
default = true;
2437
example = false;
2538
};
2639

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

3751
config = lib.mkOption {
3852
type = with types; attrsOf anything;
3953
description = ''
40-
Configurations for each language server.
54+
Configurations for ${displayName}. ${configOption.extraDescription or ""}
4155
'';
4256
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-
};
57+
example =
58+
configOption.example or {
59+
cmd = [
60+
"clangd"
61+
"--background-index"
62+
];
63+
root_markers = [
64+
"compile_commands.json"
65+
"compile_flags.txt"
66+
];
67+
filetypes = [
68+
"c"
69+
"cpp"
70+
];
71+
};
5772
};
5873
};
5974
}

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)