nixpkgs/nixos/modules/services/networking/quicktun.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

200 lines
6.4 KiB
Nix
Raw Permalink Normal View History

2023-11-01 19:47:51 +01:00
{
options,
config,
pkgs,
lib,
...
}:
2019-04-02 12:15:12 +02:00
let
2023-11-01 19:47:51 +01:00
inherit (lib) mkOption types mkIf;
2019-04-02 12:15:12 +02:00
2023-11-01 19:47:51 +01:00
opt = options.services.quicktun;
2019-04-02 12:15:12 +02:00
cfg = config.services.quicktun;
in
{
options = {
services.quicktun = mkOption {
default = { };
2023-11-01 19:47:51 +01:00
description = ''
QuickTun tunnels.
See <http://wiki.ucis.nl/QuickTun> for more information about available options.
'';
type = types.attrsOf (
types.submodule (
{ name, ... }:
let
qtcfg = cfg.${name};
in
{
2019-04-02 12:15:12 +02:00
options = {
tunMode = mkOption {
2023-11-01 19:47:51 +01:00
type = with types; coercedTo bool (b: if b then 1 else 0) (ints.between 0 1);
default = false;
example = true;
description = "Whether to operate in tun (IP) or tap (Ethernet) mode.";
2019-04-02 12:15:12 +02:00
};
2019-04-02 12:15:12 +02:00
remoteAddress = mkOption {
type = types.str;
2023-11-01 19:47:51 +01:00
default = "0.0.0.0";
2019-04-02 12:15:12 +02:00
example = "tunnel.example.com";
2023-11-01 19:47:51 +01:00
description = ''
IP address or hostname of the remote end (use `0.0.0.0` for a floating/dynamic remote endpoint).
'';
2019-04-02 12:15:12 +02:00
};
2019-04-02 12:15:12 +02:00
localAddress = mkOption {
2023-11-01 19:47:51 +01:00
type = with types; nullOr str;
default = null;
2019-04-02 12:15:12 +02:00
example = "0.0.0.0";
2023-11-01 19:47:51 +01:00
description = "IP address or hostname of the local end.";
2019-04-02 12:15:12 +02:00
};
2019-04-02 12:15:12 +02:00
localPort = mkOption {
2023-11-01 19:47:51 +01:00
type = types.port;
2019-04-02 12:15:12 +02:00
default = 2998;
2023-11-01 19:47:51 +01:00
description = "Local UDP port.";
2019-04-02 12:15:12 +02:00
};
2019-04-02 12:15:12 +02:00
remotePort = mkOption {
2023-11-01 19:47:51 +01:00
type = types.port;
default = qtcfg.localPort;
defaultText = lib.literalExpression "config.services.quicktun.<name>.localPort";
description = " remote UDP port";
2019-04-02 12:15:12 +02:00
};
2019-04-02 12:15:12 +02:00
remoteFloat = mkOption {
2023-11-01 19:47:51 +01:00
type = with types; coercedTo bool (b: if b then 1 else 0) (ints.between 0 1);
default = false;
example = true;
description = ''
Whether to allow the remote address and port to change when properly encrypted packets are received.
'';
2019-04-02 12:15:12 +02:00
};
2019-04-02 12:15:12 +02:00
protocol = mkOption {
2023-11-01 19:47:51 +01:00
type = types.enum [
"raw"
"nacl0"
"nacltai"
"salty"
];
2019-04-02 12:15:12 +02:00
default = "nacltai";
2023-11-01 19:47:51 +01:00
description = "Which protocol to use.";
2019-04-02 12:15:12 +02:00
};
2019-04-02 12:15:12 +02:00
privateKey = mkOption {
2023-11-01 19:47:51 +01:00
type = with types; nullOr str;
default = null;
description = ''
Local secret key in hexadecimal form.
2023-11-01 19:47:51 +01:00
::: {.warning}
This option is deprecated. Please use {var}`services.quicktun.<name>.privateKeyFile` instead.
:::
2023-11-01 19:47:51 +01:00
::: {.note}
Not needed when {var}`services.quicktun.<name>.protocol` is set to `raw`.
:::
'';
};
2023-11-01 19:47:51 +01:00
privateKeyFile = mkOption {
type = with types; nullOr path;
# This is a hack to deprecate `privateKey` without using `mkChangedModuleOption`
default =
if qtcfg.privateKey == null then null else pkgs.writeText "quickttun-key-${name}" qtcfg.privateKey;
defaultText = "null";
description = ''
Path to file containing local secret key in binary or hexadecimal form.
2023-11-01 19:47:51 +01:00
::: {.note}
Not needed when {var}`services.quicktun.<name>.protocol` is set to `raw`.
:::
'';
2019-04-02 12:15:12 +02:00
};
2019-04-02 12:15:12 +02:00
publicKey = mkOption {
2023-11-01 19:47:51 +01:00
type = with types; nullOr str;
default = null;
description = ''
Remote public key in hexadecimal form.
2023-11-01 19:47:51 +01:00
::: {.note}
Not needed when {var}`services.quicktun.<name>.protocol` is set to `raw`.
:::
'';
2019-04-02 12:15:12 +02:00
};
2019-04-02 12:15:12 +02:00
timeWindow = mkOption {
2023-11-01 19:47:51 +01:00
type = types.ints.unsigned;
2019-04-02 12:15:12 +02:00
default = 5;
2023-11-01 19:47:51 +01:00
description = ''
Allowed time window for first received packet in seconds (positive number allows packets from history)
'';
2019-04-02 12:15:12 +02:00
};
2019-04-02 12:15:12 +02:00
upScript = mkOption {
2023-11-01 19:47:51 +01:00
type = with types; nullOr lines;
default = null;
description = ''
Run specified command or script after the tunnel device has been opened.
'';
};
2019-04-02 12:15:12 +02:00
};
}
)
2023-11-01 19:47:51 +01:00
);
2019-04-02 12:15:12 +02:00
};
};
2023-11-01 19:47:51 +01:00
config = {
warnings = lib.pipe cfg [
(lib.mapAttrsToList (name: value: if value.privateKey != null then name else null))
(builtins.filter (n: n != null))
(map (n: " - services.quicktun.${n}.privateKey"))
(
services:
lib.optional (services != [ ]) ''
`services.quicktun.<name>.privateKey` is deprecated.
Please use `services.quicktun.<name>.privateKeyFile` instead.
2023-11-01 19:47:51 +01:00
Offending options:
${lib.concatStringsSep "\n" services}
''
)
];
systemd.services = lib.mkMerge (
lib.mapAttrsToList (name: qtcfg: {
2019-04-02 12:15:12 +02:00
"quicktun-${name}" = {
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
environment = {
2019-08-13 21:52:01 +00:00
INTERFACE = name;
TUN_MODE = toString qtcfg.tunMode;
REMOTE_ADDRESS = qtcfg.remoteAddress;
2023-11-01 19:47:51 +01:00
LOCAL_ADDRESS = mkIf (qtcfg.localAddress != null) (qtcfg.localAddress);
2019-08-13 21:52:01 +00:00
LOCAL_PORT = toString qtcfg.localPort;
REMOTE_PORT = toString qtcfg.remotePort;
REMOTE_FLOAT = toString qtcfg.remoteFloat;
2023-11-01 19:47:51 +01:00
PRIVATE_KEY_FILE = mkIf (qtcfg.privateKeyFile != null) qtcfg.privateKeyFile;
PUBLIC_KEY = mkIf (qtcfg.publicKey != null) qtcfg.publicKey;
2019-08-13 21:52:01 +00:00
TIME_WINDOW = toString qtcfg.timeWindow;
2023-11-01 19:47:51 +01:00
TUN_UP_SCRIPT = mkIf (qtcfg.upScript != null) (
pkgs.writeScript "quicktun-${name}-up.sh" qtcfg.upScript
);
2019-08-13 21:52:01 +00:00
SUID = "nobody";
2019-04-02 12:15:12 +02:00
};
serviceConfig = {
Type = "simple";
ExecStart = "${pkgs.quicktun}/bin/quicktun.${qtcfg.protocol}";
};
};
}) cfg
);
};
}