chore: reorganize lib folder hierarchy

dev
LeMarsu 2026-03-08 03:56:53 +01:00
parent bf329e5f42
commit 44bfef1f1c
10 changed files with 179 additions and 157 deletions

25
lib/evalModules.nix Normal file
View File

@ -0,0 +1,25 @@
{
pkgs,
sloth,
...
}: let
inherit (pkgs.lib) evalModules;
sLib = sloth.lib;
in {
evalSlothModules = {
modules ? [],
specialArgs ? {},
}: let
moduleConfig = evalModules {
specialArgs = specialArgs // {inherit pkgs;};
modules = modules ++ [sLib.defaultModule];
};
in
moduleConfig.config;
mkNeovimPkg = {
modules ? [],
specialArgs ? {},
}:
(sLib.evalSlothModules {inherit modules specialArgs;}).neovimPackage;
}

View File

@ -1,140 +1,16 @@
{version, ...}: {pkgs, ...}: let {version, ...}: {pkgs, ...}:
inherit (builtins) concatLists; pkgs.lib.fix (lib: let
inherit (pkgs) bash lib; inherit (pkgs.lib) callPackageWith mergeAttrsList;
inherit
(lib)
callPackageWith
concatMap
evalModules
fix
flip
literalExample
mkEnableOption
mkOption
mkPackageOption
optional
optionalString
types
;
callModule = callPackageWith {inherit pkgs callModule;}; callModule = callPackageWith {
inherit pkgs callModule;
modules = { sloth = {inherit lib version;};
init = callModule ./init.nix {};
plugin = callModule ./plugin {};
runtime = callModule ./runtime.nix {};
sloth = callModule ./sloth.nix {};
}; };
extraLuaPackagesType = callModules = modules: mergeAttrsList (map (path: callModule path {}) modules);
(with types; functionTo (listOf package))
// {
merge = loc: defs: let
fnList = map (def: def.value) defs;
concatPackages = ps: fn: fn ps;
in in
ps: concatMap (concatPackages ps) fnList; callModules [
}; ./evalModules.nix
./mkPluginsFromInputs.nix
mkDiffAlias = name: ./modules
(flip optionalString) '' ])
cat <<SH > $out/bin/${name}
#!${bash}/bin/bash
exec $out/bin/nvim -d "\''${@}"
SH
chmod 555 $out/bin/${name}
'';
defaultModule = {
config,
lib,
...
}: let
pkg = pkgs.wrapNeovimUnstable config.package config.neovimOptions;
customLuaRC = modules.init.mkCustomLuaRc config.init;
plugins = concatLists [
(map modules.plugin.extract config.plugins)
(optional (! isNull config.runtime) (modules.runtime.mkPlugin config.runtime))
[(modules.sloth.mkPlugin version config.plugins)]
];
neovimOptions = pkgs.neovimUtils.makeNeovimConfig {
inherit (config) extraLuaPackages viAlias vimAlias;
inherit customLuaRC plugins;
wrapRc = customLuaRC != null;
# inherit customRC;
};
neovimPackage = pkg.overrideAttrs (final: super: {
postBuild =
super.postBuild
+ (mkDiffAlias "vimdiff" config.vimdiffAlias)
+ (mkDiffAlias "nvimdiff" config.nvimdiffAlias);
});
in {
options = {
package = mkPackageOption pkgs "neovim-unwrapped" {};
# Will probably be not needed
# dependenciesExtraArgs = mkOption {
# default = {};
# };
extraLuaPackages = mkOption {
description = ''
function to define extra lua packages that should be included in
neovim environment.
'';
type = extraLuaPackagesType;
example = literalExample "p: [p.nvim-nio]";
defaultText = literalExample "_: []";
default = _: [];
};
init = modules.init.option;
runtime = modules.runtime.option;
plugins = modules.plugin.option;
viAlias = mkEnableOption "creation on `vi` alias";
vimAlias = mkEnableOption "creation on `vim` alias";
vimdiffAlias = mkEnableOption "creation on `vimdiff` alias";
nvimdiffAlias = mkEnableOption "creation on `nvimdiff` alias";
neovimOptions = mkOption {
type = types.attrs;
description = "The resulting configuration passed to `pkgs.wrapNeovimUnstable`";
default = neovimOptions;
};
neovimPackage = mkOption {
type = types.package;
description = "The neovim package generated from your configuration.";
# defaultText = lib.literalExpression "pkgs.hello";
default = neovimPackage;
};
};
};
in
fix (sLib: {
inherit defaultModule;
mkPluginsFromInputs = import ./mkPluginsFromInputs.nix {inherit pkgs;};
evalSlothModules = {
modules ? [],
specialArgs ? {},
}: let
moduleConfig = evalModules {
specialArgs = specialArgs // {inherit pkgs;};
modules = modules ++ [sLib.defaultModule];
};
in
moduleConfig.config;
mkNeovimPkg = {
modules ? [],
specialArgs ? {},
}:
(sLib.evalSlothModules {inherit modules specialArgs;}).neovimPackage;
})

