2023-04-29 16:46:19 +00:00
|
|
|
{ lib, config, pkgs, ... }:
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.services.wgautomesh;
|
|
|
|
settingsFormat = pkgs.formats.toml { };
|
|
|
|
configFile =
|
|
|
|
# Have to remove nulls manually as TOML generator will not just skip key
|
|
|
|
# if value is null
|
|
|
|
settingsFormat.generate "wgautomesh-config.toml"
|
|
|
|
(filterAttrs (k: v: v != null)
|
|
|
|
(mapAttrs
|
|
|
|
(k: v:
|
|
|
|
if k == "peers"
|
|
|
|
then map (e: filterAttrs (k: v: v != null) e) v
|
|
|
|
else v)
|
|
|
|
cfg.settings));
|
|
|
|
runtimeConfigFile =
|
|
|
|
if cfg.enableGossipEncryption
|
|
|
|
then "/run/wgautomesh/wgautomesh.toml"
|
|
|
|
else configFile;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.wgautomesh = {
|
2024-04-21 15:54:59 +00:00
|
|
|
enable = mkEnableOption "the wgautomesh daemon";
|
2023-04-29 16:46:19 +00:00
|
|
|
logLevel = mkOption {
|
|
|
|
type = types.enum [ "trace" "debug" "info" "warn" "error" ];
|
|
|
|
default = "info";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "wgautomesh log level.";
|
2023-04-29 16:46:19 +00:00
|
|
|
};
|
|
|
|
enableGossipEncryption = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Enable encryption of gossip traffic.";
|
2023-04-29 16:46:19 +00:00
|
|
|
};
|
|
|
|
gossipSecretFile = mkOption {
|
|
|
|
type = types.path;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2023-07-15 17:15:38 +00:00
|
|
|
File containing the gossip secret, a shared secret key to use for gossip
|
|
|
|
encryption. Required if `enableGossipEncryption` is set. This file
|
|
|
|
may contain any arbitrary-length utf8 string. To generate a new gossip
|
|
|
|
secret, use a command such as `openssl rand -base64 32`.
|
2023-04-29 16:46:19 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
enablePersistence = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Enable persistence of Wireguard peer info between restarts.";
|
2023-04-29 16:46:19 +00:00
|
|
|
};
|
|
|
|
openFirewall = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Automatically open gossip port in firewall (recommended).";
|
2023-04-29 16:46:19 +00:00
|
|
|
};
|
|
|
|
settings = mkOption {
|
|
|
|
type = types.submodule {
|
|
|
|
freeformType = settingsFormat.type;
|
|
|
|
options = {
|
|
|
|
|
|
|
|
interface = mkOption {
|
|
|
|
type = types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2023-04-29 16:46:19 +00:00
|
|
|
Wireguard interface to manage (it is NOT created by wgautomesh, you
|
|
|
|
should use another NixOS option to create it such as
|
|
|
|
`networking.wireguard.interfaces.wg0 = {...};`).
|
|
|
|
'';
|
|
|
|
example = "wg0";
|
|
|
|
};
|
|
|
|
gossip_port = mkOption {
|
|
|
|
type = types.port;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2023-04-29 16:46:19 +00:00
|
|
|
wgautomesh gossip port, this MUST be the same number on all nodes in
|
|
|
|
the wgautomesh network.
|
|
|
|
'';
|
|
|
|
default = 1666;
|
|
|
|
};
|
|
|
|
lan_discovery = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Enable discovery of peers on the same LAN using UDP broadcast.";
|
2023-04-29 16:46:19 +00:00
|
|
|
};
|
|
|
|
upnp_forward_external_port = mkOption {
|
|
|
|
type = types.nullOr types.port;
|
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2023-04-29 16:46:19 +00:00
|
|
|
Public port number to try to redirect to this machine's Wireguard
|
|
|
|
daemon using UPnP IGD.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
peers = mkOption {
|
|
|
|
type = types.listOf (types.submodule {
|
|
|
|
options = {
|
|
|
|
pubkey = mkOption {
|
|
|
|
type = types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Wireguard public key of this peer.";
|
2023-04-29 16:46:19 +00:00
|
|
|
};
|
|
|
|
address = mkOption {
|
|
|
|
type = types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2023-05-24 13:37:59 +00:00
|
|
|
Wireguard address of this peer (a single IP address, multiple
|
2023-04-29 16:46:19 +00:00
|
|
|
addresses or address ranges are not supported).
|
|
|
|
'';
|
|
|
|
example = "10.0.0.42";
|
|
|
|
};
|
|
|
|
endpoint = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2023-04-29 16:46:19 +00:00
|
|
|
Bootstrap endpoint for connecting to this Wireguard peer if no
|
|
|
|
other address is known or none are working.
|
|
|
|
'';
|
|
|
|
default = null;
|
|
|
|
example = "wgnode.mydomain.example:51820";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
default = [ ];
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "wgautomesh peer list.";
|
2023-04-29 16:46:19 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
default = { };
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Configuration for wgautomesh.";
|
2023-04-29 16:46:19 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
services.wgautomesh.settings = {
|
|
|
|
gossip_secret_file = mkIf cfg.enableGossipEncryption "$CREDENTIALS_DIRECTORY/gossip_secret";
|
|
|
|
persist_file = mkIf cfg.enablePersistence "/var/lib/wgautomesh/state";
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.wgautomesh = {
|
|
|
|
path = [ pkgs.wireguard-tools ];
|
|
|
|
environment = { RUST_LOG = "wgautomesh=${cfg.logLevel}"; };
|
|
|
|
description = "wgautomesh";
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "simple";
|
|
|
|
|
|
|
|
ExecStart = "${getExe pkgs.wgautomesh} ${runtimeConfigFile}";
|
|
|
|
Restart = "always";
|
|
|
|
RestartSec = "30";
|
|
|
|
LoadCredential = mkIf cfg.enableGossipEncryption [ "gossip_secret:${cfg.gossipSecretFile}" ];
|
|
|
|
|
|
|
|
ExecStartPre = mkIf cfg.enableGossipEncryption [
|
|
|
|
''${pkgs.envsubst}/bin/envsubst \
|
|
|
|
-i ${configFile} \
|
|
|
|
-o ${runtimeConfigFile}''
|
|
|
|
];
|
|
|
|
|
|
|
|
DynamicUser = true;
|
|
|
|
StateDirectory = "wgautomesh";
|
|
|
|
StateDirectoryMode = "0700";
|
|
|
|
RuntimeDirectory = "wgautomesh";
|
|
|
|
AmbientCapabilities = "CAP_NET_ADMIN";
|
|
|
|
CapabilityBoundingSet = "CAP_NET_ADMIN";
|
|
|
|
};
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
};
|
|
|
|
networking.firewall.allowedUDPPorts =
|
|
|
|
mkIf cfg.openFirewall [ cfg.settings.gossip_port ];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|