Initialize changes for nix

This commit is contained in:
sjcobb 2025-05-20 15:44:57 +01:00
commit bc93d67416
3 changed files with 116 additions and 112 deletions

View file

@ -1,9 +1,13 @@
{lib}: let {lib}: let
inherit (builtins) isString getAttr; inherit (builtins) isString getAttr;
inherit (lib.options) mkOption; inherit (lib.options) mkOption;
inherit (lib.types) listOf bool str submodule attrsOf anything either nullOr; inherit (lib.strings) concatStringsSep;
inherit (lib.types) listOf bool str submodule attrsOf anything either nullOr oneOf enum;
inherit (lib.attrsets) attrNames;
inherit (lib.nvim.attrsets) mapListToAttrs; inherit (lib.nvim.attrsets) mapListToAttrs;
inherit (lib.nvim.types) luaInline; inherit (lib.nvim.types) luaInline;
inherit (lib.lists) isList;
inherit (lib) genAttrs recursiveUpdate;
in { in {
# TODO: remove # TODO: remove
diagnosticsToLua = { diagnosticsToLua = {
@ -34,6 +38,18 @@ in {
description = "Turn on ${desc} for enabled languages by default"; description = "Turn on ${desc} for enabled languages by default";
}; };
# resolveLspOptions
# servers: AttrsOf lspOptions
# selected: AttrsOf lspOptions | List of string keys from servers
# Returns: AttrsOf lspOptions
resolveLspOptions = {
servers,
selected,
}:
if isList selected
then genAttrs selected (name: servers.${name})
else selected;
lspOptions = submodule { lspOptions = submodule {
freeformType = attrsOf anything; freeformType = attrsOf anything;
options = { options = {
@ -77,4 +93,27 @@ in {
}; };
}; };
}; };
mkLspOption = {servers, ...} @ args: let
serverNames = attrNames servers;
defaultAttrs = {
type = oneOf [
(attrsOf lib.nvim.languages.lspOptions)
(listOf (enum serverNames))
];
description = ''
Either a full set of selected LSP options as an attribute set,
or a list of server names from: ${concatStringsSep ", " serverNames}.
'';
default = {};
example = {
clangd = {
filetypes = ["c"];
root_markers = ["CMakeLists.txt"];
};
};
};
cleanedArgs = removeAttrs args ["servers"];
in
mkOption (recursiveUpdate defaultAttrs cleanedArgs);
} }

View file

