nixos-generators/all-formats.nix
DavHau f4a79d08d7 feat(all-formats.nix): add formatConfigs
While the `all-formats.nix` module allows to import all formats at once, it lacks an interface to customize formats or add formats.

This is fixed by adding the top-level option `formatConfigs` (attrsOf deferredModule).

All format modules created under `config.formatConfigs` are mapped so their outputs are available under `config.formats` which has also been moved to the top-level (previously `config.system.formats`).

Done:
- add option `formatConfigs`
- move option `system.formats` -> `formats`
- add test for customizing a format
2023-07-03 11:54:20 +03:00

64 lines
1.6 KiB
Nix

{
config,
lib,
extendModules,
...
}: let
inherit
(lib)
types
;
# attrs of all format modules from ./formats
formatModules =
lib.flip lib.mapAttrs' (builtins.readDir ./formats)
(fname: type: {
name = lib.removeSuffix ".nix" fname;
value = ./formats + "/${fname}";
});
# function to evaluate a given format to a config
evalFormat = formatModule:
extendModules {
modules = [
./format-module.nix
formatModule
];
};
# evaluated configs for all formats
allConfigs = lib.mapAttrs (formatName: evalFormat) config.formatConfigs;
# attrset of formats to be exposed under config.system.formats
formats = lib.flip lib.mapAttrs allConfigs (
formatName: conf:
conf.config.system.build.${conf.config.formatAttr}
);
in {
_file = ./all-formats.nix;
# This deliberate key makes sure this module will be deduplicated
# regardless of the accessor path: either via flake's nixosModule
# or as part of the nixos-generate command. These two store paths
# of the module may differ and hence don't serve as a key
key = "github:nix-community/nixos-generators/all-formats.nix";
# declare option for exposing all formats
options.formats = lib.mkOption {
type = lib.types.lazyAttrsOf lib.types.raw;
description = ''
Different target formats generated for this NixOS configuratation.
'';
};
options.formatConfigs = lib.mkOption {
type = types.attrsOf types.deferredModule;
};
# expose all formats
config.formats = formats;
#
config.formatConfigs = lib.flip lib.mapAttrs formatModules (name: module: {
imports = [module];
});
}