From b06a06888fdfe5c0a5147f0152cd54277b8e882e Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Tue, 16 Jan 2024 07:21:31 +0800 Subject: [PATCH] flake.nix: add `images` flake output --- configs/flake-parts/nixos.nix | 38 +++++++++++++++++++++-- flake.nix | 1 + modules/flake-parts/default.nix | 11 +++++++ modules/flake-parts/images.nix | 53 +++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 modules/flake-parts/default.nix create mode 100644 modules/flake-parts/images.nix diff --git a/configs/flake-parts/nixos.nix b/configs/flake-parts/nixos.nix index 3dd31e7a..ee8145b7 100644 --- a/configs/flake-parts/nixos.nix +++ b/configs/flake-parts/nixos.nix @@ -10,6 +10,8 @@ }: let + inherit (import ../../lib/extras/flake-helpers.nix { inherit lib inputs; }) mkHost mkImage listImagesWithSystems; + nixosConfigs = import ../../setups/nixos.nix { inherit lib inputs; }; # 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 # from project root. It also has some sensible default configurations. nixosConfigurations = - let - inherit (import ../../lib/extras/flake-helpers.nix { inherit lib inputs; }) mkHost listImagesWithSystems; - in lib.mapAttrs (user: metadata: mkHost { @@ -174,6 +173,39 @@ in 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 = { inherit defaultNixOSConfig nixosConfigs; }; diff --git a/flake.nix b/flake.nix index 49021787..bd87c96a 100644 --- a/flake.nix +++ b/flake.nix @@ -92,6 +92,7 @@ ]; imports = [ + ./modules/flake-parts ./configs/flake-parts ]; }; diff --git a/modules/flake-parts/default.nix b/modules/flake-parts/default.nix new file mode 100644 index 00000000..8a30fb12 --- /dev/null +++ b/modules/flake-parts/default.nix @@ -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 + ]; +} diff --git a/modules/flake-parts/images.nix b/modules/flake-parts/images.nix new file mode 100644 index 00000000..e1541673 --- /dev/null +++ b/modules/flake-parts/images.nix @@ -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}; + }; + }; +}