From 23c9863c97c9b5ed223f0f020faeb95cc8d4ff91 Mon Sep 17 00:00:00 2001 From: LeMarsu Date: Mon, 10 Jun 2024 01:00:33 +0200 Subject: [PATCH] feat: add option init config object to generate `init.lua` --- README.md | 32 +++++++++++++++++---------- lib/mkNeovimPkg.nix | 54 +++++++++++++++++++++++++++++++++++---------- lib/types.nix | 12 +++++++++- 3 files changed, 73 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 0f3ee96..efbd008 100644 --- a/README.md +++ b/README.md @@ -181,21 +181,29 @@ The function accepts the following attributes: Here's a list of all accepted arguments -| name | default | description | -|-------------------------|-------------------------|----------------------------------------------------------------| -| `pkgs` | N/A | The nixpkgs set. **REQUIRED** | -| `package` | `pkgs.neovim-unwrapped` | The unwrapped neovim package to use | -| `init` | `null` | The `init.lua` of your config (string or path)¹ | -| `runtime` | `{}` | Your Runtime configuration (see below) | -| `dependencies` | `[]` | A list of your dependencies (see below) | -| `dependenciesExtraArgs` | `{}` | Extra arguments to load your dependencies in other files | -| `viAlias` | `false` | Wether to create a `vi` alias to run neovim | -| `vimAlias` | `false` | Wether to create a `vim` alias to run neovim | -| `vimdiffAlias` | `false` | Wether to create a `vimdiff` alias to run neovim in diff mode | -| `nvimdiffAlias` | `false` | Wether to create a `nvimdiff` alias to run neovim in diff mode | +| name | default | description | +|-------------------------|-------------------------|---------------------------------------------------------------------| +| `pkgs` | N/A | The nixpkgs set. **REQUIRED** | +| `package` | `pkgs.neovim-unwrapped` | The unwrapped neovim package to use | +| `init` | `null` | The `init.lua` of your config (string, path or init config object)¹ | +| `runtime` | `{}` | Your Runtime configuration (see below) | +| `dependencies` | `[]` | A list of your dependencies (see below) | +| `dependenciesExtraArgs` | `{}` | Extra arguments to load your dependencies in other files | +| `viAlias` | `false` | Wether to create a `vi` alias to run neovim | +| `vimAlias` | `false` | Wether to create a `vim` alias to run neovim | +| `vimdiffAlias` | `false` | Wether to create a `vimdiff` alias to run neovim in diff mode | +| `nvimdiffAlias` | `false` | Wether to create a `nvimdiff` alias to run neovim in diff mode | > ¹ 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: | name | default | description | diff --git a/lib/mkNeovimPkg.nix b/lib/mkNeovimPkg.nix index fecac72..ac1a3c3 100644 --- a/lib/mkNeovimPkg.nix +++ b/lib/mkNeovimPkg.nix @@ -14,15 +14,12 @@ nvimdiffAlias ? false, ... } @ config: let - inherit (builtins) map; + inherit (builtins) map isString isPath; inherit (pkgs) callPackage bash lib; inherit (lib.strings) optionalString; inherit (lib.lists) optional; inherit (lib.trivial) flip; - # inherit (lib.lists) concatMap filter foldl' map optional reverseList; - # inherit (lib.attrsets) attrNames optionalAttrs; - # inherit (lib.strings) concatStringsSep fileContents hasSuffix removePrefix removeSuffix replaceStrings; - # inherit (lib.debug) traceIf traceSeq traceVal traceValSeq traceValFn; + inherit (lib.attrsets) optionalAttrs; deps = callPackage ./deps.nix {inherit dependenciesExtraArgs types;}; @@ -39,13 +36,46 @@ extractPlugin = p: {inherit (p) optional plugin;}; extractPlugins = map extractPlugin; - customRC = let - rc = - if isNull init - then ../lua/default_init.lua - else init; - in - deps.textOrContent rc; + buildInit = { + init ? null, + postInit ? null, + config ? null, + }: let + initStr = optionalString (! isNull init) '' + (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 = pkgs.neovimUtils.makeNeovimConfig { diff --git a/lib/types.nix b/lib/types.nix index df0becb..98a37c6 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -15,6 +15,16 @@ in rec { 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 basicPluginType = with yants; struct "basicPlugin" { @@ -96,7 +106,7 @@ in rec { package = option drv; # init.lua configuration - init = option (either string path); + init = option (eitherN [string path neovimInitType]); # An array of dependencies. dependencies = option (list dependency);