2022-02-09 11:09:49 +00:00
{ config , pkgs , . . . }:
let
cfg = config . deuxfleurs ;
in
with builtins ;
with pkgs . lib ;
{
2022-05-08 22:20:02 +00:00
options . deuxfleurs =
2022-08-23 21:55:15 +00:00
let wg_node = with types ; submodule {
options = {
hostname = mkOption {
type = str ;
description = " H o s t n a m e " ;
} ;
site_name = 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 ;
} ;
IP = 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 ;
2023-03-09 14:31:05 +00:00
default = null ;
2022-08-23 21:55:15 +00:00
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 " ;
} ;
lan_endpoint = mkOption {
type = nullOr str ;
description = " W i r e g u a r d e n d p o i n t f o r n o d e s i n t h e s a m e s i t e " ;
default = null ;
} ;
} ;
} ;
in
2022-02-09 11:09:49 +00:00
{
2022-02-25 16:52:17 +00:00
# Parameters for individual nodes
network_interface = mkOption {
description = " N e t w o r k i n t e r f a c e n a m e t o c o n f i g u r e " ;
2022-02-09 11:09:49 +00:00
type = types . str ;
} ;
2022-02-25 16:52:17 +00:00
lan_ip = mkOption {
description = " I P a d d r e s s 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 " ;
type = types . str ;
} ;
lan_ip_prefix_length = mkOption {
description = " P r e f i x l e n g t h a s s o c i a t e d w i t h l a n _ i p " ;
type = types . int ;
} ;
2022-04-20 16:04:57 +00:00
ipv6 = mkOption {
description = " 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 " ;
type = types . str ;
} ;
ipv6_prefix_length = mkOption {
description = " P r e f i x l e n g t h a s s o c i a t e d w i t h i p v 6 i p " ;
type = types . int ;
} ;
2022-02-25 16:52:17 +00:00
2022-04-20 16:04:57 +00:00
cluster_ip = mkOption {
description = " I P a d d r e s s o f t h i s n o d e o n t h e W e s h e r m e s h n e t w o r k " ;
type = types . str ;
2022-02-09 11:09:49 +00:00
} ;
2022-08-23 21:55:15 +00:00
wireguard_port = 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 " ;
type = types . port ;
default = 33799 ;
} ;
2022-02-09 11:09:49 +00:00
is_raft_server = mkOption {
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 " ;
type = types . bool ;
default = false ;
} ;
2022-02-25 16:52:17 +00:00
# Parameters that generally vary between sites
lan_default_gateway = mkOption {
2022-08-24 09:06:55 +00:00
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 " ;
type = types . str ;
} ;
ipv6_default_gateway = mkOption {
description = " I P v 6 a d d r e s s o f t h e d e f a u l t I P v 6 g a t e w a y f o r t h e t a r g e t e d n e t i n t e r f a c e " ;
2022-02-25 16:52:17 +00:00
type = types . str ;
} ;
site_name = mkOption {
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 " ;
type = types . str ;
} ;
2022-12-07 16:13:03 +00:00
public_ipv4 = mkOption {
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 " ;
type = types . nullOr types . str ;
default = null ;
} ;
2022-12-07 11:32:21 +00:00
cname_target = 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 " ;
2022-12-07 11:32:21 +00:00
type = types . nullOr types . str ;
2022-12-07 16:13:03 +00:00
default = null ;
2022-12-07 11:32:21 +00:00
} ;
2022-06-01 12:46:00 +00:00
nameservers = mkOption {
description = " E x t e r n a l D N S s e r v e r s t o u s e " ;
type = types . listOf types . str ;
} ;
2022-02-25 16:52:17 +00:00
2022-02-09 11:09:49 +00:00
# Parameters common to all nodes
cluster_name = mkOption {
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 " ;
type = types . str ;
} ;
2022-08-23 21:55:15 +00:00
cluster_prefix = mkOption {
description = " I P a d d r e s s p r e f i x 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 = types . str ;
} ;
cluster_prefix_length = mkOption {
description = " I P a d d r e s s p r e f i x 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 = types . int ;
default = 16 ;
} ;
cluster_nodes = mkOption {
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 " ;
type = types . listOf wg_node ;
} ;
2022-02-09 11:09:49 +00:00
admin_accounts = mkOption {
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 " ;
type = types . attrsOf ( types . listOf types . str ) ;
} ;
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 " ;
type = types . bool ;
default = false ;
} ;
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 =
let node_meta = {
" s i t e " = cfg . site_name ;
" p u b l i c _ i p v 6 " = cfg . ipv6 ;
} //
2022-12-07 16:13:03 +00:00
( if cfg . public_ipv4 != null
then { " p u b l i c _ i p v 4 " = cfg . public_ipv4 ; }
else { } ) //
2022-12-07 11:32:21 +00:00
( if cfg . cname_target != null
then { " c n a m e _ t a r g e t " = cfg . cname_target ; }
else { } ) ;
in
{
2023-03-09 14:31:05 +00:00
2022-02-09 11:09:49 +00:00
# Configure admin accounts on all nodes
users . users = builtins . mapAttrs ( name : publicKeys : {
isNormalUser = true ;
extraGroups = [ " w h e e l " ] ;
openssh . authorizedKeys . keys = publicKeys ;
} ) cfg . admin_accounts ;
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 = {
# We could preprend "en* eth*" to match all ethernet interfaces
Name = " ${ cfg . network_interface } " ;
2022-05-08 22:20:02 +00:00
} ;
2022-10-15 08:38:48 +00:00
networkConfig = {
IPv6AcceptRA = false ;
LinkLocalAddressing = " n o " ;
} ;
address = [
" ${ cfg . lan_ip } / ${ toString cfg . lan_ip_prefix_length } "
" ${ cfg . ipv6 } / ${ toString cfg . ipv6_prefix_length } "
] ;
routes = [
{
routeConfig = {
Gateway = cfg . lan_default_gateway ;
# 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
{
2022-10-15 08:38:48 +00:00
routeConfig = {
Gateway = cfg . ipv6_default_gateway ;
GatewayOnLink = true ;
} ;
2022-05-08 22:20:02 +00:00
}
] ;
} ;
2022-08-24 09:06:55 +00:00
} ;
2022-08-30 13:52:42 +00:00
# Configure Unbound DNS to redirect to Consul queries under .consul
# and to pass directly to public DNS resolver all others
services . unbound = {
enable = true ;
2022-11-28 21:58:39 +00:00
enableRootTrustAnchor = false ; # disable DNSSEC as it causes issues
2022-08-30 13:52:42 +00:00
settings = {
server = {
2022-10-16 11:13:43 +00:00
interface = [ " 1 2 7 . 0 . 0 . 1 " " ${ cfg . lan_ip } " " 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 "
" ${ cfg . lan_ip } / ${ toString cfg . lan_ip_prefix_length } a l l o w "
" 1 7 2 . 1 7 . 0 . 0 / 1 6 a l l o w "
] ;
} ;
forward-zone = [
# Forward .consul queries to Consul daemon
{
name = " c o n s u l . " ;
forward-addr = " ${ cfg . lan_ip } @ 8 6 0 0 " ;
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 ;
}
] ;
} ;
2022-10-16 17:36:15 +00:00
resolveLocalQueries = true ;
2022-08-30 13:52:42 +00:00
} ;
# Reach Unbound through the IP of our LAN interface,
# instead of 127.0.0.1 (this will also work in Docker containers)
2023-03-15 17:19:01 +00:00
networking . nameservers = [ # TODO remove this ?
2022-06-01 12:46:00 +00:00
cfg . lan_ip
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 = {
ips = [ " ${ cfg . cluster_ip } / 1 6 " ] ;
2022-08-23 21:55:15 +00:00
listenPort = cfg . wireguard_port ;
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 =
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 ) )
else null ;
peers = map ( { publicKey , endpoint , IP , . . . }: {
address = IP ;
pubkey = publicKey ;
endpoint = endpoint ;
2022-08-23 21:55:15 +00:00
} ) cfg . cluster_nodes ;
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 = {
# address = [ "${cfg.cluster_ip}/16" ];
# listenPort = cfg.wireguard_port;
# privateKeyFile = "/var/lib/deuxfleurs/wireguard-keys/private";
# mtu = 1420;
# peers = map ({ publicKey, endpoint, IP, site_name, lan_endpoint, ... }: {
# inherit publicKey endpoint;
# allowedIPs = [ "${IP}/32" ];
# 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
2022-08-23 21:55:15 +00:00
networking . extraHosts = builtins . concatStringsSep " \n " ( map
( { hostname , IP , . . . }: " ${ IP } ${ hostname } " )
cfg . cluster_nodes ) ;
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 =
( if cfg . is_raft_server
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 ;
2022-02-09 11:09:49 +00:00
datacenter = cfg . cluster_name ;
2022-07-27 09:18:23 +00:00
ui_config = {
enabled = true ;
} ;
2022-04-20 16:04:57 +00:00
bind_addr = " ${ cfg . cluster_ip } " ;
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 ;
} ;
2023-03-15 17:19:01 +00:00
systemd . services . consul . serviceConfig = { # TODO remove this ?
2022-06-01 12:46:00 +00:00
AmbientCapabilities = " C A P _ N E T _ B I N D _ S E R V I C E " ;
} ;
2022-02-09 11:09:49 +00:00
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 " ] ;
2022-07-27 09:18:23 +00:00
services . nomad . package = pkgs . nomad_1_3 ;
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 =
( if cfg . is_raft_server
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
{
region = cfg . cluster_name ;
datacenter = cfg . site_name ;
advertise = {
2022-04-20 16:04:57 +00:00
rpc = " ${ cfg . cluster_ip } " ;
http = " ${ cfg . cluster_ip } " ;
serf = " ${ cfg . cluster_ip } " ;
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
2022-04-20 16:04:57 +00:00
( builtins . head ( { openssh . ports = [ 22 ] ; } // config . services ) . openssh . ports )
] ;
2022-08-23 21:55:15 +00:00
allowedUDPPorts = [
# Allow peers to connect to Wireguard
cfg . wireguard_port
] ;
2022-04-20 16:04:57 +00:00
# Allow specific hosts access to specific things in the cluster
extraCommands = ''
# Allow everything from router (usefull for UPnP/IGD)
2022-05-08 22:29:17 +00:00
iptables - A INPUT - s $ { cfg . lan_default_gateway } - 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
2022-08-23 21:55:15 +00:00
iptables - A INPUT - s $ { cfg . cluster_prefix } / $ { toString cfg . cluster_prefix_length } - j ACCEPT
2022-04-20 16:04:57 +00:00
'' ;
# When stopping firewall, delete all rules that were configured manually above
extraStopCommands = ''
2022-05-08 22:29:17 +00:00
iptables - D INPUT - s $ { cfg . lan_default_gateway } - j ACCEPT
2022-04-20 16:04:57 +00:00
iptables - D INPUT - s 172.17.0.0/16 - j ACCEPT
2022-08-23 21:55:15 +00:00
iptables - D INPUT - s $ { cfg . cluster_prefix } / $ { toString cfg . cluster_prefix_length } - j ACCEPT
2022-04-20 16:04:57 +00:00
'' ;
} ;
2022-02-09 11:09:49 +00:00
} ;
}