summaryrefslogtreecommitdiffstats
path: root/krebs/3modules/syncthing.nix
blob: 4bd526dae18c497cbd7da924908b84e99c69db0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
with import <stockholm/lib>;
{ config, pkgs, ... }: let

  cfg = config.krebs.syncthing;

in {

  options.krebs.syncthing = {

    enable = mkEnableOption "krebs.syncthing";

    systemService = mkOption {
      type = types.bool;
      default = true;
      description = "Auto launch Syncthing as a system service.";
    };

    user = mkOption {
      type = types.user;
      default = {
        name = "syncthing";
        home = "/var/lib/syncthing";
      };
    };

    group = mkOption {
      type = types.group;
      default = {
        name = "syncthing";
      };
    };

    all_proxy = mkOption {
      type = types.nullOr types.string;
      default = null;
      example = "socks5://address.com:1234";
      description = ''
        Overwrites all_proxy environment variable for the syncthing process to
        the given value. This is normaly used to let relay client connect
        through SOCKS5 proxy server.
      '';
    };

    dataDir = mkOption {
      type = types.path;
      default = cfg.user.home;
    };

    openDefaultPorts = mkOption {
      type = types.bool;
      default = false;
      example = literalExample "true";
      description = ''
        Open the default ports in the firewall:
          - TCP 22000 for transfers
          - UDP 21027 for discovery
        If multiple users are running syncthing on this machine, you will need to manually open a set of ports for each instance and leave this disabled.
        Alternatively, if are running only a single instance on this machine using the default ports, enable this.
      '';
    };

    package = mkOption {
      type = types.package;
      default = pkgs.syncthing;
      defaultText = "pkgs.syncthing";
      example = literalExample "pkgs.syncthing";
      description = ''
        Syncthing package to use.
      '';
    };
  };

  config = mkIf cfg.enable {

    networking.firewall = mkIf cfg.openDefaultPorts {
      allowedTCPPorts = [ 22000 ];
      allowedUDPPorts = [ 21027 ];
    };

    systemd.packages = [ pkgs.syncthing ];

    users = mkIf (cfg.user == defaultUser) {
      extraUsers."${defaultUser}" =
        { group = cfg.group;
          home  = cfg.dataDir;
          createHome = true;
          uid = config.ids.uids.syncthing;
          description = "Syncthing daemon user";
        };

      extraGroups."${defaultUser}".gid =
        config.ids.gids.syncthing;
    };

    systemd.services = {
      syncthing = mkIf cfg.systemService {
        description = "Syncthing service";
        after = [ "network.target" ];
        environment = {
          STNORESTART = "yes";
          STNOUPGRADE = "yes";
          inherit (cfg) all_proxy;
        } // config.networking.proxy.envVars;
        wantedBy = [ "multi-user.target" ];
        serviceConfig = {
          Restart = "on-failure";
          SuccessExitStatus = "2 3 4";
          RestartForceExitStatus="3 4";
          User = cfg.user;
          Group = cfg.group;
          PermissionsStartOnly = true;
          ExecStart = "${cfg.package}/bin/syncthing -no-browser -home=${cfg.dataDir}";
        };
      };

      syncthing-resume = {
        wantedBy = [ "suspend.target" ];
      };
    };
  };
}






#    services.syncthing = {
#      enable = true;
#      dataDir =
## drwxr-xr-x 1 syncthing nogroup   78 Sep 11 20:12 index-v0.14.0.db
## drwxr-xr-x 1 syncthing nogroup   18 Sep 11 20:12 Sync
## -rw-r--r-- 1 syncthing nogroup  619 Sep 11 20:12 cert.pem
## -rw------- 1 syncthing nogroup 3.5K Sep 11 20:13 config.xml
## -rw------- 1 syncthing nogroup   33 Sep 11 20:13 csrftokens.txt
## -rw-r--r-- 1 syncthing nogroup 1.1K Sep 11 20:12 https-cert.pem
## -rw------- 1 syncthing nogroup 1.7K Sep 11 20:12 https-key.pem
## -rw------- 1 syncthing nogroup  288 Sep 11 20:12 key.pem
#
#      #dataDir = "/var/lib/syncthing";
#      #group = "nogroup";
#      systemService = true;
#      #user = "syncthing";
#    };

