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,
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;
};

View File

@ -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 = [];

View File

@ -1,8 +1,9 @@
{pkgs, ...}: let
inherit (pkgs) vimUtils;
inherit (pkgs.lib) fix mkOption types;
in fix (self: {
module = types.submodule {
in
fix (self: {
module = types.submodule ({config, ...}: {
options = {
src = mkOption {
type = with types; either path attrs;
@ -23,8 +24,15 @@ in fix (self: {
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;
@ -50,4 +58,4 @@ in fix (self: {
}
)
);
})
})

View File

@ -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);
})