0
0
Fork 0
mirror of https://github.com/NixOS/nixpkgs.git synced 2025-07-14 06:00:33 +03:00

resilio: generate configuration with toJSON

This commit is contained in:
Jörg Thalheim 2017-06-01 21:53:13 +01:00
parent b6e384732e
commit c611d03842
No known key found for this signature in database
GPG key ID: CA4106B8D7CC79FA

View file

@ -5,76 +5,41 @@ with lib;
let let
cfg = config.services.resilio; cfg = config.services.resilio;
resilioSync = pkgs.resilio; resilioSync = pkgs.resilio-sync;
listenAddr = cfg.httpListenAddr + ":" + (toString cfg.httpListenPort); sharedFoldersRecord = map (entry: {
secret = entry.secret;
dir = entry.directory;
boolStr = x: if x then "true" else "false"; use_relay_server = entry.useRelayServer;
optionalEmptyStr = b: v: optionalString (b != "") v; use_tracker = entry.useTracker;
use_dht = entry.useDHT;
webUIConfig = optionalString cfg.enableWebUI search_lan = entry.searchLAN;
'' use_sync_trash = entry.useSyncTrash;
"webui": known_hosts = knownHosts;
{ }) cfg.sharedFolders;
${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: configFile = pkgs.writeText "config.json" (builtins.toJSON ({
optionalString (e ? "knownHosts") device_name = cfg.deviceName;
(concatStringsSep "," (map (v: "\"${v}\"") e."knownHosts")); 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;
}));
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}
}
'';
in in
{ {
options = { options = {
@ -97,6 +62,7 @@ in
deviceName = mkOption { deviceName = mkOption {
type = types.str; type = types.str;
example = "Voltron"; example = "Voltron";
default = config.networking.hostName;
description = '' description = ''
Name of the Resilio Sync device. Name of the Resilio Sync device.
''; '';
@ -230,10 +196,10 @@ in
useDHT = false; useDHT = false;
searchLAN = true; searchLAN = true;
useSyncTrash = true; useSyncTrash = true;
knownHosts = knownHosts = [
[ "192.168.1.2:4444" "192.168.1.2:4444"
"192.168.1.3:4444" "192.168.1.3:4444"
]; ];
} }
]; ];
description = '' description = ''
@ -275,7 +241,6 @@ in
} }
]; ];
services.resilio.package = mkOptionDefault pkgs.resilio;
users.extraUsers.rslsync = { users.extraUsers.rslsync = {
description = "Resilio Sync Service user"; description = "Resilio Sync Service user";
@ -295,8 +260,9 @@ in
Restart = "on-abort"; Restart = "on-abort";
UMask = "0002"; UMask = "0002";
User = "rslsync"; User = "rslsync";
ExecStart = ExecStart = ''
"${resilioSync}/bin/rslsync --nodaemon --config ${configFile}"; ${resilioSync}/bin/rslsync --nodaemon --config ${configFile}
'';
}; };
}; };
@ -305,11 +271,12 @@ in
after = [ "network.target" "local-fs.target" ]; after = [ "network.target" "local-fs.target" ];
serviceConfig = { serviceConfig = {
Restart = "on-abort"; Restart = "on-abort";
ExecStart = ExecStart = ''
"${resilioSync}/bin/rslsync --nodaemon --config %h/.config/resilio-sync/config.json"; ${resilioSync}/bin/rslsync --nodaemon --config %h/.config/resilio-sync/config.json
'';
}; };
}; };
environment.systemPackages = [ cfg.package ]; environment.systemPackages = [ resilioSync ];
}; };
} }