nix-config/lib/default.nix

198 lines
5.3 KiB
Nix

# This module is for use by all C3D2 machines.
# That includes physical servers, VMs, containers, and personal machines.
#
{ config, lib, ... }:
let
hqPrefix64 = "fd23:42:c3d2:523";
# TODO: Is this stable? Is there a better place to specifiy this?
# Generate a deterministic IPv6 address for a 64 bit prefix
# and seed string. Prefix must not contain trailing ':'.
toIpv6Address = prefix64: seed:
with builtins;
let
digest = builtins.hashString "sha256" seed;
hextets = map (i: substring (4 * i) 4 digest) [ 0 1 2 3 ];
in concatStringsSep ":" ([ prefix64 ] ++ hextets);
# Generate a deterministic public IPv6 addresses
# for the HQ networking using a seed string.
toHqPrivateAddress = toIpv6Address hqPrefix64;
/* # Generate a deterministic public IPv6 addresses
# for the HQ networking using a seed string.
toHqPublicAddress = toIpv6Address publicPrefix64;
# Generate a deterministic public IPv6 addresses
# for the HQ networking using a seed string.
toserver7YggdrasilAddress = toIpv6Address server7YggrasilPrefix64;
*/
cfg = config.c3d2;
in {
options.c3d2 = with lib;
with lib.types; {
isInHq = mkEnableOption "HQ presence";
enableMotd = mkOption {
type = bool;
default = cfg.isInHq;
defaultText = literalExample "config.c3d2.isInHq";
};
mapPublicHosts = mkOption {
type = bool;
default = false;
description = ''
Whether to add all external HQ host mappings to /etc/hosts.
'';
};
mapHqHosts = mkOption {
type = bool;
default = false;
description = ''
Whether to add all internal HQ host mappings to /etc/hosts.
'';
};
hq = {
/* externalInterface = mkOption {
type = nullOr str;
default = null;
example = "eth0";
description = ''
Configure the given interface name with an external IP address.
'';
};
*/
interface = mkOption {
type = nullOr str;
default = null;
example = "eth0";
description = ''
Configure the given interface name with an internal IP address.
'';
};
statistics = { enable = mkEnableOption "statistics collection"; };
};
};
config = let
cfg = config.c3d2;
hostRegistry = import ../host-registry.nix;
mkIfIsInHq = lib.mkIf cfg.isInHq;
in {
assertions = [{
assertion = let
check = hostName: hostName == config.networking.hostName;
checkRegistry = list: builtins.any check list;
in cfg.isInHq -> checkRegistry hostRegistry.hqPrivate;
message = "${config.networking.hostName} is not registered in ${
toString ../host-registry.nix
}";
}];
networking.defaultGateway = mkIfIsInHq "172.22.99.4";
networking.domain = mkIfIsInHq "hq.c3d2.de";
users.motd = lib.mkIf cfg.enableMotd (builtins.readFile ./motd);
networking.hosts = let
mapHostsNamesToAttrs = f: list: builtins.listToAttrs (map f list);
/* hqPublicHosts = mapHostsNamesToAttrs (hostName: {
name = toHqPublicAddress hostName;
value = [ "${hostName}.hq.c3d2.de" hostName ];
}) hostRegistry.hqPublic;
*/
hqPrivateHosts = mapHostsNamesToAttrs (hostName: {
name = toHqPrivateAddress hostName;
value = [ "${hostName}.hq" hostName ];
}) hostRegistry.hqPrivate;
in if cfg.mapHqHosts then hqPrivateHosts else { };
networking.interfaces =
/* (if cfg.hq.externalInterface == null then
{ }
else {
"${cfg.hq.externalInterface}" = {
ipv6.addresses = [{
address = toHqPublicAddress config.networking.hostName;
prefixLength = 64;
}];
};
}) //
*/
(if cfg.hq.interface == null then
{ }
else {
"${cfg.hq.interface}" = {
ipv6.addresses = [{
address = toHqPrivateAddress config.networking.hostName;
prefixLength = 64;
}];
};
});
programs.ssh.knownHosts = with builtins;
let
hostNames = hostRegistry.hqPrivate;
intersectKeys = intersectAttrs {
publicKey = null;
publicKeyFile = null;
};
list = map (name:
let sshAttrs = intersectKeys (getAttr name hostRegistry.hosts);
in if sshAttrs == { } then
null
else {
inherit name;
value = {
publicKey = null;
publicKeyFile = null;
hostNames = [
(toHqPrivateAddress name)
"${name}.hq.c3d2.de"
"${name}.hq"
name
];
} // sshAttrs;
}) hostNames;
keyedHosts = filter (x: x != null) list;
in listToAttrs keyedHosts;
services.collectd = lib.mkIf cfg.hq.statistics.enable {
enable = true;
autoLoadPlugin = true;
extraConfig = ''
HostName "${config.networking.hostName}"
FQDNLookup false
Interval 10
LoadPlugin network
<Plugin "network">
Server "grafana.hq" "25826"
</Plugin>
'';
};
};
meta.maintainers = with lib.maintainers; [ ehmry ];
}