dream2nix/modules/flake-parts/all-modules.nix

59 lines
1.3 KiB
Nix
Raw Normal View History

# Automatically exports modules from the `/**/modules` directory to:
# `flake.modules.<kind>.<name>`
# Automatically imports all flake-parts modules from `/**/modules/flake-parts`
{
config,
lib,
...
}: let
inherit
(builtins)
attrValues
mapAttrs
readDir
;
# inherit all lib functions used below
inherit
(lib)
mapAttrs'
filterAttrs
nameValuePair
removeSuffix
mkOption
types
;
2023-02-11 13:16:17 +03:00
modulesDir = ../.;
moduleKinds =
filterAttrs (_: type: type == "directory") (readDir modulesDir);
2023-02-11 13:16:17 +03:00
mapModules = kind:
mapAttrs'
2023-02-11 13:16:17 +03:00
(fn: _:
nameValuePair
(removeSuffix ".nix" fn)
2023-02-11 13:16:17 +03:00
(modulesDir + "/${kind}/${fn}"))
(readDir (modulesDir + "/${kind}"));
2023-02-11 13:16:17 +03:00
flakePartsModules = attrValues (
filterAttrs
2023-02-11 13:16:17 +03:00
(modName: _: modName != "all-modules")
(mapModules "flake-parts")
);
in {
imports = flakePartsModules;
options.flake.modules = mkOption {
2023-10-07 14:16:56 +03:00
type = types.lazyAttrsOf (types.lazyAttrsOf types.raw);
2023-02-11 13:16:17 +03:00
};
# generates future flake outputs: `modules.<kind>.<module-name>`
config.flake.modules = mapAttrs (kind: _: mapModules kind) moduleKinds;
2023-02-11 13:16:17 +03:00
# comapt to current schema: `nixosModules` / `darwinModules`
config.flake.nixosModules = config.flake.modules.nixos or {};
config.flake.darwinModules = config.flake.modules.darwin or {};
}