nixos/syncthing: support syncthing gui and api over unix sockets (#247343)

This commit is contained in:
digital 2023-10-25 09:18:42 +02:00 committed by GitHub
parent 855223de8f
commit a6253e6b12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,6 +10,21 @@ let
settingsFormat = pkgs.formats.json { }; settingsFormat = pkgs.formats.json { };
cleanedConfig = converge (filterAttrsRecursive (_: v: v != null && v != {})) cfg.settings; cleanedConfig = converge (filterAttrsRecursive (_: v: v != null && v != {})) cfg.settings;
isUnixGui = (builtins.substring 0 1 cfg.guiAddress) == "/";
# Syncthing supports serving the GUI over Unix sockets. If that happens, the
# API is served over the Unix socket as well. This function returns the correct
# curl arguments for the address portion of the curl command for both network
# and Unix socket addresses.
curlAddressArgs = path: if isUnixGui
# if cfg.guiAddress is a unix socket, tell curl explicitly about it
# note that the dot in front of `${path}` is the hostname, which is
# required.
then "--unix-socket ${cfg.guiAddress} http://.${path}"
# no adjustements are needed if cfg.guiAddress is a network address
else "${cfg.guiAddress}${path}"
;
devices = mapAttrsToList (_: device: device // { devices = mapAttrsToList (_: device: device // {
deviceID = device.id; deviceID = device.id;
}) cfg.settings.devices; }) cfg.settings.devices;
@ -62,14 +77,14 @@ let
GET_IdAttrName = "deviceID"; GET_IdAttrName = "deviceID";
override = cfg.overrideDevices; override = cfg.overrideDevices;
conf = devices; conf = devices;
baseAddress = "${cfg.guiAddress}/rest/config/devices"; baseAddress = curlAddressArgs "/rest/config/devices";
}; };
dirs = { dirs = {
new_conf_IDs = map (v: v.id) folders; new_conf_IDs = map (v: v.id) folders;
GET_IdAttrName = "id"; GET_IdAttrName = "id";
override = cfg.overrideFolders; override = cfg.overrideFolders;
conf = folders; conf = folders;
baseAddress = "${cfg.guiAddress}/rest/config/folders"; baseAddress = curlAddressArgs "/rest/config/folders";
}; };
} [ } [
# Now for each of these attributes, write the curl commands that are # Now for each of these attributes, write the curl commands that are
@ -117,15 +132,14 @@ let
builtins.attrNames builtins.attrNames
(lib.subtractLists ["folders" "devices"]) (lib.subtractLists ["folders" "devices"])
(map (subOption: '' (map (subOption: ''
curl -X PUT -d ${lib.escapeShellArg (builtins.toJSON cleanedConfig.${subOption})} \ curl -X PUT -d ${lib.escapeShellArg (builtins.toJSON cleanedConfig.${subOption})} ${curlAddressArgs "/rest/config/${subOption}"}
${cfg.guiAddress}/rest/config/${subOption}
'')) ''))
(lib.concatStringsSep "\n") (lib.concatStringsSep "\n")
]) + '' ]) + ''
# restart Syncthing if required # restart Syncthing if required
if curl ${cfg.guiAddress}/rest/config/restart-required | if curl ${curlAddressArgs "/rest/config/restart-required"} |
${jq} -e .requiresRestart > /dev/null; then ${jq} -e .requiresRestart > /dev/null; then
curl -X POST ${cfg.guiAddress}/rest/system/restart curl -X POST ${curlAddressArgs "/rest/system/restart"}
fi fi
''); '');
in { in {
@ -651,7 +665,7 @@ in {
ExecStart = '' ExecStart = ''
${cfg.package}/bin/syncthing \ ${cfg.package}/bin/syncthing \
-no-browser \ -no-browser \
-gui-address=${cfg.guiAddress} \ -gui-address=${if isUnixGui then "unix://" else ""}${cfg.guiAddress} \
-home=${cfg.configDir} ${escapeShellArgs cfg.extraFlags} -home=${cfg.configDir} ${escapeShellArgs cfg.extraFlags}
''; '';
MemoryDenyWriteExecute = true; MemoryDenyWriteExecute = true;