1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.syncthing.relay;
7
8 dataDirectory = "/var/lib/syncthing-relay";
9
10 relayOptions =
11 [
12 "--keys=${dataDirectory}"
13 "--listen=${cfg.listenAddress}:${toString cfg.port}"
14 "--status-srv=${cfg.statusListenAddress}:${toString cfg.statusPort}"
15 "--provided-by=${escapeShellArg cfg.providedBy}"
16 ]
17 ++ optional (cfg.pools != null) "--pools=${escapeShellArg (concatStringsSep "," cfg.pools)}"
18 ++ optional (cfg.globalRateBps != null) "--global-rate=${toString cfg.globalRateBps}"
19 ++ optional (cfg.perSessionRateBps != null) "--per-session-rate=${toString cfg.perSessionRateBps}"
20 ++ cfg.extraOptions;
21in {
22 ###### interface
23
24 options.services.syncthing.relay = {
25 enable = mkEnableOption (lib.mdDoc "Syncthing relay service");
26
27 listenAddress = mkOption {
28 type = types.str;
29 default = "";
30 example = "1.2.3.4";
31 description = lib.mdDoc ''
32 Address to listen on for relay traffic.
33 '';
34 };
35
36 port = mkOption {
37 type = types.port;
38 default = 22067;
39 description = lib.mdDoc ''
40 Port to listen on for relay traffic. This port should be added to
41 `networking.firewall.allowedTCPPorts`.
42 '';
43 };
44
45 statusListenAddress = mkOption {
46 type = types.str;
47 default = "";
48 example = "1.2.3.4";
49 description = lib.mdDoc ''
50 Address to listen on for serving the relay status API.
51 '';
52 };
53
54 statusPort = mkOption {
55 type = types.port;
56 default = 22070;
57 description = lib.mdDoc ''
58 Port to listen on for serving the relay status API. This port should be
59 added to `networking.firewall.allowedTCPPorts`.
60 '';
61 };
62
63 pools = mkOption {
64 type = types.nullOr (types.listOf types.str);
65 default = null;
66 description = lib.mdDoc ''
67 Relay pools to join. If null, uses the default global pool.
68 '';
69 };
70
71 providedBy = mkOption {
72 type = types.str;
73 default = "";
74 description = lib.mdDoc ''
75 Human-readable description of the provider of the relay (you).
76 '';
77 };
78
79 globalRateBps = mkOption {
80 type = types.nullOr types.ints.positive;
81 default = null;
82 description = lib.mdDoc ''
83 Global bandwidth rate limit in bytes per second.
84 '';
85 };
86
87 perSessionRateBps = mkOption {
88 type = types.nullOr types.ints.positive;
89 default = null;
90 description = lib.mdDoc ''
91 Per session bandwidth rate limit in bytes per second.
92 '';
93 };
94
95 extraOptions = mkOption {
96 type = types.listOf types.str;
97 default = [];
98 description = lib.mdDoc ''
99 Extra command line arguments to pass to strelaysrv.
100 '';
101 };
102 };
103
104 ###### implementation
105
106 config = mkIf cfg.enable {
107 systemd.services.syncthing-relay = {
108 description = "Syncthing relay service";
109 wantedBy = [ "multi-user.target" ];
110 after = [ "network.target" ];
111
112 serviceConfig = {
113 DynamicUser = true;
114 StateDirectory = baseNameOf dataDirectory;
115
116 Restart = "on-failure";
117 ExecStart = "${pkgs.syncthing-relay}/bin/strelaysrv ${concatStringsSep " " relayOptions}";
118 };
119 };
120 };
121}