2022-02-09 11:09:49 +00:00
{ config , pkgs , . . . }:
let
cfg = config . deuxfleurs ;
in
with builtins ;
with pkgs . lib ;
{
2023-03-24 14:26:39 +00:00
options . deuxfleurs = with types ; {
2022-02-25 16:52:17 +00:00
# Parameters for individual nodes
2023-03-24 14:26:39 +00:00
hostName = mkOption {
description = " N o d e n a m e " ;
type = str ;
} ;
ipv6Address = mkOption {
2023-03-24 13:32:39 +00:00
description = " S t a t i c p u b l i c I P v 6 a d d r e s s o f t h i s n o d e " ;
2023-03-24 14:26:39 +00:00
type = str ;
2022-04-20 16:04:57 +00:00
} ;
2023-03-24 13:32:39 +00:00
staticIPv4 . address = mkOption {
description = " I P a d d r e s s ( w i t h p r e f i x l e n g t h ) o f t h i s n o d e o n t h e l o c a l n e t w o r k i n t e r f a c e " ;
2023-03-24 14:26:39 +00:00
type = nullOr str ;
2023-03-24 13:32:39 +00:00
default = null ;
2022-04-20 16:04:57 +00:00
} ;
2022-02-25 16:52:17 +00:00
2023-03-24 14:26:39 +00:00
isRaftServer = mkOption {
2022-02-09 11:09:49 +00:00
description = " M a k e t h i s n o d e a R A F T s e r v e r f o r t h e N o m a d a n d C o n s u l d e p l o y m e n t s " ;
2023-03-24 14:26:39 +00:00
type = bool ;
2022-02-09 11:09:49 +00:00
default = false ;
} ;
2022-02-25 16:52:17 +00:00
# Parameters that generally vary between sites
2023-03-24 14:26:39 +00:00
siteName = mkOption {
2022-02-25 16:52:17 +00:00
description = " S i t e ( a v a i l a b i l i t y z o n e ) o n w h i c h t h i s n o d e i s d e p l o y e d " ;
2023-03-24 14:26:39 +00:00
type = str ;
2022-02-25 16:52:17 +00:00
} ;
2023-03-24 13:32:39 +00:00
staticIPv4 . defaultGateway = mkOption {
description = " I P v 4 a d d r e s s o f t h e d e f a u l t r o u t e o n t h e l o c a l n e t w o r k i n t e r f a c e " ;
2023-03-24 14:26:39 +00:00
type = nullOr str ;
2023-03-24 13:32:39 +00:00
default = null ;
} ;
2023-03-24 14:26:39 +00:00
publicIPv4 = mkOption {
2022-12-07 16:13:03 +00:00
description = " P u b l i c I P v 4 t h r o u g h w h i c h t h i s n o d e i s a c c e s s i b l e ( p o s s i b l y a f t e r p o r t o p e n i n g u s i n g D i p l o N A T ) , f o r d o m a i n n a m e s t h a t a r e u p d a t e d b y D 5 3 " ;
2023-03-24 14:26:39 +00:00
type = nullOr str ;
2022-12-07 16:13:03 +00:00
default = null ;
} ;
2023-03-24 14:26:39 +00:00
cnameTarget = mkOption {
2022-12-07 16:13:03 +00:00
description = " D N S C N A M E t a r g e t t o u s e f o r s e r v i c e s h o s t e d i n t h i s s i t e , f o r d o m a i n n a m e s t h a t a r e u p d a t e d b y D 5 3 " ;
2023-03-24 14:26:39 +00:00
type = nullOr str ;
2022-12-07 16:13:03 +00:00
default = null ;
2022-12-07 11:32:21 +00:00
} ;
2022-02-25 16:52:17 +00:00
2022-02-09 11:09:49 +00:00
# Parameters common to all nodes
2023-03-24 14:26:39 +00:00
clusterName = mkOption {
2022-02-09 11:09:49 +00:00
description = " N a m e o f t h i s D e u x f l e u r s d e p l o y m e n t " ;
2023-03-24 14:26:39 +00:00
type = str ;
2022-02-09 11:09:49 +00:00
} ;
2023-03-24 14:26:39 +00:00
clusterPrefix = mkOption {
description = " I P a d d r e s s p r e f i x ( a n d l e n g t h ) f o r t h e W i r e g u a r d o v e r l a y n e t w o r k " ;
type = str ;
2022-08-23 21:55:15 +00:00
} ;
2023-03-24 14:26:39 +00:00
clusterNodes = mkOption {
2022-08-23 21:55:15 +00:00
description = " N o d e s t h a t a r e p a r t o f t h e c l u s t e r " ;
2023-03-24 14:26:39 +00:00
type = attrsOf ( submodule {
options = {
siteName = mkOption {
type = nullOr str ;
description = " S i t e w h e r e t h e n o d e i s l o c a t e d " ;
default = null ;
} ;
address = mkOption {
type = str ;
description = " I P A d d r e s s i n t h e W i r e g u a r d n e t w o r k " ;
} ;
publicKey = mkOption {
type = str ;
description = " P u b l i c k e y " ;
} ;
endpoint = mkOption {
type = nullOr str ;
default = null ;
description = " W i r e g u a r d e n d p o i n t o n t h e p u b l i c I n t e r n e t " ;
} ;
} ;
} ) ;
2022-08-23 21:55:15 +00:00
} ;
2023-03-24 14:26:39 +00:00
adminAccounts = mkOption {
2022-02-09 11:09:49 +00:00
description = " L i s t o f u s e r s h a v i n g a n a d m i n a c c o u n t o n c l u s t e r n o d e s , m a p s u s e r n a m e s t o a l i s t o f a u t h o r i z e d S S H k e y s " ;
2023-03-24 14:26:39 +00:00
type = attrsOf ( listOf str ) ;
2022-02-09 11:09:49 +00:00
} ;
2022-08-24 12:23:17 +00:00
bootstrap = mkOption {
description = " W h e t h e r t o e n a b l e b o o t s t r a p p i n g f o r N o m a d a n d C o n s u l " ;
2023-03-24 14:26:39 +00:00
type = bool ;
2022-08-24 12:23:17 +00:00
default = false ;
} ;
2023-03-24 13:32:39 +00:00
# Options that generally stay to their default value
wireguardPort = mkOption {
description = " P o r t f o r i n c o m i n g W i r e g u a r d V P N c o n n e c t i o n s " ;
2023-03-24 14:26:39 +00:00
type = port ;
2023-03-24 13:32:39 +00:00
default = 33799 ;
} ;
2022-02-09 11:09:49 +00:00
} ;
2023-03-09 14:31:05 +00:00
imports = [
./wgautomesh.nix
] ;
2022-12-07 11:32:21 +00:00
config =
2023-03-24 14:26:39 +00:00
let
clusterNodeCfg = getAttr cfg . hostName cfg . clusterNodes ;
clusterAddress = clusterNodeCfg . address ;
node_meta = {
" s i t e " = cfg . siteName ;
" p u b l i c _ i p v 6 " = cfg . ipv6Address ;
2022-12-07 11:32:21 +00:00
} //
2023-03-24 14:26:39 +00:00
( if cfg . publicIPv4 != null
then { " p u b l i c _ i p v 4 " = cfg . publicIPv4 ; }
2022-12-07 16:13:03 +00:00
else { } ) //
2023-03-24 14:26:39 +00:00
( if cfg . cnameTarget != null
then { " c n a m e _ t a r g e t " = cfg . cnameTarget ; }
2022-12-07 11:32:21 +00:00
else { } ) ;
in
{
2023-03-24 14:26:39 +00:00
networking . hostName = cfg . hostName ;
2023-03-09 14:31:05 +00:00
2022-02-09 11:09:49 +00:00
# Configure admin accounts on all nodes
2023-03-24 14:26:39 +00:00
users . users = mapAttrs ( name : publicKeys : {
2022-02-09 11:09:49 +00:00
isNormalUser = true ;
extraGroups = [ " w h e e l " ] ;
openssh . authorizedKeys . keys = publicKeys ;
2023-03-24 14:26:39 +00:00
} ) cfg . adminAccounts ;
2022-02-09 11:09:49 +00:00
2022-02-25 16:52:17 +00:00
# Configure network interfaces
2022-10-15 08:38:48 +00:00
networking . useDHCP = false ;
networking . useNetworkd = true ;
systemd . network . networks = {
" 1 0 - u p l i n k " = {
matchConfig = {
2023-03-24 13:32:39 +00:00
Name = " e n * e t h * " ;
2022-05-08 22:20:02 +00:00
} ;
2023-03-24 13:32:39 +00:00
ipv6AcceptRAConfig = {
2023-03-24 14:26:39 +00:00
Token = " s t a t i c : ${ cfg . ipv6Address } " ;
2023-03-24 13:32:39 +00:00
UseDNS = false ;
} ;
} // ( if cfg . staticIPv4 . address == null || cfg . staticIPv4 . defaultGateway == null then {
2022-10-15 08:38:48 +00:00
networkConfig = {
2023-03-24 13:32:39 +00:00
DHCP = " i p v 4 " ;
} ;
dhcpV4Config = {
UseDNS = false ;
2022-10-15 08:38:48 +00:00
} ;
2023-03-24 13:32:39 +00:00
} else {
2022-10-15 08:38:48 +00:00
address = [
2023-03-24 13:32:39 +00:00
" ${ cfg . staticIPv4 . address } "
2022-10-15 08:38:48 +00:00
] ;
routes = [
{
routeConfig = {
2023-03-24 13:32:39 +00:00
Gateway = cfg . staticIPv4 . defaultGateway ;
2022-10-15 08:38:48 +00:00
# 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 ;
} ;
}
2022-05-08 22:20:02 +00:00
] ;
2023-03-24 13:32:39 +00:00
} ) ;
2022-08-24 09:06:55 +00:00
} ;
2023-03-24 11:58:44 +00:00
# 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
2022-08-30 13:52:42 +00:00
services . unbound = {
enable = true ;
settings = {
server = {
2023-03-24 11:58:44 +00:00
interface = [ " 1 2 7 . 0 . 0 . 1 " " 1 7 2 . 1 7 . 0 . 1 " ] ;
2022-08-30 13:52:42 +00:00
domain-insecure = [ " c o n s u l . " ] ;
local-zone = [ " c o n s u l . n o d e f a u l t " ] ;
log-servfail = true ;
access-control = [
" 1 2 7 . 0 . 0 . 0 / 8 a l l o w "
" 1 7 2 . 1 7 . 0 . 0 / 1 6 a l l o w "
] ;
} ;
2023-03-24 11:58:44 +00:00
stub-zone = [
2022-08-30 13:52:42 +00:00
# Forward .consul queries to Consul daemon
{
name = " c o n s u l . " ;
2023-03-24 14:26:39 +00:00
stub-addr = " ${ clusterAddress } @ 8 6 0 0 " ;
2023-03-24 11:58:44 +00:00
stub-no-cache = true ;
stub-tcp-upstream = false ;
stub-tls-upstream = false ;
2022-08-30 13:52:42 +00:00
}
] ;
} ;
2022-10-16 17:36:15 +00:00
resolveLocalQueries = true ;
2022-08-30 13:52:42 +00:00
} ;
2022-10-16 17:36:15 +00:00
services . resolved . enable = false ;
2022-06-01 12:46:00 +00:00
2022-08-23 21:55:15 +00:00
# Configure Wireguard VPN between all nodes
2023-03-09 14:31:05 +00:00
networking . wireguard . interfaces . wg0 = {
2023-03-24 14:26:39 +00:00
ips = [ " ${ clusterAddress } / 1 6 " ] ;
2023-03-24 13:32:39 +00:00
listenPort = cfg . wireguardPort ;
2022-08-23 21:55:15 +00:00
privateKeyFile = " / v a r / l i b / d e u x f l e u r s / w i r e g u a r d - k e y s / p r i v a t e " ;
2022-11-22 15:22:05 +00:00
mtu = 1420 ;
2023-03-09 14:31:05 +00:00
} ;
services . wgautomesh = {
enable = true ;
interface = " w g 0 " ;
gossipPort = 1666 ;
2023-03-09 15:39:58 +00:00
gossipSecretFile = " / v a r / l i b / w g a u t o m e s h / g o s s i p _ s e c r e t " ;
2023-03-09 16:06:57 +00:00
persistFile = " / v a r / l i b / w g a u t o m e s h / s t a t e " ;
2023-03-09 14:31:05 +00:00
upnpForwardPublicPort =
2023-03-24 14:26:39 +00:00
if clusterNodeCfg . endpoint != null then
strings . toInt ( lists . last ( split " : " clusterNodeCfg . endpoint ) )
2023-03-09 14:31:05 +00:00
else null ;
2023-03-24 14:26:39 +00:00
peers = attrValues ( mapAttrs ( hostname : { publicKey , endpoint , address , . . . }: {
inherit address endpoint ;
2023-03-09 14:31:05 +00:00
pubkey = publicKey ;
2023-03-24 14:26:39 +00:00
} ) cfg . clusterNodes ) ;
2022-02-09 11:09:49 +00:00
} ;
2023-03-17 17:18:25 +00:00
# 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 = {
2023-03-24 14:26:39 +00:00
# address = [ "${clusterAddress}/16" ];
2023-03-24 13:32:39 +00:00
# listenPort = cfg.wireguardPort;
2023-03-17 17:18:25 +00:00
# privateKeyFile = "/var/lib/deuxfleurs/wireguard-keys/private";
# mtu = 1420;
2023-03-24 14:26:39 +00:00
# peers = map ({ publicKey, endpoint, address, ... }: {
2023-03-17 17:18:25 +00:00
# inherit publicKey endpoint;
2023-03-24 14:26:39 +00:00
# allowedIPs = [ "${address}/32" ];
2023-03-17 17:18:25 +00:00
# persistentKeepalive = 25;
# };
2022-02-09 11:09:49 +00:00
2022-12-14 17:02:30 +00:00
system . activationScripts . generate_df_wg_key = ''
if [ ! - f /var/lib/deuxfleurs/wireguard-keys/private ] ; then
mkdir - p /var/lib/deuxfleurs/wireguard-keys
( umask 077 ; $ { pkgs . wireguard-tools } /bin/wg genkey > /var/lib/deuxfleurs/wireguard-keys/private )
echo " N e w W i r e g u a r d k e y w a s g e n e r a t e d . "
echo " T h i s n o d e ' s W i r e g u a r d p u b l i c k e y i s : $ ( ${ pkgs . wireguard-tools } / b i n / w g p u b k e y < / v a r / l i b / d e u x f l e u r s / w i r e g u a r d - k e y s / p r i v a t e ) "
fi
'' ;
2022-02-09 11:09:49 +00:00
# Configure /etc/hosts to link all hostnames to their Wireguard IP
2023-03-24 14:26:39 +00:00
networking . extraHosts = concatStringsSep " \n " ( attrValues ( mapAttrs
( hostname : { address , . . . }: " ${ address } ${ hostname } " )
cfg . clusterNodes ) ) ;
2022-02-09 11:09:49 +00:00
# Enable Hashicorp Consul & Nomad
services . consul . enable = true ;
2022-11-22 12:30:00 +00:00
systemd . services . consul . after = [ " w g - q u i c k - w g 0 . s e r v i c e " ] ;
2022-02-09 11:09:49 +00:00
services . consul . extraConfig =
2023-03-24 14:26:39 +00:00
( if cfg . isRaftServer
2022-08-24 12:23:17 +00:00
then { server = true ; }
// ( if cfg . bootstrap then { bootstrap_expect = 3 ; } else { } )
2022-02-09 11:09:49 +00:00
else { } ) //
{
2022-12-07 11:32:21 +00:00
inherit node_meta ;
2023-03-24 14:26:39 +00:00
datacenter = cfg . clusterName ;
2022-07-27 09:18:23 +00:00
ui_config = {
enabled = true ;
} ;
2023-03-24 14:26:39 +00:00
bind_addr = " ${ clusterAddress } " ;
2022-02-09 11:09:49 +00:00
2022-06-01 12:46:00 +00:00
addresses = {
https = " 0 . 0 . 0 . 0 " ;
dns = " 0 . 0 . 0 . 0 " ;
} ;
ports = {
http = -1 ;
https = 8501 ;
} ;
2022-08-24 16:51:55 +00:00
performance = {
rpc_hold_timeout = " 7 0 s " ;
} ;
2022-06-01 12:46:00 +00:00
2022-02-09 11:09:49 +00:00
ca_file = " / v a r / l i b / c o n s u l / p k i / c o n s u l - c a . c r t " ;
2023-01-01 18:38:28 +00:00
cert_file = " / v a r / l i b / c o n s u l / p k i / c o n s u l . c r t " ;
key_file = " / v a r / l i b / c o n s u l / p k i / c o n s u l . k e y " ;
2022-02-09 11:09:49 +00:00
verify_incoming = true ;
verify_outgoing = true ;
verify_server_hostname = true ;
} ;
services . nomad . enable = true ;
2022-11-22 12:30:00 +00:00
systemd . services . nomad . after = [ " w g - q u i c k - w g 0 . s e r v i c e " ] ;
2023-03-24 11:01:38 +00:00
services . nomad . package = pkgs . nomad_1_4 ;
2022-05-30 12:57:05 +00:00
services . nomad . extraPackages = [
pkgs . glibc
pkgs . zstd
] ;
2022-02-09 11:09:49 +00:00
services . nomad . settings =
2023-03-24 14:26:39 +00:00
( if cfg . isRaftServer
2022-08-24 12:23:17 +00:00
then {
server = { enabled = true ; }
// ( if cfg . bootstrap then { bootstrap_expect = 3 ; } else { } ) ;
} else { } ) //
2022-02-09 11:09:49 +00:00
{
2023-03-24 14:26:39 +00:00
region = cfg . clusterName ;
datacenter = cfg . siteName ;
2022-02-09 11:09:49 +00:00
advertise = {
2023-03-24 14:26:39 +00:00
rpc = " ${ clusterAddress } " ;
http = " ${ clusterAddress } " ;
serf = " ${ clusterAddress } " ;
2022-02-09 11:09:49 +00:00
} ;
consul = {
2022-08-24 16:51:55 +00:00
address = " l o c a l h o s t : 8 5 0 1 " ;
2023-01-01 18:38:28 +00:00
ca_file = " / v a r / l i b / n o m a d / p k i / c o n s u l . c r t " ;
cert_file = " / v a r / l i b / n o m a d / p k i / c o n s u l - c l i e n t . c r t " ;
key_file = " / v a r / l i b / n o m a d / p k i / c o n s u l - c l i e n t . k e y " ;
2022-02-09 11:09:49 +00:00
ssl = true ;
2022-08-24 18:03:31 +00:00
checks_use_advertise = true ;
2022-02-09 11:09:49 +00:00
} ;
client = {
enabled = true ;
network_interface = " w g 0 " ;
2022-12-07 11:32:21 +00:00
meta = node_meta ;
2022-02-09 11:09:49 +00:00
} ;
2022-10-16 16:12:57 +00:00
telemetry = {
publish_allocation_metrics = true ;
publish_node_metrics = true ;
prometheus_metrics = true ;
} ;
2022-02-09 11:09:49 +00:00
tls = {
http = true ;
rpc = true ;
ca_file = " / v a r / l i b / n o m a d / p k i / n o m a d - c a . c r t " ;
2023-01-01 18:38:28 +00:00
cert_file = " / v a r / l i b / n o m a d / p k i / n o m a d . c r t " ;
key_file = " / v a r / l i b / n o m a d / p k i / n o m a d . k e y " ;
2022-02-09 11:09:49 +00:00
verify_server_hostname = true ;
verify_https_client = true ;
} ;
plugin = [
{
docker = [
{
config = [
{
volumes . enabled = true ;
allow_privileged = true ;
}
] ;
}
] ;
}
] ;
} ;
2022-04-20 16:04:57 +00:00
# ---- Firewall config ----
# Open ports in the firewall.
networking . firewall = {
enable = true ;
allowedTCPPorts = [
2022-08-23 21:55:15 +00:00
# Allow anyone to connect on SSH port
2023-03-24 14:26:39 +00:00
( head ( { openssh . ports = [ 22 ] ; } // config . services ) . openssh . ports )
2022-04-20 16:04:57 +00:00
] ;
2022-08-23 21:55:15 +00:00
allowedUDPPorts = [
# Allow peers to connect to Wireguard
2023-03-24 13:32:39 +00:00
cfg . wireguardPort
2022-08-23 21:55:15 +00:00
] ;
2022-04-20 16:04:57 +00:00
# Allow specific hosts access to specific things in the cluster
extraCommands = ''
2023-03-24 13:32:39 +00:00
# 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
2022-04-20 16:04:57 +00:00
# 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
2023-03-24 14:26:39 +00:00
iptables - A INPUT - s $ { cfg . clusterPrefix } - j ACCEPT
2022-04-20 16:04:57 +00:00
'' ;
# When stopping firewall, delete all rules that were configured manually above
extraStopCommands = ''
2023-03-24 13:32:39 +00:00
iptables - D INPUT - s 192.168.0.0/16 - p udp - - sport 1900 - j ACCEPT
2022-04-20 16:04:57 +00:00
iptables - D INPUT - s 172.17.0.0/16 - j ACCEPT
2023-03-24 14:26:39 +00:00
iptables - D INPUT - s $ { cfg . clusterPrefix } - j ACCEPT
2022-04-20 16:04:57 +00:00
'' ;
} ;
2022-02-09 11:09:49 +00:00
} ;
}