diff --git a/modules/lsp/servers/custom/README.md b/modules/lsp/servers/custom/README.md new file mode 100644 index 00000000..5f494fc5 --- /dev/null +++ b/modules/lsp/servers/custom/README.md @@ -0,0 +1,8 @@ +# Custom LSP server modules + +This directory contains modules that relate to a specific server. +Files are auto-imported into the submodule based on their name. + +For example, a file named `foo.nix` or `foo/default.nix` would be imported into the `lsp.servers.foo` submodule. + +A corresponding `lsp.servers.` option must exist for every module in this directory. diff --git a/modules/lsp/servers/server.nix b/modules/lsp/servers/server.nix index be7e4776..b683b843 100644 --- a/modules/lsp/servers/server.nix +++ b/modules/lsp/servers/server.nix @@ -90,5 +90,13 @@ in imports = [ ./server-renames.nix - ]; + ] + # We cannot use `config._module.args.name` in imports, since `config` causes inf-rec. + # Therefore we can only import custom modules when we have an externally supplied `name`. + ++ lib.optionals (args ? name) ( + lib.filter lib.pathExists [ + ./custom/${args.name}.nix + ./custom/${args.name}/default.nix + ] + ); } diff --git a/tests/test-sources/modules/lsp.nix b/tests/test-sources/modules/lsp.nix index e2a959a6..ea6c36bb 100644 --- a/tests/test-sources/modules/lsp.nix +++ b/tests/test-sources/modules/lsp.nix @@ -221,4 +221,37 @@ ++ assertSuffix "haskell-language-server" hls.package ); }; + + # Assert that we don't have any redundant custom modules + custom-server-modules = + { lib, options, ... }: + let + rootPrefix = toString ../../../. + "/"; + customDir = ../../../modules/lsp/servers/custom; + serverOptions = (opt: opt.type.getSubOptions opt.loc) options.lsp.servers; + modules = lib.pipe customDir [ + builtins.readDir + (lib.filterAttrs (name: _: !(serverOptions ? ${lib.strings.removeSuffix ".nix" name}))) + (lib.mapAttrsToList ( + name: type: customDir + "/${name}" + lib.optionalString (type == "directory") "/default.nix" + )) + (builtins.filter (lib.strings.hasSuffix ".nix")) + (builtins.filter lib.pathExists) + (map (module: lib.strings.removePrefix rootPrefix (toString module))) + ]; + in + { + test.buildNixvim = false; + + assertions = [ + { + assertion = modules == [ ]; + message = '' + The following custom modules do not correspond to an LSP server option:${ + lib.concatMapStrings (module: "\n- ${module}") modules + } + ''; + } + ]; + }; }