about summary refs log tree commit diff
path: root/nixos/modules/services/networking/resilio.nix
diff options
context:
space:
mode:
authorJörg Thalheim <joerg@thalheim.io>2017-06-01 21:53:13 +0100
committerJörg Thalheim <joerg@thalheim.io>2017-06-02 21:25:07 +0100
commitc611d03842803c2d1bf081f6432449f34b1cebb7 (patch)
tree49ad4c7921bf8a76e9369fb2c436ca20a28894ed /nixos/modules/services/networking/resilio.nix
parentb6e384732e34008e37d0b48fffed7d0dc0880621 (diff)
downloadnixlib-c611d03842803c2d1bf081f6432449f34b1cebb7.tar
nixlib-c611d03842803c2d1bf081f6432449f34b1cebb7.tar.gz
nixlib-c611d03842803c2d1bf081f6432449f34b1cebb7.tar.bz2
nixlib-c611d03842803c2d1bf081f6432449f34b1cebb7.tar.lz
nixlib-c611d03842803c2d1bf081f6432449f34b1cebb7.tar.xz
nixlib-c611d03842803c2d1bf081f6432449f34b1cebb7.tar.zst
nixlib-c611d03842803c2d1bf081f6432449f34b1cebb7.zip
resilio: generate configuration with toJSON
Diffstat (limited to 'nixos/modules/services/networking/resilio.nix')
-rw-r--r--nixos/modules/services/networking/resilio.nix127
1 files changed, 47 insertions, 80 deletions
diff --git a/nixos/modules/services/networking/resilio.nix b/nixos/modules/services/networking/resilio.nix
index 45274991d810..33ac24aabc45 100644
--- a/nixos/modules/services/networking/resilio.nix
+++ b/nixos/modules/services/networking/resilio.nix
@@ -5,76 +5,41 @@ with lib;
 let
   cfg = config.services.resilio;
 
