sloth-flake.nvim/lib/modules/init.nix

108 lines
2.4 KiB
Nix

{
pkgs,
callModule,
...
}: let
inherit (builtins) isAttrs isPath isString readFile;
inherit (pkgs.lib) fileContents fix mkOption optionalAttrs optionalString types;
lua = callModule ../lua.nix {};
coerceToModule = value:
if isAttrs value
then value
else {finalContent = textOrContent value;};
textOrContent = content:
if isPath content
then fileContents content
else content;
buildInit = {
init ? null,
postInit ? null,
config ? null,
...
}: let
initStr = optionalString (! isNull init) ''
(function()
${textOrContent init}
end)();
'';
postInitContent = optionalAttrs (! isNull postInit) {
post_init = lua.lambda [] (lua.raw (textOrContent postInit));
};
slothCall = "require('sloth-flake').setup ${lua.renderLua {} postInitContent};";
configStr = optionalString (! isNull config) ''
(function()
${textOrContent config}
end)()
'';
in ''
-- Generated by sloth-flake
${initStr}
${slothCall}
${configStr}
'';
in
fix (self: {
module = types.submodule ({config, ...}: {
options = {
init = mkOption {
type = with types; nullOr (either path str);
default = null;
description = ''
Lua code to call before plugins loaded
'';
};
postInit = mkOption {
type = with types; nullOr (either path str);
default = null;
description = ''
Lua code called after init but before import
'';
};
config = mkOption {
type = with types; nullOr (either path str);
default = null;
description = ''
Lua code called after all plugins are loaded
'';
};
finalContent = mkOption {
type = with types; nullOr str;
default = self.mkCustomLuaRc config;
description = ''
The resulted init package.
'';
};
};
});
option = mkOption {
default = null;
description = ''
init.lua configuration
'';
type = with types; coercedTo (nullOr (oneOf [path str])) coerceToModule self.module;
example = ./init.lua;
};
mkCustomLuaRc = init:
if isString init
then init
else if isPath init
then readFile init
else if isAttrs init
then buildInit init
else null;
})