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

113 lines
2.8 KiB
Nix
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{
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 loading any plugins or even any sloth-flake calls.
'';
};
postInit = mkOption {
type = with types; nullOr (either path str);
default = null;
description = ''
Lua code called after every non-lazy plugins init but before loading any plugin.
'';
};
config = mkOption {
type = with types; nullOr (either path str);
default = null;
description = ''
Lua code called after all plugins are loaded and configured.
'';
};
finalContent = mkOption {
type = with types; nullOr str;
default = self.mkCustomLuaRc config;
description = ''
The resulted init package.
'';
};
};
});
option = mkOption {
default = null;
description = ''
init.lua configuration can be a string, a path or attribute set.
The attribute set form will call sloth-flake for you and load your
plugins. It allows you to hook at different moments in the loading
process of your plugins: before everything, after all plugins init code
is called and after everything is loaded.
'';
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;
})