refactor: better use of readOnly options

dev
LeMarsu 2026-03-08 05:36:42 +01:00
parent 44bfef1f1c
commit 1d6bc88719
4 changed files with 147 additions and 104 deletions

View File

@ -1,7 +1,6 @@
{ {
pkgs, pkgs,
callModule, callModule,
sloth,
... ...
}: let }: let
inherit (builtins) concatLists; inherit (builtins) concatLists;
@ -56,8 +55,8 @@ in {
plugins = concatLists [ plugins = concatLists [
(map modules.plugin.extract config.plugins) (map modules.plugin.extract config.plugins)
(optional (! isNull config.runtime) (modules.runtime.mkPlugin config.runtime)) (optional (! isNull config.runtime) config.runtime.package)
[(modules.sloth.mkPlugin sloth.version config.plugins)] [config.slothPlugin]
]; ];
neovimOptions = pkgs.neovimUtils.makeNeovimConfig { neovimOptions = pkgs.neovimUtils.makeNeovimConfig {
@ -102,8 +101,29 @@ in {
vimdiffAlias = mkEnableOption "creation on `vimdiff` alias"; vimdiffAlias = mkEnableOption "creation on `vimdiff` alias";
nvimdiffAlias = mkEnableOption "creation on `nvimdiff` 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 { neovimOptions = mkOption {
type = types.attrs; type = types.attrs;
readOnly = true;
description = "The resulting configuration passed to `pkgs.wrapNeovimUnstable`"; description = "The resulting configuration passed to `pkgs.wrapNeovimUnstable`";
default = neovimOptions; default = neovimOptions;
}; };
@ -111,6 +131,7 @@ in {
neovimPackage = mkOption { neovimPackage = mkOption {
type = types.package; type = types.package;
description = "The neovim package generated from your configuration."; description = "The neovim package generated from your configuration.";
readOnly = true;
# defaultText = lib.literalExpression "pkgs.hello"; # defaultText = lib.literalExpression "pkgs.hello";
default = neovimPackage; default = neovimPackage;
}; };

View File

@ -3,7 +3,9 @@
callModule, callModule,
... ...
}: let }: 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 = { modules = {
keymap = callModule ./keymap.nix {}; keymap = callModule ./keymap.nix {};
@ -23,6 +25,37 @@
Wether this plugin has ${description}. 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 in
fix (self: { fix (self: {
module = types.submodule ({config, ...}: { 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 { # priority = mkOption {
# type = types.int; # type = types.int;
# default = []; # default = [];

View File

@ -1,53 +1,61 @@
{pkgs, ...}: let {pkgs, ...}: let
inherit (pkgs) vimUtils; inherit (pkgs) vimUtils;
inherit (pkgs.lib) fix mkOption types; inherit (pkgs.lib) fix mkOption types;
in fix (self: { in
module = types.submodule { fix (self: {
options = { module = types.submodule ({config, ...}: {
src = mkOption { options = {
type = with types; either path attrs; src = mkOption {
description = "Files to include in your runtime"; type = with types; either path attrs;
example = ./my-runtime; description = "Files to include in your runtime";
}; example = ./my-runtime;
};
version = mkOption { version = mkOption {
type = with types; nullOr str; type = with types; nullOr str;
description = "Optional version of your runtime"; description = "Optional version of your runtime";
default = null; default = null;
example = "2025.10.16"; example = "2025.10.16";
}; };
extraOptions = mkOption { extraOptions = mkOption {
type = with types; attrsOf anything; type = with types; attrsOf anything;
description = "Extra options to pass to `vimUtils.buildVimPlugin`"; description = "Extra options to pass to `vimUtils.buildVimPlugin`";
default = {}; default = {};
example.nvimRequireCheck = ["my-module.my-submodule"]; 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 { mkPlugin = opts: let
type = types.nullOr self.module; inherit (opts) src version extraOptions;
default = null; in
description = '' vimUtils.buildVimPlugin (
Your runtime submodule. You can configure what files should be extraOptions
indluded in your runtime // {inherit src;}
''; // (
}; if isNull version
then {name = "runtime";}
mkPlugin = opts: let else {
inherit (opts) src version extraOptions; pname = "runtime";
in inherit version;
vimUtils.buildVimPlugin ( }
extraOptions )
// {inherit src;} );
// ( })
if isNull version
then {name = "runtime";}
else {
pname = "runtime";
inherit version;
}
)
);
})

View File

@ -1,64 +1,20 @@
{pkgs, ...}: let {
inherit (builtins) foldl' isPath; pkgs,
sloth,
...
}: let
inherit (pkgs) vimUtils; inherit (pkgs) vimUtils;
inherit (pkgs.lib) fileContents fix optionalAttrs; inherit (pkgs.lib) fix listToAttrs nameValuePair;
fs = pkgs.lib.fileset; fs = pkgs.lib.fileset;
lua = import ../lua.nix {}; lua = import ../lua.nix {};
versionLua = version: with lua; nix2lua (return (lambda (return version))); versionLua = version: with lua; nix2lua (return (lambda (return version)));
pluginsLuaDef = plugins: luaDefsToLua = luaDefs: with lua; nix2lua (return luaDefs);
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;
});
};
in in
fix (self: { fix (self: {
mkPlugin = version: plugins: mkSlothPlugin = luaDefs:
vimUtils.buildVimPlugin { vimUtils.buildVimPlugin {
inherit version; inherit (sloth) version;
pname = "sloth-flake"; pname = "sloth-flake";
src = fs.toSource { src = fs.toSource {
root = ../..; root = ../..;
@ -69,12 +25,17 @@ in
dir=lua/sloth-flake dir=lua/sloth-flake
cat <<'LUA' > $dir/dependencies.lua cat <<'LUA' > $dir/dependencies.lua
${pluginsLuaDef plugins} ${luaDefsToLua luaDefs}
LUA LUA
cat <<'LUA' > $dir/version.lua cat <<'LUA' > $dir/version.lua
${versionLua version} ${versionLua sloth.version}
LUA LUA
''; '';
}; };
mkPluginLuaDefinitions = plugins: let
extractDef = plugin: nameValuePair plugin.pluginName plugin.luaDefinition;
in
listToAttrs (map extractDef plugins);
}) })