parts/lib: add modules.nix and add to builders.nix

This commit is contained in:
Charlie Root 2024-09-04 07:21:56 +02:00
commit 79c98ac67b
3 changed files with 79 additions and 10 deletions

View file

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

View file

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

44
parts/lib/modules.nix Normal file
View file

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