aboutsummaryrefslogtreecommitdiff
path: root/nix/deuxfleurs.nix
diff options
context:
space:
mode:
Diffstat (limited to 'nix/deuxfleurs.nix')
-rw-r--r--nix/deuxfleurs.nix366
1 files changed, 183 insertions, 183 deletions
diff --git a/nix/deuxfleurs.nix b/nix/deuxfleurs.nix
index ad5249d..664b949 100644
--- a/nix/deuxfleurs.nix
+++ b/nix/deuxfleurs.nix
@@ -6,128 +6,122 @@ in
with builtins;
with pkgs.lib;
{
- options.deuxfleurs =
- let wg_node = with types; submodule {
- options = {
- hostname = mkOption {
- type = str;
- description = "Host name";
- };
- site_name = mkOption {
- type = nullOr str;
- description = "Site where the node is located";
- default = null;
- };
- IP = mkOption {
- type = str;
- description = "IP Address in the Wireguard network";
- };
- publicKey = mkOption {
- type = str;
- description = "Public key";
- };
- endpoint = mkOption {
- type = nullOr str;
- default = null;
- description = "Wireguard endpoint on the public Internet";
- };
- };
- };
- in
- {
+ options.deuxfleurs = with types; {
# Parameters for individual nodes
- network_interface = mkOption {
- description = "Network interface name to configure";
- type = types.str;
- };
- lan_ip = mkOption {
- description = "IP address of this node on the local network interface";
- type = types.str;
- };
- lan_ip_prefix_length = mkOption {
- description = "Prefix length associated with lan_ip";
- type = types.int;
+ hostName = mkOption {
+ description = "Node name";
+ type = str;
};
- ipv6 = mkOption {
- description = "Public IPv6 address of this node";
- type = types.str;
- };
- ipv6_prefix_length = mkOption {
- description = "Prefix length associated with ipv6 ip";
- type = types.int;
- };
-
- cluster_ip = mkOption {
- description = "IP address of this node on the Wesher mesh network";
- type = types.str;
+ staticIPv4.address = mkOption {
+ description = "IP address (with prefix length) of this node on the local network interface";
+ type = nullOr str;
+ default = null;
};
- wireguard_port = mkOption {
- description = "Port for incoming Wireguard VPN connections";
- type = types.port;
- default = 33799;
+ staticIPv6.address = mkOption {
+ description = "Static public IPv6 address of this node";
+ type = nullOr str;
};
-
- is_raft_server = mkOption {
+ isRaftServer = mkOption {
description = "Make this node a RAFT server for the Nomad and Consul deployments";
- type = types.bool;
+ type = bool;
default = false;
};
# Parameters that generally vary between sites
- lan_default_gateway = mkOption {
+ siteName = mkOption {
+ description = "Site (availability zone) on which this node is deployed";
+ type = str;
+ };
+ staticIPv4.defaultGateway = mkOption {
description = "IPv4 address of the default route on the local network interface";
- type = types.str;
+ type = nullOr str;
+ default = null;
};
- ipv6_default_gateway = mkOption {
- description = "IPv6 address of the default IPv6 gateway for the targeted net interface";
- type = types.str;
+ staticIPv4.prefixLength = mkOption {
+ description = "IPv4 prefix length for LAN addresses, only used with static configuration";
+ type = int;
+ default = 24;
};
- site_name = mkOption {
- description = "Site (availability zone) on which this node is deployed";
- type = types.str;
+ staticIPv6.defaultGateway = mkOption {
+ description = ''
+ IPv6 address of the default route on the local network interface.
+ IPv6 Router Advertisements (RA) will be totally disabled if this is set.
+ '';
+ type = nullOr str;
+ default = null;
};
- public_ipv4 = mkOption {
+ staticIPv6.prefixLength = mkOption {
+ description = "IPv6 prefix length, used only when router advertisements are disabled.";
+ type = int;
+ default = 64;
+ };
+
+ publicIPv4 = mkOption {
description = "Public IPv4 through which this node is accessible (possibly after port opening using DiploNAT), for domain names that are updated by D53";
- type = types.nullOr types.str;
+ type = nullOr str;
default = null;
};
- cname_target = mkOption {
+ cnameTarget = mkOption {
description = "DNS CNAME target to use for services hosted in this site, for domain names that are updated by D53";
- type = types.nullOr types.str;
+ type = nullOr str;
default = null;
};
- nameservers = mkOption {
- description = "External DNS servers to use";
- type = types.listOf types.str;
- };
# Parameters common to all nodes
- cluster_name = mkOption {
+ clusterName = mkOption {
description = "Name of this Deuxfleurs deployment";
- type = types.str;
+ type = str;
};
- cluster_prefix = mkOption {
- description = "IP address prefix for the Wireguard overlay network";
- type = types.str;
+ clusterPrefix = mkOption {
+ description = "IP address prefix (and length) for the Wireguard overlay network";
+ type = str;
};
- cluster_prefix_length = mkOption {
- description = "IP address prefix length for the Wireguard overlay network";
- type = types.int;
- default = 16;
- };
- cluster_nodes = mkOption {
+ clusterNodes = mkOption {
description = "Nodes that are part of the cluster";
- type = types.listOf wg_node;
+ type = attrsOf (submodule {
+ options = {
+ siteName = mkOption {
+ type = nullOr str;
+ description = "Site where the node is located";
+ default = null;
+ };
+ address = mkOption {
+ type = str;
+ description = "IP Address in the Wireguard network";
+ };
+ publicKey = mkOption {
+ type = str;
+ description = "Public key";
+ };
+ endpoint = mkOption {
+ type = nullOr str;
+ default = null;
+ description = "Wireguard endpoint on the public Internet";
+ };
+ };
+ });
};
- admin_accounts = mkOption {
+ adminAccounts = mkOption {
description = "List of users having an admin account on cluster nodes, maps user names to a list of authorized SSH keys";
- type = types.attrsOf (types.listOf types.str);
+ type = attrsOf (listOf str);
};
bootstrap = mkOption {
description = "Whether to enable bootstrapping for Nomad and Consul";
- type = types.bool;
+ type = bool;
default = false;
};
+
+ # Options that generally stay to their default value
+ wireguardPort = mkOption {
+ description = "Port for incoming Wireguard VPN connections";
+ type = port;
+ default = 33799;
+ };
+ wgautomeshPort = mkOption {
+ description = "Gossip port for wgautomesh";
+ type = port;
+ default = 1666;
+ };
};
imports = [
@@ -135,69 +129,92 @@ in
];
config =
- let node_meta = {
- "site" = cfg.site_name;
- "public_ipv6" = cfg.ipv6;
+ let
+ clusterNodeCfg = getAttr cfg.hostName cfg.clusterNodes;
+ clusterAddress = clusterNodeCfg.address;
+ node_meta = {
+ "site" = cfg.siteName;
} //
- (if cfg.public_ipv4 != null
- then { "public_ipv4" = cfg.public_ipv4; }
+ (if cfg.staticIPv6.address != null
+ then { "public_ipv6" = cfg.staticIPv6.address; }
else {}) //
- (if cfg.cname_target != null
- then { "cname_target" = cfg.cname_target; }
+ (if cfg.publicIPv4 != null
+ then { "public_ipv4" = cfg.publicIPv4; }
+ else {}) //
+ (if cfg.cnameTarget != null
+ then { "cname_target" = cfg.cnameTarget; }
else {});
in
{
+ networking.hostName = cfg.hostName;
# Configure admin accounts on all nodes
- users.users = builtins.mapAttrs (name: publicKeys: {
+ users.users = mapAttrs (name: publicKeys: {
isNormalUser = true;
extraGroups = [ "wheel" ];
openssh.authorizedKeys.keys = publicKeys;
- }) cfg.admin_accounts;
+ }) cfg.adminAccounts;
# Configure network interfaces
networking.useDHCP = false;
networking.useNetworkd = true;
- systemd.network.networks = {
- "10-uplink" = {
- matchConfig = {
- # We could preprend "en* eth*" to match all ethernet interfaces
- Name = "${cfg.network_interface}";
- };
- networkConfig = {
- IPv6AcceptRA = false;
- LinkLocalAddressing = "no";
- };
- address = [
- "${cfg.lan_ip}/${toString cfg.lan_ip_prefix_length}"
- "${cfg.ipv6}/${toString cfg.ipv6_prefix_length}"
- ];
- routes = [
- {
+ systemd.network.networks."10-uplink" =
+ let
+ # IPv4 configuration is obtained by DHCP by default,
+ # unless a static v4 address and default gateway are given
+ noDHCP = cfg.staticIPv4.address != null && cfg.staticIPv4.defaultGateway != null;
+ # IPv6 configuration is obtained through router advertisements (RA),
+ # possibly using a static token to ensure a static IPv6,
+ # unless a static v6 address and default gateway are given,
+ # in which case RAs are disabled entirely
+ noRA = cfg.staticIPv6.address != null && cfg.staticIPv6.defaultGateway != null;
+ staticV6 = cfg.staticIPv6.address != null;
+ in
+ {
+ matchConfig.Name = "en* eth*";
+
+ address =
+ optional noDHCP "${cfg.staticIPv4.address}/${toString cfg.staticIPv4.prefixLength}"
+ ++ optional noRA "${cfg.staticIPv6.address}/${toString cfg.staticIPv6.prefixLength}";
+
+ routes =
+ optional noDHCP {
routeConfig = {
- Gateway = cfg.lan_default_gateway;
+ Gateway = cfg.staticIPv4.defaultGateway;
# GatewayOnLink - Takes a boolean. If set to true, the kernel does not have to check if the gateway is reachable directly by the current machine (i.e., attached to the local network), so that we can insert the route in the kernel table without it being complained about. Defaults to "no".
GatewayOnLink = true;
};
- }
- {
+ } ++ optional noRA {
routeConfig = {
- Gateway = cfg.ipv6_default_gateway;
+ Gateway = cfg.staticIPv6.defaultGateway;
GatewayOnLink = true;
};
- }
- ];
+ };
+
+ # Dynamic IPv4: enable DHCP but not for DNS servers
+ networkConfig.DHCP = mkIf (!noDHCP) "ipv4";
+ dhcpV4Config.UseDNS = mkIf (!noDHCP) false;
+
+ # Dynamic IPv6: only fetch default route, use static
+ # address and no DNS servers
+ ipv6AcceptRAConfig.Token = mkIf (!noRA && staticV6) "static:${cfg.staticIPv6.address}";
+ ipv6AcceptRAConfig.UseDNS = mkIf (!noRA) false;
+
+ # Static IPv6: disable all router advertisements and
+ # link-local addresses
+ networkConfig.IPv6AcceptRA = mkIf noRA false;
+ networkConfig.LinkLocalAddressing = mkIf noRA "no";
};
- };
- # Configure Unbound DNS to redirect to Consul queries under .consul
- # and to pass directly to public DNS resolver all others
+ # Configure Unbound as a central DNS server for everything
+ # - is its own recursor (applies DNSSec) for everything,
+ # no need to declare an outside nameserver
+ # - redirects to Consul queries under .consul
services.unbound = {
enable = true;
- enableRootTrustAnchor = false; # disable DNSSEC as it causes issues
settings = {
server = {
- interface = [ "127.0.0.1" "${cfg.lan_ip}" "172.17.0.1" ];
+ interface = [ "127.0.0.1" "172.17.0.1" ];
domain-insecure = [ "consul." ];
local-zone = [ "consul. nodefault" ];
log-servfail = true;
@@ -207,73 +224,58 @@ in
logfile = "/dev/stdout";
access-control = [
"127.0.0.0/8 allow"
- "${cfg.lan_ip}/${toString cfg.lan_ip_prefix_length} allow"
"172.17.0.0/16 allow"
- "10.83.0.0/16 allow"
+ "192.168.0.0/16 allow"
+ "${cfg.clusterPrefix} allow"
];
};
- forward-zone = [
+ stub-zone = [
# Forward .consul queries to Consul daemon
{
name = "consul.";
- forward-addr = "${cfg.lan_ip}@8600";
- forward-no-cache = true;
- forward-tcp-upstream = false;
- forward-tls-upstream = false;
- }
- # Forward all queries to our ISP's nameserver
- {
- name = ".";
- forward-addr = cfg.nameservers;
- forward-first = true;
+ stub-addr = "${clusterAddress}@8600";
+ stub-no-cache = true;
+ stub-tcp-upstream = false;
+ stub-tls-upstream = false;
}
];
};
resolveLocalQueries = true;
};
- # Reach Unbound through the IP of our LAN interface,
- # instead of 127.0.0.1 (this will also work in Docker containers)
- networking.nameservers = [ # TODO remove this ?
- cfg.lan_ip
- ];
services.resolved.enable = false;
# Configure Wireguard VPN between all nodes
networking.wireguard.interfaces.wg0 = {
- ips = [ "${cfg.cluster_ip}/16" ];
- listenPort = cfg.wireguard_port;
+ ips = [ "${clusterAddress}/16" ];
+ listenPort = cfg.wireguardPort;
privateKeyFile = "/var/lib/deuxfleurs/wireguard-keys/private";
mtu = 1420;
};
services.wgautomesh = {
enable = true;
interface = "wg0";
- gossipPort = 1666;
+ gossipPort = cfg.wgautomeshPort;
gossipSecretFile = "/var/lib/wgautomesh/gossip_secret";
persistFile = "/var/lib/wgautomesh/state";
- upnpForwardPublicPort =
- let
- us = filter ({ hostname, ...}: hostname == config.networking.hostName) cfg.cluster_nodes;
- in
- if length us > 0 && (head us).endpoint != null then
- strings.toInt (lists.last (split ":" (head us).endpoint))
+ upnpForwardPublicPort =
+ if clusterNodeCfg.endpoint != null then
+ strings.toInt (lists.last (split ":" clusterNodeCfg.endpoint))
else null;
- peers = map ({ publicKey, endpoint, IP, ... }: {
- address = IP;
+ peers = attrValues (mapAttrs (hostname: { publicKey, endpoint, address, ... }: {
+ inherit address endpoint;
pubkey = publicKey;
- endpoint = endpoint;
- }) cfg.cluster_nodes;
+ }) cfg.clusterNodes);
};
# Old code for wg-quick, we can use this as a fallback if we fail to make wgautomesh work
# systemd.services."wg-quick-wg0".after = [ "unbound.service" ];
# networking.wg-quick.interfaces.wg0 = {
- # address = [ "${cfg.cluster_ip}/16" ];
- # listenPort = cfg.wireguard_port;
+ # address = [ "${clusterAddress}/16" ];
+ # listenPort = cfg.wireguardPort;
# privateKeyFile = "/var/lib/deuxfleurs/wireguard-keys/private";
# mtu = 1420;
- # peers = map ({ publicKey, endpoint, IP, ... }: {
+ # peers = map ({ publicKey, endpoint, address, ... }: {
# inherit publicKey endpoint;
- # allowedIPs = [ "${IP}/32" ];
+ # allowedIPs = [ "${address}/32" ];
# persistentKeepalive = 25;
# };
@@ -287,25 +289,25 @@ in
'';
# Configure /etc/hosts to link all hostnames to their Wireguard IP
- networking.extraHosts = builtins.concatStringsSep "\n" (map
- ({ hostname, IP, ...}: "${IP} ${hostname}")
- cfg.cluster_nodes);
+ networking.extraHosts = concatStringsSep "\n" (attrValues (mapAttrs
+ (hostname: { address, ...}: "${address} ${hostname}")
+ cfg.clusterNodes));
# Enable Hashicorp Consul & Nomad
services.consul.enable = true;
systemd.services.consul.after = [ "wg-quick-wg0.service" ];
services.consul.extraConfig =
- (if cfg.is_raft_server
+ (if cfg.isRaftServer
then { server = true; }
// (if cfg.bootstrap then { bootstrap_expect = 3; } else {})
else {}) //
{
inherit node_meta;
- datacenter = cfg.cluster_name;
+ datacenter = cfg.clusterName;
ui_config = {
enabled = true;
};
- bind_addr = "${cfg.cluster_ip}";
+ bind_addr = "${clusterAddress}";
addresses = {
https = "0.0.0.0";
@@ -326,9 +328,6 @@ in
verify_outgoing = true;
verify_server_hostname = true;
};
- systemd.services.consul.serviceConfig = { # TODO remove this ?
- AmbientCapabilities = "CAP_NET_BIND_SERVICE";
- };
services.nomad.enable = true;
systemd.services.nomad.after = [ "wg-quick-wg0.service" ];
@@ -338,18 +337,18 @@ in
pkgs.zstd
];
services.nomad.settings =
- (if cfg.is_raft_server
+ (if cfg.isRaftServer
then {
server = { enabled = true; }
// (if cfg.bootstrap then { bootstrap_expect = 3; } else {});
} else {}) //
{
- region = cfg.cluster_name;
- datacenter = cfg.site_name;
+ region = cfg.clusterName;
+ datacenter = cfg.siteName;
advertise = {
- rpc = "${cfg.cluster_ip}";
- http = "${cfg.cluster_ip}";
- serf = "${cfg.cluster_ip}";
+ rpc = "${clusterAddress}";
+ http = "${clusterAddress}";
+ serf = "${clusterAddress}";
};
consul = {
address = "localhost:8501";
@@ -402,31 +401,32 @@ in
allowedTCPPorts = [
# Allow anyone to connect on SSH port
- (builtins.head ({ openssh.ports = [22]; } // config.services).openssh.ports)
+ (head ({ openssh.ports = [22]; } // config.services).openssh.ports)
];
allowedUDPPorts = [
# Allow peers to connect to Wireguard
- cfg.wireguard_port
+ cfg.wireguardPort
];
# Allow specific hosts access to specific things in the cluster
extraCommands = ''
- # Allow everything from router (usefull for UPnP/IGD)
- iptables -A INPUT -s ${cfg.lan_default_gateway} -j ACCEPT
+ # Allow UDP packets comming from port 1900 from a local address,
+ # these are necessary for UPnP/IGD
+ iptables -A INPUT -s 192.168.0.0/16 -p udp --sport 1900 -j ACCEPT
# Allow docker containers to access all ports
iptables -A INPUT -s 172.17.0.0/16 -j ACCEPT
# Allow other nodes on VPN to access all ports
- iptables -A INPUT -s ${cfg.cluster_prefix}/${toString cfg.cluster_prefix_length} -j ACCEPT
+ iptables -A INPUT -s ${cfg.clusterPrefix} -j ACCEPT
'';
# When stopping firewall, delete all rules that were configured manually above
extraStopCommands = ''
- iptables -D INPUT -s ${cfg.lan_default_gateway} -j ACCEPT
+ iptables -D INPUT -s 192.168.0.0/16 -p udp --sport 1900 -j ACCEPT
iptables -D INPUT -s 172.17.0.0/16 -j ACCEPT
- iptables -D INPUT -s ${cfg.cluster_prefix}/${toString cfg.cluster_prefix_length} -j ACCEPT
+ iptables -D INPUT -s ${cfg.clusterPrefix} -j ACCEPT
'';
};
};