@ -3,46 +3,11 @@
with lib ;
let
cfg = config . services . syncthing ;
defaultUser = " s y n c t h i n g " ;
header = {
description = " S y n c t h i n g s e r v i c e " ;
after = [ " n e t w o r k . t a r g e t " ] ;
environment = {
STNORESTART = " y e s " ;
STNOUPGRADE = " y e s " ;
inherit ( cfg ) all_proxy ;
} // config . networking . proxy . envVars ;
} ;
service = {
Restart = " o n - f a i l u r e " ;
SuccessExitStatus = " 2 3 4 " ;
RestartForceExitStatus = " 3 4 " ;
} ;
iNotifyHeader = {
description = " S y n c t h i n g I n o t i f y F i l e W a t c h e r s e r v i c e " ;
after = [ " n e t w o r k . t a r g e t " " s y n c t h i n g . s e r v i c e " ] ;
requires = [ " s y n c t h i n g . s e r v i c e " ] ;
} ;
iNotifyService = {
SuccessExitStatus = " 2 " ;
RestartForceExitStatus = " 3 " ;
Restart = " o n - f a i l u r e " ;
} ;
in
{
in {
###### interface
options = {
services . syncthing = {
enable = mkEnableOption ''
@ -100,6 +65,19 @@ in
'' ;
} ;
openDefaultPorts = mkOption {
type = types . bool ;
default = false ;
example = literalExample " t r u e " ;
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 .
'' ;
} ;
package = mkOption {
type = types . package ;
default = pkgs . syncthing ;
@ -117,6 +95,14 @@ in
config = mkIf cfg . enable {
networking . firewall = mkIf cfg . openDefaultPorts {
allowedTCPPorts = [ 22000 ] ;
allowedUDPPorts = [ 21027 ] ;
} ;
systemd . packages = [ pkgs . syncthing ]
++ lib . optional cfg . useInotify pkgs . syncthing-inotify ;
users = mkIf ( cfg . user == defaultUser ) {
extraUsers . " ${ defaultUser } " =
{ group = cfg . group ;
@ -131,39 +117,44 @@ in
} ;
systemd . services = {
syncthing = mkIf cfg . systemService ( header // {
wants = mkIf cfg . useInotify [ " s y n c t h i n g - i n o t i f y . s e r v i c e " ] ;
wantedBy = [ " m u l t i - u s e r . t a r g e t " ] ;
serviceConfig = service // {
User = cfg . user ;
Group = cfg . group ;
PermissionsStartOnly = true ;
ExecStart = " ${ cfg . package } / b i n / s y n c t h i n g - n o - b r o w s e r - h o m e = ${ cfg . dataDir } " ;
} ;
} ) ;
syncthing-inotify = mkIf ( cfg . systemService && cfg . useInotify ) ( iNotifyHeader // {
syncthing = mkIf cfg . systemService {
description = " S y n c t h i n g s e r v i c e " ;
after = [ " n e t w o r k . t a r g e t " ] ;
environment = {
STNORESTART = " y e s " ;
STNOUPGRADE = " y e s " ;
inherit ( cfg ) all_proxy ;
} // config . networking . proxy . envVars ;
wants = mkIf cfg . useInotify [ " s y n c t h i n g - i n o t i f y . s e r v i c e " ] ;
wantedBy = [ " m u l t i - u s e r . t a r g e t " ] ;
serviceConfig = iNotifyService // {
serviceConfig = {
Restart = " o n - f a i l u r e " ;
SuccessExitStatus = " 2 3 4 " ;
RestartForceExitStatus = " 3 4 " ;
User = cfg . user ;
ExecStart = " ${ pkgs . syncthing-inotify . bin } / b i n / s y n c t h i n g - i n o t i f y - h o m e = ${ cfg . dataDir } - l o g f l a g s = 0 " ;
Group = cfg . group ;
PermissionsStartOnly = true ;
ExecStart = " ${ cfg . package } / b i n / s y n c t h i n g - n o - b r o w s e r - h o m e = ${ cfg . dataDir } " ;
} ;
} ) ;
} ;
} ;
systemd . user . services = {
syncthing = header // {
serviceConfig = service // {
ExecStart = " ${ cfg . package } / b i n / s y n c t h i n g - n o - b r o w s e r " ;
} ;
syncthing-resume = {
wantedBy = [ " s u s p e n d . t a r g e t " ] ;
} ;
syncthing-inotify = mkIf cfg . useInotify ( iNotifyHeader // {
serviceConfig = iNotifyService // {
ExecStart = " ${ pkgs . syncthing-inotify . bin } / b i n / s y n c t h i n g - i n o t i f y - l o g f l a g s = 0 " ;
syncthing-inotify = mkIf ( cfg . systemService && cfg . useInotify ) {
description = " S y n c t h i n g I n o t i f y F i l e W a t c h e r s e r v i c e " ;
after = [ " n e t w o r k . t a r g e t " " s y n c t h i n g . s e r v i c e " ] ;
requires = [ " s y n c t h i n g . s e r v i c e " ] ;
wantedBy = [ " m u l t i - u s e r . t a r g e t " ] ;
serviceConfig = {
SuccessExitStatus = " 2 " ;
RestartForceExitStatus = " 3 " ;
Restart = " o n - f a i l u r e " ;
User = cfg . user ;
ExecStart = " ${ pkgs . syncthing-inotify . bin } / b i n / s y n c t h i n g - i n o t i f y - h o m e = ${ cfg . dataDir } - l o g f l a g s = 0 " ;
} ;
} ) ;
} ;
} ;
} ;
}