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

436 lines
13 KiB
Nix
Raw Normal View History

2023-12-02 06:04:57 +01:00
# Additional theming primitives not provided by stylix
{ pkgs, lib, config, ... }:
let
inherit (lib) types;
cfg = config.satellite.neovim;
# {{{ Custom types
myTypes = {
luaCode = types.nullOr (types.oneOf [
types.str
types.path
2023-12-02 12:16:33 +01:00
myTypes.luaLiteral
]);
luaLiteral = types.submodule (_: {
options.__luaEncoderTag = lib.mkOption {
type = types.enum [ "lua" ];
};
options.value = lib.mkOption {
type = types.str;
};
});
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
]);
fileTypes = types.nullOr (types.oneOf [
types.str
(types.listOf types.str)
]);
# {{{ Key type
lazyKey = types.oneOf [
types.str
(types.submodule
(_: {
options.mapping = lib.mkOption {
type = types.str;
};
options.action = lib.mkOption {
2023-12-02 12:16:33 +01:00
type = types.nullOr (types.oneOf [
types.str
myTypes.luaLiteral
]);
2023-12-02 06:04:57 +01:00
};
options.ft = lib.mkOption {
default = null;
type = myTypes.fileTypes;
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-02 06:04:57 +01:00
};
options.desc = lib.mkOption {
default = null;
type = types.nullOr types.str;
};
}))
];
# }}}
# {{{ Lazy module type
lazyModule = lib.fix (lazyModule: types.submodule (_: {
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 {
default = null;
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 = [ ];
type = types.listOf lazyModule;
};
dependencies.nix = lib.mkOption {
default = [ ];
type = types.listOf types.package;
};
cond = lib.mkOption {
default = null;
type = myTypes.luaCode;
description = "Condition based on which to enable/disbale loading the package";
};
setup = lib.mkOption {
default = null;
type = types.oneOf [ myTypes.luaCode types.bool ];
description = ''
Lua function (or module) to use for configuring the package.
Used instead of the canonically named `config` because said property has a special name in nix'';
};
event = lib.mkOption {
default = null;
type = types.nullOr (types.oneOf [
types.str
(types.listOf types.str)
]);
description = "Event on which the module should be lazy loaded";
};
ft = lib.mkOption {
default = null;
type = myTypes.fileTypes;
description = "Filetypes on which the module should be lazy loaded";
};
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;
type =
types.nullOr (types.oneOf [
myTypes.lazyKey
(types.listOf myTypes.lazyKey)
]);
};
};
}));
# }}}
};
# }}}
# {{{ Lua encoders
mkRawLuaObject = chunks:
''
{
${lib.concatStringsSep "," (lib.filter (s: s != "") chunks)}
}
'';
# An encoder is a function from some nix value to a string containing lua code.
# This object provides combinators for writing such encoders.
luaEncoders = {
# {{{ General helpers
identity = given: given;
bind = encoder: given: encoder given given;
conditional = predicate: caseTrue: caseFalse:
luaEncoders.bind (given: if predicate given then caseTrue else caseFalse);
map = f: encoder: given: encoder (f given);
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);
const = code: _: code;
2023-12-02 06:04:57 +01:00
# }}}
# {{{ Base types
# TODO: figure out escaping and whatnot
string = string: ''"${string}"'';
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-02 12:16:33 +01:00
anything = lib.pipe (luaEncoders.fail (v: "Cannot figure out how to encode value ${builtins.toJSON v}")) [
luaEncoders.luaCodeOr
luaEncoders.nullOr
luaEncoders.boolOr
luaEncoders.numberOr
luaEncoders.stringOr
(luaEncoders.listOfOr luaEncoders.anything)
(luaEncoders.attrsetOfOr luaEncoders.anything)
];
2023-12-02 06:04:57 +01:00
# }}}
2023-12-02 12:16:33 +01:00
# {{{ Lua code
2023-12-02 06:04:57 +01:00
luaCode = tag:
2023-12-02 12:16:33 +01:00
luaEncoders.luaCodeOr
(luaEncoders.conditional lib.isPath
(path: "dofile(${luaEncoders.string path}).${tag}")
luaEncoders.identity);
luaCodeOr =
luaEncoders.conditional (e: lib.isAttrs e && (e.__luaEncoderTag or null) == "lua")
(obj: obj.value);
# }}}
# {{{ Lists
2023-12-02 06:04:57 +01:00
listOf = encoder: list:
mkRawLuaObject (lib.lists.map encoder list);
tryNonemptyList = encoder: luaEncoders.conditional
(l: l == [ ])
luaEncoders.nil
(luaEncoders.listOf encoder);
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);
oneOrMany = encoder: luaEncoders.listOfOr encoder encoder;
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-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:
mkRawLuaObject (lib.mapAttrsToList
(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-02 06:04:57 +01:00
attrset = noNils: listOrder: listSpec: spec: attrset:
let
shouldKeep = given:
if noNils then
given != "nil"
else
true;
listChunks = lib.lists.map
(attr:
let result = listSpec.${attr} (attrset.${attr} or null);
in
lib.optionalString (shouldKeep result) result
)
listOrder;
objectChunks = lib.mapAttrsToList
(attr: encoder:
let result = encoder (attrset.${attr} or null);
in
lib.optionalString (shouldKeep result)
"${attr} = ${result}"
)
spec;
in
mkRawLuaObject (listChunks ++ objectChunks);
# }}}
};
e = luaEncoders;
# }}}
# Format and write a lua file to disk
writeLuaFile = path: name: text:
let
directory = "lua/${path}";
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}
'';
in
{
options.satellite.neovim = {
lazy = lib.mkOption {
default = { };
description = "Record of persistent locations (eg: /persist)";
type = types.attrsOf myTypes.lazyModule;
};
2023-12-02 12:16:33 +01:00
generated = {
lazy = lib.mkOption {
type = types.attrsOf (types.submodule (_: {
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-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-02 06:04:57 +01:00
};
2023-12-02 12:16:33 +01:00
lib = {
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";
};
blacklistEnv = lib.mkOption {
default = given: cfg.lib.lua ''
require(${e.string cfg.env.module}).blacklist(${e.listOf e.string given})
'';
type = types.functionTo myTypes.luaLiteral;
description = "Generate a lazy.cond predicate which disables a module if one of the given envs is active";
};
};
env = {
module = lib.mkOption {
type = types.str;
example = "my.helpers.env";
description = "Module where to import env flags from";
};
2023-12-02 06:04:57 +01:00
};
styluaConfig = lib.mkOption {
type = types.path;
description = "Config to use for formatting lua modules";
};
};
config.satellite.neovim.generated.lazy =
let
lazyKeyEncoder =
e.stringOr (e.attrset true [ "mapping" "action" ]
{
mapping = e.string;
2023-12-02 12:16:33 +01:00
action = e.nullOr (e.luaCodeOr e.string);
2023-12-02 06:04:57 +01:00
}
{
2023-12-02 12:16:33 +01:00
mode = e.nullOr
(e.map
lib.strings.stringToCharacters
(e.listAsOneOrMany e.string));
2023-12-02 06:04:57 +01:00
desc = e.nullOr e.string;
ft = e.nullOr (e.oneOrMany e.string);
});
2023-12-02 12:16:33 +01:00
lazyObjectEncoder = e.bind
(opts: e.attrset true [ "package" ]
{ package = e.string; }
2023-12-02 06:04:57 +01:00
{
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;
dependencies = e.map (d: d.lua) (e.tryNonemptyList lazyObjectEncoder);
lazy = e.nullOr e.bool;
cond = e.nullOr (e.luaCode "cond");
2023-12-02 12:16:33 +01:00
config = e.const (e.nullOr (e.boolOr (e.luaCode "config")) opts.setup);
2023-12-02 06:04:57 +01:00
init = e.nullOr (e.luaCode "init");
event = e.nullOr (e.oneOrMany e.string);
ft = e.nullOr (e.oneOrMany e.string);
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
});
makeLazyScript = opts: ''
-- This file was generated by nix
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;
};
}