grimm-nixos-laptop/common/tooling/lsp.nix

135 lines
3.8 KiB
Nix

{
pkgs,
config,
lib,
...
}:
let
inherit (config.grimmShared) enable tooling;
inherit (lib)
mkOption
types
mkIf
getName
mkEnableOption
getExe
filter
optionalString
concatLines
;
conf_def =
fmt:
(
''
[formatter.${getName fmt.package}]
command = "${fmt.command}"
includes = ${builtins.toJSON fmt.includes}
''
+ (optionalString (fmt.options != [ ]) "options = ${builtins.toJSON fmt.options}\n")
);
treefmt_conf = pkgs.writeText "treefmt.toml" (
concatLines (map (v: (conf_def v.fmt)) (filter (v: !isNull v.fmt) tooling.lang_servers))
);
find_conf = pkgs.writeShellScriptBin "find-treefmt-conf" "(${getExe pkgs.rfindup} treefmt.toml -e 2>/dev/null || echo ${treefmt_conf}) | tr -d '\n'";
in
{
config = mkIf (enable && tooling.enable) {
environment.systemPackages =
[ pkgs.treefmt ]
++ (map (v: v.lsp.package) (filter (v: !isNull v.lsp) tooling.lang_servers))
++ (map (v: v.fmt.package) (filter (v: !isNull v.fmt) tooling.lang_servers));
environment.shellAliases."treefmt" = "${getExe pkgs.treefmt} --config-file $(${getExe find_conf})";
grimmShared.tooling.nvim.extraLuaRC =
[ "vim.g.coq_settings = { auto_start = 'shut-up' }" ]
++ (map (
v:
"require'lspconfig'.${v.lsp.lspconf_mod_name}.setup(require('coq').lsp_ensure_capabilities(${v.lsp.lspconf_config}))"
) (filter (v: !isNull v.lsp && v.lsp.vimIntegration) tooling.lang_servers));
grimmShared.tooling.nvim.plugins = with pkgs.vimPlugins; [
nvim-lspconfig
coq_nvim
coq-artifacts
];
};
options.grimmShared.tooling.lang_servers = mkOption {
type = types.listOf (
types.submodule {
options = {
lsp = mkOption {
type = types.nullOr (
types.submodule (
{ config, ... }:
{
options = {
lspconf_mod_name = mkOption {
type = types.nullOr types.nonEmptyStr;
default = builtins.replaceStrings [ "-" ] [ "_" ] (getName config.package);
description = "lspconfig module name";
};
lspconf_config = mkOption {
type = types.nonEmptyStr;
default = "{}";
description = "options to pass to lspconfig";
};
package = mkOption {
type = types.package;
default = null;
description = "LSP package";
};
vimIntegration = mkEnableOption "Enable coq/nvim-lspconfig integration" // {
default = true;
};
};
}
)
);
};
fmt = mkOption {
type = types.nullOr (
types.submodule (
{ config, ... }:
{
options = {
package = mkOption {
type = types.package;
description = "FMT package";
};
options = mkOption {
type = types.listOf types.nonEmptyStr;
default = [ ];
};
includes = mkOption {
type = types.listOf types.nonEmptyStr;
default = [ ];
};
command = mkOption {
type = types.nonEmptyStr;
default = getExe config.package;
};
};
}
)
);
};
};
}
);
default = { };
description = "Language servers available on the system";
};
}