Added workaround for nil configuration

This commit is contained in:
Liyua 2025-07-01 20:02:26 +02:00
commit 3fa41b6d8b
No known key found for this signature in database
GPG key ID: AFF37010586DE14D

View file

@ -4,7 +4,8 @@
lib, lib,
inputs, inputs,
... ...
}: let }:
let
inherit (builtins) attrNames; inherit (builtins) attrNames;
inherit (lib) concatStringsSep; inherit (lib) concatStringsSep;
inherit (lib.meta) getExe; inherit (lib.meta) getExe;
@ -12,7 +13,16 @@
inherit (lib.modules) mkIf mkMerge; inherit (lib.modules) mkIf mkMerge;
inherit (lib.lists) isList; inherit (lib.lists) isList;
inherit (lib.strings) optionalString; inherit (lib.strings) optionalString;
inherit (lib.types) anything attrsOf enum either listOf nullOr package str; inherit (lib.types)
anything
attrsOf
enum
either
listOf
nullOr
package
str
;
inherit (lib.nvim.types) mkGrammarOption diagnostics; inherit (lib.nvim.types) mkGrammarOption diagnostics;
inherit (lib.nvim.lua) expToLua toLuaObject; inherit (lib.nvim.lua) expToLua toLuaObject;
@ -22,10 +32,9 @@
noFormat = "on_attach = attach_keymaps"; noFormat = "on_attach = attach_keymaps";
defaultServer = "nil"; defaultServer = "nil";
packageToCmd = package: defaultCmd: packageToCmd =
if isList package package: defaultCmd:
then expToLua package if isList package then expToLua package else ''{"${package}/bin/${defaultCmd}"}'';
else ''{"${package}/bin/${defaultCmd}"}'';
servers = { servers = {
nil = { nil = {
package = inputs.nil.packages.${pkgs.stdenv.system}.nil; package = inputs.nil.packages.${pkgs.stdenv.system}.nil;
@ -33,27 +42,22 @@
lspConfig = '' lspConfig = ''
lspconfig.nil_ls.setup{ lspconfig.nil_ls.setup{
capabilities = capabilities, capabilities = capabilities,
${ ${if cfg.format.enable then useFormat else noFormat},
if cfg.format.enable
then useFormat
else noFormat
},
cmd = ${packageToCmd cfg.lsp.package "nil"}, cmd = ${packageToCmd cfg.lsp.package "nil"},
${optionalString cfg.format.enable '' ${optionalString cfg.format.enable ''
settings = { settings = {
["nil"] = { ["nil"] = {
${optionalString (cfg.format.type == "alejandra") ${optionalString (cfg.format.type == "alejandra") ''
''
formatting = { formatting = {
command = {"${cfg.format.package}/bin/alejandra", "--quiet"}, command = {"${cfg.format.package}/bin/alejandra", "--quiet"},
}, },
''} ''}
${optionalString (cfg.format.type == "nixfmt") ${optionalString (cfg.format.type == "nixfmt") ''
''
formatting = { formatting = {
command = {"${cfg.format.package}/bin/nixfmt"}, command = {"${cfg.format.package}/bin/nixfmt"},
}, },
''} ''}
nix = ${toLuaObject cfg.lsp.options},
}, },
}, },
''} ''}
@ -67,28 +71,22 @@
lspConfig = '' lspConfig = ''
lspconfig.nixd.setup{ lspconfig.nixd.setup{
capabilities = capabilities, capabilities = capabilities,
${ ${if cfg.format.enable then useFormat else noFormat},
if cfg.format.enable
then useFormat
else noFormat
},
cmd = ${packageToCmd cfg.lsp.package "nixd"}, cmd = ${packageToCmd cfg.lsp.package "nixd"},
${optionalString cfg.format.enable '' ${optionalString cfg.format.enable ''
settings = { settings = {
nixd = { nixd = {
${optionalString (cfg.format.type == "alejandra") ${optionalString (cfg.format.type == "alejandra") ''
''
formatting = { formatting = {
command = {"${cfg.format.package}/bin/alejandra", "--quiet"}, command = {"${cfg.format.package}/bin/alejandra", "--quiet"},
}, },
''} ''}
${optionalString (cfg.format.type == "nixfmt") ${optionalString (cfg.format.type == "nixfmt") ''
''
formatting = { formatting = {
command = {"${cfg.format.package}/bin/nixfmt"}, command = {"${cfg.format.package}/bin/nixfmt"},
}, },
''} ''}
options = ${toLuaObject cfg.lsp.options}, nix = ${toLuaObject cfg.lsp.options},
}, },
}, },
''} ''}
@ -108,7 +106,10 @@
}; };
}; };
defaultDiagnosticsProvider = ["statix" "deadnix"]; defaultDiagnosticsProvider = [
"statix"
"deadnix"
];
diagnosticsProviders = { diagnosticsProviders = {
statix = { statix = {
package = pkgs.statix; package = pkgs.statix;
@ -134,17 +135,22 @@
''; '';
}; };
}; };
in { in
{
options.vim.languages.nix = { options.vim.languages.nix = {
enable = mkEnableOption "Nix language support"; enable = mkEnableOption "Nix language support";
treesitter = { treesitter = {
enable = mkEnableOption "Nix treesitter" // {default = config.vim.languages.enableTreesitter;}; enable = mkEnableOption "Nix treesitter" // {
default = config.vim.languages.enableTreesitter;
};
package = mkGrammarOption pkgs "nix"; package = mkGrammarOption pkgs "nix";
}; };
lsp = { lsp = {
enable = mkEnableOption "Nix LSP support" // {default = config.vim.lsp.enable;}; enable = mkEnableOption "Nix LSP support" // {
default = config.vim.lsp.enable;
};
server = mkOption { server = mkOption {
description = "Nix LSP server to use"; description = "Nix LSP server to use";
type = enum (attrNames servers); type = enum (attrNames servers);
@ -166,7 +172,9 @@ in {
}; };
format = { format = {
enable = mkEnableOption "Nix formatting" // {default = config.vim.languages.enableFormat;}; enable = mkEnableOption "Nix formatting" // {
default = config.vim.languages.enableFormat;
};
type = mkOption { type = mkOption {
description = "Nix formatter to use"; description = "Nix formatter to use";
@ -182,7 +190,9 @@ in {
}; };
extraDiagnostics = { extraDiagnostics = {
enable = mkEnableOption "extra Nix diagnostics" // {default = config.vim.languages.enableExtraDiagnostics;}; enable = mkEnableOption "extra Nix diagnostics" // {
default = config.vim.languages.enableExtraDiagnostics;
};
types = diagnostics { types = diagnostics {
langDesc = "Nix"; langDesc = "Nix";
@ -214,7 +224,7 @@ in {
(mkIf cfg.treesitter.enable { (mkIf cfg.treesitter.enable {
vim.treesitter.enable = true; vim.treesitter.enable = true;
vim.treesitter.grammars = [cfg.treesitter.package]; vim.treesitter.grammars = [ cfg.treesitter.package ];
}) })
(mkIf cfg.lsp.enable { (mkIf cfg.lsp.enable {
@ -225,7 +235,7 @@ in {
(mkIf (cfg.format.enable && (!cfg.lsp.enable || !servers.${cfg.lsp.server}.internalFormatter)) { (mkIf (cfg.format.enable && (!cfg.lsp.enable || !servers.${cfg.lsp.server}.internalFormatter)) {
vim.formatter.conform-nvim = { vim.formatter.conform-nvim = {
enable = true; enable = true;
setupOpts.formatters_by_ft.nix = [cfg.format.type]; setupOpts.formatters_by_ft.nix = [ cfg.format.type ];
setupOpts.formatters.${cfg.format.type} = { setupOpts.formatters.${cfg.format.type} = {
command = getExe cfg.format.package; command = getExe cfg.format.package;
}; };
@ -236,10 +246,11 @@ in {
vim.diagnostics.nvim-lint = { vim.diagnostics.nvim-lint = {
enable = true; enable = true;
linters_by_ft.nix = cfg.extraDiagnostics.types; linters_by_ft.nix = cfg.extraDiagnostics.types;
linters = mkMerge (map (name: { linters = mkMerge (
map (name: {
${name}.cmd = getExe diagnosticsProviders.${name}.package; ${name}.cmd = getExe diagnosticsProviders.${name}.package;
}) }) cfg.extraDiagnostics.types
cfg.extraDiagnostics.types); );
}; };
}) })
]); ]);