run nixfmt

This commit is contained in:
Joakim Repomaa
2025-03-01 13:23:01 +02:00
parent a93d4afbcf
commit 8dfc9b5a6d
51 changed files with 1611 additions and 778 deletions

View File

@@ -1,14 +1,24 @@
{ config, pkgs, lib, ... }:
{
config,
pkgs,
lib,
...
}:
let
cfg = config.programs.neovim;
toLua = lib.generators.toLua { };
buildPluginConfig = p:
buildPluginConfig =
p:
let
pluginConfig = if builtins.hasAttr "plugin" p then p else { plugin = p; };
name = pluginConfig.plugin.name;
config = if (builtins.isNull pluginConfig.config or null) then "" else pluginConfig.config;
mappings = if (builtins.isNull pluginConfig.mappings or null) then "" else generateMappings pluginConfig.mappings;
mappings =
if (builtins.isNull pluginConfig.mappings or null) then
""
else
generateMappings pluginConfig.mappings;
mergedConfig = config + mappings;
in
[
@@ -17,9 +27,10 @@ let
config = lib.modules.mkIf (mergedConfig != "") ''
Plugins[${toLua name}] = {}
Plugins[${toLua name}].setup = function()
${lib.strings.concatMapStrings
(line: if line == "" then "" else " ${line}\n")
(lib.strings.splitString "\n" mergedConfig)
${
lib.strings.concatMapStrings (line: if line == "" then "" else " ${line}\n") (
lib.strings.splitString "\n" mergedConfig
)
}
end
@@ -28,9 +39,8 @@ let
'';
type = "lua";
}
] ++ (
lib.lists.concatMap buildPluginConfig pluginConfig.dependencies or [ ]
);
]
++ (lib.lists.concatMap buildPluginConfig pluginConfig.dependencies or [ ]);
lspPluginConfig = with pkgs.vimPlugins; {
plugin = nvim-lspconfig;
@@ -41,76 +51,100 @@ let
luasnip
];
config = (builtins.readFile ./lsp-config.lua) + ''
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.workspace.didChangeWatchedFiles.dynamicRegistration = true
local lspconfig = require('lspconfig')
local util = require('lspconfig.util')
config =
(builtins.readFile ./lsp-config.lua)
+ ''
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.workspace.didChangeWatchedFiles.dynamicRegistration = true
local lspconfig = require('lspconfig')
local util = require('lspconfig.util')
local on_attach = function (client, bufnr)
local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end
local map = function (lhs, rhs)
print('set mapping ' .. lhs)
end
-- Enable completion triggered by <c-x><c-o>
buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')
-- Mappings.
-- See `:help vim.lsp.*` for documentation on any of the below functions
${lib.strings.concatLines (lib.attrsets.mapAttrsToList (name: value: ''
local buf_${name} = vim.lsp.buf[${toLua name}]
if buf_${name} then
vim.keymap.set('n', ${toLua value}, buf_${name}, { buffer = bufnr })
local on_attach = function (client, bufnr)
local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end
local map = function (lhs, rhs)
print('set mapping ' .. lhs)
end
''
) cfg.lsp.mappings.buf)}
${lib.strings.concatLines (lib.attrsets.mapAttrsToList (name: value: ''
local diagnostic_${name} = vim.lsp.buf[${toLua name}]
if diagnostic_${name} then
vim.keymap.set('n', ${toLua value}, diagnostic_${name}, { buffer = bufnr })
end
'') cfg.lsp.mappings.diagnostic)}
end
vim.env.PATH = ${toLua (lib.makeBinPath (map (s: s.package) (builtins.filter (s: builtins.hasAttr "package" s) cfg.lsp.servers)) + ":")} .. vim.env.PATH
local servers = ${toLua (map (
{ name, config ? {}, rootPattern ? null, ... }: {
inherit name;
config = config // (if (builtins.isNull rootPattern) then {} else { inherit rootPattern; });
}
) cfg.lsp.servers)}
-- Enable completion triggered by <c-x><c-o>
buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')
for _, server in ipairs(servers) do
local server_config = server.config
server_config.on_attach = on_attach
server_config.capabilities = capabilities
server_config.flags = {
debounce_text_changes = 150
}
-- Mappings.
-- See `:help vim.lsp.*` for documentation on any of the below functions
if server_config.rootPattern then
server_config.root_dir = util.root_pattern(
unpack(server_config.rootPattern)
${lib.strings.concatLines (
lib.attrsets.mapAttrsToList (name: value: ''
local buf_${name} = vim.lsp.buf[${toLua name}]
if buf_${name} then
vim.keymap.set('n', ${toLua value}, buf_${name}, { buffer = bufnr })
end
'') cfg.lsp.mappings.buf
)}
${lib.strings.concatLines (
lib.attrsets.mapAttrsToList (name: value: ''
local diagnostic_${name} = vim.lsp.buf[${toLua name}]
if diagnostic_${name} then
vim.keymap.set('n', ${toLua value}, diagnostic_${name}, { buffer = bufnr })
end
'') cfg.lsp.mappings.diagnostic
)}
end
vim.env.PATH = ${
toLua (
lib.makeBinPath (
map (s: s.package) (builtins.filter (s: builtins.hasAttr "package" s) cfg.lsp.servers)
)
end
+ ":"
)
} .. vim.env.PATH
local servers = ${
toLua (
map (
{
name,
config ? { },
rootPattern ? null,
...
}:
{
inherit name;
config = config // (if (builtins.isNull rootPattern) then { } else { inherit rootPattern; });
}
) cfg.lsp.servers
)
}
lspconfig[server.name].setup(server_config)
end
'';
for _, server in ipairs(servers) do
local server_config = server.config
server_config.on_attach = on_attach
server_config.capabilities = capabilities
server_config.flags = {
debounce_text_changes = 150
}
if server_config.rootPattern then
server_config.root_dir = util.root_pattern(
unpack(server_config.rootPattern)
)
end
lspconfig[server.name].setup(server_config)
end
'';
};
treesitterPluginConfig = with pkgs.vimPlugins; {
plugin = nvim-treesitter.withPlugins cfg.withTreesitterPlugins;
config = ''
require('nvim-treesitter.configs').setup(${toLua {
highlight = {
enable = true;
additional_vim_regex_highlighting = false;
};
}});
require('nvim-treesitter.configs').setup(${
toLua {
highlight = {
enable = true;
additional_vim_regex_highlighting = false;
};
}
});
'';
};
@@ -122,17 +156,24 @@ let
formatter.setup({
filetype = {
${lib.strings.concatMapStringsSep ",\n " ({ exe, args, stdin, no_append, filetypes, ... }:
${lib.strings.concatMapStringsSep ",\n " (
{
exe,
args,
stdin,
no_append,
filetypes,
...
}:
lib.strings.concatMapStringsSep ",\n " (filetype: ''
[${toLua filetype}] = {
function ()
return {
exe = ${toLua exe},
args = {
${lib.strings.concatMapStringsSep ",\n " (arg:
if arg == "<<FILE>>"
then "util.escape_path(util.get_current_buffer_file_path())"
else toLua arg
${lib.strings.concatMapStringsSep ",\n " (
arg:
if arg == "<<FILE>>" then "util.escape_path(util.get_current_buffer_file_path())" else toLua arg
) (args "<<FILE>>")}
},
stdin = ${toLua stdin},
@@ -153,137 +194,218 @@ let
'';
};
customTypes = let inherit (lib) mkOption mkEnableOption types; in {
mapping = types.submodule {
options = {
rhs = mkOption { type = types.str; };
lua = mkEnableOption { };
options = mkOption {
type = types.nullOr (
types.submodule {
options = {
buffer = mkOption { type = types.nullOr (types.either types.int types.bool); default = null; };
nowait = mkOption { type = types.nullOr types.bool; default = null; };
silent = mkOption { type = types.nullOr types.bool; default = null; };
expr = mkOption { type = types.nullOr types.bool; default = null; };
script = mkOption { type = types.nullOr types.bool; default = null; };
unique = mkOption { type = types.nullOr types.bool; default = null; };
replace_keycodes = mkOption { type = types.nullOr types.bool; default = null; };
};
}
);
default = { };
customTypes =
let
inherit (lib) mkOption mkEnableOption types;
in
{
mapping = types.submodule {
options = {
rhs = mkOption { type = types.str; };
lua = mkEnableOption { };
options = mkOption {
type = types.nullOr (
types.submodule {
options = {
buffer = mkOption {
type = types.nullOr (types.either types.int types.bool);
default = null;
};
nowait = mkOption {
type = types.nullOr types.bool;
default = null;
};
silent = mkOption {
type = types.nullOr types.bool;
default = null;
};
expr = mkOption {
type = types.nullOr types.bool;
default = null;
};
script = mkOption {
type = types.nullOr types.bool;
default = null;
};
unique = mkOption {
type = types.nullOr types.bool;
default = null;
};
replace_keycodes = mkOption {
type = types.nullOr types.bool;
default = null;
};
};
}
);
default = { };
};
};
};
modeMappings = types.attrsOf (types.either types.str customTypes.mapping);
mappings = types.submodule {
options = {
normal = mkOption {
type = customTypes.modeMappings;
default = { };
};
insert = mkOption {
type = customTypes.modeMappings;
default = { };
};
visual = mkOption {
type = customTypes.modeMappings;
default = { };
};
command = mkOption {
type = customTypes.modeMappings;
default = { };
};
select = mkOption {
type = customTypes.modeMappings;
default = { };
};
};
};
plugin = types.either types.package (
types.submodule {
options = {
plugin = mkOption { type = types.package; };
dependencies = mkOption {
type = types.listOf customTypes.plugin;
default = [ ];
};
mappings = mkOption {
type = customTypes.mappings;
default = { };
};
config = mkOption {
type = types.nullOr types.str;
default = null;
};
};
}
);
autoCommand = types.submodule {
options = {
event = mkOption { type = types.either types.str (types.listOf types.str); };
pattern = mkOption { type = types.either types.str (types.listOf types.str); };
command = mkOption { type = types.str; };
};
};
lspServer = types.submodule {
options = {
name = mkOption { type = types.str; };
config = mkOption {
type = types.attrs;
default = { };
};
package = mkOption {
type = types.nullOr types.package;
default = null;
};
rootPattern = mkOption {
type = types.nullOr (types.listOf types.str);
default = null;
};
};
};
formatter = types.submodule {
options = {
filetypes = mkOption { type = types.listOf types.str; };
globs = mkOption { type = types.listOf types.str; };
exe = mkOption { type = types.either types.path types.str; };
args = mkOption {
type = types.functionTo (types.listOf types.str);
default = _: [ ];
};
stdin = mkEnableOption { };
no_append = mkEnableOption { };
};
};
};
modeMappings = types.attrsOf (types.either types.str customTypes.mapping);
mappings = types.submodule {
options = {
normal = mkOption { type = customTypes.modeMappings; default = { }; };
insert = mkOption { type = customTypes.modeMappings; default = { }; };
visual = mkOption { type = customTypes.modeMappings; default = { }; };
command = mkOption { type = customTypes.modeMappings; default = { }; };
select = mkOption { type = customTypes.modeMappings; default = { }; };
};
};
plugin = types.either types.package (types.submodule {
options = {
plugin = mkOption { type = types.package; };
dependencies = mkOption {
type = types.listOf customTypes.plugin;
default = [ ];
};
mappings = mkOption { type = customTypes.mappings; default = { }; };
config = mkOption { type = types.nullOr types.str; default = null; };
};
});
autoCommand = types.submodule {
options = {
event = mkOption { type = types.either types.str (types.listOf types.str); };
pattern = mkOption { type = types.either types.str (types.listOf types.str); };
command = mkOption { type = types.str; };
};
};
lspServer = types.submodule {
options = {
name = mkOption { type = types.str; };
config = mkOption { type = types.attrs; default = { }; };
package = mkOption { type = types.nullOr types.package; default = null; };
rootPattern = mkOption { type = types.nullOr (types.listOf types.str); default = null; };
};
};
formatter = types.submodule {
options = {
filetypes = mkOption { type = types.listOf types.str; };
globs = mkOption { type = types.listOf types.str; };
exe = mkOption { type = types.either types.path types.str; };
args = mkOption { type = types.functionTo (types.listOf types.str); default = _: [ ]; };
stdin = mkEnableOption { };
no_append = mkEnableOption { };
};
};
};
generateMappings = mappings: lib.strings.concatLines (
lib.attrsets.mapAttrsToList
(mode: modeMappings:
generateMappings =
mappings:
lib.strings.concatLines (
lib.attrsets.mapAttrsToList (
mode: modeMappings:
lib.strings.concatLines (
lib.attrsets.mapAttrsToList
(name: value:
lib.attrsets.mapAttrsToList (
name: value:
let
mapping = { lhs = name; lua = false; options = { }; } // (if builtins.isString value then { rhs = value; } else value);
args = [
(toLua (builtins.substring 0 1 mode))
(toLua mapping.lhs)
(if mapping.lua then mapping.rhs else toLua mapping.rhs)
(
toLua (
builtins.listToAttrs (
builtins.filter ({ value, ... }: !isNull (value)) (lib.attrsets.attrsToList mapping.options)
)
)
let
mapping = {
lhs = name;
lua = false;
options = { };
} // (if builtins.isString value then { rhs = value; } else value);
args = [
(toLua (builtins.substring 0 1 mode))
(toLua mapping.lhs)
(if mapping.lua then mapping.rhs else toLua mapping.rhs)
(toLua (
builtins.listToAttrs (
builtins.filter ({ value, ... }: !isNull (value)) (lib.attrsets.attrsToList mapping.options)
)
];
in
"vim.keymap.set(${lib.strings.concatStringsSep ", " args})"
)
modeMappings
))
];
in
"vim.keymap.set(${lib.strings.concatStringsSep ", " args})"
) modeMappings
)
)
mappings
);
) mappings
);
generateAutoCommand = { event, pattern, command, group ? null }: ''
vim.api.nvim_create_autocmd(${toLua event}, {
pattern = ${toLua pattern},
command = ${toLua command},
group = ${if isNull group then toLua group else ''
vim.api.nvim_create_augroup(${toLua group}, { clear = true })
''},
})
'';
generateAutoCommand =
{
event,
pattern,
command,
group ? null,
}:
''
vim.api.nvim_create_autocmd(${toLua event}, {
pattern = ${toLua pattern},
command = ${toLua command},
group = ${
if isNull group then
toLua group
else
''
vim.api.nvim_create_augroup(${toLua group}, { clear = true })
''
},
})
'';
generateAutoCommands = autoCommands: lib.strings.concatLines (
map generateAutoCommand autoCommands
);
generateAutoCommands = autoCommands: lib.strings.concatLines (map generateAutoCommand autoCommands);
generateSignDefinitions = signs: lib.strings.concatLines (
lib.attrsets.mapAttrsToList
(name: value:
generateSignDefinitions =
signs:
lib.strings.concatLines (
lib.attrsets.mapAttrsToList (
name: value:
let
hl = "DiagnosticSign${lib.strings.toUpper (builtins.substring 0 1 name)}${builtins.substring 1 (-1) name}";
hl = "DiagnosticSign${
lib.strings.toUpper (builtins.substring 0 1 name)
}${builtins.substring 1 (-1) name}";
in
"vim.fn.sign_define(${toLua hl}, ${toLua { text = value; texthl = hl; numhl = ""; }})"
)
signs
);
"vim.fn.sign_define(${toLua hl}, ${
toLua {
text = value;
texthl = hl;
numhl = "";
}
})"
) signs
);
in
{
options.programs.neovim =
@@ -291,12 +413,18 @@ in
inherit (lib) mkOption types;
in
{
mappings = mkOption { type = customTypes.mappings; default = { }; };
mappings = mkOption {
type = customTypes.mappings;
default = { };
};
plug = mkOption {
type = types.listOf customTypes.plugin;
default = [ ];
};
leader = mkOption { type = types.str; default = "\\"; };
leader = mkOption {
type = types.str;
default = "\\";
};
options = mkOption {
type = types.attrs;
default = { };
@@ -314,10 +442,22 @@ in
default = { };
};
signs = {
error = mkOption { type = types.str; default = ""; };
warning = mkOption { type = types.str; default = ""; };
info = mkOption { type = types.str; default = ""; };
hint = mkOption { type = types.str; default = ""; };
error = mkOption {
type = types.str;
default = "";
};
warning = mkOption {
type = types.str;
default = "";
};
info = mkOption {
type = types.str;
default = "";
};
hint = mkOption {
type = types.str;
default = "";
};
};
lsp = {
servers = mkOption {
@@ -325,8 +465,14 @@ in
default = [ ];
};
mappings = {
buf = mkOption { type = types.attrsOf types.str; default = { }; };
diagnostic = mkOption { type = types.attrsOf types.str; default = { }; };
buf = mkOption {
type = types.attrsOf types.str;
default = { };
};
diagnostic = mkOption {
type = types.attrsOf types.str;
default = { };
};
};
};
withTreesitterPlugins = mkOption {
@@ -345,22 +491,30 @@ in
"local Plugins = {}"
(generateMappings cfg.mappings)
(generateAutoCommands cfg.autoCommands)
(lib.strings.concatLines (lib.attrsets.mapAttrsToList (name: value: "vim.env.${name} = ${toLua value}") cfg.env))
(lib.strings.concatLines (lib.attrsets.mapAttrsToList (name: value: "vim.opt.${name} = ${toLua value}") cfg.options))
(lib.strings.concatLines (
lib.attrsets.mapAttrsToList (name: value: "vim.env.${name} = ${toLua value}") cfg.env
))
(lib.strings.concatLines (
lib.attrsets.mapAttrsToList (name: value: "vim.opt.${name} = ${toLua value}") cfg.options
))
(generateSignDefinitions cfg.signs)
"vim.g.mapleader = ${toLua cfg.leader}"
''
local undodir = ${toLua (
if builtins.hasAttr "undodir" cfg.options
then cfg.options.undodir
else "${config.xdg.cacheHome}/nvim/undo"
)}
local undodir = ${
toLua (
if builtins.hasAttr "undodir" cfg.options then
cfg.options.undodir
else
"${config.xdg.cacheHome}/nvim/undo"
)
}
vim.opt.undodir = undodir
vim.fn.mkdir(undodir, 'p')
''
];
plugins = lib.lists.concatMap buildPluginConfig (
cfg.plug ++ [
cfg.plug
++ [
lspPluginConfig
treesitterPluginConfig
formatterPluginConfig
@@ -371,12 +525,12 @@ in
};
xdg.configFile = (
lib.attrsets.mapAttrs'
(name: value: {
name = "nvim/${name}";
value = { source = value; };
})
cfg.snippets
lib.attrsets.mapAttrs' (name: value: {
name = "nvim/${name}";
value = {
source = value;
};
}) cfg.snippets
);
home.shellAliases =