-
-
Notifications
You must be signed in to change notification settings - Fork 146
/
all-formats.nix
82 lines (74 loc) · 2.39 KB
/
all-formats.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
{
config,
lib,
pkgs,
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;
# adds an evaluated `config` to the derivation attributes of a format for introspection
exposeConfig = conf: output:
output.overrideAttrs (old: {
passthru.config = conf.config;
});
# 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")"
''
)
);
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 configuration.
'';
};
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];
});
}