neovim-flake/modules/plugins/languages/nim.nix

118 lines
2.9 KiB
Nix
Raw Permalink Normal View History

2024-02-07 05:24:44 +01:00
{
config,
pkgs,
2024-02-07 05:24:44 +01:00
lib,
...
}: let
inherit (builtins) attrNames;
inherit (lib.options) mkEnableOption mkOption;
inherit (lib.modules) mkIf mkMerge;
inherit (lib.lists) isList;
inherit (lib.types) enum either listOf package str;
inherit (lib.nvim.types) mkGrammarOption;
inherit (lib.nvim.lua) expToLua;
2024-02-07 05:24:44 +01:00
cfg = config.vim.languages.nim;
defaultServer = "nimlsp";
servers = {
nimlsp = {
package = pkgs.nimlsp;
lspConfig = ''
lspconfig.nimls.setup{
capabilities = capabilities;
on_attach = default_on_attach;
cmd = ${
if isList cfg.lsp.package
then expToLua cfg.lsp.package
2024-02-07 05:24:44 +01:00
else ''
{"${cfg.lsp.package}/bin/nimlsp"}
''
};
}
'';
};
};
defaultFormat = "nimpretty";
formats = {
nimpretty = {
package = pkgs.nim;
nullConfig = ''
table.insert(
ls_sources,
null_ls.builtins.formatting.nimpretty.with({
command = "${pkgs.nim}/bin/nimpretty",
})
)
'';
};
};
in {
options.vim.languages.nim = {
enable = mkEnableOption "Nim language support";
treesitter = {
enable = mkEnableOption "Nim treesitter" // {default = config.vim.languages.enableTreesitter;};
package = mkGrammarOption pkgs "nim";
2024-02-07 05:24:44 +01:00
};
lsp = {
enable = mkEnableOption "Nim LSP support" // {default = config.vim.languages.enableLSP;};
server = mkOption {
description = "Nim LSP server to use";
type = str;
2024-02-07 05:24:44 +01:00
default = defaultServer;
};
2024-02-07 05:24:44 +01:00
package = mkOption {
description = "Nim LSP server package, or the command to run as a list of strings";
example = ''[lib.getExe pkgs.nimlsp]'';
type = either package (listOf str);
2024-02-07 05:24:44 +01:00
default = servers.${cfg.lsp.server}.package;
};
};
format = {
enable = mkEnableOption "Nim formatting" // {default = config.vim.languages.enableFormat;};
type = mkOption {
description = "Nim formatter to use";
type = enum (attrNames formats);
2024-02-07 05:24:44 +01:00
default = defaultFormat;
};
2024-02-07 05:24:44 +01:00
package = mkOption {
description = "Nim formatter package";
type = package;
2024-02-07 05:24:44 +01:00
default = formats.${cfg.format.type}.package;
};
};
};
config = mkIf cfg.enable (mkMerge [
{
assertions = [
{
assertion = !pkgs.stdenv.isDarwin;
message = "Nim language support is only available on Linux";
}
];
}
2024-02-07 05:24:44 +01:00
(mkIf cfg.treesitter.enable {
vim.treesitter.enable = true;
vim.treesitter.grammars = [cfg.treesitter.package];
})
(mkIf cfg.lsp.enable {
vim.lsp.lspconfig.enable = true;
vim.lsp.lspconfig.sources.nim-lsp = servers.${cfg.lsp.server}.lspConfig;
})
(mkIf cfg.format.enable {
vim.lsp.null-ls.enable = true;
vim.lsp.null-ls.sources.nim-format = formats.${cfg.format.type}.nullConfig;
})
]);
}