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