Maybe this is even better and less hacky?

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

View file

@ -4,16 +4,16 @@
lib, lib,
inputs, inputs,
... ...
}: let }:
let
inherit (builtins) attrNames; inherit (builtins) attrNames;
inherit (lib) concatStringsSep; inherit (lib) concatStringsSep mkLuaInline;
inherit (lib.meta) getExe; inherit (lib.meta) getExe;
inherit (lib.options) mkEnableOption mkOption; inherit (lib.options) mkEnableOption mkOption;
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 inherit (lib.types)
(lib.types)
anything anything
attrsOf attrsOf
enum enum
@ -32,42 +32,60 @@
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;
internalFormatter = true; internalFormatter = true;
lspConfig = '' lspConfig = ''lspconfig.nil_ls.setup ${
lspconfig.nil_ls.setup{ toLuaObject {
capabilities = capabilities, capabilities = mkLuaInline "capabilities";
${ on_attach = if cfg.format.enable then mkLuaInline "default_on_attach" else "attach_keymaps";
if cfg.format.enable cmd = packageToCmd cfg.lsp.package "nil";
then useFormat
else noFormat
},
cmd = ${packageToCmd cfg.lsp.package "nil"},
${optionalString cfg.format.enable ''
settings = { settings = {
["nil"] = { nil.formatting.command =
${optionalString (cfg.format.type == "alejandra") '' if cfg.format.enable then
formatting = { if cfg.format.type == "alejandra" then
command = {"${cfg.format.package}/bin/alejandra", "--quiet"}, ''{"${cfg.format.package}/bin/alejandra", "--quiet"}''
}, else if cfg.format.type == "nixfmt" then
''} ''{"${cfg.format.package}/bin/nixfmt"}''
${optionalString (cfg.format.type == "nixfmt") '' else
formatting = { null
command = {"${cfg.format.package}/bin/nixfmt"}, else
}, null;
''} } // config.lsp.options;
nix = ${toLuaObject cfg.lsp.options},
},
},
''}
} }
''; }'';
# lspConfig = ''
# lspconfig.nil_ls.setup{
# capabilities = capabilities,
# ${
# if cfg.format.enable
# then useFormat
# else noFormat
# },
# cmd = ${packageToCmd cfg.lsp.package "nil"},
# ${optionalString cfg.format.enable ''
# settings = {
# ["nil"] = {
# ${optionalString (cfg.format.type == "alejandra") ''
# formatting = {
# command = {"${cfg.format.package}/bin/alejandra", "--quiet"},
# },
# ''}
# ${optionalString (cfg.format.type == "nixfmt") ''
# formatting = {
# command = {"${cfg.format.package}/bin/nixfmt"},
# },
# ''}
# nix = ${toLuaObject cfg.lsp.options},
# },
# },
# ''}
# }
# '';
}; };
nixd = { nixd = {
@ -76,11 +94,7 @@
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 = {
@ -144,25 +158,22 @@
''; '';
}; };
}; };
in { in
{
options.vim.languages.nix = { options.vim.languages.nix = {
enable = mkEnableOption "Nix language support"; enable = mkEnableOption "Nix language support";
treesitter = { treesitter = {
enable = enable = mkEnableOption "Nix treesitter" // {
mkEnableOption "Nix treesitter" default = config.vim.languages.enableTreesitter;
// { };
default = config.vim.languages.enableTreesitter;
};
package = mkGrammarOption pkgs "nix"; package = mkGrammarOption pkgs "nix";
}; };
lsp = { lsp = {
enable = enable = mkEnableOption "Nix LSP support" // {
mkEnableOption "Nix LSP support" default = config.vim.lsp.enable;
// { };
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);
@ -184,11 +195,9 @@ in {
}; };
format = { format = {
enable = enable = mkEnableOption "Nix formatting" // {
mkEnableOption "Nix formatting" default = config.vim.languages.enableFormat;
// { };
default = config.vim.languages.enableFormat;
};
type = mkOption { type = mkOption {
description = "Nix formatter to use"; description = "Nix formatter to use";
@ -204,11 +213,9 @@ in {
}; };
extraDiagnostics = { extraDiagnostics = {
enable = enable = mkEnableOption "extra Nix diagnostics" // {
mkEnableOption "extra Nix diagnostics" default = config.vim.languages.enableExtraDiagnostics;
// { };
default = config.vim.languages.enableExtraDiagnostics;
};
types = diagnostics { types = diagnostics {
langDesc = "Nix"; langDesc = "Nix";
@ -240,7 +247,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 {
@ -251,7 +258,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;
}; };
@ -265,8 +272,7 @@ in {
linters = mkMerge ( linters = mkMerge (
map (name: { map (name: {
${name}.cmd = getExe diagnosticsProviders.${name}.package; ${name}.cmd = getExe diagnosticsProviders.${name}.package;
}) }) cfg.extraDiagnostics.types
cfg.extraDiagnostics.types
); );
}; };
}) })