nixos-generators/all-formats.nix

81 lines
2.4 KiB
Nix
Raw Permalink Normal View History

2023-05-29 12:01:19 +03:00
{
config,
2023-05-29 12:01:19 +03:00
lib,
2023-07-21 14:03:22 +03:00
pkgs,
2023-05-29 12:01:19 +03:00
extendModules,
...
}: let
inherit
(lib)
types
;
2023-05-29 10:34:10 +03:00
# attrs of all format modules from ./formats
2023-05-29 12:01:19 +03:00
formatModules =
lib.flip lib.mapAttrs' (builtins.readDir ./formats)
2023-05-29 10:34:10 +03:00
(fname: type: {
name = lib.removeSuffix ".nix" fname;
value = ./formats + "/${fname}";
});
# function to evaluate a given format to a config
2023-05-29 12:01:19 +03:00
evalFormat = formatModule:
extendModules {
modules = [
./format-module.nix
formatModule
];
};
2023-05-29 10:34:10 +03:00
# evaluated configs for all formats
allConfigs = lib.mapAttrs (formatName: evalFormat) config.formatConfigs;
2023-05-29 10:34:10 +03:00
# adds an evaluated `config` to the derivation attributes of a format for introspection
exposeConfig = conf: output: output.overrideAttrs (old: {
passthru.config = conf.config;
});
2023-05-29 10:34:10 +03:00
# attrset of formats to be exposed under config.system.formats
formats = lib.flip lib.mapAttrs allConfigs (
formatName: conf: exposeConfig conf (
pkgs.runCommand "${conf.config.system.build.${conf.config.formatAttr}.name}" {} ''
set -efu
target=$(find '${conf.config.system.build.${conf.config.formatAttr}}' -name '*${conf.config.fileExtension}' -xtype f -print -quit)
if [ -z "$target" ]; then
echo "No target file found for ${conf.config.formatAttr} format"
echo "Check the content of this build path: ${conf.config.system.build.${conf.config.formatAttr}}"
exit 1
fi
mkdir $out
ln -s "$target" "$out/$(basename "$target")"
''
)
2023-05-29 10:34:10 +03:00
);
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 {
2023-05-29 10:34:10 +03:00
type = lib.types.lazyAttrsOf lib.types.raw;
description = ''
Different target formats generated for this NixOS configuration.
2023-05-29 10:34:10 +03:00
'';
};
options.formatConfigs = lib.mkOption {
type = types.attrsOf types.deferredModule;
};
2023-05-29 10:34:10 +03:00
# expose all formats
config.formats = formats;
#
config.formatConfigs = lib.flip lib.mapAttrs formatModules (name: module: {
imports = [module];
});
2023-05-29 10:34:10 +03:00
}