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

124 lines
2.6 KiB
Nix
Raw Normal View History

{
config,
lib,
pkgs,
...
}:
2019-06-09 11:19:42 +03:00
with lib;
let
cfg = config.services.mtprotoproxy;
configOpts =
{
PORT = cfg.port;
USERS = cfg.users;
SECURE_ONLY = cfg.secureOnly;
}
// lib.optionalAttrs (cfg.adTag != null) { AD_TAG = cfg.adTag; }
2019-06-09 11:19:42 +03:00
// cfg.extraConfig;
convertOption =
opt:
2019-06-09 11:19:42 +03:00
if isString opt || isInt opt then
builtins.toJSON opt
else if isBool opt then
if opt then "True" else "False"
else if isList opt then
"[" + concatMapStringsSep "," convertOption opt + "]"
else if isAttrs opt then
"{"
+ concatStringsSep "," (
mapAttrsToList (name: opt: "${builtins.toJSON name}: ${convertOption opt}") opt
)
+ "}"
2019-06-09 11:19:42 +03:00
else
throw "Invalid option type";
configFile = pkgs.writeText "config.py" (
concatStringsSep "\n" (mapAttrsToList (name: opt: "${name} = ${convertOption opt}") configOpts)
);
2019-06-09 11:19:42 +03:00
in
{
###### interface
options = {
services.mtprotoproxy = {
enable = mkEnableOption "mtprotoproxy";
2019-06-09 11:19:42 +03:00
port = mkOption {
type = types.port;
2019-06-09 11:19:42 +03:00
default = 3256;
description = ''
2019-06-09 11:19:42 +03:00
TCP port to accept mtproto connections on.
'';
};
users = mkOption {
type = types.attrsOf types.str;
example = {
2019-08-13 21:52:01 +00:00
tg = "00000000000000000000000000000000";
tg2 = "0123456789abcdef0123456789abcdef";
2019-06-09 11:19:42 +03:00
};
description = ''
2019-06-09 11:19:42 +03:00
Allowed users and their secrets. A secret is a 32 characters long hex string.
'';
};
secureOnly = mkOption {
type = types.bool;
default = true;
description = ''
2019-06-09 11:19:42 +03:00
Don't allow users to connect in non-secure mode (without random padding).
'';
};
adTag = mkOption {
type = types.nullOr types.str;
default = null;
# Taken from mtproxyproto's repo.
example = "3c09c680b76ee91a4c25ad51f742267d";
description = ''
2019-06-09 11:19:42 +03:00
Tag for advertising that can be obtained from @MTProxybot.
'';
};
extraConfig = mkOption {
type = types.attrs;
default = { };
2019-06-09 11:19:42 +03:00
example = {
2019-08-13 21:52:01 +00:00
STATS_PRINT_PERIOD = 600;
2019-06-09 11:19:42 +03:00
};
description = ''
2019-06-09 11:19:42 +03:00
Extra configuration options for mtprotoproxy.
'';
};
};
};
###### implementation
config = mkIf cfg.enable {
systemd.services.mtprotoproxy = {
description = "MTProto Proxy Daemon";
wantedBy = [ "multi-user.target" ];
serviceConfig = {
ExecStart = "${pkgs.mtprotoproxy}/bin/mtprotoproxy ${configFile}";
DynamicUser = true;
};
};
};
}