{ pkgs, callModule, ... }: let inherit (builtins) isAttrs isPath isString readFile; inherit (pkgs.lib) fileContents fix mkOption optionalAttrs optionalString types ; lua = callModule ../lua.nix {}; 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 [] (textOrContent (lua.raw 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 { 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 ''; }; }; }; option = mkOption { default = null; description = '' init.lua configuration ''; type = with types; nullOr (oneOf [path str 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; })