2021-01-28 05:49:10 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.services.nebula;
|
2021-04-10 22:38:44 +00:00
|
|
|
enabledNetworks = filterAttrs (n: v: v.enable) cfg.networks;
|
2021-01-28 05:49:10 +00:00
|
|
|
|
2021-02-10 01:42:33 +00:00
|
|
|
format = pkgs.formats.yaml {};
|
|
|
|
|
2021-03-01 02:31:42 +00:00
|
|
|
nameToId = netName: "nebula-${netName}";
|
2024-01-21 03:28:19 +00:00
|
|
|
|
|
|
|
resolveFinalPort = netCfg:
|
|
|
|
if netCfg.listen.port == null then
|
|
|
|
if (netCfg.isLighthouse || netCfg.isRelay) then
|
|
|
|
4242
|
|
|
|
else
|
|
|
|
0
|
|
|
|
else
|
|
|
|
netCfg.listen.port;
|
2021-01-28 05:49:10 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
# Interface
|
|
|
|
|
2021-03-01 02:31:42 +00:00
|
|
|
options = {
|
|
|
|
services.nebula = {
|
|
|
|
networks = mkOption {
|
|
|
|
description = "Nebula network definitions.";
|
|
|
|
default = {};
|
|
|
|
type = types.attrsOf (types.submodule {
|
|
|
|
options = {
|
2021-04-10 22:38:44 +00:00
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = "Enable or disable this network.";
|
|
|
|
};
|
|
|
|
|
2023-11-27 00:19:27 +00:00
|
|
|
package = mkPackageOption pkgs "nebula" { };
|
2021-03-01 02:31:42 +00:00
|
|
|
|
|
|
|
ca = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
description = "Path to the certificate authority certificate.";
|
|
|
|
example = "/etc/nebula/ca.crt";
|
|
|
|
};
|
|
|
|
|
|
|
|
cert = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
description = "Path to the host certificate.";
|
|
|
|
example = "/etc/nebula/host.crt";
|
|
|
|
};
|
|
|
|
|
|
|
|
key = mkOption {
|
2024-07-08 08:01:35 +00:00
|
|
|
type = types.oneOf [types.nonEmptyStr types.path];
|
|
|
|
description = "Path or reference to the host key.";
|
2021-03-01 02:31:42 +00:00
|
|
|
example = "/etc/nebula/host.key";
|
|
|
|
};
|
|
|
|
|
|
|
|
staticHostMap = mkOption {
|
|
|
|
type = types.attrsOf (types.listOf (types.str));
|
|
|
|
default = {};
|
|
|
|
description = ''
|
|
|
|
The static host map defines a set of hosts with fixed IP addresses on the internet (or any network).
|
|
|
|
A host can have multiple fixed IP addresses defined here, and nebula will try each when establishing a tunnel.
|
|
|
|
'';
|
2021-10-03 16:06:03 +00:00
|
|
|
example = { "192.168.100.1" = [ "100.64.22.11:4242" ]; };
|
2021-03-01 02:31:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
isLighthouse = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "Whether this node is a lighthouse.";
|
|
|
|
};
|
|
|
|
|
2022-12-29 03:48:59 +00:00
|
|
|
isRelay = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "Whether this node is a relay.";
|
|
|
|
};
|
|
|
|
|
2021-03-01 02:31:42 +00:00
|
|
|
lighthouses = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
description = ''
|
|
|
|
List of IPs of lighthouse hosts this node should report to and query from. This should be empty on lighthouse
|
|
|
|
nodes. The IPs should be the lighthouse's Nebula IPs, not their external IPs.
|
|
|
|
'';
|
2021-10-03 16:06:03 +00:00
|
|
|
example = [ "192.168.100.1" ];
|
2021-03-01 02:31:42 +00:00
|
|
|
};
|
|
|
|
|
2022-12-29 03:48:59 +00:00
|
|
|
relays = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
description = ''
|
|
|
|
List of IPs of relays that this node should allow traffic from.
|
|
|
|
'';
|
|
|
|
example = [ "192.168.100.1" ];
|
|
|
|
};
|
|
|
|
|
2021-03-01 02:31:42 +00:00
|
|
|
listen.host = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "0.0.0.0";
|
|
|
|
description = "IP address to listen on.";
|
|
|
|
};
|
|
|
|
|
|
|
|
listen.port = mkOption {
|
2024-01-13 23:27:20 +00:00
|
|
|
type = types.nullOr types.port;
|
|
|
|
default = null;
|
|
|
|
defaultText = lib.literalExpression ''
|
|
|
|
if (config.services.nebula.networks.''${name}.isLighthouse ||
|
|
|
|
config.services.nebula.networks.''${name}.isRelay) then
|
|
|
|
4242
|
|
|
|
else
|
|
|
|
0;
|
|
|
|
'';
|
2021-03-01 02:31:42 +00:00
|
|
|
description = "Port number to listen on.";
|
|
|
|
};
|
|
|
|
|
|
|
|
tun.disable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
When tun is disabled, a lighthouse can be started without a local tun interface (and therefore without root).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
tun.device = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
description = "Name of the tun device. Defaults to nebula.\${networkName}.";
|
|
|
|
};
|
|
|
|
|
|
|
|
firewall.outbound = mkOption {
|
|
|
|
type = types.listOf types.attrs;
|
|
|
|
default = [];
|
|
|
|
description = "Firewall rules for outbound traffic.";
|
2021-10-03 16:06:03 +00:00
|
|
|
example = [ { port = "any"; proto = "any"; host = "any"; } ];
|
2021-03-01 02:31:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
firewall.inbound = mkOption {
|
|
|
|
type = types.listOf types.attrs;
|
|
|
|
default = [];
|
|
|
|
description = "Firewall rules for inbound traffic.";
|
2021-10-03 16:06:03 +00:00
|
|
|
example = [ { port = "any"; proto = "any"; host = "any"; } ];
|
2021-03-01 02:31:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
settings = mkOption {
|
|
|
|
type = format.type;
|
|
|
|
default = {};
|
|
|
|
description = ''
|
|
|
|
Nebula configuration. Refer to
|
|
|
|
<https://github.com/slackhq/nebula/blob/master/examples/config.yml>
|
|
|
|
for details on supported values.
|
|
|
|
'';
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression ''
|
2021-03-01 02:31:42 +00:00
|
|
|
{
|
|
|
|
lighthouse.dns = {
|
|
|
|
host = "0.0.0.0";
|
|
|
|
port = 53;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
2021-01-28 05:49:10 +00:00
|
|
|
};
|
2021-03-01 02:31:42 +00:00
|
|
|
});
|
|
|
|
};
|
2021-01-28 05:49:10 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
# Implementation
|
2021-04-10 22:38:44 +00:00
|
|
|
config = mkIf (enabledNetworks != {}) {
|
|
|
|
systemd.services = mkMerge (mapAttrsToList (netName: netCfg:
|
2021-03-01 02:31:42 +00:00
|
|
|
let
|
|
|
|
networkId = nameToId netName;
|
2021-04-10 22:38:44 +00:00
|
|
|
settings = recursiveUpdate {
|
2021-03-01 02:31:42 +00:00
|
|
|
pki = {
|
|
|
|
ca = netCfg.ca;
|
|
|
|
cert = netCfg.cert;
|
|
|
|
key = netCfg.key;
|
|
|
|
};
|
|
|
|
static_host_map = netCfg.staticHostMap;
|
|
|
|
lighthouse = {
|
|
|
|
am_lighthouse = netCfg.isLighthouse;
|
|
|
|
hosts = netCfg.lighthouses;
|
|
|
|
};
|
2022-12-29 03:48:59 +00:00
|
|
|
relay = {
|
|
|
|
am_relay = netCfg.isRelay;
|
|
|
|
relays = netCfg.relays;
|
2022-12-29 07:30:33 +00:00
|
|
|
use_relays = true;
|
2022-12-29 03:48:59 +00:00
|
|
|
};
|
2021-03-01 02:31:42 +00:00
|
|
|
listen = {
|
|
|
|
host = netCfg.listen.host;
|
2024-01-21 03:28:19 +00:00
|
|
|
port = resolveFinalPort netCfg;
|
2021-03-01 02:31:42 +00:00
|
|
|
};
|
|
|
|
tun = {
|
|
|
|
disabled = netCfg.tun.disable;
|
|
|
|
dev = if (netCfg.tun.device != null) then netCfg.tun.device else "nebula.${netName}";
|
|
|
|
};
|
|
|
|
firewall = {
|
|
|
|
inbound = netCfg.firewall.inbound;
|
|
|
|
outbound = netCfg.firewall.outbound;
|
|
|
|
};
|
|
|
|
} netCfg.settings;
|
2024-01-13 23:27:20 +00:00
|
|
|
configFile = format.generate "nebula-config-${netName}.yml" (
|
|
|
|
warnIf
|
2024-01-21 03:28:19 +00:00
|
|
|
((settings.lighthouse.am_lighthouse || settings.relay.am_relay) && settings.listen.port == 0)
|
2024-01-13 23:27:20 +00:00
|
|
|
''
|
|
|
|
Nebula network '${netName}' is configured as a lighthouse or relay, and its port is ${builtins.toString settings.listen.port}.
|
2024-01-21 03:28:19 +00:00
|
|
|
You will likely experience connectivity issues: https://nebula.defined.net/docs/config/listen/#listenport
|
2024-01-13 23:27:20 +00:00
|
|
|
''
|
|
|
|
settings
|
|
|
|
);
|
2021-03-01 02:31:42 +00:00
|
|
|
in
|
2021-02-10 01:45:17 +00:00
|
|
|
{
|
2022-12-29 03:48:59 +00:00
|
|
|
# Create the systemd service for Nebula.
|
2021-03-01 02:31:42 +00:00
|
|
|
"nebula@${netName}" = {
|
|
|
|
description = "Nebula VPN service for ${netName}";
|
2021-03-05 05:04:18 +00:00
|
|
|
wants = [ "basic.target" ];
|
|
|
|
after = [ "basic.target" "network.target" ];
|
2021-03-01 02:31:42 +00:00
|
|
|
before = [ "sshd.service" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2022-12-29 03:48:59 +00:00
|
|
|
serviceConfig = {
|
2023-12-13 02:57:50 +00:00
|
|
|
Type = "notify";
|
2022-12-29 03:48:59 +00:00
|
|
|
Restart = "always";
|
|
|
|
ExecStart = "${netCfg.package}/bin/nebula -config ${configFile}";
|
2022-12-29 21:57:17 +00:00
|
|
|
UMask = "0027";
|
2022-12-29 03:48:59 +00:00
|
|
|
CapabilityBoundingSet = "CAP_NET_ADMIN";
|
|
|
|
AmbientCapabilities = "CAP_NET_ADMIN";
|
2022-12-29 21:57:17 +00:00
|
|
|
LockPersonality = true;
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
PrivateDevices = false; # needs access to /dev/net/tun (below)
|
|
|
|
DeviceAllow = "/dev/net/tun rw";
|
|
|
|
DevicePolicy = "closed";
|
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateUsers = false; # CapabilityBoundingSet needs to apply to the host namespace
|
|
|
|
ProtectClock = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectProc = "invisible";
|
2024-07-08 08:04:15 +00:00
|
|
|
ProtectSystem = true;
|
2022-12-29 21:57:17 +00:00
|
|
|
RestrictNamespaces = true;
|
|
|
|
RestrictSUIDSGID = true;
|
2022-12-29 03:48:59 +00:00
|
|
|
User = networkId;
|
|
|
|
Group = networkId;
|
|
|
|
};
|
2022-05-02 14:45:44 +00:00
|
|
|
unitConfig.StartLimitIntervalSec = 0; # ensure Restart=always is always honoured (networks can go down for arbitrarily long)
|
2021-03-01 02:31:42 +00:00
|
|
|
};
|
2021-04-10 22:38:44 +00:00
|
|
|
}) enabledNetworks);
|
2021-03-01 02:31:42 +00:00
|
|
|
|
|
|
|
# Open the chosen ports for UDP.
|
|
|
|
networking.firewall.allowedUDPPorts =
|
2024-01-21 03:28:19 +00:00
|
|
|
unique (filter (port: port > 0) (mapAttrsToList (netName: netCfg: resolveFinalPort netCfg) enabledNetworks));
|
2021-03-01 02:31:42 +00:00
|
|
|
|
|
|
|
# Create the service users and groups.
|
2021-04-10 22:38:44 +00:00
|
|
|
users.users = mkMerge (mapAttrsToList (netName: netCfg:
|
2022-12-29 03:48:59 +00:00
|
|
|
{
|
2021-03-01 02:31:42 +00:00
|
|
|
${nameToId netName} = {
|
|
|
|
group = nameToId netName;
|
|
|
|
description = "Nebula service user for network ${netName}";
|
|
|
|
isSystemUser = true;
|
|
|
|
};
|
2021-04-10 22:38:44 +00:00
|
|
|
}) enabledNetworks);
|
2021-03-01 02:31:42 +00:00
|
|
|
|
2022-12-29 21:57:17 +00:00
|
|
|
users.groups = mkMerge (mapAttrsToList (netName: netCfg: {
|
|
|
|
${nameToId netName} = {};
|
|
|
|
}) enabledNetworks);
|
2021-02-10 01:45:17 +00:00
|
|
|
};
|
2024-07-08 08:06:58 +00:00
|
|
|
|
2024-08-16 19:13:40 +00:00
|
|
|
meta.maintainers = with maintainers; [ numinit ];
|
2021-03-05 05:39:04 +00:00
|
|
|
}
|