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

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

177 lines
5.9 KiB
Nix
Raw Normal View History

2023-11-01 18:47:51 +00:00
{ options, config, pkgs, lib, ... }:
2019-04-02 10:15:12 +00:00
let
2023-11-01 18:47:51 +00:00
inherit (lib) mkOption mdDoc types mkIf;
2019-04-02 10:15:12 +00:00
2023-11-01 18:47:51 +00:00
opt = options.services.quicktun;
2019-04-02 10:15:12 +00:00
cfg = config.services.quicktun;
in
{
options = {
services.quicktun = mkOption {
default = { };
2023-11-01 18:47:51 +00:00
description = mdDoc ''
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 10:15:12 +00:00
options = {
tunMode = mkOption {
2023-11-01 18:47:51 +00:00
type = with types; coercedTo bool (b: if b then 1 else 0) (ints.between 0 1);
default = false;
example = true;
description = mdDoc "Whether to operate in tun (IP) or tap (Ethernet) mode.";
2019-04-02 10:15:12 +00:00
};
remoteAddress = mkOption {
type = types.str;
2023-11-01 18:47:51 +00:00
default = "0.0.0.0";
2019-04-02 10:15:12 +00:00
example = "tunnel.example.com";
2023-11-01 18:47:51 +00:00
description = mdDoc ''
IP address or hostname of the remote end (use `0.0.0.0` for a floating/dynamic remote endpoint).
'';
2019-04-02 10:15:12 +00:00
};
localAddress = mkOption {
2023-11-01 18:47:51 +00:00
type = with types; nullOr str;
default = null;
2019-04-02 10:15:12 +00:00
example = "0.0.0.0";
2023-11-01 18:47:51 +00:00
description = mdDoc "IP address or hostname of the local end.";
2019-04-02 10:15:12 +00:00
};
localPort = mkOption {
2023-11-01 18:47:51 +00:00
type = types.port;
2019-04-02 10:15:12 +00:00
default = 2998;
2023-11-01 18:47:51 +00:00
description = mdDoc "Local UDP port.";
2019-04-02 10:15:12 +00:00
};
remotePort = mkOption {
2023-11-01 18:47:51 +00:00
type = types.port;
default = qtcfg.localPort;
defaultText = lib.literalExpression "config.services.quicktun.<name>.localPort";
description = mdDoc " remote UDP port";
2019-04-02 10:15:12 +00:00
};
remoteFloat = mkOption {
2023-11-01 18:47:51 +00:00
type = with types; coercedTo bool (b: if b then 1 else 0) (ints.between 0 1);
default = false;
example = true;
description = mdDoc ''
Whether to allow the remote address and port to change when properly encrypted packets are received.
'';
2019-04-02 10:15:12 +00:00
};
protocol = mkOption {
2023-11-01 18:47:51 +00:00
type = types.enum [ "raw" "nacl0" "nacltai" "salty" ];
2019-04-02 10:15:12 +00:00
default = "nacltai";
2023-11-01 18:47:51 +00:00
description = mdDoc "Which protocol to use.";
2019-04-02 10:15:12 +00:00
};
privateKey = mkOption {
2023-11-01 18:47:51 +00:00
type = with types; nullOr str;
default = null;
description = mdDoc ''
Local secret key in hexadecimal form.
::: {.warning}
This option is deprecated. Please use {var}`services.quicktun.<name>.privateKeyFile` instead.
:::
::: {.note}
Not needed when {var}`services.quicktun.<name>.protocol` is set to `raw`.
:::
'';
};
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 = mdDoc ''
Path to file containing local secret key in binary or hexadecimal form.
::: {.note}
Not needed when {var}`services.quicktun.<name>.protocol` is set to `raw`.
:::
'';
2019-04-02 10:15:12 +00:00
};
publicKey = mkOption {
2023-11-01 18:47:51 +00:00
type = with types; nullOr str;
default = null;
description = mdDoc ''
Remote public key in hexadecimal form.
::: {.note}
Not needed when {var}`services.quicktun.<name>.protocol` is set to `raw`.
:::
'';
2019-04-02 10:15:12 +00:00
};
timeWindow = mkOption {
2023-11-01 18:47:51 +00:00
type = types.ints.unsigned;
2019-04-02 10:15:12 +00:00
default = 5;
2023-11-01 18:47:51 +00:00
description = mdDoc ''
Allowed time window for first received packet in seconds (positive number allows packets from history)
'';
2019-04-02 10:15:12 +00:00
};
upScript = mkOption {
2023-11-01 18:47:51 +00:00
type = with types; nullOr lines;
default = null;
description = mdDoc ''
Run specified command or script after the tunnel device has been opened.
'';
2019-04-02 10:15:12 +00:00
};
};
2023-11-01 18:47:51 +00:00
}));
2019-04-02 10:15:12 +00:00
};
};
2023-11-01 18:47:51 +00: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.
Offending options:
${lib.concatStringsSep "\n" services}
'')
];
systemd.services = lib.mkMerge (
lib.mapAttrsToList (name: qtcfg: {
2019-04-02 10:15:12 +00: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 18:47:51 +00: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 18:47:51 +00: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 18:47:51 +00: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 10:15:12 +00:00
};
serviceConfig = {
Type = "simple";
ExecStart = "${pkgs.quicktun}/bin/quicktun.${qtcfg.protocol}";
};
};
}) cfg
);
};
}