diff --git a/all-formats.nix b/all-formats.nix index 7784f1c..3b4857f 100644 --- a/all-formats.nix +++ b/all-formats.nix @@ -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]; + }); } diff --git a/checks/test-all-formats.nix b/checks/test-all-formats.nix index acb3362..edfcb65 100644 --- a/checks/test-all-formats.nix +++ b/checks/test-all-formats.nix @@ -28,6 +28,6 @@ testedFormats = lib.filterAttrs (name: _: ! exclude ? ${name}) - conf.config.system.formats; + conf.config.formats; in testedFormats diff --git a/checks/test-customize-format.nix b/checks/test-customize-format.nix new file mode 100644 index 0000000..b26d65f --- /dev/null +++ b/checks/test-customize-format.nix @@ -0,0 +1,27 @@ +{ + 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 diff --git a/flake.nix b/flake.nix index a6c2742..6a9262f 100644 --- a/flake.nix +++ b/flake.nix @@ -11,8 +11,23 @@ self, nixpkgs, nixlib, - }: - # Library modules (depend on 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 nixosModules = nixlib.lib.mapAttrs' (file: _: { @@ -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 - { - inherit - (self.packages.${system}) - nixos-generate - ; - is-formatted = import ./checks/is-formatted.nix { - pkgs = nixpkgs.legacyPackages.${system}; - }; - } - // allFormats + 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