diff --git a/lib/neovim-plugin.nix b/lib/neovim-plugin.nix index 7edc362d21..1d45689626 100644 --- a/lib/neovim-plugin.nix +++ b/lib/neovim-plugin.nix @@ -36,6 +36,9 @@ with lib; hasSettings ? true, extraOptions ? { }, # config + configLocation ? [ (if isColorscheme then "extraConfigLuaPre" else "extraConfigLua") ], + # Required for plugins.flash, as it used to define config + skipConfigAttrs ? false, luaName ? name, setup ? ".setup", extraConfig ? cfg: { }, @@ -79,6 +82,13 @@ with lib; package = helpers.mkPluginPackageOption originalName defaultPackage; } + // optionalAttrs (!skipConfigAttrs) { + config = lib.mkOption { + type = nixvim.nixvimTypes.pluginConfig; + default = { }; + description = "The plugin's lua configuration"; + }; + } // optionalAttrs hasSettings { settings = helpers.mkSettingsOption { description = settingsDescription; @@ -91,18 +101,27 @@ with lib; config = let cfg = config.${namespace}.${name}; - extraConfigNamespace = if isColorscheme then "extraConfigLuaPre" else "extraConfigLua"; in mkIf cfg.enable (mkMerge [ { extraPlugins = (optional installPackage cfg.package) ++ extraPlugins; inherit extraPackages; } - (optionalAttrs callSetup { - ${extraConfigNamespace} = '' - require('${luaName}')${setup}(${optionalString (cfg ? settings) (helpers.toLuaObject cfg.settings)}) - ''; - }) + ( + let + setupCode = '' + require('${luaName}')${setup}(${optionalString (cfg ? settings) (helpers.toLuaObject cfg.settings)}) + ''; + in + if !skipConfigAttrs then + (optionalAttrs callSetup { ${namespace}.${name}.config.init = setupCode; }) + // (optionalAttrs (configLocation != null) (setAttrByPath (toList configLocation) cfg.config.final)) + else + assert assertMsg ( + callSetup -> configLocation != null + ) "When a plugin has no config attrs and has a setup function it must have a config location"; + optionalAttrs callSetup (setAttrByPath (toList configLocation) setupCode) + ) (optionalAttrs (isColorscheme && (colorscheme != null)) { colorscheme = mkDefault colorscheme; }) (extraConfig cfg) ]); diff --git a/lib/types.nix b/lib/types.nix index 489011ee43..9dd4859544 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -90,6 +90,41 @@ rec { optionDescriptionPhrase (class: class == "noun" || class == "composite") elemType }"; }; + + pluginConfig = types.submodule ( + { config, ... }: + { + options = { + pre = mkOption { + type = types.lines; + default = ""; + description = "Configuration before the initialization of the plugin"; + }; + post = mkOption { + type = types.lines; + default = ""; + description = "Configuration after the initialization of the plugin"; + }; + init = mkOption { + type = types.lines; + internal = true; + default = ""; + description = "Initialization code (between pre & post)"; + }; + final = mkOption { + type = types.str; + description = "Complete plugin configuration"; + readOnly = true; + }; + }; + + config.final = nixvim.concatNonEmptyLines [ + config.pre + config.init + config.post + ]; + } + ); } # Allow to do `with nixvimTypes;` instead of `with types;` // lib.types diff --git a/plugins/colorschemes/ayu.nix b/plugins/colorschemes/ayu.nix index 3f46d23f1b..0952d6ddf1 100644 --- a/plugins/colorschemes/ayu.nix +++ b/plugins/colorschemes/ayu.nix @@ -40,7 +40,7 @@ helpers.neovim-plugin.mkNeovimPlugin config { }; extraConfig = cfg: { - extraConfigLuaPre = '' + colorschemes.ayu.config.init = '' local ayu = require("ayu") ayu.setup(${helpers.toLuaObject cfg.settings}) ayu.colorscheme() diff --git a/plugins/colorschemes/base16/default.nix b/plugins/colorschemes/base16/default.nix index 9461aff81a..24e0c74f47 100644 --- a/plugins/colorschemes/base16/default.nix +++ b/plugins/colorschemes/base16/default.nix @@ -179,7 +179,7 @@ helpers.neovim-plugin.mkNeovimPlugin config { # `settings` can either be passed to `with_config` before calling `setup`, # or it can be passed as `setup`'s 2nd argument. # See https://github.com/RRethy/base16-nvim/blob/6ac181b5733518040a33017dde654059cd771b7c/lua/base16-colorscheme.lua#L107-L125 - extraConfigLuaPre = '' + colorschemes.base16.config.init = '' do local base16 = require('${luaName}') base16.with_config(${helpers.toLuaObject cfg.settings}) diff --git a/plugins/colorschemes/onedark.nix b/plugins/colorschemes/onedark.nix index a1ce76a3e2..9130a70006 100644 --- a/plugins/colorschemes/onedark.nix +++ b/plugins/colorschemes/onedark.nix @@ -37,10 +37,11 @@ helpers.neovim-plugin.mkNeovimPlugin config { callSetup = false; colorscheme = null; extraConfig = cfg: { - extraConfigLuaPre = '' + colorschemes.onedark.config.init = '' _onedark = require('onedark') _onedark.setup(${helpers.toLuaObject cfg.settings}) _onedark.load() ''; + extraConfigLuaPre = cfg.config.final; }; } diff --git a/plugins/colorschemes/vscode.nix b/plugins/colorschemes/vscode.nix index 931b6e1634..45b1bc65ac 100644 --- a/plugins/colorschemes/vscode.nix +++ b/plugins/colorschemes/vscode.nix @@ -34,7 +34,7 @@ helpers.neovim-plugin.mkNeovimPlugin config { }; extraConfig = cfg: { - extraConfigLuaPre = '' + colorschemes.vscode.config.init = '' local _vscode = require("vscode") _vscode.setup(${helpers.toLuaObject cfg.settings}) _vscode.load() diff --git a/plugins/completion/cmp/default.nix b/plugins/completion/cmp/default.nix index 45aa74e142..4033900075 100644 --- a/plugins/completion/cmp/default.nix +++ b/plugins/completion/cmp/default.nix @@ -32,7 +32,7 @@ helpers.neovim-plugin.mkNeovimPlugin config { callSetup = false; extraConfig = cfg: { - extraConfigLua = + plugins.cmp.config.init = '' local cmp = require('cmp') cmp.setup(${helpers.toLuaObject cfg.settings}) diff --git a/plugins/completion/coq.nix b/plugins/completion/coq.nix index 89f31ccc41..f3d2dc54f8 100644 --- a/plugins/completion/coq.nix +++ b/plugins/completion/coq.nix @@ -80,7 +80,7 @@ helpers.neovim-plugin.mkNeovimPlugin config { coq_settings = cfg.settings; }; - extraConfigLua = "require('coq')"; + plugins.coq-nvim.config.init = "require('coq')"; plugins.lsp = { preConfig = '' diff --git a/plugins/languages/rust/rustaceanvim/default.nix b/plugins/languages/rust/rustaceanvim/default.nix index 78fd23431f..c30b298af9 100644 --- a/plugins/languages/rust/rustaceanvim/default.nix +++ b/plugins/languages/rust/rustaceanvim/default.nix @@ -50,13 +50,9 @@ helpers.neovim-plugin.mkNeovimPlugin config { }; callSetup = false; + configLocation = null; extraConfig = cfg: - let - configStr = '' - vim.g.rustaceanvim = ${helpers.toLuaObject cfg.settings} - ''; - in mkMerge [ { extraPackages = [ cfg.rustAnalyzerPackage ]; @@ -74,6 +70,9 @@ helpers.neovim-plugin.mkNeovimPlugin config { Note that if you supplied an attrset and not a function you need to set this attr set in: `plugins.rustaceanvim.settings.server.default_settings.rust-analyzer'. ''; + plugins.rustaceanvim.config.init = '' + vim.g.rustaceanvim = ${helpers.toLuaObject cfg.settings} + ''; } # If nvim-lspconfig is enabled: (mkIf config.plugins.lsp.enable { @@ -82,9 +81,9 @@ helpers.neovim-plugin.mkNeovimPlugin config { # Ensure the plugin config is placed **after** the rest of the LSP configuration # (and thus after the declaration of `__lspOnAttach`) - plugins.lsp.postConfig = configStr; + plugins.lsp.postConfig = cfg.config.final; }) # Else, just put the plugin config anywhere - (mkIf (!config.plugins.lsp.enable) { extraConfigLua = configStr; }) + (mkIf (!config.plugins.lsp.enable) { extraConfigLua = cfg.config.final; }) ]; } diff --git a/plugins/languages/treesitter/treesitter.nix b/plugins/languages/treesitter/treesitter.nix index 73dad3cb78..6d3907e5f0 100644 --- a/plugins/languages/treesitter/treesitter.nix +++ b/plugins/languages/treesitter/treesitter.nix @@ -296,7 +296,7 @@ helpers.neovim-plugin.mkNeovimPlugin config { installPackage = false; extraConfig = cfg: { - extraConfigLua = + plugins.treesitter.config.init = # NOTE: Upstream state that the parser MUST be at the beginning of runtimepath. # Otherwise the parsers from Neovim takes precedent, which may be incompatible with some queries. (optionalString (cfg.settings.parser_install_dir != null) '' diff --git a/plugins/none-ls/default.nix b/plugins/none-ls/default.nix index 7b63befe2b..3c2bee1d36 100644 --- a/plugins/none-ls/default.nix +++ b/plugins/none-ls/default.nix @@ -148,7 +148,7 @@ helpers.neovim-plugin.mkNeovimPlugin config { ]; # We only do this here because of enableLspFormat - extraConfigLua = '' + plugins.none-ls.config.init = '' require("null-ls").setup(${helpers.toLuaObject setupOptions}) ''; }; diff --git a/plugins/pluginmanagers/lz-n.nix b/plugins/pluginmanagers/lz-n.nix index 3645f3396a..c05110a69c 100644 --- a/plugins/pluginmanagers/lz-n.nix +++ b/plugins/pluginmanagers/lz-n.nix @@ -193,7 +193,7 @@ nixvim.neovim-plugin.mkNeovimPlugin config { extraConfig = cfg: { globals.lz_n = modules.mkAliasAndWrapDefsWithPriority id options.plugins.lz-n.settings; - extraConfigLua = mkIf (cfg.plugins != [ ]) '' + plugins.lz-n.config.init = mkIf (cfg.plugins != [ ]) '' require('lz.n').load( ${nixvim.toLuaObject cfg.plugins}) ''; }; diff --git a/plugins/telescope/default.nix b/plugins/telescope/default.nix index 706a9320b0..94bfc064b7 100644 --- a/plugins/telescope/default.nix +++ b/plugins/telescope/default.nix @@ -104,7 +104,7 @@ helpers.neovim-plugin.mkNeovimPlugin config { } ) cfg.keymaps; - extraConfigLua = '' + plugins.telescope.config.init = '' require('telescope').setup(${helpers.toLuaObject cfg.settings}) local __telescopeExtensions = ${helpers.toLuaObject cfg.enabledExtensions} diff --git a/plugins/utils/ccc.nix b/plugins/utils/ccc.nix index a91cf07b2b..629d3583b4 100644 --- a/plugins/utils/ccc.nix +++ b/plugins/utils/ccc.nix @@ -248,7 +248,7 @@ helpers.neovim-plugin.mkNeovimPlugin config { # ccc requires `termguicolors` to be enabled. opts.termguicolors = lib.mkDefault true; - extraConfigLua = '' + plugins.ccc.config.init = '' ccc = require('ccc') ccc.setup(${helpers.toLuaObject cfg.settings}) ''; diff --git a/plugins/utils/flash.nix b/plugins/utils/flash.nix index da5c041fca..eda355ce8e 100644 --- a/plugins/utils/flash.nix +++ b/plugins/utils/flash.nix @@ -11,6 +11,7 @@ helpers.neovim-plugin.mkNeovimPlugin config { originalName = "flash.nvim"; defaultPackage = pkgs.vimPlugins.flash-nvim; + skipConfigAttrs = true; maintainers = with maintainers; [ traxys MattSturgeon diff --git a/plugins/utils/hydra/default.nix b/plugins/utils/hydra/default.nix index 338fe8b0b4..1fc8c3c693 100644 --- a/plugins/utils/hydra/default.nix +++ b/plugins/utils/hydra/default.nix @@ -38,7 +38,7 @@ helpers.neovim-plugin.mkNeovimPlugin config { callSetup = false; extraConfig = cfg: { - extraConfigLua = '' + plugins.hydra.config.init = '' hydra = require('hydra') hydra.setup(${helpers.toLuaObject cfg.settings}) diff --git a/plugins/utils/which-key.nix b/plugins/utils/which-key.nix index 69613d5e4e..7e3bc7e548 100644 --- a/plugins/utils/which-key.nix +++ b/plugins/utils/which-key.nix @@ -595,7 +595,7 @@ lib.nixvim.neovim-plugin.mkNeovimPlugin config { '' ]; - extraConfigLua = lib.optionalString opt.registrations.isDefined '' + plugins.which-key.config.init = lib.optionalString opt.registrations.isDefined '' require("which-key").register(${toLuaObject cfg.registrations}) ''; }; diff --git a/plugins/utils/yanky.nix b/plugins/utils/yanky.nix index 637d7030a3..aa731ee005 100644 --- a/plugins/utils/yanky.nix +++ b/plugins/utils/yanky.nix @@ -356,7 +356,7 @@ helpers.neovim-plugin.mkNeovimPlugin config { } ]; - extraConfigLua = '' + plugins.yanky.config.init = '' do local utils = require('yanky.utils') ${optionalString config.plugins.telescope.enable "local mapping = require('yanky.telescope.mapping')"}