cluster: init

This commit is contained in:
Max Headroom 2022-06-23 20:13:28 +02:00
parent f8784da880
commit cf1dd21418
10 changed files with 117 additions and 1 deletions

19
cluster/default.nix Normal file
View file

@ -0,0 +1,19 @@
{ lib, hostName }:
lib.evalModules {
modules = [
# Arbitrary variables to reference across multiple services
./lib/vars
{ vars = { inherit hostName; }; }
# Cluster-level port-magic
../modules/port-magic
../tools/inject.nix
./lib/load-hosts.nix
./lib/services.nix
./lib/inject-nixos-config.nix
./import-services.nix
];
}

View file

@ -0,0 +1,9 @@
{ lib, ... }:
let
svcs' = builtins.readDir ./services;
svcs = lib.filterAttrs (_: type: type == "directory") svcs';
loadService = ent: import ./services/${ent};
in {
imports = map loadService (builtins.attrNames svcs);
}

15
cluster/inject.nix Normal file
View file

@ -0,0 +1,15 @@
hostName:
{ lib, ... }:
let
cluster = import ./. { inherit lib hostName; };
in
{
_module.args.cluster = {
inherit (cluster.config) vars;
inherit (cluster.config.vars) hosts;
inherit (cluster) config;
};
imports = cluster.config.out.injectedNixosConfig;
}

View file

@ -0,0 +1,10 @@
{ config, lib, ... }:
with lib;
{
options.out.injectedNixosConfig = mkOption {
description = "NixOS configuration modules to inject into the host.";
type = with types; listOf anything;
default = {};
};
}

View file

@ -0,0 +1,9 @@
{ config, lib, ... }:
let
hosts = import ../../hosts;
self = hosts.${config.vars.hostName};
others = lib.filterAttrs (_: host: host != self) hosts;
in
{
config.vars.hosts = hosts // { inherit self others; };
}

View file

@ -0,0 +1,26 @@
{ name, config, lib, ... }:
with lib;
{
options = {
nodes = mkOption {
description = ''
Groups of worker machines to run this service on.
Allows for arbitrary multi-node constructs, such as:
* 1 master, N workers
* N masters, M workers
* N nodes
* 1 node
* X evaluators, Y smallBuilders, Z bigBuilders
etc.
'';
type = with types; attrsOf (oneOf [ str (listOf str) ]);
default = [];
};
nixos = mkOption {
description = "NixOS configurations per node group.";
type = with types; attrs;
default = {};
};
};
}

19
cluster/lib/services.nix Normal file
View file

@ -0,0 +1,19 @@
{ config, lib, ... }:
with lib;
let
getHostConfigurations = svcConfig: hostName:
lib.mapAttrsToList (groupName: _: svcConfig.nixos.${groupName})
(lib.filterAttrs (_: v: lib.elem hostName v) svcConfig.nodes);
getServiceConfigurations = svcConfig: getHostConfigurations svcConfig config.vars.hostName;
in
{
options.services = mkOption {
description = "Cluster services.";
type = with types; attrsOf (submodule (import ./service-module.nix ));
default = {};
};
config.out.injectedNixosConfig = lib.flatten (lib.mapAttrsToList (_: getServiceConfigurations) config.services);
}

View file

@ -0,0 +1,9 @@
{ lib, ... }:
with lib;
{
options.vars = mkOption {
description = "Miscellaneous variables.";
type = types.attrs;
default = {};
};
}

0
cluster/services/.keep Normal file
View file

View file

@ -36,7 +36,7 @@
mkNixOS' = lib: name: let host = hosts.${name}; in lib.nixosSystem {
inherit specialArgs;
system = "${host.arch}-linux";
modules = [ host.nixos ./tools/inject.nix ];
modules = [ host.nixos ./tools/inject.nix (import ./cluster/inject.nix name) ];
};
mkNixOS = mkNixOS' lib;