From 1d6bc8871949c56b967efc6c33484f60ee31d75e Mon Sep 17 00:00:00 2001 From: LeMarsu Date: Sun, 8 Mar 2026 05:36:42 +0100 Subject: [PATCH] refactor: better use of readOnly options --- lib/modules/default.nix | 27 ++++++++-- lib/modules/plugin/default.nix | 55 ++++++++++++++++++- lib/modules/runtime.nix | 98 ++++++++++++++++++---------------- lib/modules/sloth.nix | 71 ++++++------------------ 4 files changed, 147 insertions(+), 104 deletions(-) diff --git a/lib/modules/default.nix b/lib/modules/default.nix index efc25fe..29221e9 100644 --- a/lib/modules/default.nix +++ b/lib/modules/default.nix @@ -1,7 +1,6 @@ { pkgs, callModule, - sloth, ... }: let inherit (builtins) concatLists; @@ -56,8 +55,8 @@ in { plugins = concatLists [ (map modules.plugin.extract config.plugins) - (optional (! isNull config.runtime) (modules.runtime.mkPlugin config.runtime)) - [(modules.sloth.mkPlugin sloth.version config.plugins)] + (optional (! isNull config.runtime) config.runtime.package) + [config.slothPlugin] ]; neovimOptions = pkgs.neovimUtils.makeNeovimConfig { @@ -102,8 +101,29 @@ in { vimdiffAlias = mkEnableOption "creation on `vimdiff` alias"; nvimdiffAlias = mkEnableOption "creation on `nvimdiff` alias"; + pluginLuaDefinitions = mkOption { + description = '' + All lua definitions of plugins. Used by sloth vim plugin + ''; + type = with types; attrsOf anything; + readOnly = true; + internal = true; + default = modules.sloth.mkPluginLuaDefinitions config.plugins; + }; + + slothPlugin = mkOption { + description = '' + The resulted sloth plugin + ''; + type = types.package; + readOnly = true; + internal = true; + default = modules.sloth.mkSlothPlugin config.pluginLuaDefinitions; + }; + neovimOptions = mkOption { type = types.attrs; + readOnly = true; description = "The resulting configuration passed to `pkgs.wrapNeovimUnstable`"; default = neovimOptions; }; @@ -111,6 +131,7 @@ in { neovimPackage = mkOption { type = types.package; description = "The neovim package generated from your configuration."; + readOnly = true; # defaultText = lib.literalExpression "pkgs.hello"; default = neovimPackage; }; diff --git a/lib/modules/plugin/default.nix b/lib/modules/plugin/default.nix index 268ba2b..152b5b9 100644 --- a/lib/modules/plugin/default.nix +++ b/lib/modules/plugin/default.nix @@ -3,7 +3,9 @@ callModule, ... }: let - inherit (pkgs.lib) fix literalExample mkOption types; + inherit (builtins) isPath; + inherit (pkgs.lib) fileContents fix literalExample mergeAttrsList mkOption optionalAttrs types; + lua = import ../../lua.nix {}; modules = { keymap = callModule ./keymap.nix {}; @@ -23,6 +25,37 @@ Wether this plugin has ${description}. ''; }; + + textOrContent = content: + if isPath content + then fileContents content + else content; + + getPluginName = plugin: plugin.pname or plugin.name; + mkLuaDefinition = plugin: let + mkTypeFn = type: let + content = textOrContent plugin.${type}; + in + optionalAttrs (! isNull plugin.${type}) { + ${type} = with lua; lambda (raw content); + }; + name = getPluginName plugin.plugin; + in + mergeAttrsList ([ + { + inherit name; + dependencies = map getPluginName plugin.dependencies; + } + (mkTypeFn "init") + (mkTypeFn "config") + ] + ++ (with plugin; [ + (optionalAttrs lazy {lazy = true;}) + (optionalAttrs hasCommands {inherit cmd;}) + (optionalAttrs hasFileTypes {inherit ft;}) + (optionalAttrs hasEvents {inherit events;}) + (optionalAttrs hasKeymaps {inherit keymaps;}) + ])); in fix (self: { module = types.submodule ({config, ...}: { @@ -111,6 +144,26 @@ in ''; }; + pluginName = mkOption { + type = types.str; + readOnly = true; + internal = true; + default = getPluginName config.plugin; + description = '' + Name of the plugin. + ''; + }; + + luaDefinition = mkOption { + type = with types; attrsOf anything; + readOnly = true; + internal = true; + default = mkLuaDefinition config; + description = '' + Lua definition of the plugin. + ''; + }; + # priority = mkOption { # type = types.int; # default = []; diff --git a/lib/modules/runtime.nix b/lib/modules/runtime.nix index b1c34b9..bf4030b 100644 --- a/lib/modules/runtime.nix +++ b/lib/modules/runtime.nix @@ -1,53 +1,61 @@ {pkgs, ...}: let inherit (pkgs) vimUtils; inherit (pkgs.lib) fix mkOption types; -in fix (self: { - module = types.submodule { - options = { - src = mkOption { - type = with types; either path attrs; - description = "Files to include in your runtime"; - example = ./my-runtime; - }; +in + fix (self: { + module = types.submodule ({config, ...}: { + options = { + src = mkOption { + type = with types; either path attrs; + description = "Files to include in your runtime"; + example = ./my-runtime; + }; - version = mkOption { - type = with types; nullOr str; - description = "Optional version of your runtime"; - default = null; - example = "2025.10.16"; - }; + version = mkOption { + type = with types; nullOr str; + description = "Optional version of your runtime"; + default = null; + example = "2025.10.16"; + }; - extraOptions = mkOption { - type = with types; attrsOf anything; - description = "Extra options to pass to `vimUtils.buildVimPlugin`"; - default = {}; - example.nvimRequireCheck = ["my-module.my-submodule"]; + extraOptions = mkOption { + type = with types; attrsOf anything; + description = "Extra options to pass to `vimUtils.buildVimPlugin`"; + default = {}; + example.nvimRequireCheck = ["my-module.my-submodule"]; + }; + + package = mkOption { + type = types.package; + description = "The resulted runtime package"; + default = self.mkPlugin config; + example.nvimRequireCheck = ["my-module.my-submodule"]; + }; }; + }); + + option = mkOption { + type = types.nullOr self.module; + default = null; + description = '' + Your runtime submodule. You can configure what files should be + indluded in your runtime + ''; }; - }; - option = mkOption { - type = types.nullOr self.module; - default = null; - description = '' - Your runtime submodule. You can configure what files should be - indluded in your runtime - ''; - }; - - mkPlugin = opts: let - inherit (opts) src version extraOptions; - in - vimUtils.buildVimPlugin ( - extraOptions - // {inherit src;} - // ( - if isNull version - then {name = "runtime";} - else { - pname = "runtime"; - inherit version; - } - ) - ); -}) + mkPlugin = opts: let + inherit (opts) src version extraOptions; + in + vimUtils.buildVimPlugin ( + extraOptions + // {inherit src;} + // ( + if isNull version + then {name = "runtime";} + else { + pname = "runtime"; + inherit version; + } + ) + ); + }) diff --git a/lib/modules/sloth.nix b/lib/modules/sloth.nix index a91d581..138f7bf 100644 --- a/lib/modules/sloth.nix +++ b/lib/modules/sloth.nix @@ -1,64 +1,20 @@ -{pkgs, ...}: let - inherit (builtins) foldl' isPath; +{ + pkgs, + sloth, + ... +}: let inherit (pkgs) vimUtils; - inherit (pkgs.lib) fileContents fix optionalAttrs; - + inherit (pkgs.lib) fix listToAttrs nameValuePair; fs = pkgs.lib.fileset; - lua = import ../lua.nix {}; versionLua = version: with lua; nix2lua (return (lambda (return version))); - pluginsLuaDef = plugins: - with lua; nix2lua (return (foldl' pluginLuaDef {} plugins)); - - textOrContent = content: - if isPath content - then fileContents content - else content; - - pluginLuaDef = memo: plugin: let - mkTypeFn = type: let - content = textOrContent plugin.${type}; - in - optionalAttrs (! isNull plugin.${type}) { - ${type} = with lua; lambda (raw content); - }; - pluginName = plugin: - if plugin ? pname - then plugin.pname - else plugin.name; - name = pluginName plugin.plugin; - in - memo - // { - ${name} = - { - name = pluginName plugin.plugin; - dependencies = map pluginName plugin.dependencies; - } - // (mkTypeFn "init") - // (mkTypeFn "config") - // (optionalAttrs plugin.lazy { - lazy = true; - }) - // (optionalAttrs plugin.hasCommands { - inherit (plugin) cmd; - }) - // (optionalAttrs plugin.hasFileTypes { - inherit (plugin) ft; - }) - // (optionalAttrs plugin.hasEvents { - inherit (plugin) events; - }) - // (optionalAttrs plugin.hasKeymaps { - inherit (plugin) keymaps; - }); - }; + luaDefsToLua = luaDefs: with lua; nix2lua (return luaDefs); in fix (self: { - mkPlugin = version: plugins: + mkSlothPlugin = luaDefs: vimUtils.buildVimPlugin { - inherit version; + inherit (sloth) version; pname = "sloth-flake"; src = fs.toSource { root = ../..; @@ -69,12 +25,17 @@ in dir=lua/sloth-flake cat <<'LUA' > $dir/dependencies.lua - ${pluginsLuaDef plugins} + ${luaDefsToLua luaDefs} LUA cat <<'LUA' > $dir/version.lua - ${versionLua version} + ${versionLua sloth.version} LUA ''; }; + + mkPluginLuaDefinitions = plugins: let + extractDef = plugin: nameValuePair plugin.pluginName plugin.luaDefinition; + in + listToAttrs (map extractDef plugins); })