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, lib,
extendModules, extendModules,
... ...
}: let }: let
inherit
(lib)
types
;
# attrs of all format modules from ./formats # attrs of all format modules from ./formats
formatModules = formatModules =
lib.flip lib.mapAttrs' (builtins.readDir ./formats) lib.flip lib.mapAttrs' (builtins.readDir ./formats)
@ -21,7 +26,7 @@
}; };
# evaluated configs for all formats # 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 # attrset of formats to be exposed under config.system.formats
formats = lib.flip lib.mapAttrs allConfigs ( formats = lib.flip lib.mapAttrs allConfigs (
@ -37,13 +42,22 @@ in {
key = "github:nix-community/nixos-generators/all-formats.nix"; key = "github:nix-community/nixos-generators/all-formats.nix";
# declare option for exposing all formats # declare option for exposing all formats
options.system.formats = lib.mkOption { options.formats = lib.mkOption {
type = lib.types.lazyAttrsOf lib.types.raw; type = lib.types.lazyAttrsOf lib.types.raw;
description = '' description = ''
Different target formats generated for this NixOS configuratation. Different target formats generated for this NixOS configuratation.
''; '';
}; };
options.formatConfigs = lib.mkOption {
type = types.attrsOf types.deferredModule;
};
# expose all formats # 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 = testedFormats =
lib.filterAttrs lib.filterAttrs
(name: _: ! exclude ? ${name}) (name: _: ! exclude ? ${name})
conf.config.system.formats; conf.config.formats;
in in
testedFormats 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,8 +11,23 @@
self, self,
nixpkgs, nixpkgs,
nixlib, nixlib,
}: }: let
# Library modules (depend on nixlib) 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 # export all generator formats in ./formats
nixosModules = nixlib.lib.mapAttrs' (file: _: { nixosModules = nixlib.lib.mapAttrs' (file: _: {
@ -109,23 +124,31 @@
}); });
checks = checks =
nixpkgs.lib.genAttrs ["x86_64-linux" "aarch64-linux"] lib.genAttrs ["x86_64-linux" "aarch64-linux"]
( (
system: let system: let
allFormats = import ./checks/test-all-formats.nix { allFormats = import ./checks/test-all-formats.nix {
inherit nixpkgs system; inherit nixpkgs system;
}; };
test-customize-format = import ./checks/test-customize-format.nix {
inherit nixpkgs system;
};
in in
{ lib.mapAttrs makeLazyDrv (
inherit {
(self.packages.${system}) inherit
nixos-generate (self.packages.${system})
; nixos-generate
is-formatted = import ./checks/is-formatted.nix { ;
pkgs = nixpkgs.legacyPackages.${system};
}; inherit test-customize-format;
}
// allFormats is-formatted = import ./checks/is-formatted.nix {
pkgs = nixpkgs.legacyPackages.${system};
};
}
// allFormats
)
); );
devShells = forAllSystems (system: let devShells = forAllSystems (system: let