View File

@ -1,11 +1,12 @@
{pkgs, ...}: { {pkgs, ...}: let
inherit (builtins) attrNames filter foldl' mapAttrs;
in {
mkPluginsFromInputs = {
inputs, inputs,
predicate ? pkgs.lib.strings.hasPrefix "plugin-", predicate ? pkgs.lib.strings.hasPrefix "plugin-",
nameMap ? builtins.substring 7 (-1), nameMap ? builtins.substring 7 (-1),
buildVimPlugin ? pkgs.vimUtils.buildVimPlugin, buildVimPlugin ? pkgs.vimUtils.buildVimPlugin,
}: let }: let
inherit (builtins) attrNames filter foldl' mapAttrs;
names = filter predicate (attrNames inputs); names = filter predicate (attrNames inputs);
mkPlugin = m: k: let mkPlugin = m: k: let
name = nameMap k; name = nameMap k;
@ -17,4 +18,5 @@
m // {${name} = pluginDef;}; m // {${name} = pluginDef;};
plugins = foldl' mkPlugin {} names; plugins = foldl' mkPlugin {} names;
in in
mapAttrs (_: buildVimPlugin) plugins mapAttrs (_: buildVimPlugin) plugins;
}

119
lib/modules/default.nix Normal file
View File

@ -0,0 +1,119 @@
{
pkgs,
callModule,
sloth,
...
}: let
inherit (builtins) concatLists;
inherit (pkgs) bash lib;
inherit
(lib)
concatMap
flip
literalExample
mkEnableOption
mkOption
mkPackageOption
optional
optionalString
types
;
modules = {
init = callModule ./init.nix {};
plugin = callModule ./plugin {};
runtime = callModule ./runtime.nix {};
sloth = callModule ./sloth.nix {};
};
extraLuaPackagesType =
(with types; functionTo (listOf package))
// {
merge = loc: defs: let
fnList = map (def: def.value) defs;
concatPackages = ps: fn: fn ps;
in
ps: concatMap (concatPackages ps) fnList;
};
mkDiffAlias = name:
(flip optionalString) ''
cat <<SH > $out/bin/${name}
#!${bash}/bin/bash
exec $out/bin/nvim -d "\''${@}"
SH
chmod 555 $out/bin/${name}
'';
in {
defaultModule = {
config,
lib,
...
}: let
pkg = pkgs.wrapNeovimUnstable config.package config.neovimOptions;
customLuaRC = modules.init.mkCustomLuaRc config.init;
plugins = concatLists [
(map modules.plugin.extract config.plugins)
(optional (! isNull config.runtime) (modules.runtime.mkPlugin config.runtime))
[(modules.sloth.mkPlugin sloth.version config.plugins)]
];
neovimOptions = pkgs.neovimUtils.makeNeovimConfig {
inherit (config) extraLuaPackages viAlias vimAlias;
inherit customLuaRC plugins;
wrapRc = customLuaRC != null;
# inherit customRC;
};
neovimPackage = pkg.overrideAttrs (final: super: {
postBuild =
super.postBuild
+ (mkDiffAlias "vimdiff" config.vimdiffAlias)
+ (mkDiffAlias "nvimdiff" config.nvimdiffAlias);
});
in {
options = {
package = mkPackageOption pkgs "neovim-unwrapped" {};
# Will probably be not needed
# dependenciesExtraArgs = mkOption {
# default = {};
# };
extraLuaPackages = mkOption {
description = ''
function to define extra lua packages that should be included in
neovim environment.
'';
type = extraLuaPackagesType;
example = literalExample "p: [p.nvim-nio]";
defaultText = literalExample "_: []";
default = _: [];
};
init = modules.init.option;
runtime = modules.runtime.option;
plugins = modules.plugin.option;
viAlias = mkEnableOption "creation on `vi` alias";
vimAlias = mkEnableOption "creation on `vim` alias";
vimdiffAlias = mkEnableOption "creation on `vimdiff` alias";
nvimdiffAlias = mkEnableOption "creation on `nvimdiff` alias";
neovimOptions = mkOption {
type = types.attrs;
description = "The resulting configuration passed to `pkgs.wrapNeovimUnstable`";
default = neovimOptions;
};
neovimPackage = mkOption {
type = types.package;
description = "The neovim package generated from your configuration.";
# defaultText = lib.literalExpression "pkgs.hello";
default = neovimPackage;
};
};
};
}

View File

@ -5,7 +5,7 @@
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: pluginsLuaDef = plugins:
@ -61,8 +61,8 @@ in
inherit version; inherit version;
pname = "sloth-flake"; pname = "sloth-flake";
src = fs.toSource { src = fs.toSource {
root = ../.; root = ../..;
fileset = ../lua/sloth-flake; fileset = ../../lua/sloth-flake;
}; };
nvimRequireCheck = "sloth-flake"; nvimRequireCheck = "sloth-flake";
buildPhase = '' buildPhase = ''