/*
<configuration version="27">
    <folder id="default" label="Default Folder" path="/var/lib/syncthing/Sync" type="readwrite" rescanIntervalS="60" fsWatcherEnabled="false" fsWatcherDelayS="10" ignorePerms="false" autoNormalize="true">
        <filesystemType>basic</filesystemType>
        <device id="EP6B26A-FOFUIJY-ALGPLNB-HGAXJH4-JSGDZTY-EK6LVWT-357ZJN7-SQE27QQ" introducedBy=""></device>
        <minDiskFree unit="%">1</minDiskFree>
        <versioning></versioning>
        <copiers>0</copiers>
        <pullerMaxPendingKiB>0</pullerMaxPendingKiB>
        <hashers>0</hashers>
        <order>random</order>
        <ignoreDelete>false</ignoreDelete>
        <scanProgressIntervalS>0</scanProgressIntervalS>
        <pullerPauseS>0</pullerPauseS>
        <maxConflicts>-1</maxConflicts>
        <disableSparseFiles>false</disableSparseFiles>
        <disableTempIndexes>false</disableTempIndexes>
        <paused>false</paused>
        <weakHashThresholdPct>25</weakHashThresholdPct>
        <markerName>.stfolder</markerName>
    </folder>
    <device id="EP6B26A-FOFUIJY-ALGPLNB-HGAXJH4-JSGDZTY-EK6LVWT-357ZJN7-SQE27QQ" name="xu" compression="metadata" introducer="false" skipIntroductionRemovals="false" introducedBy="">
        <address>dynamic</address>
        <paused>false</paused>
        <autoAcceptFolders>false</autoAcceptFolders>
    </device>
    <gui enabled="true" tls="false" debugging="false">
        <address>127.0.0.1:8384</address>
        <apikey>wPjcG4LurZgdxYVfrS7Ra5oa4w577mCs</apikey>
        <theme>default</theme>
    </gui>
    <options>
        <listenAddress>default</listenAddress>
        <globalAnnounceServer>default</globalAnnounceServer>
        <globalAnnounceEnabled>true</globalAnnounceEnabled>
        <localAnnounceEnabled>true</localAnnounceEnabled>
        <localAnnouncePort>21027</localAnnouncePort>
        <localAnnounceMCAddr>[ff12::8384]:21027</localAnnounceMCAddr>
        <maxSendKbps>0</maxSendKbps>
        <maxRecvKbps>0</maxRecvKbps>
        <reconnectionIntervalS>60</reconnectionIntervalS>
        <relaysEnabled>true</relaysEnabled>
        <relayReconnectIntervalM>10</relayReconnectIntervalM>
        <startBrowser>true</startBrowser>
        <natEnabled>true</natEnabled>
        <natLeaseMinutes>60</natLeaseMinutes>
        <natRenewalMinutes>30</natRenewalMinutes>
        <natTimeoutSeconds>10</natTimeoutSeconds>
        <urAccepted>-1</urAccepted>
        <urSeen>3</urSeen>
        <urUniqueID>ryNit35r</urUniqueID>
        <urURL>https://data.syncthing.net/newdata</urURL>
        <urPostInsecurely>false</urPostInsecurely>
        <urInitialDelayS>1800</urInitialDelayS>
        <restartOnWakeup>true</restartOnWakeup>
        <autoUpgradeIntervalH>12</autoUpgradeIntervalH>
        <upgradeToPreReleases>false</upgradeToPreReleases>
        <keepTemporariesH>24</keepTemporariesH>
        <cacheIgnoredFiles>false</cacheIgnoredFiles>
        <progressUpdateIntervalS>5</progressUpdateIntervalS>
        <limitBandwidthInLan>false</limitBandwidthInLan>
        <minHomeDiskFree unit="%">1</minHomeDiskFree>
        <releasesURL>https://upgrades.syncthing.net/meta.json</releasesURL>
        <overwriteRemoteDeviceNamesOnConnect>false</overwriteRemoteDeviceNamesOnConnect>
        <tempIndexMinBlocks>10</tempIndexMinBlocks>
        <trafficClass>0</trafficClass>
        <weakHashSelectionMethod>auto</weakHashSelectionMethod>
        <defaultFolderPath>~</defaultFolderPath>
        <setLowPriority>true</setLowPriority>
        <minHomeDiskFreePct>0</minHomeDiskFreePct>
    </options>
</configuration>
*/