summary refs log tree commit diff
path: root/nixos
diff options
context:
space:
mode:
authorPeter Hoeg <peter@speartail.com>2016-04-14 16:42:04 +0800
committerPeter Hoeg <peter@speartail.com>2016-04-14 21:22:31 +0800
commit32bc5cfa240466be9a5352acb49183412a1935e1 (patch)
treec63c9c37f3fbf8513d2c25550c115d318b7ae2fa /nixos
parentacaf2556877ca7b9895a68d302af562aa5ceee91 (diff)
downloadnixpkgs-32bc5cfa240466be9a5352acb49183412a1935e1.tar
nixpkgs-32bc5cfa240466be9a5352acb49183412a1935e1.tar.gz
nixpkgs-32bc5cfa240466be9a5352acb49183412a1935e1.tar.bz2
nixpkgs-32bc5cfa240466be9a5352acb49183412a1935e1.tar.lz
nixpkgs-32bc5cfa240466be9a5352acb49183412a1935e1.tar.xz
nixpkgs-32bc5cfa240466be9a5352acb49183412a1935e1.tar.zst
nixpkgs-32bc5cfa240466be9a5352acb49183412a1935e1.zip
syncthing service: support running from systemd --user instance
Diffstat (limited to 'nixos')
-rw-r--r--nixos/modules/services/networking/syncthing.nix74
1 files changed, 56 insertions, 18 deletions
diff --git a/nixos/modules/services/networking/syncthing.nix b/nixos/modules/services/networking/syncthing.nix
index da9a270f30b..a9198542ad6 100644
--- a/nixos/modules/services/networking/syncthing.nix
+++ b/nixos/modules/services/networking/syncthing.nix
@@ -7,6 +7,21 @@ let
   cfg = config.services.syncthing;
   defaultUser = "syncthing";
 
+  header = {
+    description = "Syncthing service";
+    environment = {
+      STNORESTART = "yes";
+      STNOUPGRADE = "yes";
+      inherit (cfg) all_proxy;
+    } // config.networking.proxy.envVars;
+  };
+
+  service = {
+    Restart = "on-failure";
+    SuccessExitStatus = "2 3 4";
+    RestartForceExitStatus="3 4";
+  };
+
 in
 
 {
@@ -17,22 +32,35 @@ in
 
     services.syncthing = {
 
-      enable = mkOption {
-        type = types.bool;
-        default = false;
+      enable = mkEnableOption {
         description = ''
-          Whether to enable the Syncthing, self-hosted open-source alternative
-          to Dropbox and BittorrentSync. Initial interface will be
+          Whether to enable Syncthing - the self-hosted open-source alternative
+          to Dropbox and Bittorrent Sync. Initial interface will be
           available on http://127.0.0.1:8384/.
         '';
       };
 
+      systemService = mkOption {
+        type = types.bool;
+        default = true;
+        description = "Auto launch Syncthing as a system service.";
+      };
+
       user = mkOption {
         type = types.string;
         default = defaultUser;
         description = ''
-          Syncthing will be run under this user (user must exist,
-          this can be your user name).
+          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).
         '';
       };
 
@@ -64,10 +92,7 @@ in
           Syncthing package to use.
         '';
       };
-
-
     };
-
   };
 
 
@@ -77,7 +102,7 @@ in
 
     users = mkIf (cfg.user == defaultUser) {
       extraUsers."${defaultUser}" =
-        { group = defaultUser;
+        { group = cfg.group;
           home  = cfg.dataDir;
           createHome = true;
           uid = config.ids.uids.syncthing;
@@ -101,17 +126,30 @@ in
 
         serviceConfig = {
           User  = cfg.user;
-          Group = optionalString (cfg.user == defaultUser) defaultUser;
+          Group = cfg.group;
+    };
+
+    environment.systemPackages = [ cfg.package ];
+
+    systemd.services = mkIf cfg.systemService {
+      syncthing = header // {
+        after = [ "network.target" ];
+        wantedBy = [ "multi-user.target" ];
+        serviceConfig = service // {
+          User = cfg.user;
+          Group = cfg.group;
           PermissionsStartOnly = true;
-          Restart = "on-failure";
           ExecStart = "${pkgs.syncthing}/bin/syncthing -no-browser -home=${cfg.dataDir}";
-          SuccessExitStatus = "2 3 4";
-          RestartForceExitStatus="3 4";
         };
       };
+    };
 
-    environment.systemPackages = [ cfg.package ];
-
+    systemd.user.services =  {
+      syncthing = header // {
+        serviceConfig = service // {
+          ExecStart = "${pkgs.syncthing}/bin/syncthing -no-browser";
+        };
+      };
+    };
   };
-
 }