feat: add option init config object to generate `init.lua`

main
LeMarsu 2024-06-10 01:00:33 +02:00
parent b6630684fb
commit 23c9863c97
3 changed files with 73 additions and 25 deletions

View File

@ -182,10 +182,10 @@ The function accepts the following attributes:
Here's a list of all accepted arguments Here's a list of all accepted arguments
| name | default | description | | name | default | description |
|-------------------------|-------------------------|----------------------------------------------------------------| |-------------------------|-------------------------|---------------------------------------------------------------------|
| `pkgs` | N/A | The nixpkgs set. **REQUIRED** | | `pkgs` | N/A | The nixpkgs set. **REQUIRED** |
| `package` | `pkgs.neovim-unwrapped` | The unwrapped neovim package to use | | `package` | `pkgs.neovim-unwrapped` | The unwrapped neovim package to use |
| `init` | `null` | The `init.lua` of your config (string or path)¹ | | `init` | `null` | The `init.lua` of your config (string, path or init config object)¹ |
| `runtime` | `{}` | Your Runtime configuration (see below) | | `runtime` | `{}` | Your Runtime configuration (see below) |
| `dependencies` | `[]` | A list of your dependencies (see below) | | `dependencies` | `[]` | A list of your dependencies (see below) |
| `dependenciesExtraArgs` | `{}` | Extra arguments to load your dependencies in other files | | `dependenciesExtraArgs` | `{}` | Extra arguments to load your dependencies in other files |
@ -196,6 +196,14 @@ Here's a list of all accepted arguments
> ¹ If you give your own `init.lua` as string or path, you'll have to call `sloth-flake` lua plugin yourself. See more below. > ¹ If you give your own `init.lua` as string or path, you'll have to call `sloth-flake` lua plugin yourself. See more below.
The init configuration object accepts the following properties:
| name | default | description |
|------------|---------|----------------------------------------------------------------------------|
| `init` | `null` | Lua code call before plugins init. String or path. |
| `postInit` | `null` | Lua code call after plugins init and before plugin config. String or path. |
| `config` | `null` | Luas cod call after plugins config. String or path. |
The Runtime configuration object accepts the following properties: The Runtime configuration object accepts the following properties:
| name | default | description | | name | default | description |

View File

@ -14,15 +14,12 @@
nvimdiffAlias ? false, nvimdiffAlias ? false,
... ...
} @ config: let } @ config: let
inherit (builtins) map; inherit (builtins) map isString isPath;
inherit (pkgs) callPackage bash lib; inherit (pkgs) callPackage bash lib;
inherit (lib.strings) optionalString; inherit (lib.strings) optionalString;
inherit (lib.lists) optional; inherit (lib.lists) optional;
inherit (lib.trivial) flip; inherit (lib.trivial) flip;
# inherit (lib.lists) concatMap filter foldl' map optional reverseList; inherit (lib.attrsets) optionalAttrs;
# inherit (lib.attrsets) attrNames optionalAttrs;
# inherit (lib.strings) concatStringsSep fileContents hasSuffix removePrefix removeSuffix replaceStrings;
# inherit (lib.debug) traceIf traceSeq traceVal traceValSeq traceValFn;
deps = callPackage ./deps.nix {inherit dependenciesExtraArgs types;}; deps = callPackage ./deps.nix {inherit dependenciesExtraArgs types;};
@ -39,13 +36,46 @@
extractPlugin = p: {inherit (p) optional plugin;}; extractPlugin = p: {inherit (p) optional plugin;};
extractPlugins = map extractPlugin; extractPlugins = map extractPlugin;
customRC = let buildInit = {
rc = init ? null,
if isNull init postInit ? null,
then ../lua/default_init.lua config ? null,
else init; }: let
in initStr = optionalString (! isNull init) ''
deps.textOrContent rc; (function()
${deps.textOrContent init}
end)();
'';
slothCall =
if isNull postInit
then "require('sloth-flake').setup {}"
else ''
require('sloth-flake').setup {
post_init = function()
${deps.textOrContent postInit}
end,
};
'';
configStr = optionalString (! isNull config) ''
(function()
${deps.textOrContent config}
end)()
'';
in ''
-- Generated by sloth-flake
${initStr}
${slothCall}
${configStr}
'';
customRC =
if isString init || isPath init
then deps.textOrContent init
else buildInit (optionalAttrs (! isNull init) init);
neovimConfig = neovimConfig =
pkgs.neovimUtils.makeNeovimConfig { pkgs.neovimUtils.makeNeovimConfig {

View File

@ -15,6 +15,16 @@ in rec {
src = any; src = any;
}; };
neovimInitType = with yants;
struct "neovimInit" {
# Lua code to call before plugins loaded
init = option (either string path);
# Lua code called after init but before import
postInit = option (either string path);
# Lua code called after all plugins are loaded
config = option (either string path);
};
# As simple remote plugin definition # As simple remote plugin definition
basicPluginType = with yants; basicPluginType = with yants;
struct "basicPlugin" { struct "basicPlugin" {
@ -96,7 +106,7 @@ in rec {
package = option drv; package = option drv;
# init.lua configuration # init.lua configuration
init = option (either string path); init = option (eitherN [string path neovimInitType]);
# An array of dependencies. # An array of dependencies.
dependencies = option (list dependency); dependencies = option (list dependency);