cluster: init
This commit is contained in:
parent
f8784da880
commit
cf1dd21418
10 changed files with 117 additions and 1 deletions
19
cluster/default.nix
Normal file
19
cluster/default.nix
Normal 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
|
||||||
|
];
|
||||||
|
}
|
9
cluster/import-services.nix
Normal file
9
cluster/import-services.nix
Normal 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
15
cluster/inject.nix
Normal 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;
|
||||||
|
}
|
10
cluster/lib/inject-nixos-config.nix
Normal file
10
cluster/lib/inject-nixos-config.nix
Normal 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 = {};
|
||||||
|
};
|
||||||
|
}
|
9
cluster/lib/load-hosts.nix
Normal file
9
cluster/lib/load-hosts.nix
Normal 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; };
|
||||||
|
}
|
26
cluster/lib/service-module.nix
Normal file
26
cluster/lib/service-module.nix
Normal 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
19
cluster/lib/services.nix
Normal 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);
|
||||||
|
}
|
9
cluster/lib/vars/default.nix
Normal file
9
cluster/lib/vars/default.nix
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
{ lib, ... }:
|
||||||
|
with lib;
|
||||||
|
{
|
||||||
|
options.vars = mkOption {
|
||||||
|
description = "Miscellaneous variables.";
|
||||||
|
type = types.attrs;
|
||||||
|
default = {};
|
||||||
|
};
|
||||||
|
}
|
0
cluster/services/.keep
Normal file
0
cluster/services/.keep
Normal file
|
@ -36,7 +36,7 @@
|
||||||
mkNixOS' = lib: name: let host = hosts.${name}; in lib.nixosSystem {
|
mkNixOS' = lib: name: let host = hosts.${name}; in lib.nixosSystem {
|
||||||
inherit specialArgs;
|
inherit specialArgs;
|
||||||
system = "${host.arch}-linux";
|
system = "${host.arch}-linux";
|
||||||
modules = [ host.nixos ./tools/inject.nix ];
|
modules = [ host.nixos ./tools/inject.nix (import ./cluster/inject.nix name) ];
|
||||||
};
|
};
|
||||||
mkNixOS = mkNixOS' lib;
|
mkNixOS = mkNixOS' lib;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue