config: make platforms as a list for users and images

The `images` output attribute is now similar to packages as an attribute
set of packages with their platforms as the first level.
This commit is contained in:
Gabriel Arazas 2023-07-30 07:11:53 +08:00
parent 25cf8a9af4
commit f282aa2066
No known key found for this signature in database
GPG Key ID: ADE0C41DAB221FCC
4 changed files with 105 additions and 57 deletions

112
flake.nix
View File

@ -83,7 +83,7 @@
users = lib'.importTOML ./users.toml; users = lib'.importTOML ./users.toml;
# A set of image-related utilities for the flake outputs. # A set of image-related utilities for the flake outputs.
inherit (import ./lib/images.nix { inherit inputs; lib = lib'; }) mkHost mkHome mkImage; inherit (import ./lib/images.nix { inherit inputs; lib = lib'; }) mkHost mkHome mkImage listImagesWithSystems;
# The order here is important(?). # The order here is important(?).
overlays = [ overlays = [
@ -294,30 +294,35 @@
# A list of NixOS configurations from the `./hosts` folder. It also has # A list of NixOS configurations from the `./hosts` folder. It also has
# some sensible default configurations. # some sensible default configurations.
nixosConfigurations = lib'.mapAttrs nixosConfigurations =
(host: metadata: let
let images' = listImagesWithSystems images;
path = ./hosts/${host}; in
extraModules = [ lib'.mapAttrs
({ lib, ... }: { (_: host:
config = lib.mkMerge [ let
{ networking.hostName = metadata.hostname or host; } name = host._name;
path = ./hosts/${name};
extraModules = [
({ lib, ... }: {
config = lib.mkMerge [
{ networking.hostName = lib.mkForce name; }
(lib.mkIf (metadata ? domain) (lib.mkIf (host ? domain)
{ networking.domain = metadata.domain; }) { networking.domain = lib.mkForce host.domain; })
]; ];
}) })
hostSharedConfig hostSharedConfig
path path
]; ];
in in
mkHost { mkHost {
inherit extraModules extraArgs; inherit extraModules extraArgs;
system = metadata.system or defaultSystem; system = host._system;
nixpkgs-channel = metadata.nixpkgs-channel or "nixpkgs"; nixpkgs-channel = host.nixpkgs-channel or "nixpkgs";
}) })
(lib'.filterAttrs (name: host: (host.format or "iso") == "iso") images); (lib'.filterAttrs (_: host: (host.format or "iso") == "iso") images');
# We're going to make our custom modules available for our flake. Whether # We're going to make our custom modules available for our flake. Whether
# or not this is a good thing is debatable, I just want to test it. # or not this is a good thing is debatable, I just want to test it.
@ -325,10 +330,15 @@
# I can now install home-manager users in non-NixOS systems. # I can now install home-manager users in non-NixOS systems.
# NICE! # NICE!
homeConfigurations = lib'.mapAttrs homeConfigurations =
let
users' = listImagesWithSystems users;
in
lib'.mapAttrs
(name: metadata: (name: metadata:
let let
system = metadata.system or defaultSystem; name = metadata._name;
system = metadata._system;
pkgs = import inputs."${metadata.nixpkgs-channel or "nixpkgs"}" { pkgs = import inputs."${metadata.nixpkgs-channel or "nixpkgs"}" {
inherit system overlays; inherit system overlays;
}; };
@ -359,7 +369,7 @@
inherit pkgs system extraModules extraArgs; inherit pkgs system extraModules extraArgs;
home-manager-channel = metadata.home-manager-channel or "home-manager"; home-manager-channel = metadata.home-manager-channel or "home-manager";
}) })
users; users';
# Extending home-manager with my custom modules, if anyone cares. # Extending home-manager with my custom modules, if anyone cares.
homeModules = homeModules =
@ -386,30 +396,34 @@
# somewhere else including those NixOS configurations that are built as # somewhere else including those NixOS configurations that are built as
# an ISO. # an ISO.
images = images =
lib'.mapAttrs forAllSystems (system:
(host: metadata: let
let images' = lib'.filterAttrs (host: metadata: lib'.elem system metadata.systems) images;
system = metadata.system or defaultSystem; in
nixpkgs-channel = metadata.nixpkgs-channel or "nixpkgs"; lib'.mapAttrs
pkgs = import inputs."${nixpkgs-channel}" { inherit system overlays; }; (host: metadata:
format = metadata.format or "iso"; let
in inherit system;
mkImage { nixpkgs-channel = metadata.nixpkgs-channel or "nixpkgs";
inherit format system pkgs extraArgs; pkgs = import inputs."${nixpkgs-channel}" { inherit system overlays; };
extraModules = [ format = metadata.format or "iso";
({ lib, ... }: { in
config = lib.mkMerge [ mkImage {
{ networking.hostName = lib.mkForce metadata.hostname or host; } inherit format system pkgs extraArgs;
extraModules = [
({ lib, ... }: {
config = lib.mkMerge [
{ networking.hostName = lib.mkForce metadata.hostname or host; }
(lib.mkIf (metadata ? domain) (lib.mkIf (metadata ? domain)
{ networking.domain = lib.mkForce metadata.domain; }) { networking.domain = lib.mkForce metadata.domain; })
]; ];
}) })
hostSharedConfig hostSharedConfig
./hosts/${host} ./hosts/${host}
]; ];
}) })
images; images');
# My several development shells for usual type of projects. This is much # My several development shells for usual type of projects. This is much
# more preferable than installing all of the packages at the system # more preferable than installing all of the packages at the system

View File

@ -8,13 +8,12 @@
# #
# Take note, any images with the "iso" format is essentially made to be # Take note, any images with the "iso" format is essentially made to be
# deployed somewhere (e.g., desktop, homelab server, VPS). # deployed somewhere (e.g., desktop, homelab server, VPS).
[ni] [ni]
system = "x86_64-linux" systems = [ "x86_64-linux" ]
format = "iso" format = "iso"
[plover] [plover]
system = "x86_64-linux" systems = [ "x86_64-linux" ]
format = "iso" format = "iso"
domain = "foodogsquared.one" domain = "foodogsquared.one"
@ -24,14 +23,20 @@ auto-rollback = true
magic-rollback = true magic-rollback = true
[void] [void]
system = "x86_64-linux" systems = [ "x86_64-linux" ]
format = "vm" format = "vm"
[bootstrap] [bootstrap]
system = "x86_64-linux" systems = [
"aarch64-linux",
"x86_64-linux",
]
format = "install-iso" format = "install-iso"
nixpkgs-channel = "nixos-unstable-small" nixpkgs-channel = "nixos-unstable-small"
[graphical-installer] [graphical-installer]
system = "x86_64-linux" systems = [
"aarch64-linux",
"x86_64-linux",
]
format = "install-iso" format = "install-iso"

View File

@ -42,4 +42,30 @@
# Our own modules. # Our own modules.
++ extraModules; ++ extraModules;
}; };
listImagesWithSystems = data:
lib.foldlAttrs
(acc: name: metadata:
let
name' = metadata.hostname or name;
in
if lib.length metadata.systems > 1 then
acc // (lib.foldl
(images: system: images // {
"${name'}-${system}" = metadata // {
_system = system;
_name = name';
};
})
{}
metadata.systems)
else
acc // {
"${name'}" = metadata // {
_system = lib.head metadata.systems;
_name = name';
};
})
{}
data;
} }

View File

@ -2,7 +2,10 @@
# of the users defined here should correspond to one of the home-manager users # of the users defined here should correspond to one of the home-manager users
# at `./users/home-manager/`. # at `./users/home-manager/`.
[foo-dogsquared] [foo-dogsquared]
system = "x86_64-linux" systems = [
"aarch64-linux",
"x86_64-linux",
]
[plover] [plover]
system = "x86_64-linux" systems = [ "x86_64-linux" ]