···2525 folder.enable
2626 ) cfg.declarative.folders);
27272828- # get the api key by parsing the config.xml
2929- getApiKey = pkgs.writers.writeDash "getAPIKey" ''
3030- ${pkgs.libxml2}/bin/xmllint \
3131- --xpath 'string(configuration/gui/apikey)'\
3232- ${cfg.configDir}/config.xml
3333- '';
3434-3528 updateConfig = pkgs.writers.writeDash "merge-syncthing-config" ''
3629 set -efu
3737- # wait for syncthing port to open
3838- until ${pkgs.curl}/bin/curl -Ss ${cfg.guiAddress} -o /dev/null; do
3939- sleep 1
4040- done
41304242- API_KEY=$(${getApiKey})
4343- OLD_CFG=$(${pkgs.curl}/bin/curl -Ss \
4444- -H "X-API-Key: $API_KEY" \
4545- ${cfg.guiAddress}/rest/system/config)
3131+ # get the api key by parsing the config.xml
3232+ while
3333+ ! api_key=$(${pkgs.libxml2}/bin/xmllint \
3434+ --xpath 'string(configuration/gui/apikey)' \
3535+ ${cfg.configDir}/config.xml)
3636+ do sleep 1; done
46374747- # generate the new config by merging with the nixos config options
4848- NEW_CFG=$(echo "$OLD_CFG" | ${pkgs.jq}/bin/jq -s '.[] as $in | $in * {
4949- "devices": (${builtins.toJSON devices}${optionalString (! cfg.declarative.overrideDevices) " + $in.devices"}),
5050- "folders": (${builtins.toJSON folders}${optionalString (! cfg.declarative.overrideFolders) " + $in.folders"})
5151- }')
3838+ curl() {
3939+ while
4040+ ${pkgs.curl}/bin/curl -Ss -H "X-API-Key: $api_key" \
4141+ --retry 100 --retry-delay 1 --retry-connrefused "$@"
4242+ status=$?
4343+ [ "$status" -eq 52 ] # retry on empty reply from server
4444+ do sleep 1; done
4545+ return "$status"
4646+ }
52475353- # POST the new config to syncthing
5454- echo "$NEW_CFG" | ${pkgs.curl}/bin/curl -Ss \
5555- -H "X-API-Key: $API_KEY" \
5656- ${cfg.guiAddress}/rest/system/config -d @-
4848+ # query the old config
4949+ old_cfg=$(curl ${cfg.guiAddress}/rest/config)
57505858- # restart syncthing after sending the new config
5959- ${pkgs.curl}/bin/curl -Ss \
6060- -H "X-API-Key: $API_KEY" \
6161- -X POST \
6262- ${cfg.guiAddress}/rest/system/restart
5151+ # generate the new config by merging with the NixOS config options
5252+ new_cfg=$(echo "$old_cfg" | ${pkgs.jq}/bin/jq -c '. * {
5353+ "devices": (${builtins.toJSON devices}${optionalString (! cfg.declarative.overrideDevices) " + .devices"}),
5454+ "folders": (${builtins.toJSON folders}${optionalString (! cfg.declarative.overrideFolders) " + .folders"})
5555+ } * ${builtins.toJSON cfg.declarative.extraOptions}')
5656+5757+ # send the new config
5858+ curl -X PUT -d "$new_cfg" ${cfg.guiAddress}/rest/config
5959+6060+ # restart Syncthing if required
6161+ if curl ${cfg.guiAddress}/rest/config/restart-required |
6262+ ${pkgs.jq}/bin/jq -e .requiresRestart > /dev/null; then
6363+ curl -X POST ${cfg.guiAddress}/rest/system/restart
6464+ fi
6365 '';
6466in {
6567 ###### interface
···7779 type = types.nullOr types.str;
7880 default = null;
7981 description = ''
8080- Path to users cert.pem file, will be copied into the syncthing's
8282+ Path to users cert.pem file, will be copied into Syncthing's
8183 <literal>configDir</literal>
8284 '';
8385 };
···8688 type = types.nullOr types.str;
8789 default = null;
8890 description = ''
8989- Path to users key.pem file, will be copied into the syncthing's
9191+ Path to users key.pem file, will be copied into Syncthing's
9092 <literal>configDir</literal>
9193 '';
9294 };
···105107 devices = mkOption {
106108 default = {};
107109 description = ''
108108- Peers/devices which syncthing should communicate with.
110110+ Peers/devices which Syncthing should communicate with.
109111 '';
110112 example = {
111113 bigbox = {
···168170 folders = mkOption {
169171 default = {};
170172 description = ''
171171- folders which should be shared by syncthing.
173173+ Folders which should be shared by Syncthing.
172174 '';
173175 example = literalExample ''
174176 {
···227229 versioning = mkOption {
228230 default = null;
229231 description = ''
230230- How to keep changed/deleted files with syncthing.
232232+ How to keep changed/deleted files with Syncthing.
231233 There are 4 different types of versioning with different parameters.
232234 See https://docs.syncthing.net/users/versioning.html
233235 '';
···335337 upstream's docs</link>.
336338 '';
337339 };
338338-339340 };
340341 }));
341342 };
343343+344344+ extraOptions = mkOption {
345345+ type = types.addCheck (pkgs.formats.json {}).type isAttrs;
346346+ default = {};
347347+ description = ''
348348+ Extra configuration options for Syncthing.
349349+ '';
350350+ example = {
351351+ options.localAnnounceEnabled = false;
352352+ gui.theme = "black";
353353+ };
354354+ };
342355 };
343356344357 guiAddress = mkOption {
···378391 default = null;
379392 example = "socks5://address.com:1234";
380393 description = ''
381381- Overwrites all_proxy environment variable for the syncthing process to
394394+ Overwrites all_proxy environment variable for the Syncthing process to
382395 the given value. This is normaly used to let relay client connect
383396 through SOCKS5 proxy server.
384397 '';
···412425 Open the default ports in the firewall:
413426 - TCP 22000 for transfers
414427 - UDP 21027 for discovery
415415- 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.
428428+ 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.
416429 Alternatively, if are running only a single instance on this machine using the default ports, enable this.
417430 '';
418431 };
···431444432445 imports = [
433446 (mkRemovedOptionModule ["services" "syncthing" "useInotify"] ''
434434- This option was removed because syncthing now has the inotify functionality included under the name "fswatcher".
447447+ This option was removed because Syncthing now has the inotify functionality included under the name "fswatcher".
435448 It can be enabled on a per-folder basis through the webinterface.
436449 '')
437450 ];
···516529 };
517530 };
518531 syncthing-init = mkIf (
519519- cfg.declarative.devices != {} || cfg.declarative.folders != {}
532532+ cfg.declarative.devices != {} || cfg.declarative.folders != {} || cfg.declarative.extraOptions != {}
520533 ) {
534534+ description = "Syncthing configuration updater";
521535 after = [ "syncthing.service" ];
522536 wantedBy = [ "multi-user.target" ];
523537