Skip to content

Commit f4a79d0

Browse files
committed
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
1 parent cf341a2 commit f4a79d0

File tree

4 files changed

+81
-17
lines changed

4 files changed

+81
-17
lines changed

all-formats.nix

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
{
2+
config,
23
lib,
34
extendModules,
45
...
56
}: let
7+
inherit
8+
(lib)
9+
types
10+
;
611
# attrs of all format modules from ./formats
712
formatModules =
813
lib.flip lib.mapAttrs' (builtins.readDir ./formats)
@@ -21,7 +26,7 @@
2126
};
2227

2328
# evaluated configs for all formats
24-
allConfigs = lib.mapAttrs (formatName: evalFormat) formatModules;
29+
allConfigs = lib.mapAttrs (formatName: evalFormat) config.formatConfigs;
2530

2631
# attrset of formats to be exposed under config.system.formats
2732
formats = lib.flip lib.mapAttrs allConfigs (
@@ -37,13 +42,22 @@ in {
3742
key = "github:nix-community/nixos-generators/all-formats.nix";
3843

3944
# declare option for exposing all formats
40-
options.system.formats = lib.mkOption {
45+
options.formats = lib.mkOption {
4146
type = lib.types.lazyAttrsOf lib.types.raw;
4247
description = ''
4348
Different target formats generated for this NixOS configuratation.
4449
'';
4550
};
4651

52+
options.formatConfigs = lib.mkOption {
53+
type = types.attrsOf types.deferredModule;
54+
};
55+
4756
# expose all formats
48-
config.system = {inherit formats;};
57+
config.formats = formats;
58+
59+
#
60+
config.formatConfigs = lib.flip lib.mapAttrs formatModules (name: module: {
61+
imports = [module];
62+
});
4963
}

checks/test-all-formats.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@
2828
testedFormats =
2929
lib.filterAttrs
3030
(name: _: ! exclude ? ${name})
31-
conf.config.system.formats;
31+
conf.config.formats;
3232
in
3333
testedFormats

checks/test-customize-format.nix

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
nixpkgs ? <nixpkgs>,
3+
system ? builtins.currentSystem,
4+
lib ? import (nixpkgs + /lib),
5+
}: let
6+
nixosSystem = import (nixpkgs + /nixos/lib/eval-config.nix);
7+
8+
userModule1 = {...}: {
9+
formatConfigs.amazon.amazonImage.name = "xyz";
10+
};
11+
12+
userModule2 = {...}: {
13+
formatConfigs.amazon.amazonImage.name = lib.mkForce "custom-name";
14+
};
15+
16+
conf = nixosSystem {
17+
inherit system;
18+
modules = [
19+
../configuration.nix
20+
../all-formats.nix
21+
userModule1
22+
userModule2
23+
];
24+
};
25+
in
26+
assert lib.hasInfix "custom-name" "${conf.config.formats.amazon}";
27+
conf.config.formats.amazon

flake.nix

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,23 @@
1111
self,
1212
nixpkgs,
1313
nixlib,
14-
}:
15-
# Library modules (depend on nixlib)
14+
}: let
15+
lib = nixpkgs.lib;
16+
17+
# Ensures a derivation's name can be accessed without evaluating it deeply.
18+
# Prevents `nix flake show` from being very slow.
19+
makeLazyDrv = name: drv: {
20+
inherit name;
21+
inherit
22+
(drv)
23+
drvPath
24+
outPath
25+
outputName
26+
;
27+
type = "derivation";
28+
};
29+
in
30+
# Library modules (depend on nixlib)
1631
{
1732
# export all generator formats in ./formats
1833
nixosModules = nixlib.lib.mapAttrs' (file: _: {
@@ -109,23 +124,31 @@
109124
});
110125

111126
checks =
112-
nixpkgs.lib.genAttrs ["x86_64-linux" "aarch64-linux"]
127+
lib.genAttrs ["x86_64-linux" "aarch64-linux"]
113128
(
114129
system: let
115130
allFormats = import ./checks/test-all-formats.nix {
116131
inherit nixpkgs system;
117132
};
133+
test-customize-format = import ./checks/test-customize-format.nix {
134+
inherit nixpkgs system;
135+
};
118136
in
119-
{
120-
inherit
121-
(self.packages.${system})
122-
nixos-generate
123-
;
124-
is-formatted = import ./checks/is-formatted.nix {
125-
pkgs = nixpkgs.legacyPackages.${system};
126-
};
127-
}
128-
// allFormats
137+
lib.mapAttrs makeLazyDrv (
138+
{
139+
inherit
140+
(self.packages.${system})
141+
nixos-generate
142+
;
143+
144+
inherit test-customize-format;
145+
146+
is-formatted = import ./checks/is-formatted.nix {
147+
pkgs = nixpkgs.legacyPackages.${system};
148+
};
149+
}
150+
// allFormats
151+
)
129152
);
130153

131154
devShells = forAllSystems (system: let

0 commit comments

Comments
 (0)