1
Fork 0
satellite/modules/common/neovim.nix

761 lines
25 KiB
Nix
Raw Normal View History

2023-12-07 22:35:57 +01:00
# This module provides personalised helpers for managing plugins
# using lazy.nvim and a set of custom runtime primitives.
2023-12-02 06:04:57 +01:00
{ pkgs, lib, config, ... }:
let
inherit (lib) types;
cfg = config.satellite.neovim;
# {{{ Custom types
myTypes = {
2023-12-07 22:35:57 +01:00
oneOrMany = t: types.either t (types.listOf t);
zeroOrMore = t: types.nullOr (myTypes.oneOrMany t);
2023-12-04 09:06:02 +01:00
2023-12-07 22:35:57 +01:00
# {{{ Lua code
2023-12-02 06:04:57 +01:00
luaCode = types.nullOr (types.oneOf [
types.str
types.path
2023-12-02 12:16:33 +01:00
myTypes.luaLiteral
]);
2023-12-07 22:35:57 +01:00
luaLiteral = types.submodule {
2023-12-02 12:16:33 +01:00
options.__luaEncoderTag = lib.mkOption {
type = types.enum [ "lua" ];
};
options.value = lib.mkOption {
type = types.str;
};
2023-12-07 22:35:57 +01:00
};
2023-12-02 12:16:33 +01:00
luaValue = types.nullOr (types.oneOf [
types.str
types.number
types.bool
(types.attrsOf myTypes.luaValue)
(types.listOf myTypes.luaValue)
2023-12-02 06:04:57 +01:00
]);
2023-12-07 22:35:57 +01:00
# }}}
# {{{ Lazy key
2023-12-02 06:04:57 +01:00
lazyKey = types.oneOf [
types.str
(types.submodule
2023-12-07 22:35:57 +01:00
{
2023-12-02 06:04:57 +01:00
options.mapping = lib.mkOption {
type = types.str;
2023-12-04 09:06:02 +01:00
description = "The lhs of the neovim mapping";
2023-12-02 06:04:57 +01:00
};
options.action = lib.mkOption {
2023-12-02 12:16:33 +01:00
type = types.nullOr (types.oneOf [
types.str
myTypes.luaLiteral
]);
2023-12-04 09:06:02 +01:00
description = "The rhs of the neovim mapping";
2023-12-02 06:04:57 +01:00
};
options.ft = lib.mkOption {
default = null;
2023-12-04 09:06:02 +01:00
type = myTypes.zeroOrMore types.str;
2023-12-02 06:04:57 +01:00
description = "Filetypes on which this keybind should take effect";
};
options.mode = lib.mkOption {
default = null;
2023-12-02 12:16:33 +01:00
type = types.nullOr types.str;
2023-12-04 09:06:02 +01:00
description = "The vim modes the mapping should take effect in";
2023-12-02 06:04:57 +01:00
};
options.desc = lib.mkOption {
default = null;
type = types.nullOr types.str;
2023-12-04 09:06:02 +01:00
description = "Description for the current keymapping";
2023-12-02 06:04:57 +01:00
};
2023-12-07 22:35:57 +01:00
})
2023-12-02 06:04:57 +01:00
];
# }}}
2023-12-07 22:35:57 +01:00
# {{{ Tempest key
tempestKey = types.submodule {
options = {
mapping = lib.mkOption {
example = "<leader>a";
type = types.str;
description = "The lhs of the neovim mapping";
};
action = lib.mkOption {
example = "<C-^>";
type = types.either types.str myTypes.luaLiteral;
description = "The rhs of the neovim mapping";
};
bufnr = lib.mkOption {
default = null;
example = true;
type = types.nullOr
(types.oneOf [
types.bool
types.integer
myTypes.luaLiteral
]);
description = ''
The index of the buffer to apply local keymaps to. Can be set to
`true` to refer to the current buffer
'';
};
mode = lib.mkOption {
default = null;
example = "nov";
type = types.nullOr types.str;
description = "The vim modes the mapping should take effect in";
};
silent = lib.mkOption {
default = null;
example = true;
type = types.nullOr types.bool;
description = "Whether the logs emitted by the keymap should be supressed";
};
expr = lib.mkOption {
default = null;
example = true;
type = types.nullOr types.bool;
description = "If set to `true`, the mapping is treated as an action factory";
};
desc = lib.mkOption {
default = null;
type = types.nullOr types.str;
description = "Description for the current keymapping";
};
};
};
# }}}
# {{{ Tempest autocmd
tempestAutocmd = types.submodule {
options = {
event = lib.mkOption {
example = "InsertEnter";
type = myTypes.oneOrMany types.str;
description = "Events to bind autocmd to";
};
pattern = lib.mkOption {
example = "Cargo.toml";
type = myTypes.oneOrMany types.str;
description = "File name patterns to run autocmd on";
};
group = lib.mkOption {
example = "CargoCmpSource";
type = types.str;
description = "Name of the group to create and assign autocmd to";
};
callback = lib.mkOption {
example.vim.opt.cmdheight = 1;
type = types.oneOf [
myTypes.tempestConfiguration
myTypes.luaCode
];
description = ''
Code to run when the respctive event occurs. Will pass the event
object as context, which might be used for things like assigning
a buffer number to local keymaps automatically.
'';
};
};
};
# }}}
# {{{ Tempest configuration
tempestConfiguration = types.submodule {
options = {
vim = lib.mkOption {
default = null;
type = myTypes.luaValue;
example.opt.cmdheight = 0;
description = "Values to assign to the `vim` lua global object";
};
keys = lib.mkOption {
default = null;
type = myTypes.zeroOrMore myTypes.tempestKey;
description = ''
Arbitrary key mappings to create. The keymappings might
automatically be buffer specific depending on the context. For
instance, keymappings created inside autocmds will be local unless
otherwise specified.
'';
};
autocmds = lib.mkOption {
default = null;
type = myTypes.zeroOrMore myTypes.tempestAutocmd;
description = "Arbitrary autocmds to create";
};
setup = lib.mkOption {
default = null;
type = types.nullOr (types.attrsOf myTypes.luaValue);
example.lualine.opts.theme = "auto";
description = ''
Key-pair mappings for options to pass to .setup functions imported
from different modules
'';
};
callback = lib.mkOption {
default = null;
type = types.nullOr myTypes.luaCode;
description = "Arbitrary code to run after everything else has been configured";
};
};
};
# }}}
# {{{ Lazy module
lazyModule = lib.fix (lazyModule: types.submodule ({ name ? null, ... }: {
2023-12-02 06:04:57 +01:00
options = {
package = lib.mkOption {
type = types.oneOf [
types.package
types.str
];
description = "Package to configure the module around";
example = "nvim-telescope/telescope.nvim";
};
2023-12-02 12:16:33 +01:00
name = lib.mkOption {
2023-12-07 22:35:57 +01:00
default = name;
2023-12-02 12:16:33 +01:00
type = types.nullOr types.str;
description = "Custom name to use for the module";
example = "lualine";
};
main = lib.mkOption {
default = null;
type = types.nullOr types.str;
description = "The name of the lua entrypoint for the plugin (usually auto-detected)";
example = "lualine";
};
2023-12-02 06:04:57 +01:00
version = lib.mkOption {
default = null;
type = types.nullOr types.str;
description = "Pin the package to a certain version (useful for non-nix managed packages)";
};
tag = lib.mkOption {
default = null;
type = types.nullOr types.str;
description = "Pin the package to a certain git tag (useful for non-nix managed packages)";
};
lazy = lib.mkOption {
default = null;
type = types.nullOr types.bool;
description = "Specifies whether this module should be lazy-loaded";
};
dependencies.lua = lib.mkOption {
default = [ ];
2023-12-07 22:35:57 +01:00
type = types.listOf (types.either types.str lazyModule);
2023-12-04 09:06:02 +01:00
description = "Lazy.nvim module dependencies";
2023-12-02 06:04:57 +01:00
};
dependencies.nix = lib.mkOption {
default = [ ];
type = types.listOf types.package;
2023-12-04 09:06:02 +01:00
description = "Nix packages to give nvim access to";
2023-12-02 06:04:57 +01:00
};
cond = lib.mkOption {
default = null;
type = myTypes.luaCode;
description = "Condition based on which to enable/disbale loading the package";
};
2023-12-07 22:35:57 +01:00
env.blacklist = lib.mkOption {
default = [ ];
type = types.listOf (types.enum [ "firenvim" "vscode" "neovide" ]);
description = "Environments to blacklist plugin on";
};
2023-12-02 06:04:57 +01:00
setup = lib.mkOption {
default = null;
2023-12-07 22:35:57 +01:00
type = types.nullOr (types.oneOf [
myTypes.tempestConfiguration
myTypes.luaCode
types.bool
]);
2023-12-02 06:04:57 +01:00
description = ''
Lua function (or module) to use for configuring the package.
2023-12-07 22:35:57 +01:00
Used instead of the canonically named `config` because said name has a special meaning in nix
'';
2023-12-02 06:04:57 +01:00
};
event = lib.mkOption {
default = null;
2023-12-04 09:06:02 +01:00
type = myTypes.zeroOrMore types.str;
2023-12-02 06:04:57 +01:00
description = "Event on which the module should be lazy loaded";
};
ft = lib.mkOption {
default = null;
2023-12-04 09:06:02 +01:00
type = myTypes.zeroOrMore types.str;
2023-12-02 06:04:57 +01:00
description = "Filetypes on which the module should be lazy loaded";
};
2023-12-04 09:06:02 +01:00
cmd = lib.mkOption {
default = null;
type = myTypes.zeroOrMore types.str;
description = "Comands on which to load this plugin";
};
2023-12-02 06:04:57 +01:00
init = lib.mkOption {
default = null;
type = myTypes.luaCode;
description = "Lua function (or module) to run right away (even if the package is not yet loaded)";
};
passthrough = lib.mkOption {
default = null;
type = myTypes.luaCode;
description = "Attach additional things to the lazy module";
};
2023-12-02 12:16:33 +01:00
opts = lib.mkOption {
default = null;
type = myTypes.luaValue;
description = "Custom data to pass to the plugin .setup function";
};
2023-12-02 06:04:57 +01:00
keys = lib.mkOption {
default = null;
2023-12-04 09:06:02 +01:00
type = myTypes.zeroOrMore myTypes.lazyKey;
description = "Keybinds to lazy-load the module on";
2023-12-02 06:04:57 +01:00
};
};
}));
# }}}
};
# }}}
# {{{ Lua encoders
2023-12-07 22:35:57 +01:00
# We provide a custom set of helpers for generating lua code for nix.enable
#
2023-12-02 06:04:57 +01:00
# An encoder is a function from some nix value to a string containing lua code.
# This object provides combinators for writing such encoders.
luaEncoders = {
2023-12-07 22:35:57 +01:00
# {{{ "Raw" helpers
mkRawLuaObject = chunks:
''
{
${lib.concatStringsSep "," (lib.filter (s: s != "") chunks)}
}
'';
# }}}
2023-12-02 06:04:57 +01:00
# {{{ General helpers
identity = given: given;
2023-12-07 22:35:57 +01:00
# `const` is mostly useful together with `bind`. See the lua encoder for
# lazy modules for example usage.
const = code: _: code;
# Conceptually, this is the monadic bind operation for encoders.
# This implementation is isomoprhic to that of the reader monad in haskell.
2023-12-02 06:04:57 +01:00
bind = encoder: given: encoder given given;
2023-12-07 22:35:57 +01:00
# This is probably the most useful combinnator defined in this entire object.
# Most of the combinators in the other categories are based on this.
2023-12-02 06:04:57 +01:00
conditional = predicate: caseTrue: caseFalse:
luaEncoders.bind (given: if predicate given then caseTrue else caseFalse);
2023-12-07 22:35:57 +01:00
# This is simply left-composition of functions
2023-12-02 06:04:57 +01:00
map = f: encoder: given: encoder (f given);
2023-12-07 22:35:57 +01:00
# This is simply right-composition of functions
postmap = f: encoder: given: f (encoder given);
# This is mostly useful for debugging
2023-12-02 12:16:33 +01:00
trace = message: luaEncoders.map (f: lib.traceSeq message (lib.traceVal f));
fail = mkMessage: v: builtins.throw (mkMessage v);
2023-12-02 06:04:57 +01:00
# }}}
# {{{ Base types
2023-12-07 22:35:57 +01:00
string = given: ''"${lib.escape ["\"" "\\"] (toString given)}"'';
2023-12-02 06:04:57 +01:00
bool = bool: if bool then "true" else "false";
2023-12-02 12:16:33 +01:00
number = toString;
2023-12-02 06:04:57 +01:00
nil = _: "nil";
stringOr = luaEncoders.conditional lib.isString luaEncoders.string;
boolOr = luaEncoders.conditional lib.isBool luaEncoders.bool;
2023-12-02 12:16:33 +01:00
numberOr = luaEncoders.conditional (e: lib.isFloat e || lib.isInt e) luaEncoders.number;
2023-12-02 06:04:57 +01:00
nullOr = luaEncoders.conditional (e: e == null) luaEncoders.nil;
2023-12-07 22:35:57 +01:00
# We pipe a combinator which always fail through a bunch of
# `(thing)or : encoder -> encoder` functions, building up a combinator which
# can handle more and more kinds of values, until we eventually build up
# something that should be able to handle everything we throw at it.
2023-12-02 12:16:33 +01:00
anything = lib.pipe (luaEncoders.fail (v: "Cannot figure out how to encode value ${builtins.toJSON v}")) [
2023-12-04 06:25:00 +01:00
(luaEncoders.attrsetOfOr luaEncoders.anything)
(luaEncoders.listOfOr luaEncoders.anything)
2023-12-02 12:16:33 +01:00
luaEncoders.nullOr
luaEncoders.boolOr
luaEncoders.numberOr
luaEncoders.stringOr
2023-12-04 06:25:00 +01:00
luaEncoders.luaCodeOr # Lua code expressions have priority over attrsets
2023-12-02 12:16:33 +01:00
];
2023-12-02 06:04:57 +01:00
# }}}
2023-12-02 12:16:33 +01:00
# {{{ Lua code
2023-12-07 22:35:57 +01:00
# Tagged lua code can be combined with other combinators without worrying
# about conflicts regarding how strings are interpreted.
2023-12-02 12:16:33 +01:00
luaCodeOr =
luaEncoders.conditional (e: lib.isAttrs e && (e.__luaEncoderTag or null) == "lua")
(obj: obj.value);
2023-12-07 22:35:57 +01:00
# This is the most rudimentary (and currently only) way of handling paths.
luaImportOr = tag:
luaEncoders.conditional lib.isPath
(path: "dofile(${luaEncoders.string path}).${tag}");
# Accepts both tagged and untagged strings of lua code.
luaString = luaEncoders.luaCodeOr luaEncoders.identity;
# This simply combines the above combinators into one.
luaCode = tag: luaEncoders.luaImportOr tag luaEncoders.luaString;
2023-12-02 12:16:33 +01:00
# }}}
# {{{ Lists
2023-12-02 06:04:57 +01:00
listOf = encoder: list:
2023-12-07 22:35:57 +01:00
luaEncoders.mkRawLuaObject (lib.lists.map encoder list);
2023-12-02 12:16:33 +01:00
listOfOr = encoder:
2023-12-02 06:04:57 +01:00
luaEncoders.conditional
lib.isList
2023-12-02 12:16:33 +01:00
(luaEncoders.listOf encoder);
2023-12-07 22:35:57 +01:00
# Returns nil when given empty lists
tryNonemptyList = encoder: luaEncoders.conditional
(l: l == [ ])
luaEncoders.nil
(luaEncoders.listOf encoder);
2023-12-02 12:16:33 +01:00
oneOrMany = encoder: luaEncoders.listOfOr encoder encoder;
2023-12-07 22:35:57 +01:00
# Can encode:
# - zero values as nil
# - one value as itself
# - multiple values as a list
2023-12-04 09:06:02 +01:00
zeroOrMany = encoder: luaEncoders.nullOr (luaEncoders.oneOrMany encoder);
2023-12-07 22:35:57 +01:00
# Coerces non list values to lists of one element.
2023-12-02 06:04:57 +01:00
oneOrManyAsList = encoder: luaEncoders.map
(given: if lib.isList given then given else [ given ])
(luaEncoders.listOf encoder);
2023-12-07 22:35:57 +01:00
# Coerces lists of one element to said element.
2023-12-02 12:16:33 +01:00
listAsOneOrMany = encoder:
luaEncoders.map
(l: if lib.length l == 1 then lib.head l else l)
(luaEncoders.oneOrMany encoder);
# }}}
# {{{ Attrsets
attrsetOf = encoder: object:
2023-12-07 22:35:57 +01:00
luaEncoders.mkRawLuaObject (lib.mapAttrsToList
2023-12-02 12:16:33 +01:00
(name: value:
let result = encoder value;
in
lib.optionalString (result != "nil")
"${name} = ${result}"
)
object
);
attrsetOfOr = of: luaEncoders.conditional lib.isAttrs (luaEncoders.attrsetOf of);
2023-12-07 22:35:57 +01:00
# This is the most general combinator provided in this section.
#
# We accept:
# - a `noNils` flag which will automatically remove any nil properties
# - order of props that should be interpreted as list elements
# - spec of props that should be interpreted as list elements
# - record of props that should be interpreted as attribute props
attrset = noNils: listOrder: spec: attrset:
2023-12-02 06:04:57 +01:00
let
shouldKeep = given:
if noNils then
given != "nil"
else
true;
listChunks = lib.lists.map
(attr:
2023-12-07 22:35:57 +01:00
let result = spec.${attr} (attrset.${attr} or null);
2023-12-02 06:04:57 +01:00
in
lib.optionalString (shouldKeep result) result
)
listOrder;
objectChunks = lib.mapAttrsToList
(attr: encoder:
let result = encoder (attrset.${attr} or null);
in
2023-12-07 22:35:57 +01:00
lib.optionalString (!(lib.elem attr listOrder) && shouldKeep result)
2023-12-02 06:04:57 +01:00
"${attr} = ${result}"
)
spec;
in
2023-12-07 22:35:57 +01:00
luaEncoders.mkRawLuaObject (listChunks ++ objectChunks);
2023-12-02 06:04:57 +01:00
# }}}
};
e = luaEncoders;
# }}}
2023-12-07 22:35:57 +01:00
# {{{ Helpers
2023-12-02 06:04:57 +01:00
# Format and write a lua file to disk
writeLuaFile = path: name: text:
let
2023-12-04 09:06:02 +01:00
directory = "lua/${path}";
2023-12-02 06:04:57 +01:00
destination = "${directory}/${name}.lua";
unformatted = pkgs.writeText "raw-lua-${name}" text;
in
pkgs.runCommand "formatted-lua-${name}" { } ''
mkdir -p $out/${directory}
cp --no-preserve=mode ${unformatted} $out/${destination}
${lib.getExe pkgs.stylua} --config-path ${cfg.styluaConfig} $out/${destination}
'';
2023-12-07 22:35:57 +01:00
# }}}
2023-12-02 06:04:57 +01:00
in
{
2023-12-07 22:35:57 +01:00
# {{{ Option declaration
2023-12-02 06:04:57 +01:00
options.satellite.neovim = {
lazy = lib.mkOption {
default = { };
2023-12-07 22:35:57 +01:00
description = "Record of plugins to install using lazy.nvim";
2023-12-02 06:04:57 +01:00
type = types.attrsOf myTypes.lazyModule;
};
2023-12-07 22:35:57 +01:00
# {{{ Generated
2023-12-02 12:16:33 +01:00
generated = {
lazy = lib.mkOption {
2023-12-07 22:35:57 +01:00
type = types.attrsOf (types.submodule {
2023-12-02 12:16:33 +01:00
options = {
raw = lib.mkOption {
type = types.lines;
description = "The lua script generated using the other options";
};
module = lib.mkOption {
type = types.package;
description = "The lua script generated using the other options";
};
2023-12-02 06:04:57 +01:00
};
2023-12-07 22:35:57 +01:00
});
2023-12-02 12:16:33 +01:00
description = "Attrset containing every module generated from the lazy configuration";
};
2023-12-02 06:04:57 +01:00
2023-12-02 12:16:33 +01:00
all = lib.mkOption {
default = { };
type = types.package;
description = "Derivation building all the given nix modules";
};
2023-12-04 06:25:00 +01:00
dependencies = lib.mkOption {
default = [ ];
type = types.listOf types.package;
description = "List of packages to give neovim access to";
};
2023-12-02 06:04:57 +01:00
};
2023-12-07 22:35:57 +01:00
# }}}
# {{{ Lua generation lib
2023-12-02 12:16:33 +01:00
lib = {
2023-12-07 22:35:57 +01:00
# {{{ Basic lua generators
2023-12-02 12:16:33 +01:00
lua = lib.mkOption {
default = value: { inherit value; __luaEncoderTag = "lua"; };
type = types.functionTo myTypes.luaLiteral;
description = "include some raw lua code inside module configuration";
};
import = lib.mkOption {
default = path: tag: cfg.lib.lua "dofile(${e.string path}).${tag}";
type = types.functionTo (types.functionTo myTypes.luaLiteral);
description = "import some identifier from some module";
};
2023-12-07 22:35:57 +01:00
# }}}
# {{{ Encoders
encode = lib.mkOption {
default = luaEncoders.anything;
type = types.functionTo types.str;
description = "Encode a nix value to a lua string";
};
2023-12-02 12:16:33 +01:00
2023-12-07 22:35:57 +01:00
encodeTempestConfiguration = lib.mkOption {
default = given:
e.attrset true [ ]
{
vim = e.anything;
callback = e.nullOr e.luaString;
setup = e.nullOr (e.attrsetOf e.anything);
keys = e.zeroOrMany (e.attrset true [ ] {
mapping = e.string;
action = e.luaCodeOr e.string;
desc = e.nullOr e.string;
expr = e.nullOr e.bool;
mode = e.nullOr e.string;
silent = e.nullOr e.bool;
buffer = e.nullOr (e.luaCodeOr (e.boolOr e.number));
});
autocmds = e.zeroOrMany (e.attrset true [ ] {
event = e.oneOrMany e.string;
pattern = e.oneOrMany e.string;
group = e.string;
callback = e.conditional lib.isAttrs
cfg.lib.encodeTempestConfiguration
e.luaString;
});
}
given;
type = types.functionTo types.str;
description = "Generate a lua object for passing to my own lua runtime for configuration";
};
# }}}
# {{{ Thunks
# This version of `nlib.thunk` is required in ceratain cases because
# of issues with `types.oneOf [types.submodule ..., types.submodule]` not
# working as intended atm.
thunkString = lib.mkOption {
default = given: /* lua */ ''
function() ${e.luaString given} end
2023-12-02 12:16:33 +01:00
'';
2023-12-07 22:35:57 +01:00
type = types.functionTo types.str;
description = "Wrap a lua expression into a lua function as a string";
2023-12-02 12:16:33 +01:00
};
2023-12-04 09:06:02 +01:00
thunk = lib.mkOption {
2023-12-07 22:35:57 +01:00
default = given: cfg.lib.lua (cfg.lib.thunkString given);
2023-12-04 09:06:02 +01:00
type = types.functionTo myTypes.luaLiteral;
description = "Wrap a lua expression into a lua function";
};
2023-12-02 12:16:33 +01:00
2023-12-07 22:35:57 +01:00
contextThunk = lib.mkOption {
default = given: cfg.lib.lua /* lua */ ''
function(context) ${e.luaString given} end
'';
type = types.functionTo myTypes.luaLiteral;
description = "Wrap a lua expression into a lua function taking an argument named `context`";
};
# }}}
# {{{ Language server on attach
languageServerOnAttach = lib.mkOption {
default = given: cfg.lib.lua /* lua */ ''
function(client, bufnr)
require(${e.string cfg.runtime.tempest}).configure(${cfg.lib.encodeTempestConfiguration given},
{ client = client; bufnr = bufnr; })
require(${e.string cfg.runtime.languageServerOnAttach}).on_attach(client, bufnr)
end
'';
type = types.functionTo myTypes.luaCode;
description = "Attach a language server and run some additional code";
};
# }}}
};
# }}}
# {{{ Neovim runtime module paths
runtime = {
env = lib.mkOption {
2023-12-02 12:16:33 +01:00
type = types.str;
example = "my.helpers.env";
2023-12-07 22:35:57 +01:00
description = "Module to import env flags from";
};
tempest = lib.mkOption {
type = types.str;
example = "my.runtime.tempest";
description = "Module to import the tempest runtime from";
};
languageServerOnAttach = lib.mkOption {
type = types.str;
example = "my.runtime.lspconfig";
description = "Module to import langauge server .on_attach function from";
2023-12-02 12:16:33 +01:00
};
2023-12-02 06:04:57 +01:00
};
2023-12-07 22:35:57 +01:00
# }}}
2023-12-02 06:04:57 +01:00
styluaConfig = lib.mkOption {
type = types.path;
description = "Config to use for formatting lua modules";
};
};
2023-12-07 22:35:57 +01:00
# }}}
# {{{ Config generation
# {{{ Lazy module generation
2023-12-02 06:04:57 +01:00
config.satellite.neovim.generated.lazy =
let
2023-12-07 22:35:57 +01:00
# {{{ Lazy key encoder
2023-12-02 06:04:57 +01:00
lazyKeyEncoder =
2023-12-07 22:35:57 +01:00
e.stringOr (e.attrset true [ "mapping" "action" ] {
mapping = e.string;
action = e.nullOr (e.luaCodeOr e.string);
mode = e.nullOr
(e.map
lib.strings.stringToCharacters
(e.listAsOneOrMany e.string));
desc = e.nullOr e.string;
ft = e.zeroOrMany e.string;
});
# }}}
# {{{ Lazy spec encoder
2023-12-02 12:16:33 +01:00
lazyObjectEncoder = e.bind
(opts: e.attrset true [ "package" ]
2023-12-02 06:04:57 +01:00
{
2023-12-07 22:35:57 +01:00
package = e.string;
2023-12-02 12:16:33 +01:00
name = e.nullOr e.string;
main = e.nullOr e.string;
2023-12-02 06:04:57 +01:00
tag = e.nullOr e.string;
version = e.nullOr e.string;
2023-12-07 22:35:57 +01:00
dependencies = e.map (d: d.lua) (e.tryNonemptyList (e.stringOr lazyObjectEncoder));
2023-12-02 06:04:57 +01:00
lazy = e.nullOr e.bool;
2023-12-07 22:35:57 +01:00
cond =
if opts.env.blacklist != [ ] then
assert lib.asserts.assertMsg (opts.cond == null)
"env.blacklist overrides plugin condition";
e.const /* lua */ ''
require(${e.string cfg.runtime.env}).blacklist(${e.listOf e.string opts.env.blacklist})
''
else
e.nullOr (e.luaCode "cond");
config = _:
let
wrap = given: /* lua */''
function(lazy, opts)
require(${e.string cfg.runtime.tempest}).configure(${given},
{ lazy = lazy; opts = opts; })
end
'';
in
e.conditional lib.isAttrs
(e.postmap wrap cfg.lib.encodeTempestConfiguration)
(e.nullOr (e.boolOr (e.luaCode "config")))
opts.setup;
2023-12-02 06:04:57 +01:00
init = e.nullOr (e.luaCode "init");
2023-12-04 09:06:02 +01:00
event = e.zeroOrMany e.string;
cmd = e.zeroOrMany e.string;
ft = e.zeroOrMany e.string;
2023-12-02 06:04:57 +01:00
keys = e.nullOr (e.oneOrManyAsList lazyKeyEncoder);
2023-12-02 12:16:33 +01:00
passthrough = e.anything;
opts = e.anything;
2023-12-02 06:04:57 +01:00
});
2023-12-07 22:35:57 +01:00
# }}}
2023-12-02 06:04:57 +01:00
makeLazyScript = opts: ''
2023-12-07 22:35:57 +01:00
-- This file was generated using nix ^~^
2023-12-02 06:04:57 +01:00
return ${lazyObjectEncoder opts}
'';
in
lib.attrsets.mapAttrs
(name: opts: rec {
raw = makeLazyScript opts;
module = writeLuaFile "nix/plugins" name raw;
})
cfg.lazy;
config.satellite.neovim.generated.all =
pkgs.symlinkJoin {
name = "lazy-nvim-modules";
paths = lib.attrsets.mapAttrsToList (_: m: m.module) cfg.generated.lazy;
};
2023-12-07 22:35:57 +01:00
# }}}
2023-12-04 06:25:00 +01:00
config.satellite.neovim.generated.dependencies =
lib.pipe cfg.lazy
[
(lib.attrsets.mapAttrsToList (_: m: m.dependencies.nix))
lib.lists.flatten
2023-12-07 22:35:57 +01:00
];
# }}}
2023-12-02 06:04:57 +01:00
}
2023-12-07 22:35:57 +01:00