2023-04-17 20:27:27 +00:00
|
|
|
{
|
|
|
|
config,
|
2024-03-09 05:49:22 +00:00
|
|
|
pkgs,
|
2023-04-17 20:27:27 +00:00
|
|
|
lib,
|
|
|
|
...
|
2023-11-08 02:16:46 +00:00
|
|
|
}: let
|
2023-11-14 07:18:32 +00:00
|
|
|
inherit (builtins) attrNames;
|
2024-03-09 05:49:22 +00:00
|
|
|
inherit (lib.options) mkEnableOption mkOption;
|
|
|
|
inherit (lib.modules) mkIf mkMerge;
|
|
|
|
inherit (lib.lists) isList;
|
|
|
|
inherit (lib.strings) optionalString;
|
|
|
|
inherit (lib.types) enum either listOf package str;
|
2024-03-24 00:14:39 +00:00
|
|
|
inherit (lib.nvim.types) mkGrammarOption diagnostics;
|
2024-03-09 05:49:22 +00:00
|
|
|
inherit (lib.nvim.dag) entryAnywhere;
|
2024-03-24 00:14:39 +00:00
|
|
|
inherit (lib.nvim.lua) expToLua;
|
|
|
|
inherit (lib.nvim.languages) diagnosticsToLua;
|
2023-11-08 02:16:46 +00:00
|
|
|
|
2023-04-17 20:27:27 +00:00
|
|
|
cfg = config.vim.languages.nix;
|
|
|
|
|
2023-04-17 21:38:57 +00:00
|
|
|
useFormat = "on_attach = default_on_attach";
|
2023-04-17 20:27:27 +00:00
|
|
|
noFormat = "on_attach = attach_keymaps";
|
|
|
|
|
|
|
|
defaultServer = "nil";
|
2023-09-11 13:19:55 +00:00
|
|
|
packageToCmd = package: defaultCmd:
|
|
|
|
if isList package
|
2024-03-24 00:14:39 +00:00
|
|
|
then expToLua package
|
2023-09-11 13:19:55 +00:00
|
|
|
else ''{"${package}/bin/${defaultCmd}"}'';
|
2023-04-17 20:27:27 +00:00
|
|
|
servers = {
|
|
|
|
rnix = {
|
|
|
|
package = pkgs.rnix-lsp;
|
2023-04-17 22:11:02 +00:00
|
|
|
internalFormatter = cfg.format.type == "nixpkgs-fmt";
|
2023-04-17 20:27:27 +00:00
|
|
|
lspConfig = ''
|
|
|
|
lspconfig.rnix.setup{
|
|
|
|
capabilities = capabilities,
|
|
|
|
${
|
|
|
|
if (cfg.format.enable && cfg.format.type == "nixpkgs-fmt")
|
|
|
|
then useFormat
|
|
|
|
else noFormat
|
|
|
|
},
|
2023-09-11 13:19:55 +00:00
|
|
|
cmd = ${packageToCmd cfg.lsp.package "rnix-lsp"},
|
2023-04-17 20:27:27 +00:00
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
nil = {
|
|
|
|
package = pkgs.nil;
|
2023-04-17 22:11:02 +00:00
|
|
|
internalFormatter = true;
|
2023-04-17 20:27:27 +00:00
|
|
|
lspConfig = ''
|
|
|
|
lspconfig.nil_ls.setup{
|
|
|
|
capabilities = capabilities,
|
|
|
|
${
|
|
|
|
if cfg.format.enable
|
|
|
|
then useFormat
|
|
|
|
else noFormat
|
|
|
|
},
|
2023-09-11 13:19:55 +00:00
|
|
|
cmd = ${packageToCmd cfg.lsp.package "nil"},
|
2023-04-17 20:27:27 +00:00
|
|
|
${optionalString cfg.format.enable ''
|
|
|
|
settings = {
|
|
|
|
["nil"] = {
|
|
|
|
${optionalString (cfg.format.type == "alejandra")
|
|
|
|
''
|
|
|
|
formatting = {
|
|
|
|
command = {"${cfg.format.package}/bin/alejandra", "--quiet"},
|
|
|
|
},
|
|
|
|
''}
|
|
|
|
${optionalString (cfg.format.type == "nixpkgs-fmt")
|
|
|
|
''
|
|
|
|
formatting = {
|
|
|
|
command = {"${cfg.format.package}/bin/nixpkgs-fmt"},
|
|
|
|
},
|
|
|
|
''}
|
|
|
|
},
|
2023-08-05 18:33:31 +00:00
|
|
|
},
|
|
|
|
''}
|
2023-04-17 20:27:27 +00:00
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
defaultFormat = "alejandra";
|
|
|
|
formats = {
|
|
|
|
alejandra = {
|
|
|
|
package = pkgs.alejandra;
|
|
|
|
nullConfig = ''
|
|
|
|
table.insert(
|
|
|
|
ls_sources,
|
|
|
|
null_ls.builtins.formatting.alejandra.with({
|
|
|
|
command = "${cfg.format.package}/bin/alejandra"
|
|
|
|
})
|
|
|
|
)
|
|
|
|
'';
|
|
|
|
};
|
2024-03-09 05:49:22 +00:00
|
|
|
|
2023-04-17 20:27:27 +00:00
|
|
|
nixpkgs-fmt = {
|
|
|
|
package = pkgs.nixpkgs-fmt;
|
|
|
|
# Never need to use null-ls for nixpkgs-fmt
|
|
|
|
};
|
|
|
|
};
|
2023-07-31 10:34:51 +00:00
|
|
|
|
2024-03-24 00:14:39 +00:00
|
|
|
defaultDiagnosticsProvider = ["statix" "deadnix"];
|
|
|
|
diagnosticsProviders = {
|
2023-07-31 10:34:51 +00:00
|
|
|
statix = {
|
|
|
|
package = pkgs.statix;
|
|
|
|
nullConfig = pkg: ''
|
|
|
|
table.insert(
|
|
|
|
ls_sources,
|
|
|
|
null_ls.builtins.diagnostics.statix.with({
|
|
|
|
command = "${pkg}/bin/statix",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
'';
|
|
|
|
};
|
2024-03-09 05:49:22 +00:00
|
|
|
|
2023-07-31 10:34:51 +00:00
|
|
|
deadnix = {
|
|
|
|
package = pkgs.deadnix;
|
|
|
|
nullConfig = pkg: ''
|
|
|
|
table.insert(
|
|
|
|
ls_sources,
|
|
|
|
null_ls.builtins.diagnostics.deadnix.with({
|
|
|
|
command = "${pkg}/bin/deadnix",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2023-04-17 20:27:27 +00:00
|
|
|
in {
|
|
|
|
options.vim.languages.nix = {
|
|
|
|
enable = mkEnableOption "Nix language support";
|
|
|
|
|
|
|
|
treesitter = {
|
2024-03-09 05:49:22 +00:00
|
|
|
enable = mkEnableOption "Nix treesitter" // {default = config.vim.languages.enableTreesitter;};
|
|
|
|
package = mkGrammarOption pkgs "nix";
|
2023-04-17 20:27:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
lsp = {
|
2023-10-21 17:15:36 +00:00
|
|
|
enable = mkEnableOption "Nix LSP support" // {default = config.vim.languages.enableLSP;};
|
2023-04-17 20:27:27 +00:00
|
|
|
server = mkOption {
|
|
|
|
description = "Nix LSP server to use";
|
2024-03-09 05:49:22 +00:00
|
|
|
type = str;
|
2023-04-17 20:27:27 +00:00
|
|
|
default = defaultServer;
|
|
|
|
};
|
2024-03-09 05:49:22 +00:00
|
|
|
|
2023-04-17 20:27:27 +00:00
|
|
|
package = mkOption {
|
2023-09-22 22:28:39 +00:00
|
|
|
description = "Nix LSP server package, or the command to run as a list of strings";
|
|
|
|
example = ''[lib.getExe pkgs.jdt-language-server "-data" "~/.cache/jdtls/workspace"]'';
|
2024-03-09 05:49:22 +00:00
|
|
|
type = either package (listOf str);
|
2023-04-17 20:27:27 +00:00
|
|
|
default = servers.${cfg.lsp.server}.package;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
format = {
|
2023-10-21 17:15:36 +00:00
|
|
|
enable = mkEnableOption "Nix formatting" // {default = config.vim.languages.enableFormat;};
|
2023-06-05 20:10:25 +00:00
|
|
|
|
2023-04-17 20:27:27 +00:00
|
|
|
type = mkOption {
|
|
|
|
description = "Nix formatter to use";
|
2024-03-09 05:49:22 +00:00
|
|
|
type = enum (attrNames formats);
|
2023-04-17 20:27:27 +00:00
|
|
|
default = defaultFormat;
|
|
|
|
};
|
|
|
|
package = mkOption {
|
|
|
|
description = "Nix formatter package";
|
2024-03-09 05:49:22 +00:00
|
|
|
type = package;
|
2023-04-17 20:27:27 +00:00
|
|
|
default = formats.${cfg.format.type}.package;
|
|
|
|
};
|
|
|
|
};
|
2023-07-31 10:34:51 +00:00
|
|
|
|
|
|
|
extraDiagnostics = {
|
2024-03-09 05:49:22 +00:00
|
|
|
enable = mkEnableOption "extra Nix diagnostics" // {default = config.vim.languages.enableExtraDiagnostics;};
|
|
|
|
|
2024-03-24 00:14:39 +00:00
|
|
|
types = diagnostics {
|
2023-07-31 10:34:51 +00:00
|
|
|
langDesc = "Nix";
|
2024-03-24 00:14:39 +00:00
|
|
|
inherit diagnosticsProviders;
|
|
|
|
inherit defaultDiagnosticsProvider;
|
2023-07-31 10:34:51 +00:00
|
|
|
};
|
|
|
|
};
|
2023-04-17 20:27:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable (mkMerge [
|
|
|
|
{
|
2024-03-09 05:49:22 +00:00
|
|
|
vim.configRC.nix = entryAnywhere ''
|
2023-04-17 20:27:27 +00:00
|
|
|
autocmd filetype nix setlocal tabstop=2 shiftwidth=2 softtabstop=2
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
|
|
|
|
(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.nix-lsp = servers.${cfg.lsp.server}.lspConfig;
|
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf (cfg.format.enable && !servers.${cfg.lsp.server}.internalFormatter) {
|
|
|
|
vim.lsp.null-ls.enable = true;
|
|
|
|
vim.lsp.null-ls.sources.nix-format = formats.${cfg.format.type}.nullConfig;
|
|
|
|
})
|
2023-07-31 10:34:51 +00:00
|
|
|
|
|
|
|
(mkIf cfg.extraDiagnostics.enable {
|
|
|
|
vim.lsp.null-ls.enable = true;
|
2024-03-24 00:14:39 +00:00
|
|
|
vim.lsp.null-ls.sources = diagnosticsToLua {
|
2023-07-31 10:34:51 +00:00
|
|
|
lang = "nix";
|
|
|
|
config = cfg.extraDiagnostics.types;
|
2024-03-24 00:14:39 +00:00
|
|
|
inherit diagnosticsProviders;
|
2023-07-31 10:34:51 +00:00
|
|
|
};
|
|
|
|
})
|
2023-04-17 20:27:27 +00:00
|
|
|
]);
|
|
|
|
}
|