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
This commit is contained in:
DavHau 2023-06-25 20:48:53 +03:00
parent cf341a2c94
commit f4a79d08d7
4 changed files with 81 additions and 17 deletions

View File

@ -1,8 +1,13 @@
{
config,
lib,
extendModules,
...
}: let
inherit
(lib)
types
;
# attrs of all format modules from ./formats
formatModules =
lib.flip lib.mapAttrs' (builtins.readDir ./formats)
@ -21,7 +26,7 @@
};
# evaluated configs for all formats
allConfigs = lib.mapAttrs (formatName: evalFormat) formatModules;
allConfigs = lib.mapAttrs (formatName: evalFormat) config.formatConfigs;
# attrset of formats to be exposed under config.system.formats
formats = lib.flip lib.mapAttrs allConfigs (
@ -37,13 +42,22 @@ in {
key = "github:nix-community/nixos-generators/all-formats.nix";
# declare option for exposing all formats
options.system.formats = lib.mkOption {
options.formats = lib.mkOption {
type = lib.types.lazyAttrsOf lib.types.raw;
description = ''
Different target formats generated for this NixOS configuratation.
'';
};
options.formatConfigs = lib.mkOption {
type = types.attrsOf types.deferredModule;
};
# expose all formats
config.system = {inherit formats;};
config.formats = formats;
#
config.formatConfigs = lib.flip lib.mapAttrs formatModules (name: module: {
imports = [module];
});
}

View File

@ -28,6 +28,6 @@
testedFormats =
lib.filterAttrs
(name: _: ! exclude ? ${name})
conf.config.system.formats;
conf.config.formats;
in
testedFormats

View File

@ -0,0 +1,27 @@
{
nixpkgs ? <nixpkgs>,
system ? builtins.currentSystem,
lib ? import (nixpkgs + /lib),
}: let
nixosSystem = import (nixpkgs + /nixos/lib/eval-config.nix);
userModule1 = {...}: {
formatConfigs.amazon.amazonImage.name = "xyz";
};
userModule2 = {...}: {
formatConfigs.amazon.amazonImage.name = lib.mkForce "custom-name";
};
conf = nixosSystem {
inherit system;
modules = [
../configuration.nix
../all-formats.nix
userModule1
userModule2
];
};
in
assert lib.hasInfix "custom-name" "${conf.config.formats.amazon}";
conf.config.formats.amazon

View File

@ -11,7 +11,22 @@
self,
nixpkgs,
nixlib,
}:
}: let
lib = nixpkgs.lib;
# Ensures a derivation's name can be accessed without evaluating it deeply.
# Prevents `nix flake show` from being very slow.
makeLazyDrv = name: drv: {
inherit name;
inherit
(drv)
drvPath
outPath
outputName
;
type = "derivation";
};
in
# Library modules (depend on nixlib)
{
# export all generator formats in ./formats
@ -109,23 +124,31 @@
});
checks =
nixpkgs.lib.genAttrs ["x86_64-linux" "aarch64-linux"]
lib.genAttrs ["x86_64-linux" "aarch64-linux"]
(
system: let
allFormats = import ./checks/test-all-formats.nix {
inherit nixpkgs system;
};
test-customize-format = import ./checks/test-customize-format.nix {
inherit nixpkgs system;
};
in
lib.mapAttrs makeLazyDrv (
{
inherit
(self.packages.${system})
nixos-generate
;
inherit test-customize-format;
is-formatted = import ./checks/is-formatted.nix {
pkgs = nixpkgs.legacyPackages.${system};
};
}
// allFormats
)
);
devShells = forAllSystems (system: let