2023-03-09 14:31:05 +00:00
|
|
|
let
|
2023-03-17 17:01:35 +00:00
|
|
|
wgautomesh = builtins.fetchTarball {
|
|
|
|
url = "https://git.deuxfleurs.fr/attachments/ce203833-1ae7-43d4-9bf4-b49b560c9f4b";
|
|
|
|
sha256 = "sha256:1kc990s7xkwff53vs6c3slg7ljsyr9xz1i13j61ivfj6djyh8rmj";
|
2023-03-09 14:31:05 +00:00
|
|
|
};
|
|
|
|
in
|
|
|
|
|
|
|
|
{ lib, config, pkgs, ... }:
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.services.wgautomesh;
|
|
|
|
in
|
|
|
|
with builtins;
|
|
|
|
{
|
|
|
|
options.services.wgautomesh = {
|
|
|
|
enable = mkEnableOption "wgautomesh";
|
2023-03-17 17:21:50 +00:00
|
|
|
logLevel = mkOption {
|
2023-03-20 10:17:38 +00:00
|
|
|
type = types.enum [ "trace" "debug" "info" "warn" "error" ];
|
2023-03-17 17:21:50 +00:00
|
|
|
default = "info";
|
|
|
|
description = "wgautomesh log level (trace/debug/info/warn/error)";
|
|
|
|
};
|
2023-03-09 14:31:05 +00:00
|
|
|
interface = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "Wireguard interface to manage";
|
|
|
|
};
|
|
|
|
gossipPort = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
description = "wgautomesh gossip port";
|
|
|
|
};
|
2023-03-09 15:39:58 +00:00
|
|
|
gossipSecretFile = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
description = "File containing the gossip secret encryption key";
|
|
|
|
};
|
2023-03-09 16:06:57 +00:00
|
|
|
persistFile = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
description = "Path where to persist known peer addresses";
|
|
|
|
};
|
2023-03-09 14:31:05 +00:00
|
|
|
lanDiscovery = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = "Enable discovery using LAN broadcast";
|
|
|
|
};
|
|
|
|
openFirewall = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = "Automatically open gossip port in firewall";
|
|
|
|
};
|
|
|
|
upnpForwardPublicPort = mkOption {
|
|
|
|
type = types.nullOr types.port;
|
|
|
|
default = null;
|
|
|
|
description = "Public port number to try to redirect to this machine using UPnP IGD";
|
|
|
|
};
|
|
|
|
peers = mkOption {
|
|
|
|
type = types.listOf (types.submodule {
|
|
|
|
options = {
|
|
|
|
pubkey = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "Wireguard public key";
|
|
|
|
};
|
|
|
|
address = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "Wireguard peer address";
|
|
|
|
};
|
|
|
|
endpoint = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
description = "bootstrap endpoint";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
description = "wgautomesh peer list";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable (
|
|
|
|
let
|
|
|
|
peerDefs = map (peer:
|
|
|
|
let endpointDef = if peer.endpoint == null then ""
|
|
|
|
else ''endpoint = "${peer.endpoint}"'';
|
|
|
|
in
|
|
|
|
''
|
|
|
|
[[peers]]
|
|
|
|
pubkey = "${peer.pubkey}"
|
|
|
|
address = "${peer.address}"
|
|
|
|
${endpointDef}
|
|
|
|
'') cfg.peers;
|
|
|
|
extraDefs = (if cfg.lanDiscovery then ["lan_discovery = true"] else [])
|
2023-03-09 15:39:58 +00:00
|
|
|
++ (if (cfg.gossipSecretFile != null)
|
|
|
|
then [''gossip_secret_file = "${cfg.gossipSecretFile}"''] else [])
|
2023-03-09 16:06:57 +00:00
|
|
|
++ (if (cfg.persistFile != null)
|
|
|
|
then [''persist_file = "${cfg.persistFile}"''] else [])
|
2023-03-09 14:31:05 +00:00
|
|
|
++ (if (cfg.upnpForwardPublicPort != null)
|
|
|
|
then [''upnp_forward_external_port = ${toString cfg.upnpForwardPublicPort}''] else []);
|
|
|
|
configfile = pkgs.writeText "wgautomesh.toml" ''
|
|
|
|
interface = "${cfg.interface}"
|
|
|
|
gossip_port = ${toString cfg.gossipPort}
|
2023-03-09 15:39:58 +00:00
|
|
|
${concatStringsSep "\n" extraDefs}
|
2023-03-09 14:31:05 +00:00
|
|
|
|
2023-03-09 15:39:58 +00:00
|
|
|
${concatStringsSep "\n" peerDefs}
|
2023-03-09 14:31:05 +00:00
|
|
|
'';
|
|
|
|
in {
|
|
|
|
systemd.services.wgautomesh = {
|
|
|
|
enable = true;
|
|
|
|
path = [ pkgs.wireguard-tools ];
|
|
|
|
environment = {
|
2023-03-17 17:21:50 +00:00
|
|
|
RUST_LOG = "wgautomesh=${cfg.logLevel}";
|
2023-03-09 14:31:05 +00:00
|
|
|
};
|
|
|
|
description = "wgautomesh";
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "simple";
|
|
|
|
|
|
|
|
ExecStart = "${wgautomesh}/bin/wgautomesh ${configfile}";
|
|
|
|
Restart = "always";
|
|
|
|
RestartSec = "30";
|
|
|
|
|
2023-03-09 15:39:58 +00:00
|
|
|
ExecStartPre = [ "+${pkgs.coreutils}/bin/chown wgautomesh /var/lib/wgautomesh/gossip_secret" ];
|
|
|
|
|
2023-03-09 14:31:05 +00:00
|
|
|
DynamicUser = true;
|
2023-03-09 15:39:58 +00:00
|
|
|
User = "wgautomesh";
|
|
|
|
StateDirectory = "wgautomesh";
|
|
|
|
StateDirectoryMode = "0700";
|
2023-03-24 11:01:38 +00:00
|
|
|
AmbientCapabilities = "CAP_NET_ADMIN";
|
|
|
|
CapabilityBoundingSet = "CAP_NET_ADMIN";
|
2023-03-09 14:31:05 +00:00
|
|
|
};
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
};
|
|
|
|
networking.firewall.allowedUDPPorts = mkIf cfg.openFirewall [ cfg.gossipPort ];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|