From 44bfef1f1c00f990af3bb90402cb0b084679ee3a Mon Sep 17 00:00:00 2001 From: LeMarsu Date: Sun, 8 Mar 2026 03:56:53 +0100 Subject: [PATCH] chore: reorganize lib folder hierarchy --- lib/evalModules.nix | 25 +++++ lib/lib.nix | 148 +++------------------------ lib/mkPluginsFromInputs.nix | 38 +++---- lib/modules/default.nix | 119 +++++++++++++++++++++ lib/{ => modules}/init.nix | 0 lib/{ => modules}/plugin/default.nix | 0 lib/{ => modules}/plugin/event.nix | 0 lib/{ => modules}/plugin/keymap.nix | 0 lib/{ => modules}/runtime.nix | 0 lib/{ => modules}/sloth.nix | 6 +- 10 files changed, 179 insertions(+), 157 deletions(-) create mode 100644 lib/evalModules.nix create mode 100644 lib/modules/default.nix rename lib/{ => modules}/init.nix (100%) rename lib/{ => modules}/plugin/default.nix (100%) rename lib/{ => modules}/plugin/event.nix (100%) rename lib/{ => modules}/plugin/keymap.nix (100%) rename lib/{ => modules}/runtime.nix (100%) rename lib/{ => modules}/sloth.nix (95%) diff --git a/lib/evalModules.nix b/lib/evalModules.nix new file mode 100644 index 0000000..ec3a3f4 --- /dev/null +++ b/lib/evalModules.nix @@ -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; +} diff --git a/lib/lib.nix b/lib/lib.nix index ce0a14b..1111993 100644 --- a/lib/lib.nix +++ b/lib/lib.nix @@ -1,140 +1,16 @@ -{version, ...}: {pkgs, ...}: let - inherit (builtins) concatLists; - inherit (pkgs) bash lib; - inherit - (lib) - callPackageWith - concatMap - evalModules - fix - flip - literalExample - mkEnableOption - mkOption - mkPackageOption - optional - optionalString - types - ; +{version, ...}: {pkgs, ...}: +pkgs.lib.fix (lib: let + inherit (pkgs.lib) callPackageWith mergeAttrsList; - callModule = callPackageWith {inherit pkgs callModule;}; - - modules = { - init = callModule ./init.nix {}; - plugin = callModule ./plugin {}; - runtime = callModule ./runtime.nix {}; - sloth = callModule ./sloth.nix {}; + callModule = callPackageWith { + inherit pkgs callModule; + sloth = {inherit lib version;}; }; - 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 < $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; - }; - }; - }; + callModules = modules: mergeAttrsList (map (path: callModule path {}) modules); 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; - }) + callModules [ + ./evalModules.nix + ./mkPluginsFromInputs.nix + ./modules + ]) diff --git a/lib/mkPluginsFromInputs.nix b/lib/mkPluginsFromInputs.nix index 546bbfc..194cd37 100644 --- a/lib/mkPluginsFromInputs.nix +++ b/lib/mkPluginsFromInputs.nix @@ -1,20 +1,22 @@ -{pkgs, ...}: { - inputs, - predicate ? pkgs.lib.strings.hasPrefix "plugin-", - nameMap ? builtins.substring 7 (-1), - buildVimPlugin ? pkgs.vimUtils.buildVimPlugin, -}: let +{pkgs, ...}: let inherit (builtins) attrNames filter foldl' mapAttrs; - - names = filter predicate (attrNames inputs); - mkPlugin = m: k: let - name = nameMap k; - pluginDef = { - inherit name; - src = inputs.${k}; - }; +in { + mkPluginsFromInputs = { + inputs, + predicate ? pkgs.lib.strings.hasPrefix "plugin-", + nameMap ? builtins.substring 7 (-1), + buildVimPlugin ? pkgs.vimUtils.buildVimPlugin, + }: let + names = filter predicate (attrNames inputs); + mkPlugin = m: k: let + name = nameMap k; + pluginDef = { + inherit name; + src = inputs.${k}; + }; + in + m // {${name} = pluginDef;}; + plugins = foldl' mkPlugin {} names; in - m // {${name} = pluginDef;}; - plugins = foldl' mkPlugin {} names; -in - mapAttrs (_: buildVimPlugin) plugins + mapAttrs (_: buildVimPlugin) plugins; +} diff --git a/lib/modules/default.nix b/lib/modules/default.nix new file mode 100644 index 0000000..efc25fe --- /dev/null +++ b/lib/modules/default.nix @@ -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 < $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; + }; + }; + }; +} diff --git a/lib/init.nix b/lib/modules/init.nix similarity index 100% rename from lib/init.nix rename to lib/modules/init.nix diff --git a/lib/plugin/default.nix b/lib/modules/plugin/default.nix similarity index 100% rename from lib/plugin/default.nix rename to lib/modules/plugin/default.nix diff --git a/lib/plugin/event.nix b/lib/modules/plugin/event.nix similarity index 100% rename from lib/plugin/event.nix rename to lib/modules/plugin/event.nix diff --git a/lib/plugin/keymap.nix b/lib/modules/plugin/keymap.nix similarity index 100% rename from lib/plugin/keymap.nix rename to lib/modules/plugin/keymap.nix diff --git a/lib/runtime.nix b/lib/modules/runtime.nix similarity index 100% rename from lib/runtime.nix rename to lib/modules/runtime.nix diff --git a/lib/sloth.nix b/lib/modules/sloth.nix similarity index 95% rename from lib/sloth.nix rename to lib/modules/sloth.nix index 9be5f7b..a91d581 100644 --- a/lib/sloth.nix +++ b/lib/modules/sloth.nix @@ -5,7 +5,7 @@ fs = pkgs.lib.fileset; - lua = import ./lua.nix {}; + lua = import ../lua.nix {}; versionLua = version: with lua; nix2lua (return (lambda (return version))); pluginsLuaDef = plugins: @@ -61,8 +61,8 @@ in inherit version; pname = "sloth-flake"; src = fs.toSource { - root = ../.; - fileset = ../lua/sloth-flake; + root = ../..; + fileset = ../../lua/sloth-flake; }; nvimRequireCheck = "sloth-flake"; buildPhase = ''