-  resilioSync = pkgs.resilio;
-
-  listenAddr = cfg.httpListenAddr + ":" + (toString cfg.httpListenPort);
-
-  boolStr = x: if x then "true" else "false";
-  optionalEmptyStr = b: v: optionalString (b != "") v;
-
-  webUIConfig = optionalString cfg.enableWebUI
-    ''
-      "webui":
-      {
-        ${optionalEmptyStr cfg.httpLogin     "\"login\":          \"${cfg.httpLogin}\","}
-        ${optionalEmptyStr cfg.httpPass      "\"password\":       \"${cfg.httpPass}\","}
-        ${optionalEmptyStr cfg.apiKey        "\"api_key\":        \"${cfg.apiKey}\","}
-        ${optionalEmptyStr cfg.directoryRoot "\"directory_root\": \"${cfg.directoryRoot}\","}
-        "listen": "${listenAddr}"
-      }
-    '';
-
-  knownHosts = e:
-    optionalString (e ? "knownHosts")
-      (concatStringsSep "," (map (v: "\"${v}\"") e."knownHosts"));
-
-  sharedFoldersRecord =
-    concatStringsSep "," (map (entry:
-      let helper = attr: v:
-        if (entry ? attr) then boolStr entry.attr else boolStr v;
-      in
-      ''
-        {
-          "secret": "${entry.secret}",
-          "dir":    "${entry.directory}",
-
-          "use_relay_server": ${helper "useRelayServer" true},
-          "use_tracker":      ${helper "useTracker"     true},
-          "use_dht":          ${helper "useDHT"        false},
-
-          "search_lan":       ${helper "searchLAN"      true},
-          "use_sync_trash":   ${helper "useSyncTrash"   true},
-
-          "known_hosts": [${knownHosts entry}]
-        }
-      '') cfg.sharedFolders);
-
-  sharedFoldersConfig = optionalString (cfg.sharedFolders != [])
-    ''
-      "shared_folders":
-        [
-        ${sharedFoldersRecord}
-        ]
-    '';
-
-  configFile = pkgs.writeText "config.json"
-    ''
-      {
-        "device_name":     "${cfg.deviceName}",
-        "storage_path":    "${cfg.storagePath}",
-        "listening_port":  ${toString cfg.listeningPort},
-        "use_gui":         false,
-
-        "check_for_updates": ${boolStr cfg.checkForUpdates},
-        "use_upnp":          ${boolStr cfg.useUpnp},
-        "download_limit":    ${toString cfg.downloadLimit},
-        "upload_limit":      ${toString cfg.uploadLimit},
-        "lan_encrypt_data":  ${boolStr cfg.encryptLAN},
-
-        ${webUIConfig}
-        ${sharedFoldersConfig}
-      }
-    '';
+  resilioSync = pkgs.resilio-sync;
+
+  sharedFoldersRecord = map (entry: {
+    secret = entry.secret;
+    dir = entry.directory;
+
+    use_relay_server = entry.useRelayServer;
+    use_tracker = entry.useTracker;
+    use_dht = entry.useDHT;
+
+    search_lan = entry.searchLAN;
+    use_sync_trash = entry.useSyncTrash;
+    known_hosts = knownHosts;
+  }) cfg.sharedFolders;
+
+  configFile = pkgs.writeText "config.json" (builtins.toJSON ({
+    device_name = cfg.deviceName;
+    storage_path = cfg.storagePath;
+    listening_port = cfg.listeningPort;
+    use_gui = false;
+    check_for_updates = cfg.checkForUpdates;
+    use_upnp = cfg.useUpnp;
+    download_limit = cfg.downloadLimit;
+    upload_limit = cfg.uploadLimit;
+    lan_encrypt_data = cfg.encryptLAN;
+  } // optionalAttrs cfg.enableWebUI {
+    webui = { listen = "${cfg.httpListenAddr}:${toString cfg.httpListenPort}"; } //
+      (optionalAttrs (cfg.httpLogin != "") { login = cfg.httpLogin; }) //
+      (optionalAttrs (cfg.httpPass != "") { password = cfg.httpPass; }) //
+      (optionalAttrs (cfg.apiKey != "") { api_key = cfg.apiKey; }) //
+      (optionalAttrs (cfg.directoryRoot != "") { directory_root = cfg.directoryRoot; });
+  } // optionalAttrs (sharedFoldersRecord != []) {
+    shared_folders = sharedFoldersRecord;
+  }));
+
 in
 {
   options = {
@@ -97,6 +62,7 @@ in
       deviceName = mkOption {
         type = types.str;
         example = "Voltron";
+        default = config.networking.hostName;
         description = ''
           Name of the Resilio Sync device.
         '';
@@ -230,10 +196,10 @@ in
               useDHT         = false;
               searchLAN      = true;
               useSyncTrash   = true;
-              knownHosts     =
-                [ "192.168.1.2:4444"
-                  "192.168.1.3:4444"
-                ];
+              knownHosts     = [
+                "192.168.1.2:4444"
+                "192.168.1.3:4444"
+              ];
             }
           ];
         description = ''
@@ -275,7 +241,6 @@ in
         }
       ];
 
-    services.resilio.package = mkOptionDefault pkgs.resilio;
 
     users.extraUsers.rslsync = {
       description     = "Resilio Sync Service user";
@@ -295,8 +260,9 @@ in
         Restart   = "on-abort";
         UMask     = "0002";
         User      = "rslsync";
-        ExecStart =
-          "${resilioSync}/bin/rslsync --nodaemon --config ${configFile}";
+        ExecStart = ''
+          ${resilioSync}/bin/rslsync --nodaemon --config ${configFile}
+        '';
       };
     };
 
@@ -305,11 +271,12 @@ in
       after       = [ "network.target" "local-fs.target" ];
       serviceConfig = {
         Restart   = "on-abort";
-        ExecStart =
-          "${resilioSync}/bin/rslsync --nodaemon --config %h/.config/resilio-sync/config.json";
+        ExecStart = ''
+         ${resilioSync}/bin/rslsync --nodaemon --config %h/.config/resilio-sync/config.json
+        '';
       };
     };
 
-    environment.systemPackages = [ cfg.package ];
+    environment.systemPackages = [ resilioSync ];
   };
 }