2014-04-14 14:26:48 +00:00
|
|
|
{ config, lib, 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;
|
2016-03-31 23:26:52 +00:00
|
|
|
defaultUser = "syncthing";
|
2016-09-26 07:40:21 +00:00
|
|
|
in {
|
2014-04-03 16:54:10 +00:00
|
|
|
###### interface
|
|
|
|
options = {
|
|
|
|
services.syncthing = {
|
|
|
|
|
2016-04-14 14:44:02 +00: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 16:54:10 +00:00
|
|
|
|
2016-08-06 15:20:18 +00:00
|
|
|
useInotify = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "Provide syncthing-inotify as a service.";
|
|
|
|
};
|
|
|
|
|
2016-04-14 08:42:04 +00:00
|
|
|
systemService = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = "Auto launch Syncthing as a system service.";
|
|
|
|
};
|
|
|
|
|
2014-04-03 16:54:10 +00:00
|
|
|
user = mkOption {
|
2016-03-31 23:26:52 +00:00
|
|
|
type = types.string;
|
|
|
|
default = defaultUser;
|
2014-04-03 16:54:10 +00:00
|
|
|
description = ''
|
2016-04-14 08:42:04 +00: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 16:54:10 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-12-15 12:07:40 +00:00
|
|
|
all_proxy = mkOption {
|
2016-03-31 23:26:52 +00: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 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";
|
|
|
|
description = ''
|
2015-08-27 14:01:10 +00:00
|
|
|
Path where the settings and keys will exist.
|
2014-04-03 16:54:10 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-09-26 07:40:21 +00: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 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 ];
|
|
|
|
allowedUDPPorts = [ 21027 ];
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.packages = [ pkgs.syncthing ]
|
|
|
|
++ lib.optional cfg.useInotify pkgs.syncthing-inotify;
|
|
|
|
|
2016-03-31 23:26:52 +00:00
|
|
|
users = mkIf (cfg.user == defaultUser) {
|
|
|
|
extraUsers."${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";
|
|
|
|
};
|
|
|
|
|
|
|
|
extraGroups."${defaultUser}".gid =
|
|
|
|
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;
|
|
|
|
wants = mkIf cfg.useInotify [ "syncthing-inotify.service" ];
|
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;
|
|
|
|
PermissionsStartOnly = true;
|
|
|
|
ExecStart = "${cfg.package}/bin/syncthing -no-browser -home=${cfg.dataDir}";
|
2014-04-03 16:54:10 +00:00
|
|
|
};
|
2016-09-26 07:40:21 +00:00
|
|
|
};
|
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-07-21 02:49:58 +00:00
|
|
|
|
2016-09-26 07:40:21 +00: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 15:20:18 +00:00
|
|
|
};
|
2016-09-26 07:40:21 +00:00
|
|
|
};
|
2016-08-06 15:20:18 +00:00
|
|
|
};
|
2014-04-03 16:54:10 +00:00
|
|
|
};
|
|
|
|
}
|