2021-12-05 21:06:49 +00:00
|
|
|
{ config, lib, options, pkgs, ... }:
|
2014-04-03 16:54:10 +00:00
|
|
|
|
2014-04-14 14:26:48 +00:00
|
|
|
with lib;
|
2014-04-03 16:54:10 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.syncthing;
|
2021-12-05 21:06:49 +00:00
|
|
|
opt = options.services.syncthing;
|
2016-03-31 23:26:52 +00:00
|
|
|
defaultUser = "syncthing";
|
2021-07-29 13:20:30 +00:00
|
|
|
defaultGroup = defaultUser;
|
2019-04-21 21:05:07 +00:00
|
|
|
|
|
|
|
devices = mapAttrsToList (name: device: {
|
|
|
|
deviceID = device.id;
|
2021-08-29 09:14:55 +00:00
|
|
|
inherit (device) name addresses introducer autoAcceptFolders;
|
2021-07-28 09:14:09 +00:00
|
|
|
}) cfg.devices;
|
2019-04-21 21:05:07 +00:00
|
|
|
|
|
|
|
folders = mapAttrsToList ( _: folder: {
|
|
|
|
inherit (folder) path id label type;
|
2021-07-28 09:14:09 +00:00
|
|
|
devices = map (device: { deviceId = cfg.devices.${device}.id; }) folder.devices;
|
2019-04-21 21:05:07 +00:00
|
|
|
rescanIntervalS = folder.rescanInterval;
|
|
|
|
fsWatcherEnabled = folder.watch;
|
|
|
|
fsWatcherDelayS = folder.watchDelay;
|
|
|
|
ignorePerms = folder.ignorePerms;
|
2020-08-30 07:53:59 +00:00
|
|
|
ignoreDelete = folder.ignoreDelete;
|
2019-11-14 15:51:38 +00:00
|
|
|
versioning = folder.versioning;
|
2019-05-21 20:24:36 +00:00
|
|
|
}) (filterAttrs (
|
|
|
|
_: folder:
|
|
|
|
folder.enable
|
2021-07-28 09:14:09 +00:00
|
|
|
) cfg.folders);
|
2019-04-21 21:05:07 +00:00
|
|
|
|
|
|
|
updateConfig = pkgs.writers.writeDash "merge-syncthing-config" ''
|
|
|
|
set -efu
|
2021-07-27 16:15:41 +00:00
|
|
|
|
2022-07-23 11:57:25 +00:00
|
|
|
# be careful not to leak secrets in the filesystem or in process listings
|
|
|
|
|
|
|
|
umask 0077
|
|
|
|
|
2021-07-27 16:15:41 +00:00
|
|
|
# get the api key by parsing the config.xml
|
|
|
|
while
|
2022-07-23 11:57:25 +00:00
|
|
|
! ${pkgs.libxml2}/bin/xmllint \
|
2021-07-27 16:15:41 +00:00
|
|
|
--xpath 'string(configuration/gui/apikey)' \
|
2022-07-23 11:57:25 +00:00
|
|
|
${cfg.configDir}/config.xml \
|
|
|
|
>"$RUNTIME_DIRECTORY/api_key"
|
2021-07-27 16:15:41 +00:00
|
|
|
do sleep 1; done
|
|
|
|
|
2022-07-23 11:57:25 +00:00
|
|
|
(printf "X-API-Key: "; cat "$RUNTIME_DIRECTORY/api_key") >"$RUNTIME_DIRECTORY/headers"
|
|
|
|
|
2021-07-27 16:15:41 +00:00
|
|
|
curl() {
|
2022-07-23 11:57:25 +00:00
|
|
|
${pkgs.curl}/bin/curl -sSLk -H "@$RUNTIME_DIRECTORY/headers" \
|
2021-08-01 12:44:46 +00:00
|
|
|
--retry 1000 --retry-delay 1 --retry-all-errors \
|
|
|
|
"$@"
|
2021-07-27 16:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# query the old config
|
|
|
|
old_cfg=$(curl ${cfg.guiAddress}/rest/config)
|
|
|
|
|
|
|
|
# generate the new config by merging with the NixOS config options
|
2021-08-29 20:08:36 +00:00
|
|
|
new_cfg=$(printf '%s\n' "$old_cfg" | ${pkgs.jq}/bin/jq -c '. * {
|
2021-07-28 09:14:09 +00:00
|
|
|
"devices": (${builtins.toJSON devices}${optionalString (! cfg.overrideDevices) " + .devices"}),
|
|
|
|
"folders": (${builtins.toJSON folders}${optionalString (! cfg.overrideFolders) " + .folders"})
|
|
|
|
} * ${builtins.toJSON cfg.extraOptions}')
|
2021-07-27 16:15:41 +00:00
|
|
|
|
|
|
|
# send the new config
|
|
|
|
curl -X PUT -d "$new_cfg" ${cfg.guiAddress}/rest/config
|
|
|
|
|
|
|
|
# restart Syncthing if required
|
|
|
|
if curl ${cfg.guiAddress}/rest/config/restart-required |
|
|
|
|
${pkgs.jq}/bin/jq -e .requiresRestart > /dev/null; then
|
|
|
|
curl -X POST ${cfg.guiAddress}/rest/system/restart
|
|
|
|
fi
|
2019-04-21 21:05:07 +00:00
|
|
|
'';
|
2016-09-26 07:40:21 +00:00
|
|
|
in {
|
2014-04-03 16:54:10 +00:00
|
|
|
###### interface
|
|
|
|
options = {
|
|
|
|
services.syncthing = {
|
|
|
|
|
2021-07-29 13:20:30 +00:00
|
|
|
enable = mkEnableOption
|
|
|
|
"Syncthing, a self-hosted open-source alternative to Dropbox and Bittorrent Sync";
|
2014-04-03 16:54:10 +00:00
|
|
|
|
2021-07-28 09:14:09 +00:00
|
|
|
cert = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2022-06-03 19:47:57 +00:00
|
|
|
description = mdDoc ''
|
|
|
|
Path to the `cert.pem` file, which will be copied into Syncthing's
|
|
|
|
[configDir](#opt-services.syncthing.configDir).
|
2021-07-28 09:14:09 +00:00
|
|
|
'';
|
|
|
|
};
|
2019-04-21 21:05:07 +00:00
|
|
|
|
2021-07-28 09:14:09 +00:00
|
|
|
key = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2022-06-03 19:47:57 +00:00
|
|
|
description = mdDoc ''
|
|
|
|
Path to the `key.pem` file, which will be copied into Syncthing's
|
|
|
|
[configDir](#opt-services.syncthing.configDir).
|
2021-07-28 09:14:09 +00:00
|
|
|
'';
|
|
|
|
};
|
2019-04-21 21:05:07 +00:00
|
|
|
|
2021-07-28 09:14:09 +00:00
|
|
|
overrideDevices = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-06-03 19:47:57 +00:00
|
|
|
description = mdDoc ''
|
2021-07-28 09:14:09 +00:00
|
|
|
Whether to delete the devices which are not configured via the
|
2022-06-03 19:47:57 +00:00
|
|
|
[devices](#opt-services.syncthing.devices) option.
|
|
|
|
If set to `false`, devices added via the web
|
2021-07-29 13:20:30 +00:00
|
|
|
interface will persist and will have to be deleted manually.
|
2021-07-28 09:14:09 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
devices = mkOption {
|
|
|
|
default = {};
|
2022-06-03 19:47:57 +00:00
|
|
|
description = mdDoc ''
|
2021-07-28 09:14:09 +00:00
|
|
|
Peers/devices which Syncthing should communicate with.
|
2021-07-29 13:20:30 +00:00
|
|
|
|
|
|
|
Note that you can still add devices manually, but those changes
|
2022-06-03 19:47:57 +00:00
|
|
|
will be reverted on restart if [overrideDevices](#opt-services.syncthing.overrideDevices)
|
2021-07-29 13:20:30 +00:00
|
|
|
is enabled.
|
2021-07-28 09:14:09 +00:00
|
|
|
'';
|
|
|
|
example = {
|
|
|
|
bigbox = {
|
|
|
|
id = "7CFNTQM-IMTJBHJ-3UWRDIU-ZGQJFR6-VCXZ3NB-XUH3KZO-N52ITXR-LAIYUAU";
|
|
|
|
addresses = [ "tcp://192.168.0.10:51820" ];
|
|
|
|
};
|
2019-04-21 21:05:07 +00:00
|
|
|
};
|
2021-07-28 09:14:09 +00:00
|
|
|
type = types.attrsOf (types.submodule ({ name, ... }: {
|
|
|
|
options = {
|
|
|
|
|
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = name;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-07-29 13:20:30 +00:00
|
|
|
The name of the device.
|
2021-07-28 09:14:09 +00:00
|
|
|
'';
|
|
|
|
};
|
2019-04-21 21:05:07 +00:00
|
|
|
|
2021-07-28 09:14:09 +00:00
|
|
|
addresses = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-07-28 09:14:09 +00:00
|
|
|
The addresses used to connect to the device.
|
2021-07-29 13:20:30 +00:00
|
|
|
If this is left empty, dynamic configuration is attempted.
|
2021-07-28 09:14:09 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
id = mkOption {
|
|
|
|
type = types.str;
|
2022-06-03 19:47:57 +00:00
|
|
|
description = mdDoc ''
|
|
|
|
The device ID. See <https://docs.syncthing.net/dev/device-ids.html>.
|
2021-07-28 09:14:09 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
introducer = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-06-03 19:47:57 +00:00
|
|
|
description = mdDoc ''
|
2021-07-29 13:20:30 +00:00
|
|
|
Whether the device should act as an introducer and be allowed
|
2021-07-28 09:14:09 +00:00
|
|
|
to add folders on this computer.
|
2022-06-03 19:47:57 +00:00
|
|
|
See <https://docs.syncthing.net/users/introducer.html>.
|
2021-07-28 09:14:09 +00:00
|
|
|
'';
|
2019-05-26 07:20:12 +00:00
|
|
|
};
|
2021-07-28 09:14:09 +00:00
|
|
|
|
2021-08-29 09:14:55 +00:00
|
|
|
autoAcceptFolders = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-06-03 19:47:57 +00:00
|
|
|
description = mdDoc ''
|
2021-08-29 09:14:55 +00:00
|
|
|
Automatically create or share folders that this device advertises at the default path.
|
2022-06-03 19:47:57 +00:00
|
|
|
See <https://docs.syncthing.net/users/config.html?highlight=autoaccept#config-file-format>.
|
2021-08-29 09:14:55 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-05-26 07:20:12 +00:00
|
|
|
};
|
2021-07-28 09:14:09 +00:00
|
|
|
}));
|
|
|
|
};
|
2019-04-21 21:05:07 +00:00
|
|
|
|
2021-07-28 09:14:09 +00:00
|
|
|
overrideFolders = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-06-03 19:47:57 +00:00
|
|
|
description = mdDoc ''
|
2021-07-28 09:14:09 +00:00
|
|
|
Whether to delete the folders which are not configured via the
|
2022-06-03 19:47:57 +00:00
|
|
|
[folders](#opt-services.syncthing.folders) option.
|
|
|
|
If set to `false`, folders added via the web
|
2021-07-29 13:20:30 +00:00
|
|
|
interface will persist and will have to be deleted manually.
|
2021-07-28 09:14:09 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
folders = mkOption {
|
|
|
|
default = {};
|
2022-06-03 19:47:57 +00:00
|
|
|
description = mdDoc ''
|
2021-07-28 09:14:09 +00:00
|
|
|
Folders which should be shared by Syncthing.
|
2021-07-29 13:20:30 +00:00
|
|
|
|
2022-06-23 18:44:59 +00:00
|
|
|
Note that you can still add folders manually, but those changes
|
|
|
|
will be reverted on restart if [overrideFolders](#opt-services.syncthing.overrideFolders)
|
2021-07-29 13:20:30 +00:00
|
|
|
is enabled.
|
2021-07-28 09:14:09 +00:00
|
|
|
'';
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression ''
|
2021-07-28 09:14:09 +00:00
|
|
|
{
|
|
|
|
"/home/user/sync" = {
|
|
|
|
id = "syncme";
|
|
|
|
devices = [ "bigbox" ];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
type = types.attrsOf (types.submodule ({ name, ... }: {
|
|
|
|
options = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-07-29 13:20:30 +00:00
|
|
|
Whether to share this folder.
|
2021-07-28 09:14:09 +00:00
|
|
|
This option is useful when you want to define all folders
|
|
|
|
in one place, but not every machine should share all folders.
|
|
|
|
'';
|
2019-04-21 21:05:07 +00:00
|
|
|
};
|
|
|
|
|
2021-07-28 09:14:09 +00:00
|
|
|
path = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = name;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-07-28 09:14:09 +00:00
|
|
|
The path to the folder which should be shared.
|
|
|
|
'';
|
|
|
|
};
|
2019-04-21 21:05:07 +00:00
|
|
|
|
2021-07-28 09:14:09 +00:00
|
|
|
id = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = name;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-07-29 13:20:30 +00:00
|
|
|
The ID of the folder. Must be the same on all devices.
|
2021-07-28 09:14:09 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
label = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = name;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-07-28 09:14:09 +00:00
|
|
|
The label of the folder.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
devices = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
2022-06-03 19:47:57 +00:00
|
|
|
description = mdDoc ''
|
2021-07-29 13:20:30 +00:00
|
|
|
The devices this folder should be shared with. Each device must
|
2022-06-03 19:47:57 +00:00
|
|
|
be defined in the [devices](#opt-services.syncthing.devices) option.
|
2021-07-28 09:14:09 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
versioning = mkOption {
|
|
|
|
default = null;
|
2022-06-03 19:47:57 +00:00
|
|
|
description = mdDoc ''
|
2021-07-28 09:14:09 +00:00
|
|
|
How to keep changed/deleted files with Syncthing.
|
|
|
|
There are 4 different types of versioning with different parameters.
|
2022-06-03 19:47:57 +00:00
|
|
|
See <https://docs.syncthing.net/users/versioning.html>.
|
2021-07-28 09:14:09 +00:00
|
|
|
'';
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression ''
|
2021-07-29 13:20:30 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
versioning = {
|
|
|
|
type = "simple";
|
|
|
|
params.keep = "10";
|
2019-11-14 15:51:38 +00:00
|
|
|
};
|
2021-07-29 13:20:30 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
versioning = {
|
|
|
|
type = "trashcan";
|
|
|
|
params.cleanoutDays = "1000";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
{
|
|
|
|
versioning = {
|
|
|
|
type = "staggered";
|
|
|
|
params = {
|
|
|
|
cleanInterval = "3600";
|
|
|
|
maxAge = "31536000";
|
|
|
|
versionsPath = "/syncthing/backup";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
{
|
|
|
|
versioning = {
|
|
|
|
type = "external";
|
|
|
|
params.versionsPath = pkgs.writers.writeBash "backup" '''
|
|
|
|
folderpath="$1"
|
|
|
|
filepath="$2"
|
|
|
|
rm -rf "$folderpath/$filepath"
|
|
|
|
''';
|
|
|
|
};
|
|
|
|
}
|
|
|
|
]
|
|
|
|
'';
|
2021-07-28 09:14:09 +00:00
|
|
|
type = with types; nullOr (submodule {
|
|
|
|
options = {
|
|
|
|
type = mkOption {
|
|
|
|
type = enum [ "external" "simple" "staggered" "trashcan" ];
|
2022-06-03 19:47:57 +00:00
|
|
|
description = mdDoc ''
|
2021-07-29 13:20:30 +00:00
|
|
|
The type of versioning.
|
2022-06-03 19:47:57 +00:00
|
|
|
See <https://docs.syncthing.net/users/versioning.html>.
|
2021-07-28 09:14:09 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
params = mkOption {
|
|
|
|
type = attrsOf (either str path);
|
2022-06-03 19:47:57 +00:00
|
|
|
description = mdDoc ''
|
2021-07-29 13:20:30 +00:00
|
|
|
The parameters for versioning. Structure depends on
|
2022-06-03 19:47:57 +00:00
|
|
|
[versioning.type](#opt-services.syncthing.folders._name_.versioning.type).
|
|
|
|
See <https://docs.syncthing.net/users/versioning.html>.
|
2021-07-28 09:14:09 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
2019-04-21 21:05:07 +00:00
|
|
|
};
|
2021-07-27 16:15:41 +00:00
|
|
|
|
2021-07-28 09:14:09 +00:00
|
|
|
rescanInterval = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 3600;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-07-29 13:20:30 +00:00
|
|
|
How often the folder should be rescanned for changes.
|
2021-07-28 09:14:09 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
type = mkOption {
|
|
|
|
type = types.enum [ "sendreceive" "sendonly" "receiveonly" ];
|
|
|
|
default = "sendreceive";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-07-29 13:20:30 +00:00
|
|
|
Whether to only send changes for this folder, only receive them
|
|
|
|
or both.
|
2021-07-28 09:14:09 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
watch = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-07-28 09:14:09 +00:00
|
|
|
Whether the folder should be watched for changes by inotify.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
watchDelay = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 10;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-07-28 09:14:09 +00:00
|
|
|
The delay after an inotify event is triggered.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
ignorePerms = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-07-29 13:20:30 +00:00
|
|
|
Whether to ignore permission changes.
|
2021-07-28 09:14:09 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
ignoreDelete = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-06-03 19:47:57 +00:00
|
|
|
description = mdDoc ''
|
2021-07-29 13:20:30 +00:00
|
|
|
Whether to skip deleting files that are deleted by peers.
|
2022-06-03 19:47:57 +00:00
|
|
|
See <https://docs.syncthing.net/advanced/folder-ignoredelete.html>.
|
2021-07-28 09:14:09 +00:00
|
|
|
'';
|
|
|
|
};
|
2021-07-27 16:15:41 +00:00
|
|
|
};
|
2021-07-28 09:14:09 +00:00
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
extraOptions = mkOption {
|
|
|
|
type = types.addCheck (pkgs.formats.json {}).type isAttrs;
|
|
|
|
default = {};
|
2022-06-03 19:47:57 +00:00
|
|
|
description = mdDoc ''
|
2021-07-28 09:14:09 +00:00
|
|
|
Extra configuration options for Syncthing.
|
2022-06-03 19:47:57 +00:00
|
|
|
See <https://docs.syncthing.net/users/config.html>.
|
2021-07-28 09:14:09 +00:00
|
|
|
'';
|
|
|
|
example = {
|
|
|
|
options.localAnnounceEnabled = false;
|
|
|
|
gui.theme = "black";
|
2021-07-27 16:15:41 +00:00
|
|
|
};
|
2019-04-21 21:05:07 +00:00
|
|
|
};
|
|
|
|
|
2018-10-09 20:13:58 +00:00
|
|
|
guiAddress = mkOption {
|
2018-10-14 04:46:36 +00:00
|
|
|
type = types.str;
|
2018-10-09 20:13:58 +00:00
|
|
|
default = "127.0.0.1:8384";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-07-29 13:20:30 +00:00
|
|
|
The address to serve the web interface at.
|
2018-10-09 20:13:58 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-04-14 08:42:04 +00:00
|
|
|
systemService = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-07-29 13:20:30 +00:00
|
|
|
Whether to auto-launch Syncthing as a system service.
|
|
|
|
'';
|
2016-04-14 08:42:04 +00:00
|
|
|
};
|
|
|
|
|
2014-04-03 16:54:10 +00:00
|
|
|
user = mkOption {
|
2018-10-14 04:46:36 +00:00
|
|
|
type = types.str;
|
2016-03-31 23:26:52 +00:00
|
|
|
default = defaultUser;
|
2021-07-29 13:20:30 +00:00
|
|
|
example = "yourUser";
|
2022-06-03 19:47:57 +00:00
|
|
|
description = mdDoc ''
|
2021-07-29 13:20:30 +00:00
|
|
|
The user to run Syncthing as.
|
2022-06-03 19:47:57 +00:00
|
|
|
By default, a user named `${defaultUser}` will be created.
|
2016-04-14 08:42:04 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
group = mkOption {
|
2018-10-14 04:46:36 +00:00
|
|
|
type = types.str;
|
2021-07-29 13:20:30 +00:00
|
|
|
default = defaultGroup;
|
|
|
|
example = "yourGroup";
|
2022-06-03 19:47:57 +00:00
|
|
|
description = mdDoc ''
|
2021-07-29 13:20:30 +00:00
|
|
|
The group to run Syncthing under.
|
2022-06-03 19:47:57 +00:00
|
|
|
By default, a group named `${defaultGroup}` will be created.
|
2014-04-03 16:54:10 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-12-15 12:07:40 +00:00
|
|
|
all_proxy = mkOption {
|
2018-10-14 04:46:36 +00:00
|
|
|
type = with types; nullOr str;
|
2016-03-31 23:26:52 +00:00
|
|
|
default = null;
|
2015-12-15 12:07:40 +00:00
|
|
|
example = "socks5://address.com:1234";
|
2022-06-03 19:47:57 +00:00
|
|
|
description = mdDoc ''
|
2021-07-29 13:20:30 +00:00
|
|
|
Overwrites the all_proxy environment variable for the Syncthing process to
|
|
|
|
the given value. This is normally used to let Syncthing connect
|
|
|
|
through a SOCKS5 proxy server.
|
2022-06-03 19:47:57 +00:00
|
|
|
See <https://docs.syncthing.net/users/proxying.html>.
|
2015-12-15 12:07:40 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-04-03 16:54:10 +00:00
|
|
|
dataDir = mkOption {
|
2016-03-31 23:26:52 +00:00
|
|
|
type = types.path;
|
2014-04-03 16:54:10 +00:00
|
|
|
default = "/var/lib/syncthing";
|
2021-07-29 13:20:30 +00:00
|
|
|
example = "/home/yourUser";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-07-29 13:20:30 +00:00
|
|
|
The path where synchronised directories will exist.
|
2018-10-08 21:24:36 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-07-29 13:20:30 +00:00
|
|
|
configDir = let
|
|
|
|
cond = versionAtLeast config.system.stateVersion "19.03";
|
|
|
|
in mkOption {
|
2018-10-08 21:24:36 +00:00
|
|
|
type = types.path;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-07-29 13:20:30 +00:00
|
|
|
The path where the settings and keys will exist.
|
2014-04-03 16:54:10 +00:00
|
|
|
'';
|
2021-10-03 16:06:03 +00:00
|
|
|
default = cfg.dataDir + optionalString cond "/.config/syncthing";
|
2022-06-03 19:47:57 +00:00
|
|
|
defaultText = literalMD ''
|
|
|
|
* if `stateVersion >= 19.03`:
|
|
|
|
|
|
|
|
config.${opt.dataDir} + "/.config/syncthing"
|
|
|
|
* otherwise:
|
|
|
|
|
|
|
|
config.${opt.dataDir}
|
2021-12-05 21:06:49 +00:00
|
|
|
'';
|
2014-04-03 16:54:10 +00:00
|
|
|
};
|
|
|
|
|
2021-08-29 09:20:02 +00:00
|
|
|
extraFlags = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
example = [ "--reset-deltas" ];
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-08-29 09:20:02 +00:00
|
|
|
Extra flags passed to the syncthing command in the service definition.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-09-26 07:40:21 +00:00
|
|
|
openDefaultPorts = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2021-07-29 13:20:30 +00:00
|
|
|
example = true;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-01-18 01:40:06 +00:00
|
|
|
Whether to open the default ports in the firewall: TCP/UDP 22000 for transfers
|
2021-07-29 13:20:30 +00:00
|
|
|
and 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 you are running only a single instance on this machine
|
|
|
|
using the default ports, enable this.
|
2016-09-26 07:40:21 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-11-28 19:17:49 +00:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.syncthing;
|
2021-10-03 16:06:03 +00:00
|
|
|
defaultText = literalExpression "pkgs.syncthing";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-07-29 13:20:30 +00:00
|
|
|
The Syncthing package to use.
|
2015-11-28 19:17:49 +00:00
|
|
|
'';
|
|
|
|
};
|
2014-04-03 16:54:10 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-02-23 18:59:06 +00:00
|
|
|
imports = [
|
2021-07-28 09:14:09 +00:00
|
|
|
(mkRemovedOptionModule [ "services" "syncthing" "useInotify" ] ''
|
2021-07-27 16:15:41 +00:00
|
|
|
This option was removed because Syncthing now has the inotify functionality included under the name "fswatcher".
|
2021-07-29 13:20:30 +00:00
|
|
|
It can be enabled on a per-folder basis through the web interface.
|
2018-02-23 18:59:06 +00:00
|
|
|
'')
|
2021-07-28 09:14:09 +00:00
|
|
|
] ++ map (o:
|
|
|
|
mkRenamedOptionModule [ "services" "syncthing" "declarative" o ] [ "services" "syncthing" o ]
|
|
|
|
) [ "cert" "key" "devices" "folders" "overrideDevices" "overrideFolders" "extraOptions"];
|
2014-04-03 16:54:10 +00:00
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
2014-04-04 08:46:19 +00:00
|
|
|
config = mkIf cfg.enable {
|
2014-04-03 16:54:10 +00:00
|
|
|
|
2016-09-26 07:40:21 +00:00
|
|
|
networking.firewall = mkIf cfg.openDefaultPorts {
|
|
|
|
allowedTCPPorts = [ 22000 ];
|
2022-01-18 01:40:06 +00:00
|
|
|
allowedUDPPorts = [ 21027 22000 ];
|
2016-09-26 07:40:21 +00:00
|
|
|
};
|
|
|
|
|
2018-02-23 18:59:06 +00:00
|
|
|
systemd.packages = [ pkgs.syncthing ];
|
2016-09-26 07:40:21 +00:00
|
|
|
|
2019-07-29 18:56:12 +00:00
|
|
|
users.users = mkIf (cfg.systemService && cfg.user == defaultUser) {
|
2019-08-13 21:52:01 +00:00
|
|
|
${defaultUser} =
|
2016-04-14 08:42:04 +00:00
|
|
|
{ group = cfg.group;
|
2016-03-31 23:26:52 +00:00
|
|
|
home = cfg.dataDir;
|
|
|
|
createHome = true;
|
|
|
|
uid = config.ids.uids.syncthing;
|
|
|
|
description = "Syncthing daemon user";
|
|
|
|
};
|
2019-07-29 18:56:12 +00:00
|
|
|
};
|
2016-03-31 23:26:52 +00:00
|
|
|
|
2021-07-29 13:20:30 +00:00
|
|
|
users.groups = mkIf (cfg.systemService && cfg.group == defaultGroup) {
|
|
|
|
${defaultGroup}.gid =
|
2016-03-31 23:26:52 +00:00
|
|
|
config.ids.gids.syncthing;
|
|
|
|
};
|
|
|
|
|
2016-08-06 15:20:18 +00:00
|
|
|
systemd.services = {
|
2016-09-26 07:40:21 +00:00
|
|
|
syncthing = mkIf cfg.systemService {
|
|
|
|
description = "Syncthing service";
|
|
|
|
after = [ "network.target" ];
|
|
|
|
environment = {
|
|
|
|
STNORESTART = "yes";
|
|
|
|
STNOUPGRADE = "yes";
|
|
|
|
inherit (cfg) all_proxy;
|
|
|
|
} // config.networking.proxy.envVars;
|
2016-04-14 08:42:04 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2016-09-26 07:40:21 +00:00
|
|
|
serviceConfig = {
|
|
|
|
Restart = "on-failure";
|
|
|
|
SuccessExitStatus = "2 3 4";
|
|
|
|
RestartForceExitStatus="3 4";
|
2016-04-14 08:42:04 +00:00
|
|
|
User = cfg.user;
|
2016-09-26 07:40:21 +00:00
|
|
|
Group = cfg.group;
|
2021-07-28 09:14:09 +00:00
|
|
|
ExecStartPre = mkIf (cfg.cert != null || cfg.key != null)
|
2019-04-21 21:05:07 +00:00
|
|
|
"+${pkgs.writers.writeBash "syncthing-copy-keys" ''
|
2019-07-10 13:29:25 +00:00
|
|
|
install -dm700 -o ${cfg.user} -g ${cfg.group} ${cfg.configDir}
|
2021-07-28 09:14:09 +00:00
|
|
|
${optionalString (cfg.cert != null) ''
|
|
|
|
install -Dm400 -o ${cfg.user} -g ${cfg.group} ${toString cfg.cert} ${cfg.configDir}/cert.pem
|
2019-04-21 21:05:07 +00:00
|
|
|
''}
|
2021-07-28 09:14:09 +00:00
|
|
|
${optionalString (cfg.key != null) ''
|
|
|
|
install -Dm400 -o ${cfg.user} -g ${cfg.group} ${toString cfg.key} ${cfg.configDir}/key.pem
|
2019-04-21 21:05:07 +00:00
|
|
|
''}
|
|
|
|
''}"
|
|
|
|
;
|
2018-10-09 20:13:58 +00:00
|
|
|
ExecStart = ''
|
|
|
|
${cfg.package}/bin/syncthing \
|
|
|
|
-no-browser \
|
|
|
|
-gui-address=${cfg.guiAddress} \
|
2021-08-29 09:20:02 +00:00
|
|
|
-home=${cfg.configDir} ${escapeShellArgs cfg.extraFlags}
|
2018-10-09 20:13:58 +00:00
|
|
|
'';
|
2020-01-20 20:15:47 +00:00
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
PrivateMounts = true;
|
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateUsers = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
RestrictNamespaces = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictSUIDSGID = true;
|
|
|
|
CapabilityBoundingSet = [
|
|
|
|
"~CAP_SYS_PTRACE" "~CAP_SYS_ADMIN"
|
|
|
|
"~CAP_SETGID" "~CAP_SETUID" "~CAP_SETPCAP"
|
|
|
|
"~CAP_SYS_TIME" "~CAP_KILL"
|
|
|
|
];
|
2014-04-03 16:54:10 +00:00
|
|
|
};
|
2016-09-26 07:40:21 +00:00
|
|
|
};
|
2019-05-28 13:38:10 +00:00
|
|
|
syncthing-init = mkIf (
|
2021-07-28 09:14:09 +00:00
|
|
|
cfg.devices != {} || cfg.folders != {} || cfg.extraOptions != {}
|
2019-05-28 13:38:10 +00:00
|
|
|
) {
|
2021-07-27 16:15:41 +00:00
|
|
|
description = "Syncthing configuration updater";
|
2021-08-01 12:44:46 +00:00
|
|
|
requisite = [ "syncthing.service" ];
|
2019-04-21 21:05:07 +00:00
|
|
|
after = [ "syncthing.service" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
User = cfg.user;
|
|
|
|
RemainAfterExit = true;
|
2022-07-23 11:57:25 +00:00
|
|
|
RuntimeDirectory = "syncthing-init";
|
2019-04-21 21:05:07 +00:00
|
|
|
Type = "oneshot";
|
|
|
|
ExecStart = updateConfig;
|
|
|
|
};
|
|
|
|
};
|
2014-04-03 16:54:10 +00:00
|
|
|
|
2016-09-26 07:40:21 +00:00
|
|
|
syncthing-resume = {
|
|
|
|
wantedBy = [ "suspend.target" ];
|
2016-04-14 08:42:04 +00:00
|
|
|
};
|
2016-08-06 15:20:18 +00:00
|
|
|
};
|
2014-04-03 16:54:10 +00:00
|
|
|
};
|
|
|
|
}
|