at 17.09-beta 4.8 kB view raw
1{ config, lib, pkgs, ... }: 2 3with lib; 4 5let 6 cfg = config.services.syncthing; 7 defaultUser = "syncthing"; 8in { 9 ###### interface 10 options = { 11 services.syncthing = { 12 13 enable = mkEnableOption '' 14 Syncthing - the self-hosted open-source alternative 15 to Dropbox and Bittorrent Sync. Initial interface will be 16 available on http://127.0.0.1:8384/. 17 ''; 18 19 useInotify = mkOption { 20 type = types.bool; 21 default = false; 22 description = "Provide syncthing-inotify as a service."; 23 }; 24 25 systemService = mkOption { 26 type = types.bool; 27 default = true; 28 description = "Auto launch Syncthing as a system service."; 29 }; 30 31 user = mkOption { 32 type = types.string; 33 default = defaultUser; 34 description = '' 35 Syncthing will be run under this user (user will be created if it doesn't exist. 36 This can be your user name). 37 ''; 38 }; 39 40 group = mkOption { 41 type = types.string; 42 default = "nogroup"; 43 description = '' 44 Syncthing will be run under this group (group will not be created if it doesn't exist. 45 This can be your user name). 46 ''; 47 }; 48 49 all_proxy = mkOption { 50 type = types.nullOr types.string; 51 default = null; 52 example = "socks5://address.com:1234"; 53 description = '' 54 Overwrites all_proxy environment variable for the syncthing process to 55 the given value. This is normaly used to let relay client connect 56 through SOCKS5 proxy server. 57 ''; 58 }; 59 60 dataDir = mkOption { 61 type = types.path; 62 default = "/var/lib/syncthing"; 63 description = '' 64 Path where the settings and keys will exist. 65 ''; 66 }; 67 68 openDefaultPorts = mkOption { 69 type = types.bool; 70 default = false; 71 example = literalExample "true"; 72 description = '' 73 Open the default ports in the firewall: 74 - TCP 22000 for transfers 75 - UDP 21027 for discovery 76 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. 77 Alternatively, if are running only a single instance on this machine using the default ports, enable this. 78 ''; 79 }; 80 81 package = mkOption { 82 type = types.package; 83 default = pkgs.syncthing; 84 defaultText = "pkgs.syncthing"; 85 example = literalExample "pkgs.syncthing"; 86 description = '' 87 Syncthing package to use. 88 ''; 89 }; 90 }; 91 }; 92 93 94 ###### implementation 95 96 config = mkIf cfg.enable { 97 98 networking.firewall = mkIf cfg.openDefaultPorts { 99 allowedTCPPorts = [ 22000 ]; 100 allowedUDPPorts = [ 21027 ]; 101 }; 102 103 systemd.packages = [ pkgs.syncthing ] 104 ++ lib.optional cfg.useInotify pkgs.syncthing-inotify; 105 106 users = mkIf (cfg.user == defaultUser) { 107 extraUsers."${defaultUser}" = 108 { group = cfg.group; 109 home = cfg.dataDir; 110 createHome = true; 111 uid = config.ids.uids.syncthing; 112 description = "Syncthing daemon user"; 113 }; 114 115 extraGroups."${defaultUser}".gid = 116 config.ids.gids.syncthing; 117 }; 118 119 systemd.services = { 120 syncthing = mkIf cfg.systemService { 121 description = "Syncthing service"; 122 after = [ "network.target" ]; 123 environment = { 124 STNORESTART = "yes"; 125 STNOUPGRADE = "yes"; 126 inherit (cfg) all_proxy; 127 } // config.networking.proxy.envVars; 128 wants = mkIf cfg.useInotify [ "syncthing-inotify.service" ]; 129 wantedBy = [ "multi-user.target" ]; 130 serviceConfig = { 131 Restart = "on-failure"; 132 SuccessExitStatus = "2 3 4"; 133 RestartForceExitStatus="3 4"; 134 User = cfg.user; 135 Group = cfg.group; 136 PermissionsStartOnly = true; 137 ExecStart = "${cfg.package}/bin/syncthing -no-browser -home=${cfg.dataDir}"; 138 }; 139 }; 140 141 syncthing-resume = { 142 wantedBy = [ "suspend.target" ]; 143 }; 144 145 syncthing-inotify = mkIf (cfg.systemService && cfg.useInotify) { 146 description = "Syncthing Inotify File Watcher service"; 147 after = [ "network.target" "syncthing.service" ]; 148 requires = [ "syncthing.service" ]; 149 wantedBy = [ "multi-user.target" ]; 150 serviceConfig = { 151 SuccessExitStatus = "2"; 152 RestartForceExitStatus = "3"; 153 Restart = "on-failure"; 154 User = cfg.user; 155 ExecStart = "${pkgs.syncthing-inotify.bin}/bin/syncthing-inotify -home=${cfg.dataDir} -logflags=0"; 156 }; 157 }; 158 }; 159 }; 160}