From bf7fd28d79e740388f37086da98022578e4ecd06 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Sat, 20 Jun 2026 02:01:33 +0300 Subject: [PATCH] wrapper/lazy: allow conditionally calling `setup()` for plugins Signed-off-by: NotAShelf Change-Id: I644a46924f6a7fbb46e507d2599f03b06a6a6964 --- modules/wrapper/lazy/config.nix | 32 ++++++++++++++++------- modules/wrapper/lazy/lazy.nix | 46 ++++++++++++++++++++++++++------- 2 files changed, 60 insertions(+), 18 deletions(-) diff --git a/modules/wrapper/lazy/config.nix b/modules/wrapper/lazy/config.nix index 026b165c..eed8c851 100644 --- a/modules/wrapper/lazy/config.nix +++ b/modules/wrapper/lazy/config.nix @@ -29,7 +29,7 @@ then spec.package.pname else spec.package.name; in - (removeAttrs spec ["package" "setupModule" "setupOpts" "keys"]) + (removeAttrs spec ["package" "setupModule" "setupOpts" "setupCond" "keys"]) // { "@1" = if spec.package != null && packageName != name && spec.load == null @@ -73,14 +73,20 @@ after = if spec.setupModule == null && spec.after == null then null - else + else let + setupCall = "require(${toJSON spec.setupModule}).setup(${toLuaObject spec.setupOpts})"; + conditionalSetup = + if spec.setupCond != null + then '' + if (${spec.setupCond}) then + ${setupCall} + end'' + else setupCall; + in mkLuaInline '' function() ${optionalString (spec.beforeSetup != null) spec.beforeSetup} - ${ - optionalString (spec.setupModule != null) - "require(${toJSON spec.setupModule}).setup(${toLuaObject spec.setupOpts})" - } + ${optionalString (spec.setupModule != null) conditionalSetup} ${optionalString (spec.after != null) spec.after} end ''; @@ -105,11 +111,19 @@ pluginPackages = filter (x: x != null) (mapAttrsToList (_: plugin: plugin.package) cfg.plugins); - specToNotLazyConfig = _: spec: '' + specToNotLazyConfig = _: spec: let + setupCall = "require(${toJSON spec.setupModule}).setup(${toLuaObject spec.setupOpts})"; + conditionalSetup = + if spec.setupCond != null + then '' + if (${spec.setupCond}) then + ${setupCall} + end'' + else setupCall; + in '' do ${optionalString (spec.before != null) spec.before} - ${optionalString (spec.setupModule != null) - "require(${toJSON spec.setupModule}).setup(${toLuaObject spec.setupOpts})"} + ${optionalString (spec.setupModule != null) conditionalSetup} ${optionalString (spec.after != null) spec.after} end ''; diff --git a/modules/wrapper/lazy/lazy.nix b/modules/wrapper/lazy/lazy.nix index 7ff38b12..6061c9b1 100644 --- a/modules/wrapper/lazy/lazy.nix +++ b/modules/wrapper/lazy/lazy.nix @@ -16,9 +16,10 @@ default = null; description = "Action to trigger."; }; + lua = mkBool false '' - If true, `action` is considered to be lua code. - Thus, it will not be wrapped in `""`. + If true, `action` is considered to be lua code. Thus, it will not be + wrapped in `""`. ''; desc = mkOption { @@ -70,7 +71,7 @@ description = '' Plugin package. - If null, a custom load function must be provided + If `null`, a custom load function must be provided ''; }; @@ -90,29 +91,56 @@ }; setupOpts = mkOption { - type = attrsOf anything; + type = either (attrsOf anything) luaInline; default = {}; - description = "Options to pass to the setup function"; + description = '' + Options to pass to the setup function. + + Accepts either an attribute set or a raw Lua expression via + `lib.mkLuaInline`. When set to a `luaInline` value, the expression + is passed verbatim as the argument to `setup()`. + ''; + }; + + setupCond = mkOption { + type = nullOr lines; + default = null; + description = '' + A Lua expression used as the condition for calling setup. When set, + the setup call is wrapped as `if (condition) then ... end`, allowing + runtime-conditional plugin initialization. + + For example, `"not vim.g.vscode"` will only call setup when not running + inside VSCode with vscode-neovim. + ''; }; # lz.n options - enabled = mkOption { type = nullOr (either bool luaInline); default = null; - description = "When false, or if the lua function returns false, this plugin will not be included in the spec"; + description = '' + When `false`, or if the Lua function returns false, this plugin will + not be included in the spec. + ''; }; beforeAll = mkOption { type = nullOr lines; default = null; - description = "Lua code to run before any plugins are loaded. This will be wrapped in a function."; + description = '' + Lua code to run before any plugins are loaded. This will be wrapped + in a function. + ''; }; before = mkOption { type = nullOr lines; default = null; - description = "Lua code to run before plugin is loaded. This will be wrapped in a function."; + description = '' + Lua code to run before plugin is loaded. This will be wrapped in a + function. + ''; }; after = mkOption {