From 11c23713ae6315caa2ee0648e989ed4618db65d2 Mon Sep 17 00:00:00 2001 From: psfloyd Date: Thu, 8 Aug 2024 22:39:14 -0300 Subject: [PATCH 1/5] test/test-derivation: symlink test configurations on output directory --- tests/test-derivation.nix | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/test-derivation.nix b/tests/test-derivation.nix index cd95b576c..2b133551b 100644 --- a/tests/test-derivation.nix +++ b/tests/test-derivation.nix @@ -38,6 +38,7 @@ let buildPhase = lib.optionalString (!dontRun) ( '' mkdir -p .cache/nvim + mkdir $out '' + lib.concatStringsSep "\n" ( builtins.map ( @@ -47,6 +48,8 @@ let dontRun ? false, }: lib.optionalString (!dontRun) '' + ln -s ${derivation} $out/${name} + echo "Running test for ${name}" output=$(HOME=$(realpath .) ${lib.getExe derivation} -mn --headless "+q" 2>&1 >/dev/null) @@ -58,11 +61,6 @@ let ) testList ) ); - - # If we don't do this nix is not happy - installPhase = '' - mkdir $out - ''; }; # Create a nix derivation from a nixvim configuration. From 71939ebd107fd1d4336e058e1027afa5f7fbc4a0 Mon Sep 17 00:00:00 2001 From: psfloyd Date: Fri, 9 Aug 2024 02:07:00 -0300 Subject: [PATCH 2/5] lib/options: added mkLazyLoadOption --- lib/helpers.nix | 1 + lib/options.nix | 104 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) diff --git a/lib/helpers.nix b/lib/helpers.nix index 5e35ad673..9a7609f90 100644 --- a/lib/helpers.nix +++ b/lib/helpers.nix @@ -44,6 +44,7 @@ let defaultNullOpts mkCompositeOption mkCompositeOption' + mkLazyLoadOption mkNullOrLua mkNullOrLua' mkNullOrLuaFn diff --git a/lib/options.nix b/lib/options.nix index d187d5a25..9c70d84f0 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -366,4 +366,108 @@ rec { else example; }; + + mkLazyLoadOption = + { + originalName, + lazyLoadDefaults ? { }, + }: + let + pluginDefault = { + enable = false; + } // lazyLoadDefaults; + in + mkOption { + description = '' + Lazy-load settings for ${originalName}. + ''; + type = + with helpers.nixvimTypes; + let + triggerType = oneOf [ + rawLua + str + (listOf str) + ]; + in + submodule { + options = with defaultNullOpts; { + + enable = mkOption { + type = bool; + default = pluginDefault.enable; + description = '' + Enable lazy-loading for ${originalName} + ''; + }; + + # Spec loading: + enabled = mkStrLuaFnOr bool pluginDefault.enabledInSpec or null '' + When false, or if the function returns false, then ${originalName} will not be included in the spec. + Equivalence: lz.n => enabled; lazy.nvim => enabled + ''; + + priority = mkNullable number pluginDefault.priority or null '' + Only useful for start plugins (not lazy-loaded) to force loading certain plugins first. + Equivalence: lz.n => priority; lazy.nvim => priority + ''; + + # Spec setup + # Actions + beforeAll = mkLuaFn pluginDefault.beforeAll or null '' + Always executed before any plugins are loaded. + Equivalence: lz.n => beforeAll; lazy.nvim => init + ''; + + before = mkLuaFn pluginDefault.before or null '' + Executed before ${originalName} is loaded. + Equivalence: lz.n => before; lazy.nvim => None + ''; + + after = mkLuaFn pluginDefault.after or null '' + Executed after ${originalName} is loaded. + Equivalence: lz.n => after; lazy.nvim => config + ''; + + # Triggers + event = mkNullable triggerType pluginDefault.event or null '' + Lazy-load on event. Events can be specified as `BufEnter` or with a pattern like `BufEnter *.lua` + Equivalence: lz.n => event; lazy.nvim => event + ''; + + cmd = mkNullable triggerType pluginDefault.cmd or null '' + Lazy-load on command. + Equivalence: lz.n => cmd; lazy.nvim => cmd + ''; + + ft = mkNullable triggerType pluginDefault.ft or null '' + Lazy-load on filetype. + Equivalence: lz.n => ft; lazy.nvim => ft + ''; + + keys = mkNullable (listOf helpers.keymaps.mapOptionSubmodule) pluginDefault.keys or null '' + Lazy-load on key mapping. Use the same format as `config.keymaps`. + Equivalence: lz.n => keys; lazy.nvim => keys + ''; + + colorscheme = mkNullable triggerType pluginDefault.colorscheme or null '' + Lazy-load on colorscheme. + Equivalence: lz.n => colorscheme; lazy.nvim => None + ''; + + extraSettings = mkSettingsOption { + description = '' + Extra settings to pass to the lazy loader backend. + ''; + example = { + dependencies = { + __unkeyed-1 = "nvim-lua/plenary.nvim"; + lazy = true; + }; + }; + }; + }; + }; + default = pluginDefault; + }; } From 106cf85f2596e124cd7b4dddad5ffab41970c8a6 Mon Sep 17 00:00:00 2001 From: psfloyd Date: Tue, 13 Aug 2024 23:29:34 -0300 Subject: [PATCH 3/5] lib/options: mkLazyLoadOption fixed defaults, arguments and paragraphs --- lib/options.nix | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/lib/options.nix b/lib/options.nix index 9c70d84f0..73d0a98a2 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -372,11 +372,6 @@ rec { originalName, lazyLoadDefaults ? { }, }: - let - pluginDefault = { - enable = false; - } // lazyLoadDefaults; - in mkOption { description = '' Lazy-load settings for ${originalName}. @@ -393,65 +388,71 @@ rec { submodule { options = with defaultNullOpts; { - enable = mkOption { - type = bool; - default = pluginDefault.enable; - description = '' - Enable lazy-loading for ${originalName} - ''; - }; + enable = mkEnableOption '' + lazy-loading for ${originalName} + ''; # Spec loading: - enabled = mkStrLuaFnOr bool pluginDefault.enabledInSpec or null '' + enabled = mkStrLuaFnOr bool (lazyLoadDefaults.enabledInSpec or null) '' When false, or if the function returns false, then ${originalName} will not be included in the spec. + Equivalence: lz.n => enabled; lazy.nvim => enabled ''; - priority = mkNullable number pluginDefault.priority or null '' + priority = mkNullable number (lazyLoadDefaults.priority or null) '' Only useful for start plugins (not lazy-loaded) to force loading certain plugins first. + Equivalence: lz.n => priority; lazy.nvim => priority ''; # Spec setup # Actions - beforeAll = mkLuaFn pluginDefault.beforeAll or null '' + beforeAll = mkLuaFn (lazyLoadDefaults.beforeAll or null) '' Always executed before any plugins are loaded. + Equivalence: lz.n => beforeAll; lazy.nvim => init ''; - before = mkLuaFn pluginDefault.before or null '' + before = mkLuaFn (lazyLoadDefaults.before or null) '' Executed before ${originalName} is loaded. + Equivalence: lz.n => before; lazy.nvim => None ''; - after = mkLuaFn pluginDefault.after or null '' + after = mkLuaFn (lazyLoadDefaults.after or null) '' Executed after ${originalName} is loaded. + Equivalence: lz.n => after; lazy.nvim => config ''; # Triggers - event = mkNullable triggerType pluginDefault.event or null '' + event = mkNullable triggerType (lazyLoadDefaults.event or null) '' Lazy-load on event. Events can be specified as `BufEnter` or with a pattern like `BufEnter *.lua` + Equivalence: lz.n => event; lazy.nvim => event ''; - cmd = mkNullable triggerType pluginDefault.cmd or null '' + cmd = mkNullable triggerType (lazyLoadDefaults.cmd or null) '' Lazy-load on command. + Equivalence: lz.n => cmd; lazy.nvim => cmd ''; - ft = mkNullable triggerType pluginDefault.ft or null '' + ft = mkNullable triggerType (lazyLoadDefaults.ft or null) '' Lazy-load on filetype. + Equivalence: lz.n => ft; lazy.nvim => ft ''; - keys = mkNullable (listOf helpers.keymaps.mapOptionSubmodule) pluginDefault.keys or null '' + keys = mkNullable (listOf helpers.keymaps.mapOptionSubmodule) (lazyLoadDefaults.keys or null) '' Lazy-load on key mapping. Use the same format as `config.keymaps`. + Equivalence: lz.n => keys; lazy.nvim => keys ''; - colorscheme = mkNullable triggerType pluginDefault.colorscheme or null '' + colorscheme = mkNullable triggerType (lazyLoadDefaults.colorscheme or null) '' Lazy-load on colorscheme. + Equivalence: lz.n => colorscheme; lazy.nvim => None ''; @@ -468,6 +469,6 @@ rec { }; }; }; - default = pluginDefault; + default = lazyLoadDefaults; }; } From e325dd27aa5feaa6e048ffaaac2376f4cc00c087 Mon Sep 17 00:00:00 2001 From: psfloyd Date: Tue, 13 Aug 2024 23:31:09 -0300 Subject: [PATCH 4/5] lib/neovim-plugin: lazyLoad lz-n backend --- lib/neovim-plugin.nix | 92 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 81 insertions(+), 11 deletions(-) diff --git a/lib/neovim-plugin.nix b/lib/neovim-plugin.nix index 4095bbf4a..c6cdf40b3 100644 --- a/lib/neovim-plugin.nix +++ b/lib/neovim-plugin.nix @@ -43,9 +43,14 @@ with lib; extraPackages ? [ ], callSetup ? true, installPackage ? true, + # lazyLoad + allowLazyLoad ? true, + packageName ? originalName, # Name of the package folder created in {runtimepath}/pack/start or {runtimepath}/pack/opt + lazyLoadDefaults ? { }, }: let namespace = if isColorscheme then "colorschemes" else "plugins"; + cfg = config.${namespace}.${name}; in { meta = { @@ -94,25 +99,90 @@ with lib; example = settingsExample; }; } + // optionalAttrs allowLazyLoad { + lazyLoad = helpers.mkLazyLoadOption { + inherit originalName; + lazyLoadDefaults = + (optionalAttrs (isColorscheme && colorscheme != null) { inherit colorscheme; }) // lazyLoadDefaults; + }; + } // extraOptions; config = let - cfg = config.${namespace}.${name}; extraConfigNamespace = if isColorscheme then "extraConfigLuaPre" else "extraConfigLua"; + lazyLoaded = cfg.lazyLoad.enable or false; + + # lz-n lazyLoad backend + # Transform plugin into attrset and set optional to true + # See `tests/test-sources/plugins/pluginmanagers/lz-n.nix` + optionalPlugin = + x: + if isColorscheme then x else ((if x ? plugin then x else { plugin = x; }) // { optional = true; }); + mkFn = str: if str != "" then "function()\n" + str + "end" else null; 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)}) - ''; - }) + # Always set (optionalAttrs (isColorscheme && (colorscheme != null)) { colorscheme = mkDefault colorscheme; }) - (extraConfig cfg) + + # Normal loading + (mkIf (!lazyLoaded) (mkMerge [ + (extraConfig cfg) + { + extraPlugins = (optional installPackage cfg.package) ++ extraPlugins; + inherit extraPackages; + } + (optionalAttrs callSetup { + ${extraConfigNamespace} = '' + require('${luaName}')${setup}(${optionalString (cfg ? settings) (helpers.toLuaObject cfg.settings)}) + ''; + }) + ])) + + # Lazy loading with lz-n + (mkIf (lazyLoaded && config.plugins.lz-n.enable) (mkMerge [ + (lib.removeAttrs (extraConfig cfg) [ + "extraConfigLua" + "extraConfigLuaPre" + ]) + { + extraPlugins = (optional installPackage (optionalPlugin cfg.package)) ++ extraPlugins; + inherit extraPackages; + } + { + plugins.lz-n.plugins = [ + { + __unkeyed-1 = packageName; + after = + if cfg.lazyLoad.after == null then + mkFn ( + (extraConfig cfg).extraConfigLuaPre or "" + + optionalString callSetup ''require('${luaName}')${setup}(${ + optionalString (cfg ? settings) (helpers.toLuaObject cfg.settings) + }) '' + + (extraConfig cfg).extraConfigLua or "" + ) + else + cfg.lazyLoad.after; + inherit (cfg.lazyLoad) + enabled + priority + before + beforeAll + # after defined above + event + cmd + ft + keys + colorscheme + extraSettings + ; + } + ]; + } + ])) + + # Lazy loading with lazy.nvim ]); }; } From 8cebb3f332ab4b0be1675d28186a825e1683b249 Mon Sep 17 00:00:00 2001 From: psfloyd Date: Wed, 14 Aug 2024 01:03:28 -0300 Subject: [PATCH 5/5] tests/lazy-load: added test --- tests/test-sources/modules/lazy-load.nix | 39 ++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/test-sources/modules/lazy-load.nix diff --git a/tests/test-sources/modules/lazy-load.nix b/tests/test-sources/modules/lazy-load.nix new file mode 100644 index 000000000..15e19af34 --- /dev/null +++ b/tests/test-sources/modules/lazy-load.nix @@ -0,0 +1,39 @@ +{ + example = { + plugins.lz-n.enable = true; + plugins = { + cmp = { + enable = true; + settings = { + sources = [ { name = "snippets"; } ]; + mapping.__raw = "require('cmp').mapping.preset.insert()"; + }; + }; + + nvim-snippets = { + enable = true; + lazyLoad = { + enable = true; + event = "InsertEnter"; + }; + }; + + telescope = { + enable = true; + lazyLoad = { + enable = true; + cmd = "Telescope"; + }; + }; + }; + + colorschemes.ayu = { + enable = true; + settings.mirage = true; + lazyLoad = { + enable = true; + event = "InsertEnter"; + }; + }; + }; +}