diff --git a/parts/lib/builders.nix b/parts/lib/builders.nix index aac7266..fb47e03 100644 --- a/parts/lib/builders.nix +++ b/parts/lib/builders.nix @@ -1,9 +1,12 @@ -{inputs, ...}: let - inherit (inputs) self nixpkgs; - inherit (nixpkgs) lib; - inherit (lib) mkDefault nixosSystem recursiveUpdate singleton; - inherit (builtins) concatLists; -in { +{ + inputs, + lib, + ... +}: let + inherit (inputs) self; + inherit (lib) mkDefault nixosSystem recursiveUpdate; + inherit (lib.lists) singleton concatLists flatten; + inherit (lib.extendedLib.modules) mkModuleTree'; mkSystem = { withSystem, system, @@ -38,4 +41,18 @@ in { ]; } ); + mkModulesForSystem = { + hostname, + modulePath ? ./modules, + ... + } @ args: + flatten ( + concatLists [ + (self.outPath + ./.) + (mkModuleTree' {path = self.outPath + modulePath;}) + # singleton + ] + ); +in { + inherit mkSystem mkModulesForSystem; } diff --git a/parts/lib/default.nix b/parts/lib/default.nix index c544c46..925ffbe 100644 --- a/parts/lib/default.nix +++ b/parts/lib/default.nix @@ -17,6 +17,7 @@ in { extendedLib = { builders = callLibs ./builders.nix; + modules = callLibs ./modules.nix; }; # This makes mkSytem available *in addition* to `lib.extendedLib.builders.mkSystem`. @@ -25,12 +26,20 @@ # absolutely sure we *never* conflict with nixpkgs. Likewise, the function names inherited # here should also be different from ones available under `lib` by default, i.e., you cannot # re-define functions. - inherit (self.extendedLib) builders; - inherit (self.extendedLib.builders) mkSystem; + # inherit (self.extendedLib) builders; + # inherit (self.extendedLib.builders) mkSystem; }; + # Merge layers of libraries into one as a subject of convenience + # and easy access. + extensions = lib.composeManyExtensions [ + (_: _: inputs.nixpkgs.lib) + (_: _: inputs.flake-parts.lib) + (_: _: inputs.neovim-flake.lib) + ]; + # Extend default library + extendedLib = (lib.makeExtensible lib0).extend extensions; # This can be used with `.extend` to extend the extended library. - extendedLib = lib.makeExtensible lib0; in { # Set the `lib` argument in `perSystem`, for example: # ``` @@ -44,6 +53,5 @@ in { # `flake.lib` is set. flake = { lib = extendedLib; - # _module.args.lib = extendedLib; }; } diff --git a/parts/lib/modules.nix b/parts/lib/modules.nix new file mode 100644 index 0000000..3ef4b75 --- /dev/null +++ b/parts/lib/modules.nix @@ -0,0 +1,44 @@ +{lib, ...}: let + inherit (builtins) filter map toString elem; + inherit (lib.filesystem) listFilesRecursive; + inherit (lib.strings) hasSuffix; + + # NOTE: This was bluntly taken from raf. + + # `mkModuleTree` is used to recursively import all Nix file in a given directory, assuming the + # given directory to be the module root, where rest of the modules are to be imported. This + # retains a sense of explicitness in the module tree, and allows for a more organized module + # imports, discarding the vague `default.nix` name for directories that are *modules*. + mkModuleTree = { + path, + ignoredPaths ? [./default.nix], + }: + filter (hasSuffix ".nix") ( + map toString ( + # List all files in the given path, and filter out paths that are in + # the ignoredPaths list + filter (path: !elem path ignoredPaths) (listFilesRecursive path) + ) + ); + + # A variant of mkModuleTree that provides more granular control over the files that are imported. + # While `mkModuleTree` imports all Nix files in the given directory, `mkModuleTree'` will look + # for a specific + mkModuleTree' = { + path, + ignoredPaths ? [], + }: ( + # Two conditions fill satisfy filter here: + # - The path should end with a module.nix, indicating + # that it is in fact a module file. + # - The path is not contained in the ignoredPaths list. + # If we cannot satisfy both of the conditions, then the path will be ignored + filter (hasSuffix "module.nix") ( + map toString ( + filter (path: !elem path ignoredPaths) (listFilesRecursive path) + ) + ) + ); +in { + inherit mkModuleTree mkModuleTree'; +}