@ -18,12 +18,36 @@
lspConfigurations = lspConfigurations =
mapAttrsToList ( mapAttrsToList (
name: value: '' # TODO: Determine the best thing to do about merging in lspconfig
vim.lsp.config["${name}"] = ${toLuaObject value} name: value:
'' /*
lua
*/
''vim.lsp.config["${name}"] = ${toLuaObject value}''
) )
cfg.servers; cfg.servers;
# Approach 1:
# Create function perhaps called mkLspConfig
# mkLspConfig servers;
# that expands to something like
# vim.lsp.servers = cfg.lsp.servers......
# vim.luaConfigRC.lspconfigMerge = entryAfter ["lsp-servers"] ''vim.lsp.config["${name}"] = vim.tbl_deep_extend("force", lspconfig.${name}, vim.lsp.config["${name}"])''
# Approach 2:
# lspConfigurations =
# mapAttrsToList (
# name: value: ''vim.lsp.config["${name}"] = vim.tbl_deep_extend("force", lspconfig.${name}, ${toLuaObject value})''
# )
# (filterAttrs (n: _: n != "*") cfg.servers);
# Then also need to configure global * settings
# globalConfiguration =
# mapAttrsToList (
# name: value: ''vim.lsp.config["${name}"] = ${toLuaObject value}''
# )
# (filterAttrs (n: _: n == "*") cfg.servers);
enabledServers = filterAttrs (_: u: u.enable) cfg.servers; enabledServers = filterAttrs (_: u: u.enable) cfg.servers;
in { in {
options = { options = {
@ -81,12 +105,17 @@ in {
} }
(mkIf (cfg.servers != {}) { (mkIf (cfg.servers != {}) {
vim.luaConfigRC.lsp-servers = entryAnywhere '' vim.luaConfigRC.lsp-servers =
-- Individual LSP configurations managed by nvf. entryAnywhere
# Or entryAfter ["lspconfig"] if we go with the second approach
/*
lua
*/
''
${concatLines lspConfigurations} ${concatLines lspConfigurations}
-- Enable configured LSPs explicitly -- Enable configured LSPs explicitly
vim.lsp.enable(${toLuaObject (filter (name: name != "*") (attrNames enabledServers))}) vim.lsp.enable(${toLuaObject (filter (name: name != "*") (attrNames enabledServers))});
''; '';
}) })
]; ];

View file

@ -2,99 +2,54 @@
config, config,
pkgs, pkgs,
lib, lib,
inputs,
... ...
}: let }: let
inherit (builtins) attrNames; inherit (builtins) attrNames;
inherit (lib) concatStringsSep; inherit (lib) concatStringsSep;
inherit (lib.generators) 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.types) enum package;
inherit (lib.types) anything attrsOf enum either listOf nullOr package str oneOf;
inherit (lib.nvim.types) mkGrammarOption diagnostics; inherit (lib.nvim.types) mkGrammarOption diagnostics;
inherit (lib.nvim.lua) expToLua toLuaObject; inherit (lib.nvim.lua) expToLua;
inherit (lib.nvim.languages) lspOptions; inherit (lib.nvim.languages) resolveLspOptions mkLspOption;
cfg = config.vim.languages.nix; cfg = config.vim.languages.nix;
useFormat = "on_attach = default_on_attach";
noFormat = "on_attach = attach_keymaps";
defaultServer = "nil";
packageToCmd = package: defaultCmd: packageToCmd = package: defaultCmd:
if isList package if isList package
then expToLua package then expToLua package
else ''{"${package}/bin/${defaultCmd}"}''; else ''{"${package}/bin/${defaultCmd}"}'';
formattingCmd = lib.mkIf (cfg.format.enable && cfg.lsp.enable) {
formatting = lib.mkMerge [
(lib.mkIf (cfg.format.type == "alejandra") {
command = ["${cfg.format.package}/bin/alejandra" "--quiet"];
})
(lib.mkIf (cfg.format.type == "nixfmt") {
command = ["${cfg.format.package}/bin/nixfmt"];
})
];
};
defaultServers = ["nil_ls"]
servers = { servers = {
nil = { nil_ls = {
package = inputs.nil.packages.${pkgs.stdenv.system}.nil; enable = true;
internalFormatter = true; cmd = ["${pkgs.nil}/bin/nil"];
# lspConfig = '' settings = {
# lspconfig.nil_ls.setup{ nil = formattingCmd;
# 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"},
# },
# ''}
# },
# },
# ''}
# }
# '';
}; };
nixd = { nixd = {
package = pkgs.nixd; enable = true;
internalFormatter = true; cmd = ["${pkgs.nixd}/bin/nixd"];
# lspConfig = '' settings = {
# lspconfig.nixd.setup{ nixd = formattingCmd;
# capabilities = capabilities, };
# ${
# if cfg.format.enable
# then useFormat
# else noFormat
# },
# cmd = ${packageToCmd cfg.lsp.package "nixd"},
# ${optionalString cfg.format.enable ''
# settings = {
# nixd = {
# ${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"},
# },
# ''}
# options = ${toLuaObject cfg.lsp.options},
# },
# },
# ''}
# }
# '';
}; };
}; };
@ -146,26 +101,9 @@ in {
lsp = { lsp = {
enable = mkEnableOption "Nix LSP support" // {default = config.vim.lsp.enable;}; enable = mkEnableOption "Nix LSP support" // {default = config.vim.lsp.enable;};
server = mkOption { servers = mkLspOption {
description = "Nix LSP server(s) to use"; inherit servers;
type = listOf oneOf [ default = defaultServers;
(attrsOf lspOptions)
(enum (attrNames servers))
];
default = defaultServer;
};
package = mkOption {
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"]'';
type = either package (listOf str);
default = servers.${cfg.lsp.server}.package;
};
options = mkOption {
type = nullOr (attrsOf anything);
default = null;
description = "Options to pass to nixd LSP server";
}; };
}; };
@ -206,13 +144,6 @@ in {
${concatStringsSep ", " (attrNames formats)} ${concatStringsSep ", " (attrNames formats)}
''; '';
} }
{
assertion = cfg.lsp.server != "rnix";
message = ''
rnix-lsp has been archived upstream. Please use one of the following available language servers:
${concatStringsSep ", " (attrNames servers)}
'';
}
]; ];
} }
@ -222,10 +153,15 @@ in {
}) })
(mkIf cfg.lsp.enable { (mkIf cfg.lsp.enable {
vim.lsp.servers = cfg.lsp.server; # TODO: Map this to include lspconfig stuff so that we can do
vim.lsp.servers = resolveLspOptions {
inherit servers;
selected = cfg.lsp.servers;
};
}) })
(mkIf (cfg.format.enable && (!cfg.lsp.enable || !servers.${cfg.lsp.server}.internalFormatter)) { # TODO: Figure out what do here. This is not necessarily correct as other lsps might not have formatting by default
(mkIf (cfg.format.enable && !cfg.lsp.enable) {
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];