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