Skip to content

Commit 8a04d5c

Browse files
committed
feat(all-formats.nix): add readme section and test
1 parent f4a79d0 commit 8a04d5c

File tree

3 files changed

+155
-30
lines changed

3 files changed

+155
-30
lines changed

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,63 @@ 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. `vmware` or
155+
`my-custom-format` images can be built from the same `configuration.nix` by running:
156+
157+
- `nix build .#vmware` or
158+
- `nix build .#my-custom-format`
159+
160+
```nix
161+
{
162+
inputs = {
163+
nixpkgs.url = "nixpkgs/nixos-unstable";
164+
nixos-generators = {
165+
url = "github:nix-community/nixos-generators";
166+
inputs.nixpkgs.follows = "nixpkgs";
167+
};
168+
};
169+
outputs = { self, nixpkgs, nixos-generators, ... }: {
170+
171+
# A single nixos config outputting multiple formats.
172+
# Alternatively put this in a configuration.nix.
173+
nixosModules.my-machine = {config, ...}: {
174+
imports = [
175+
nixos-generators.nixosModules.all-formats
176+
];
177+
178+
nixpkgs.hostPlatform = "x86_64-linux";
179+
180+
# customize an existing format
181+
formatConfigs.vmware = {config, ...}: {
182+
services.openssh.enable = true;
183+
};
184+
185+
# define a new format
186+
formatConfigs.my-custom-format = {config, modulesPath, ...}: {
187+
imports = ["${toString modulesPath}/installer/cd-dvd/installation-cd-base.nix"];
188+
formatAttr = "isoImage";
189+
filename = "*.iso";
190+
networking.wireless.networks = {
191+
# ...
192+
};
193+
};
194+
195+
# the evaluated machine
196+
nixosConfigurations.my-machine = nixpkgs.lib.nixosSystem {
197+
modules = [self.nixosModules.my-machine];
198+
};
199+
200+
# optionally re-expose all formats as packages
201+
packages.x86_64-linux =
202+
self.nixosConfigurations.my-machine.config.formats;
203+
};
204+
}
205+
```
206+
150207
## Using in a Flake
151208

152209
`nixos-generators` can be included as a `Flake` input and provides
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 = { self, nixpkgs, nixos-generators, ... }: {
15+
16+
nixosModules.my-machine = {config, ...}: {
17+
imports = [
18+
nixos-generators.nixosModules.all-formats
19+
];
20+
21+
nixpkgs.hostPlatform = "x86_64-linux";
22+
23+
# customize an existing format
24+
formatConfigs.vmware = {config, ...}: {
25+
services.openssh.enable = false;
26+
};
27+
28+
# define a new format
29+
formatConfigs.my-custom-format = {config, modulesPath, ...}: {
30+
imports = ["${toString modulesPath}/installer/cd-dvd/installation-cd-base.nix"];
31+
formatAttr = "isoImage";
32+
filename = "*.iso";
33+
networking.wireless.networks = {
34+
# ...
35+
};
36+
};
37+
};
38+
39+
nixosConfigurations.my-machine = nixpkgs.lib.nixosSystem {
40+
modules = [self.nixosModules.my-machine];
41+
};
42+
43+
checks.x86_64-linux = {
44+
test-flake_vmware =
45+
self.nixosConfigurations.my-machine.config.formats.vmware;
46+
test-flake_my-custom-format =
47+
self.nixosConfigurations.my-machine.config.formats.my-custom-format;
48+
};
49+
};
50+
}

flake.nix

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@
1111
self,
1212
nixpkgs,
1313
nixlib,
14-
}: let
14+
} @ inputs: let
1515
lib = nixpkgs.lib;
1616

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 subFlake;
26+
1727
# Ensures a derivation's name can be accessed without evaluating it deeply.
1828
# Prevents `nix flake show` from being very slow.
1929
makeLazyDrv = name: drv: {
@@ -30,11 +40,15 @@
3040
# Library modules (depend on nixlib)
3141
{
3242
# export all generator formats in ./formats
33-
nixosModules = nixlib.lib.mapAttrs' (file: _: {
34-
name = nixlib.lib.removeSuffix ".nix" file;
35-
# The exported module should include the internal format* options
36-
value.imports = [(./formats + "/${file}") ./format-module.nix];
37-
}) (builtins.readDir ./formats);
43+
nixosModules =
44+
{
45+
all-formats = ./all-formats.nix;
46+
}
47+
// (nixlib.lib.mapAttrs' (file: _: {
48+
name = nixlib.lib.removeSuffix ".nix" file;
49+
# The exported module should include the internal format* options
50+
value.imports = [(./formats + "/${file}") ./format-module.nix];
51+
}) (builtins.readDir ./formats));
3852

3953
# example usage in flakes:
4054
# outputs = { self, nixpkgs, nixos-generators, ...}: {
@@ -124,31 +138,35 @@
124138
});
125139

126140
checks =
127-
lib.genAttrs ["x86_64-linux" "aarch64-linux"]
141+
lib.recursiveUpdate
142+
(callFlake ./checks/test-all-formats-flake/flake.nix).checks
128143
(
129-
system: let
130-
allFormats = import ./checks/test-all-formats.nix {
131-
inherit nixpkgs system;
132-
};
133-
test-customize-format = import ./checks/test-customize-format.nix {
134-
inherit nixpkgs system;
135-
};
136-
in
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-
)
144+
lib.genAttrs ["x86_64-linux" "aarch64-linux"]
145+
(
146+
system: let
147+
allFormats = import ./checks/test-all-formats.nix {
148+
inherit nixpkgs system;
149+
};
150+
test-customize-format = import ./checks/test-customize-format.nix {
151+
inherit nixpkgs system;
152+
};
153+
in
154+
lib.mapAttrs makeLazyDrv (
155+
{
156+
inherit
157+
(self.packages.${system})
158+
nixos-generate
159+
;
160+
161+
inherit test-customize-format;
162+
163+
is-formatted = import ./checks/is-formatted.nix {
164+
pkgs = nixpkgs.legacyPackages.${system};
165+
};
166+
}
167+
// allFormats
168+
)
169+
)
152170
);
153171

154172
devShells = forAllSystems (system: let

0 commit comments

Comments
 (0)