nixeovim/modules/fn.nix

66 lines
1.8 KiB
Nix

{ lib, pkgs, ... }:
let
inherit (lib.nix2lua) lambda raw;
fnOpts = { name, config, ... }: {
options = with lib; {
args = mkOption {
type = with types; listOf str;
default = [ ];
};
content = mkOption {
type = types.functionTo (lib.types.submoduleWith {
modules = [
{
options.extra = with lib; mkOption {
type = with types; nullOr attrs;
default = null;
};
}
./vim/keymap.nix
./vim/augroup.nix
# ./vim/opts.nix
##################################################
(pkgs.path + "/nixos/modules/misc/assertions.nix")
];
specialArgs = { inherit lib; };
});
default = { };
};
lambda = mkOption {
type = types.attrs;
readOnly = true;
internal = true;
};
};
config = {
lambda =
let
inherit (builtins) listToAttrs attrValues;
fnArgs = listToAttrs (map (v: { name = v; value = raw v; }) config.args);
innerCfg = config.content fnArgs;
in
lambda config.args (lib.flatten [
innerCfg.extra
# Autocommands
(map (v: v.genConfig) (attrValues innerCfg.vim.augroup))
# Keymaps
(lib.flip map innerCfg.vim.keymap.set ({ mode, lhs, rhs, ... } @ vars:
let m = if builtins.isList mode then lib.head mode else mode; in
if innerCfg.vim.keymap._validate."${m}"."${lhs}" == rhs then vars.genConfig
else abort "This case should never happen."
))
]);
};
};
in
{
options.fn = with lib; mkOption {
type = with types; attrsOf (submodule fnOpts);
default = { };
};
}