flake.nix: add images flake output

This commit is contained in:
Gabriel Arazas 2024-01-16 07:21:31 +08:00
parent e4ad727c56
commit b06a06888f
No known key found for this signature in database
GPG Key ID: ADE0C41DAB221FCC
4 changed files with 100 additions and 3 deletions

View File

@ -10,6 +10,8 @@
}: }:
let let
inherit (import ../../lib/extras/flake-helpers.nix { inherit lib inputs; }) mkHost mkImage listImagesWithSystems;
nixosConfigs = import ../../setups/nixos.nix { inherit lib inputs; }; nixosConfigs = import ../../setups/nixos.nix { inherit lib inputs; };
# A function that generates a NixOS module setting up the baseline # A function that generates a NixOS module setting up the baseline
@ -138,9 +140,6 @@ in
# A list of NixOS configurations from the `./configs/nixos` folder starting # A list of NixOS configurations from the `./configs/nixos` folder starting
# from project root. It also has some sensible default configurations. # from project root. It also has some sensible default configurations.
nixosConfigurations = nixosConfigurations =
let
inherit (import ../../lib/extras/flake-helpers.nix { inherit lib inputs; }) mkHost listImagesWithSystems;
in
lib.mapAttrs lib.mapAttrs
(user: metadata: (user: metadata:
mkHost { mkHost {
@ -174,6 +173,39 @@ in
inputs.self.nixosConfigurations; inputs.self.nixosConfigurations;
}; };
perSystem = { system, lib, ... }: {
# This contains images that are meant to be built and distributed
# somewhere else including those NixOS configurations that are built as
# an ISO.
images =
let
validImages = lib.filterAttrs
(host: metadata:
metadata.format != null && (lib.elem system metadata.systems))
nixosConfigs;
in
lib.mapAttrs'
(host: metadata:
let
name = metadata.hostname or host;
nixpkgs-channel = metadata.nixpkgs-channel or "nixpkgs";
in
lib.nameValuePair name (mkImage {
inherit (metadata) format;
inherit nixpkgs-channel;
extraModules = [
(hostSpecificModule host metadata)
# Forcing the host platform set by the host (if there's any).
# Ideally, there shouldn't be.
({ lib, ... }: {
nixpkgs.hostPlatform = lib.mkForce system;
})
];
}))
validImages;
};
_module.args = { _module.args = {
inherit defaultNixOSConfig nixosConfigs; inherit defaultNixOSConfig nixosConfigs;
}; };

View File

@ -92,6 +92,7 @@
]; ];
imports = [ imports = [
./modules/flake-parts
./configs/flake-parts ./configs/flake-parts
]; ];
}; };

View File

@ -0,0 +1,11 @@
# Unlike other custom modules such as from NixOS and home-manager, all
# flake-part modules are considered internal so there's no need for an internal
# flag. We can just import these directly. Nobody should be using this except
# this project (and also my other projects).
{ lib, ... }:
{
imports = [
./images.nix
];
}

View File

@ -0,0 +1,53 @@
# A custom flake-parts module to configure my NixOS images generated with
# nixos-generators. For more details, see the "Declarative hosts management"
# section from the documentation.
{ config, lib, flake-parts-lib, ... }:
let
inherit (flake-parts-lib) mkSubmoduleOptions mkPerSystemOption;
in
{
options = {
flake = mkSubmoduleOptions {
images = lib.mkOption {
type = with lib.types; lazyAttrsOf (attrsOf package);
default = { };
description = ''
An attribute set of per-system NixOS configurations built as an image
output supported by
[nixos-generators](https://github.com/nix-community/nixos-generators).
This is exclusively used for foodogsquared's NixOS setup.
'';
};
};
perSystem = mkPerSystemOption {
options = {
images = lib.mkOption {
type = with lib.types; attrsOf package;
default = {};
description = ''
An attribute set of NixOS configurations built as an image output
supported by
[nixos-generators](https://github.com/nix-community/nixos-generators).
'';
};
};
};
};
config = {
flake.images =
lib.mapAttrs
(k: v: v.images)
(lib.filterAttrs
(k: v: v.images != {})
config.allSystems
);
perInput = system: flake:
lib.optionalAttrs (flake ? images.${system}) {
images = flake.images.${system};
};
};
}