2014-04-14 15:26:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2014-04-03 17:54:10 +01:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2014-04-03 17:54:10 +01:00
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.syncthing;
|
2016-04-01 00:26:52 +01:00
|
|
|
defaultUser = "syncthing";
|
2016-09-26 08:40:21 +01:00
|
|
|
in {
|
2014-04-03 17:54:10 +01:00
|
|
|
###### interface
|
|
|
|
options = {
|
|
|
|
services.syncthing = {
|
|
|
|
|
2016-04-14 15:44:02 +01:00
|
|
|
enable = mkEnableOption ''
|
|
|
|
Syncthing - the self-hosted open-source alternative
|
|
|
|
to Dropbox and Bittorrent Sync. Initial interface will be
|
|
|
|
available on http://127.0.0.1:8384/.
|
|
|
|
'';
|
2014-04-03 17:54:10 +01:00
|
|
|
|
2016-08-06 16:20:18 +01:00
|
|
|
useInotify = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "Provide syncthing-inotify as a service.";
|
|
|
|
};
|
|
|
|
|
2016-04-14 09:42:04 +01:00
|
|
|
systemService = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = "Auto launch Syncthing as a system service.";
|
|
|
|
};
|
|
|
|
|
2014-04-03 17:54:10 +01:00
|
|
|
user = mkOption {
|
2016-04-01 00:26:52 +01:00
|
|
|
type = types.string;
|
|
|
|
default = defaultUser;
|
2014-04-03 17:54:10 +01:00
|
|
|
description = ''
|
2016-04-14 09:42:04 +01:00
|
|
|
Syncthing will be run under this user (user will be created if it doesn't exist.
|
|
|
|
This can be your user name).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
type = types.string;
|
|
|
|
default = "nogroup";
|
|
|
|
description = ''
|
|
|
|
Syncthing will be run under this group (group will not be created if it doesn't exist.
|
|
|
|
This can be your user name).
|
2014-04-03 17:54:10 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-12-15 12:07:40 +00:00
|
|
|
all_proxy = mkOption {
|
2016-04-01 00:26:52 +01:00
|
|
|
type = types.nullOr types.string;
|
|
|
|
default = null;
|
2015-12-15 12:07:40 +00:00
|
|
|
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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-04-03 17:54:10 +01:00
|
|
|
dataDir = mkOption {
|
2016-04-01 00:26:52 +01:00
|
|
|
type = types.path;
|
2014-04-03 17:54:10 +01:00
|
|
|
default = "/var/lib/syncthing";
|
|
|
|
description = ''
|
2015-08-27 15:01:10 +01:00
|
|
|
Path where the settings and keys will exist.
|
2014-04-03 17:54:10 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-09-26 08:40:21 +01:00
|
|
|
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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-11-28 19:17:49 +00:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.syncthing;
|
2016-01-17 18:34:55 +00:00
|
|
|
defaultText = "pkgs.syncthing";
|
2015-11-28 19:17:49 +00:00
|
|
|
example = literalExample "pkgs.syncthing";
|
|
|
|
description = ''
|
|
|
|
Syncthing package to use.
|
|
|
|
'';
|
|
|
|
};
|
2014-04-03 17:54:10 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
2014-04-04 09:46:19 +01:00
|
|
|
config = mkIf cfg.enable {
|
2014-04-03 17:54:10 +01:00
|
|
|
|
2016-09-26 08:40:21 +01:00
|
|
|
networking.firewall = mkIf cfg.openDefaultPorts {
|
|
|
|
allowedTCPPorts = [ 22000 ];
|
|
|
|
allowedUDPPorts = [ 21027 ];
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.packages = [ pkgs.syncthing ]
|
|
|
|
++ lib.optional cfg.useInotify pkgs.syncthing-inotify;
|
|
|
|
|
2016-04-01 00:26:52 +01:00
|
|
|
users = mkIf (cfg.user == defaultUser) {
|
|
|
|
extraUsers."${defaultUser}" =
|
2016-04-14 09:42:04 +01:00
|
|
|
{ group = cfg.group;
|
2016-04-01 00:26:52 +01:00
|
|
|
home = cfg.dataDir;
|
|
|
|
createHome = true;
|
|
|
|
uid = config.ids.uids.syncthing;
|
|
|
|
description = "Syncthing daemon user";
|
|
|
|
};
|
|
|
|
|
|
|
|
extraGroups."${defaultUser}".gid =
|
|
|
|
config.ids.gids.syncthing;
|
|
|
|
};
|
|
|
|
|
2016-08-06 16:20:18 +01:00
|
|
|
systemd.services = {
|
2016-09-26 08:40:21 +01:00
|
|
|
syncthing = mkIf cfg.systemService {
|
|
|
|
description = "Syncthing service";
|
|
|
|
after = [ "network.target" ];
|
|
|
|
environment = {
|
|
|
|
STNORESTART = "yes";
|
|
|
|
STNOUPGRADE = "yes";
|
|
|
|
inherit (cfg) all_proxy;
|
|
|
|
} // config.networking.proxy.envVars;
|
|
|
|
wants = mkIf cfg.useInotify [ "syncthing-inotify.service" ];
|
2016-04-14 09:42:04 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2016-09-26 08:40:21 +01:00
|
|
|
serviceConfig = {
|
|
|
|
Restart = "on-failure";
|
|
|
|
SuccessExitStatus = "2 3 4";
|
|
|
|
RestartForceExitStatus="3 4";
|
2016-04-14 09:42:04 +01:00
|
|
|
User = cfg.user;
|
2016-09-26 08:40:21 +01:00
|
|
|
Group = cfg.group;
|
|
|
|
PermissionsStartOnly = true;
|
|
|
|
ExecStart = "${cfg.package}/bin/syncthing -no-browser -home=${cfg.dataDir}";
|
2014-04-03 17:54:10 +01:00
|
|
|
};
|
2016-09-26 08:40:21 +01:00
|
|
|
};
|
2014-04-03 17:54:10 +01:00
|
|
|
|
2016-09-26 08:40:21 +01:00
|
|
|
syncthing-resume = {
|
|
|
|
wantedBy = [ "suspend.target" ];
|
2016-04-14 09:42:04 +01:00
|
|
|
};
|
2016-07-21 03:49:58 +01:00
|
|
|
|
2016-09-26 08:40:21 +01:00
|
|
|
syncthing-inotify = mkIf (cfg.systemService && cfg.useInotify) {
|
|
|
|
description = "Syncthing Inotify File Watcher service";
|
|
|
|
after = [ "network.target" "syncthing.service" ];
|
|
|
|
requires = [ "syncthing.service" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
SuccessExitStatus = "2";
|
|
|
|
RestartForceExitStatus = "3";
|
|
|
|
Restart = "on-failure";
|
|
|
|
User = cfg.user;
|
|
|
|
ExecStart = "${pkgs.syncthing-inotify.bin}/bin/syncthing-inotify -home=${cfg.dataDir} -logflags=0";
|
2016-08-06 16:20:18 +01:00
|
|
|
};
|
2016-09-26 08:40:21 +01:00
|
|
|
};
|
2016-08-06 16:20:18 +01:00
|
|
|
};
|
2014-04-03 17:54:10 +01:00
|
|
|
};
|
|
|
|
}
|