Skip to content

Commit 9191c85

Browse files
authored
Merge pull request nix-community#256 from DavHau/all-formats
2 parents cf341a2 + 34bbb3e commit 9191c85

File tree

6 files changed

+222
-26
lines changed

6 files changed

+222
-26
lines changed

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,61 @@ For more details on configuring `binfmt`, have a look at:
147147
Once you've run `nixos-rebuild` with these options,
148148
you can use the `--system` option to create images for other architectures.
149149

150+
## Using as a nixos-module
151+
152+
`nixos-generators` can be included as a `NixOS module` into your existing `configuration.nix` making all available formats available through `config.formats` and configurable through `config.formatConfigs`. New formats can be defined by adding a new entry like `config.formatConfigs.my-new-format = {config, ...}: {}`.
153+
154+
An example `flake.nix` demonstrating this approach is below.
155+
156+
images can be built from that flake by running:
157+
158+
- `nix build .#nixosConfigurations.my-machine.config.formats.vmware` or
159+
- `nix build .#nixosConfigurations.my-machine.config.formats.my-custom-format` or
160+
- `nix build .#nixosConfigurations.my-machine.config.formats.<any-other-format>`
161+
162+
```nix
163+
{
164+
inputs = {
165+
nixpkgs.url = "nixpkgs/nixos-unstable";
166+
nixos-generators = {
167+
url = "github:nix-community/nixos-generators";
168+
inputs.nixpkgs.follows = "nixpkgs";
169+
};
170+
};
171+
outputs = { self, nixpkgs, nixos-generators, ... }: {
172+
173+
# A single nixos config outputting multiple formats.
174+
# Alternatively put this in a configuration.nix.
175+
nixosModules.my-machine = {config, ...}: {
176+
imports = [
177+
nixos-generators.nixosModules.all-formats
178+
];
179+
180+
nixpkgs.hostPlatform = "x86_64-linux";
181+
182+
# customize an existing format
183+
formatConfigs.vmware = {config, ...}: {
184+
services.openssh.enable = true;
185+
};
186+
187+
# define a new format
188+
formatConfigs.my-custom-format = {config, modulesPath, ...}: {
189+
imports = ["${toString modulesPath}/installer/cd-dvd/installation-cd-base.nix"];
190+
formatAttr = "isoImage";
191+
filename = "*.iso";
192+
networking.wireless.networks = {
193+
# ...
194+
};
195+
};
196+
197+
# the evaluated machine
198+
nixosConfigurations.my-machine = nixpkgs.lib.nixosSystem {
199+
modules = [self.nixosModules.my-machine];
200+
};
201+
};
202+
}
203+
```
204+
150205
## Using in a Flake
151206

152207
`nixos-generators` can be included as a `Flake` input and provides

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
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Tests using the all-formats module through a flake.
3+
- Tests if foramts can be customized.
4+
- Tests if new foramts can be added
5+
*/
6+
{
7+
inputs = {
8+
nixpkgs.url = "nixpkgs/nixos-unstable";
9+
nixos-generators = {
10+
url = "github:nix-community/nixos-generators";
11+
inputs.nixpkgs.follows = "nixpkgs";
12+
};
13+
};
14+
outputs = {
15+
self,
16+
nixpkgs,
17+
nixos-generators,
18+
...
19+
}: {
20+
nixosModules.my-machine = {config, ...}: {
21+
imports = [
22+
nixos-generators.nixosModules.all-formats
23+
];
24+
25+
nixpkgs.hostPlatform = "x86_64-linux";
26+
27+
# customize an existing format
28+
formatConfigs.vmware = {config, ...}: {
29+
services.openssh.enable = false;
30+
};
31+
32+
# define a new format
33+
formatConfigs.my-custom-format = {
34+
config,
35+
modulesPath,
36+
...
37+
}: {
38+
imports = ["${toString modulesPath}/installer/cd-dvd/installation-cd-base.nix"];
39+
formatAttr = "isoImage";
40+
filename = "*.iso";
41+
networking.wireless.networks = {
42+
# ...
43+
};
44+
};
45+
};
46+
47+
nixosConfigurations.my-machine = nixpkgs.lib.nixosSystem {
48+
modules = [self.nixosModules.my-machine];
49+
};
50+
51+
checks.x86_64-linux = {
52+
test-flake_vmware =
53+
self.nixosConfigurations.my-machine.config.formats.vmware;
54+
test-flake_my-custom-format =
55+
self.nixosConfigurations.my-machine.config.formats.my-custom-format;
56+
};
57+
};
58+
}

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: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,45 @@
1111
self,
1212
nixpkgs,
1313
nixlib,
14-
}:
15-
# Library modules (depend on nixlib)
14+
} @ inputs: let
15+
lib = nixpkgs.lib;
16+
17+
callFlake = flake: let
18+
args =
19+
inputs
20+
// {
21+
nixos-generators = self;
22+
self = subFlake;
23+
};
24+
subFlake = (import flake).outputs args;
25+
in
26+
subFlake;
27+
28+
# Ensures a derivation's name can be accessed without evaluating it deeply.
29+
# Prevents `nix flake show` from being very slow.
30+
makeLazyDrv = name: drv: {
31+
inherit name;
32+
inherit
33+
(drv)
34+
drvPath
35+
outPath
36+
outputName
37+
;
38+
type = "derivation";
39+
};
40+
in
41+
# Library modules (depend on nixlib)
1642
{
1743
# export all generator formats in ./formats
18-
nixosModules = nixlib.lib.mapAttrs' (file: _: {
19-
name = nixlib.lib.removeSuffix ".nix" file;
20-
# The exported module should include the internal format* options
21-
value.imports = [(./formats + "/${file}") ./format-module.nix];
22-
}) (builtins.readDir ./formats);
44+
nixosModules =
45+
{
46+
all-formats = ./all-formats.nix;
47+
}
48+
// (nixlib.lib.mapAttrs' (file: _: {
49+
name = nixlib.lib.removeSuffix ".nix" file;
50+
# The exported module should include the internal format* options
51+
value.imports = [(./formats + "/${file}") ./format-module.nix];
52+
}) (builtins.readDir ./formats));
2353

2454
# example usage in flakes:
2555
# outputs = { self, nixpkgs, nixos-generators, ...}: {
@@ -109,23 +139,35 @@
109139
});
110140

111141
checks =
112-
nixpkgs.lib.genAttrs ["x86_64-linux" "aarch64-linux"]
142+
lib.recursiveUpdate
143+
(callFlake ./checks/test-all-formats-flake/flake.nix).checks
113144
(
114-
system: let
115-
allFormats = import ./checks/test-all-formats.nix {
116-
inherit nixpkgs system;
117-
};
118-
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};
145+
lib.genAttrs ["x86_64-linux" "aarch64-linux"]
146+
(
147+
system: let
148+
allFormats = import ./checks/test-all-formats.nix {
149+
inherit nixpkgs system;
126150
};
127-
}
128-
// allFormats
151+
test-customize-format = import ./checks/test-customize-format.nix {
152+
inherit nixpkgs system;
153+
};
154+
in
155+
lib.mapAttrs makeLazyDrv (
156+
{
157+
inherit
158+
(self.packages.${system})
159+
nixos-generate
160+
;
161+
162+
inherit test-customize-format;
163+
164+
is-formatted = import ./checks/is-formatted.nix {
165+
pkgs = nixpkgs.legacyPackages.${system};
166+
};
167+
}
168+
// allFormats
169+
)
170+
)
129171
);
130172

131173
devShells = forAllSystems (system: let

0 commit comments

Comments
 (0)