neovim-flake/modules/core/default.nix

395 lines
11 KiB
Nix
Raw Normal View History

{
config,
lib,
...
}: let
inherit (builtins) attrValues attrNames map mapAttrs toJSON isString concatStringsSep filter;
2024-03-24 01:14:39 +01:00
inherit (lib.options) mkOption literalExpression mdDoc;
inherit (lib.attrsets) filterAttrs getAttrs;
inherit (lib.strings) optionalString;
inherit (lib.misc) mapAttrsFlatten;
inherit (lib.trivial) showWarnings;
inherit (lib.types) bool str listOf oneOf attrsOf nullOr attrs submodule unspecified lines;
inherit (lib.nvim.types) dagOf pluginsOpt extraPluginType;
inherit (lib.nvim.dag) entryAnywhere entryAfter topoSort;
2024-03-16 10:29:32 +01:00
inherit (lib.generators) mkLuaInline;
2024-03-24 01:14:39 +01:00
inherit (lib.nvim.lua) toLuaObject;
inherit (lib.nvim.vim) valToVim;
2023-11-06 10:33:38 +01:00
cfg = config.vim;
wrapLuaConfig = luaConfig: ''
lua << EOF
${optionalString cfg.enableLuaLoader ''
vim.loader.enable()
''}
${luaConfig}
EOF
'';
mkBool = value: description:
mkOption {
2024-03-24 01:14:39 +01:00
type = bool;
default = value;
2023-11-06 10:33:38 +01:00
inherit description;
};
# Most of the keybindings code is highly inspired by pta2002/nixvim. Thank you!
mapConfigOptions = {
silent =
mkBool false
"Whether this mapping should be silent. Equivalent to adding <silent> to a map.";
nowait =
mkBool false
"Whether to wait for extra input on ambiguous mappings. Equivalent to adding <nowait> to a map.";
script =
mkBool false
"Equivalent to adding <script> to a map.";
expr =
mkBool false
"Means that the action is actually an expression. Equivalent to adding <expr> to a map.";
unique =
mkBool false
"Whether to fail if the map is already defined. Equivalent to adding <unique> to a map.";
noremap =
mkBool true
"Whether to use the 'noremap' variant of the command, ignoring any custom mappings on the defined action. It is highly advised to keep this on, which is the default.";
desc = mkOption {
2024-03-24 01:14:39 +01:00
type = nullOr str;
default = null;
description = "A description of this keybind, to be shown in which-key, if you have it enabled.";
};
};
genMaps = mode: maps: let
/*
Take a user-defined action (string or attrs) and return the following attribute set:
{
action = (string) the actual action to map to this key
config = (attrs) the configuration options for this mapping (noremap, silent...)
}
*/
normalizeAction = action: let
# Extract the values of the config options that have been explicitly set by the user
config =
2023-11-06 10:33:38 +01:00
filterAttrs (_: v: v != null)
(getAttrs (attrNames mapConfigOptions) action);
in {
config =
if config == {}
then {"__empty" = null;}
else config;
action =
if action.lua
2024-03-16 10:29:32 +01:00
then mkLuaInline action.action
else action.action;
};
in
attrValues (mapAttrs
(key: action: let
normalizedAction = normalizeAction action;
in {
inherit (normalizedAction) action config;
2023-11-06 10:33:38 +01:00
inherit key;
inherit mode;
})
maps);
2024-03-24 01:14:39 +01:00
mapOption = submodule {
options =
mapConfigOptions
// {
action = mkOption {
2024-03-24 01:14:39 +01:00
type = str;
description = "The action to execute.";
};
lua = mkOption {
2024-03-24 01:14:39 +01:00
type = bool;
description = ''
If true, `action` is considered to be lua code.
Thus, it will not be wrapped in `""`.
'';
default = false;
};
};
};
mapOptions = mode:
mkOption {
description = "Mappings for ${mode} mode";
2024-03-24 01:14:39 +01:00
type = attrsOf mapOption;
default = {};
};
in {
2023-11-06 10:33:38 +01:00
options = {
2024-03-24 01:14:39 +01:00
assertions = mkOption {
type = listOf unspecified;
2023-11-06 10:33:38 +01:00
internal = true;
default = [];
example = literalExpression ''
[
{
assertion = false;
message = "you can't enable this for that reason";
}
]
'';
};
2023-11-06 10:33:38 +01:00
warnings = mkOption {
internal = true;
default = [];
2024-03-24 01:14:39 +01:00
type = listOf str;
2023-11-06 10:33:38 +01:00
example = ["The `foo' service is deprecated and will go away soon!"];
2024-03-24 01:14:39 +01:00
description = mdDoc ''
2023-11-06 10:33:38 +01:00
This option allows modules to show warnings to users during
the evaluation of the system configuration.
'';
};
2023-11-06 10:33:38 +01:00
vim = {
viAlias = mkOption {
description = "Enable vi alias";
2024-03-24 01:14:39 +01:00
type = bool;
2023-11-06 10:33:38 +01:00
default = true;
};
2023-11-06 10:33:38 +01:00
vimAlias = mkOption {
description = "Enable vim alias";
2024-03-24 01:14:39 +01:00
type = bool;
2023-11-06 10:33:38 +01:00
default = true;
};
2023-11-06 10:33:38 +01:00
configRC = mkOption {
description = "vimrc contents";
2024-03-24 01:14:39 +01:00
type = oneOf [(dagOf lines) str];
2023-11-06 10:33:38 +01:00
default = {};
};
2023-11-06 10:33:38 +01:00
luaConfigRC = mkOption {
description = "vim lua config";
2024-03-24 01:14:39 +01:00
type = oneOf [(dagOf lines) str];
2023-11-06 10:33:38 +01:00
default = {};
};
2023-11-06 10:33:38 +01:00
builtConfigRC = mkOption {
internal = true;
2024-03-24 01:14:39 +01:00
type = lines;
2023-11-06 10:33:38 +01:00
description = "The built config for neovim after resolving the DAG";
};
2024-03-24 01:14:39 +01:00
startPlugins = pluginsOpt {
2023-11-06 10:33:38 +01:00
default = [];
description = "List of plugins to startup.";
};
2024-03-24 01:14:39 +01:00
optPlugins = pluginsOpt {
2023-11-06 10:33:38 +01:00
default = [];
description = "List of plugins to optionally load";
};
2023-11-06 10:33:38 +01:00
extraPlugins = mkOption {
2024-03-24 01:14:39 +01:00
type = attrsOf extraPluginType;
2023-11-06 10:33:38 +01:00
default = {};
description = ''
List of plugins and related config.
Note that these are setup after builtin plugins.
'';
example = literalExpression ''
with pkgs.vimPlugins; {
aerial = {
package = aerial-nvim;
setup = "require('aerial').setup {}";
};
harpoon = {
package = harpoon;
setup = "require('harpoon').setup {}";
after = ["aerial"];
};
}'';
};
luaPackages = mkOption {
2024-03-24 01:14:39 +01:00
type = listOf str;
default = [];
description = ''
List of lua packages to install.
'';
};
2023-11-06 10:33:38 +01:00
globals = mkOption {
default = {};
description = "Set containing global variable values";
2024-03-24 01:14:39 +01:00
type = attrs;
2023-11-06 10:33:38 +01:00
};
2023-11-06 10:33:38 +01:00
maps = mkOption {
2024-03-24 01:14:39 +01:00
type = submodule {
2023-11-06 10:33:38 +01:00
options = {
normal = mapOptions "normal";
insert = mapOptions "insert";
select = mapOptions "select";
visual = mapOptions "visual and select";
terminal = mapOptions "terminal";
normalVisualOp = mapOptions "normal, visual, select and operator-pending (same as plain 'map')";
visualOnly = mapOptions "visual only";
operator = mapOptions "operator-pending";
insertCommand = mapOptions "insert and command-line";
lang = mapOptions "insert, command-line and lang-arg";
command = mapOptions "command-line";
};
};
2023-11-06 10:33:38 +01:00
default = {};
description = ''
Custom keybindings for any mode.
For plain maps (e.g. just 'map' or 'remap') use maps.normalVisualOp.
'';
example = ''
maps = {
normal."<leader>m" = {
silent = true;
action = "<cmd>make<CR>";
}; # Same as nnoremap <leader>m <silent> <cmd>make<CR>
};
'';
};
2023-04-04 22:48:37 +02:00
};
};
config = let
2023-03-31 04:20:35 +02:00
filterNonNull = mappings: filterAttrs (_name: value: value != null) mappings;
globalsScript =
mapAttrsFlatten (name: value: "let g:${name}=${valToVim value}")
(filterNonNull cfg.globals);
toLuaBindings = mode: maps:
map (value: ''
vim.keymap.set(${toLuaObject mode}, ${toLuaObject value.key}, ${toLuaObject value.action}, ${toLuaObject value.config})
'') (genMaps mode maps);
# I'm not sure if every one of these will work.
allmap = toLuaBindings "" config.vim.maps.normalVisualOp;
nmap = toLuaBindings "n" config.vim.maps.normal;
vmap = toLuaBindings "v" config.vim.maps.visual;
xmap = toLuaBindings "x" config.vim.maps.visualOnly;
smap = toLuaBindings "s" config.vim.maps.select;
2023-04-10 22:44:18 +02:00
imap = toLuaBindings "i" config.vim.maps.insert;
cmap = toLuaBindings "c" config.vim.maps.command;
tmap = toLuaBindings "t" config.vim.maps.terminal;
lmap = toLuaBindings "l" config.vim.maps.lang;
omap = toLuaBindings "o" config.vim.maps.operator;
icmap = toLuaBindings "ic" config.vim.maps.insertCommand;
resolveDag = {
name,
dag,
mapResult,
}: let
2023-05-22 19:00:33 +02:00
# When the value is a string, default it to dag.entryAnywhere
2024-03-24 01:14:39 +01:00
finalDag = mapAttrs (_: value:
if isString value
2024-03-24 01:14:39 +01:00
then entryAnywhere value
else value)
dag;
2024-03-24 01:14:39 +01:00
sortedDag = topoSort finalDag;
result =
if sortedDag ? result
then mapResult sortedDag.result
else abort ("Dependency cycle in ${name}: " + toJSON sortedDag);
in
result;
in {
vim = {
startPlugins = map (x: x.package) (attrValues cfg.extraPlugins);
configRC = {
2024-03-24 01:14:39 +01:00
globalsScript = entryAnywhere (concatStringsSep "\n" globalsScript);
luaScript = let
mkSection = r: ''
-- SECTION: ${r.name}
${r.data}
'';
mapResult = r: (wrapLuaConfig (concatStringsSep "\n" (map mkSection r)));
luaConfig = resolveDag {
name = "lua config script";
dag = cfg.luaConfigRC;
inherit mapResult;
};
in
2024-03-24 01:14:39 +01:00
entryAfter ["globalsScript"] luaConfig;
2023-07-21 15:25:40 +02:00
extraPluginConfigs = let
mkSection = r: ''
-- SECTION: ${r.name}
${r.data}
'';
mapResult = r: (wrapLuaConfig (concatStringsSep "\n" (map mkSection r)));
extraPluginsDag = mapAttrs (_: {
2023-07-26 15:27:08 +02:00
after,
2023-07-21 15:25:40 +02:00
setup,
...
}:
2024-03-24 01:14:39 +01:00
entryAfter after setup)
2023-07-21 15:25:40 +02:00
cfg.extraPlugins;
pluginConfig = resolveDag {
name = "extra plugins config";
dag = extraPluginsDag;
inherit mapResult;
};
in
2024-03-24 01:14:39 +01:00
entryAfter ["luaScript"] pluginConfig;
2023-07-21 15:25:40 +02:00
2023-04-10 22:27:13 +02:00
# This is probably not the right way to set the config. I'm not sure how it should look like.
mappings = let
maps = [
nmap
imap
vmap
xmap
smap
cmap
omap
tmap
lmap
icmap
allmap
];
mapConfig = wrapLuaConfig (concatStringsSep "\n" (map (v: concatStringsSep "\n" v) maps));
in
2024-03-24 01:14:39 +01:00
entryAfter ["globalsScript"] mapConfig;
};
builtConfigRC = let
2023-11-06 10:33:38 +01:00
failedAssertions = map (x: x.message) (filter (x: !x.assertion) config.assertions);
baseSystemAssertWarn =
if failedAssertions != []
then throw "\nFailed assertions:\n${concatStringsSep "\n" (map (x: "- ${x}") failedAssertions)}"
2024-03-24 01:14:39 +01:00
else showWarnings config.warnings;
2023-11-06 10:33:38 +01:00
mkSection = r: ''
" SECTION: ${r.name}
${r.data}
'';
mapResult = r: (concatStringsSep "\n" (map mkSection r));
vimConfig = resolveDag {
name = "vim config script";
dag = cfg.configRC;
inherit mapResult;
};
in
2023-11-06 10:33:38 +01:00
baseSystemAssertWarn vimConfig;
};
};
}