19.09: migrate to upstream syncthing

This commit is contained in:
Ingolf Wagner 2019-10-26 13:44:27 +02:00
parent 2046a2dc10
commit e4ee96e986
Signed by: palo
GPG key ID: 76BF5F1928B9618B
8 changed files with 221 additions and 499 deletions

View file

@ -1,7 +1,7 @@
{ config, pkgs, lib, ... }:
{
test.services.syncthing = {
services.syncthing = {
enable = true;
openDefaultPorts = false;
user = "palo";
@ -16,20 +16,49 @@
# on encrypted drive
# ------------------
desktop.path = "/home/palo/desktop";
finance.path = "/home/palo/finance";
desktop = {
enable = true;
path = "/home/palo/desktop";
};
finance = {
enable = true;
path = "/home/palo/finance";
};
# no need to be encrypted
# -----------------------
book.path = "/home/palo/books";
fotos-encrypted.path = "/home/palo/private/.fotos.ct";
kruck-pepe.path = "/home/palo/pepe-kruck";
music-library.path = "/home/palo/music-library";
music-projects.path = "/home/palo/music-projects";
porani-pepe.path = "/home/palo/pepe-porani";
schasch-pepe.path = "/home/palo/pepe-schasch";
smartphone-fotos.path = "/home/palo/smartphone-fotos";
workout-pepe.path = "/home/palo/pepe-workout";
book = {
enable = true;
path = "/home/palo/books";
};
kruck-pepe = {
enable = true;
path = "/home/palo/pepe-kruck";
};
music-library = {
enable = true;
path = "/home/palo/music-library";
};
music-projects = {
enable = true;
path = "/home/palo/music-projects";
};
porani-pepe = {
enable = true;
path = "/home/palo/pepe-porani";
};
schasch-pepe = {
enable = true;
path = "/home/palo/pepe-schasch";
};
smartphone-fotos = {
enable = true;
path = "/home/palo/smartphone-fotos";
};
workout-pepe = {
enable = true;
path = "/home/palo/pepe-workout";
};
};
};

View file

@ -1,7 +1,7 @@
{ config, pkgs, lib, ... }:
{
test.services.syncthing = {
services.syncthing = {
enable = true;
openDefaultPorts = true;
declarative = {
@ -11,16 +11,37 @@
folders = {
# on encrypted device
# -------------------
desktop.path = "/var/lib/syncthing/desktop";
finance.path = "/var/lib/syncthing/finance";
lost-fotos.path = "/var/lib/syncthing/lost-fotos.ct";
desktop = {
enable = true;
path = "/var/lib/syncthing/desktop";
};
finance = {
enable = true;
path = "/var/lib/syncthing/finance";
};
lost-fotos = {
enable = true;
path = "/var/lib/syncthing/lost-fotos.ct";
};
# on media hard drive (not encrypted)
# -----------------------------------
movies.path = "/media/movies";
music-library.path = "/media/music-library";
podcasts.path = "/media/podcasts";
series.path = "/media/series";
movies = {
enable = true;
path = "/media/movies";
};
music-library = {
enable = true;
path = "/media/music-library";
};
podcasts = {
enable = true;
path = "/media/podcasts";
};
series = {
enable = true;
path = "/media/series";
};
};
};
};

View file

@ -1,7 +1,7 @@
{ config, pkgs, lib, ... }:
{
test.services.syncthing = {
services.syncthing = {
enable = true;
openDefaultPorts = false;
user = "palo";
@ -15,16 +15,37 @@
# on encrypted drive
# ------------------
desktop.path = "/home/palo/desktop";
finance.path = "/home/palo/finance";
lost-fotos.path = "/home/palo/.lost-fotos.ct";
desktop = {
enable = true;
path = "/home/palo/desktop";
};
finance = {
enable = true;
path = "/home/palo/finance";
};
lost-fotos = {
enable = true;
path = "/home/palo/.lost-fotos.ct";
};
smartphone-fotos = {
enable = true;
path = "/home/palo/smartphone-fotos";
};
# no need to be encrypted
# -----------------------
book.path = "/home/palo/books";
fotos-encrypted.path = "/home/palo/private/.fotos.ct";
music-library.path = "/home/palo/music-library";
music-projects.path = "/home/palo/music-projects";
book = {
enable = true;
path = "/home/palo/books";
};
music-library = {
enable = true;
path = "/home/palo/music-library";
};
music-projects = {
enable = true;
path = "/home/palo/music-projects";
};
};
};

View file

@ -1,7 +1,7 @@
{ config, pkgs, lib, ... }:
{
test.services.syncthing = {
services.syncthing = {
enable = true;
openDefaultPorts = false;
dataDir = "/home/syncthing";
@ -17,20 +17,53 @@
# on encrypted hard drive
# -----------------------
desktop.path = "/home/syncthing/desktop";
finance.path = "/home/syncthing/finance";
lost-fotos.path = "/home/syncthing/lost-fotos.ct";
music-projects.path = "/home/syncthing/music-projects";
video-material.path = "/home/syncthing/video-material";
desktop = {
enable = true;
path = "/home/syncthing/desktop";
};
finance = {
enable = true;
path = "/home/syncthing/finance";
};
lost-fotos = {
enable = true;
path = "/home/syncthing/lost-fotos.ct";
};
music-projects = {
enable = true;
path = "/home/syncthing/music-projects";
};
video-material = {
enable = true;
path = "/home/syncthing/video-material";
};
# on media hard drive (not encrypted)
# -----------------------------------
music-library-free.path = "/media/syncthing/music-library-free";
samples.path = "/media/syncthing/samples";
movies.path = "/media/syncthing/movies";
music-library.path = "/media/syncthing/music-library";
podcasts.path = "/media/syncthing/podcasts";
series.path = "/media/syncthing/series";
music-library-free = {
enable = true;
path = "/media/syncthing/music-library-free";
};
samples = {
enable = true;
path = "/media/syncthing/samples";
};
movies = {
enable = true;
path = "/media/syncthing/movies";
};
music-library = {
enable = true;
path = "/media/syncthing/music-library";
};
podcasts = {
enable = true;
path = "/media/syncthing/podcasts";
};
series = {
enable = true;
path = "/media/syncthing/series";
};
};
};

View file

@ -1,7 +1,7 @@
{ config, pkgs, lib, ... }:
{
test.services.syncthing = {
services.syncthing = {
enable = true;
openDefaultPorts = false;
user = "palo";
@ -16,29 +16,64 @@
# on encrypted drive
# ------------------
desktop.path = "/home/palo/desktop";
finance.path = "/home/palo/finance";
lost-fotos.path = "/home/palo/.lost-fotos.ct";
desktop = {
enable = true;
path = "/home/palo/desktop";
};
finance = {
enable = true;
path = "/home/palo/finance";
};
lost-fotos = {
enable = true;
path = "/home/palo/.lost-fotos.ct";
};
# no need to be encrypted
# -----------------------
book.path = "/home/palo/books";
#desktop-encrypted.path = "/home/palo/.desktop.ct";
#finance-encrypted.path = "/home/palo/.finance.ct";
fotos-encrypted.path = "/home/palo/private/.fotos.ct";
kruck-workout.path = "/home/palo/workout-kruck";
music-library-free.path = "/home/palo/music-library-free";
music-library.path = "/home/palo/music-library";
music-projects.path = "/home/palo/music-projects";
porani-workout.path = "/home/palo/workout-porani";
samples.path = "/home/palo/samples";
schasch-workout.path = "/home/palo/workout-schasch";
smartphone-fotos.path = "/home/palo/smartphone-fotos";
smartphone-music.path = "/home/palo/smartphone-music";
video-material.path = "/home/palo/video-material";
workout-pepe.path = "/home/palo/workout-pepe";
windows-sync.path = "/home/palo/windows-sync";
book = {
enable = true;
path = "/home/palo/books";
};
music-library-free = {
enable = true;
path = "/home/palo/music-library-free";
};
music-library = {
enable = true;
path = "/home/palo/music-library";
};
music-projects = {
enable = true;
path = "/home/palo/music-projects";
};
porani-workout = {
enable = true;
path = "/home/palo/workout-porani";
};
samples = {
enable = true;
path = "/home/palo/samples";
};
smartphone-fotos = {
enable = true;
path = "/home/palo/smartphone-fotos";
};
smartphone-music = {
path = "/home/palo/smartphone-music";
};
video-material = {
enable = true;
path = "/home/palo/video-material";
};
workout-pepe = {
enable = true;
path = "/home/palo/workout-pepe";
};
windows-sync = {
enable = true;
path = "/home/palo/windows-sync";
};
};
};

View file

@ -2,8 +2,6 @@
imports = [
./later/syncthing.nix
./services/castget.nix
./services/home-assistant.nix
./services/lektor.nix

View file

@ -1,434 +0,0 @@
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.test.services.syncthing;
defaultUser = "syncthing";
devices = mapAttrsToList (name: device: {
deviceID = device.id;
inherit (device) name addresses introducer;
}) cfg.declarative.devices;
folders = mapAttrsToList ( _: folder: {
inherit (folder) path id label type;
devices = map (device: { deviceId = cfg.declarative.devices.${device}.id; }) folder.devices;
rescanIntervalS = folder.rescanInterval;
fsWatcherEnabled = folder.watch;
fsWatcherDelayS = folder.watchDelay;
ignorePerms = folder.ignorePerms;
}) (filterAttrs (
_: value:
(value.path != null) && (value.devices != [])
) cfg.declarative.folders);
# get the api key by parsing the config.xml
getApiKey = pkgs.writers.writeDash "getAPIKey" ''
${pkgs.libxml2}/bin/xmllint \
--xpath 'string(configuration/gui/apikey)'\
${cfg.configDir}/config.xml
'';
updateConfig = pkgs.writers.writeDash "merge-syncthing-config" ''
set -efu
# wait for syncthing port to open
until ${pkgs.curl}/bin/curl -Ss ${cfg.guiAddress} -o /dev/null; do
sleep 1
done
API_KEY=$(${getApiKey})
OLD_CFG=$(${pkgs.curl}/bin/curl -Ss \
-H "X-API-Key: $API_KEY" \
${cfg.guiAddress}/rest/system/config)
# generate the new config by merging with the nixos config options
NEW_CFG=$(echo "$OLD_CFG" | ${pkgs.jq}/bin/jq -s '.[] as $in | $in * {
"devices": (${builtins.toJSON devices}${optionalString (! cfg.declarative.overrideDevices) " + $in.devices"}),
"folders": (${builtins.toJSON folders}${optionalString (! cfg.declarative.overrideFolders) " + $in.folders"})
}')
# POST the new config to syncthing
echo "$NEW_CFG" | ${pkgs.curl}/bin/curl -Ss \
-H "X-API-Key: $API_KEY" \
${cfg.guiAddress}/rest/system/config -d @-
# restart syncthing after sending the new config
${pkgs.curl}/bin/curl -Ss \
-H "X-API-Key: $API_KEY" \
-X POST \
${cfg.guiAddress}/rest/system/restart
'';
in {
###### interface
options = {
test.services.syncthing = {
enable = mkEnableOption ''
Syncthing - the self-hosted open-source alternative
to Dropbox and Bittorrent Sync. Initial interface will be
available on http://127.0.0.1:8384/.
'';
declarative = {
cert = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
Path to users cert.pem file, will be copied into the syncthing's
<literal>configDir</literal>
'';
};
key = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
Path to users key.pem file, will be copied into the syncthing's
<literal>configDir</literal>
'';
};
overrideDevices = mkOption {
type = types.bool;
default = true;
description = ''
Whether to delete the devices which are not configured via the
<literal>declarative.devices</literal> option.
If set to false, devices added via the webinterface will
persist but will have to be deleted manually.
'';
};
devices = mkOption {
default = {};
description = ''
Peers/devices which syncthing should communicate with.
'';
example = [
{
name = "bigbox";
id = "7CFNTQM-IMTJBHJ-3UWRDIU-ZGQJFR6-VCXZ3NB-XUH3KZO-N52ITXR-LAIYUAU";
addresses = [ "tcp://192.168.0.10:51820" ];
}
];
type = types.attrsOf (types.submodule ({ config, ... }: {
options = {
name = mkOption {
type = types.str;
default = config._module.args.name;
description = ''
Name of the device
'';
};
addresses = mkOption {
type = types.listOf types.str;
default = [];
description = ''
The addresses used to connect to the device.
If this is let empty, dynamic configuration is attempted
'';
};
id = mkOption {
type = types.str;
description = ''
The id of the other peer, this is mandatory. It's documented at
https://docs.syncthing.net/dev/device-ids.html
'';
};
introducer = mkOption {
type = types.bool;
default = false;
description = ''
If the device should act as an introducer and be allowed
to add folders on this computer.
'';
};
};
}));
};
overrideFolders = mkOption {
type = types.bool;
default = true;
description = ''
Whether to delete the folders which are not configured via the
<literal>declarative.folders</literal> option.
If set to false, folders added via the webinterface will persist
but will have to be deleted manually.
'';
};
folders = mkOption {
default = {};
description = ''
folders which should be shared by syncthing.
'';
type = types.attrsOf (types.submodule ({ config, ... }: {
options = {
path = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
The path to the folder which should be shared.
'';
};
id = mkOption {
type = types.str;
default = config._module.args.name;
description = ''
The id of the folder. Must be the same on all devices.
'';
};
label = mkOption {
type = types.str;
default = config._module.args.name;
description = ''
The label of the folder.
'';
};
devices = mkOption {
type = types.listOf types.str;
default = [];
description = ''
The devices this folder should be shared with. Must be defined
in the <literal>declarative.devices</literal> attribute.
'';
};
rescanInterval = mkOption {
type = types.int;
default = 3600;
description = ''
How often the folders should be rescaned for changes.
'';
};
type = mkOption {
type = types.enum [ "sendreceive" "sendonly" "receiveonly" ];
default = "sendreceive";
description = ''
Whether to send only changes from this folder, only receive them
or propagate both.
'';
};
watch = mkOption {
type = types.bool;
default = true;
description = ''
Whether the folder should be watched for changes by inotify.
'';
};
watchDelay = mkOption {
type = types.int;
default = 10;
description = ''
The delay after an inotify event is triggered.
'';
};
ignorePerms = mkOption {
type = types.bool;
default = true;
description = ''
Whether to propagate permission changes.
'';
};
};
}));
};
};
guiAddress = mkOption {
type = types.str;
default = "127.0.0.1:8384";
description = ''
Address to serve the GUI.
'';
};
systemService = mkOption {
type = types.bool;
default = true;
description = "Auto launch Syncthing as a system service.";
};
user = mkOption {
type = types.str;
default = defaultUser;
description = ''
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.str;
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).
'';
};
all_proxy = mkOption {
type = with types; nullOr str;
default = null;
example = "socks5://address.com:1234";
description = ''
Overwrites all_proxy environment variable for the syncthing process to
the given value. This is normaly used to let relay client connect
through SOCKS5 proxy server.
'';
};
dataDir = mkOption {
type = types.path;
default = "/var/lib/syncthing";
description = ''
Path where synced directories will exist.
'';
};
configDir = mkOption {
type = types.path;
description = ''
Path where the settings and keys will exist.
'';
default =
let
nixos = config.system.stateVersion;
cond = versionAtLeast nixos "19.03";
in cfg.dataDir + (optionalString cond "/.config/syncthing");
};
openDefaultPorts = mkOption {
type = types.bool;
default = false;
example = literalExample "true";
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;
defaultText = "pkgs.syncthing";
example = literalExample "pkgs.syncthing";
description = ''
Syncthing package to use.
'';
};
};
};
imports = [
(mkRemovedOptionModule ["services" "syncthing" "useInotify"] ''
This option was removed because syncthing now has the inotify functionality included under the name "fswatcher".
It can be enabled on a per-folder basis through the webinterface.
'')
];
###### implementation
config = mkIf cfg.enable {
networking.firewall = mkIf cfg.openDefaultPorts {
allowedTCPPorts = [ 22000 ];
allowedUDPPorts = [ 21027 ];
};
systemd.packages = [ pkgs.syncthing ];
users = mkIf (cfg.systemService && cfg.user == defaultUser) {
users."${defaultUser}" =
{ group = cfg.group;
home = cfg.dataDir;
createHome = true;
uid = config.ids.uids.syncthing;
description = "Syncthing daemon user";
};
groups."${defaultUser}".gid =
config.ids.gids.syncthing;
};
systemd.services = {
syncthing = mkIf cfg.systemService {
description = "Syncthing service";
after = [ "network.target" ];
environment = {
STNORESTART = "yes";
STNOUPGRADE = "yes";
inherit (cfg) all_proxy;
} // config.networking.proxy.envVars;
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Restart = "on-failure";
SuccessExitStatus = "2 3 4";
RestartForceExitStatus="3 4";
User = cfg.user;
Group = cfg.group;
ExecStartPre = mkIf (cfg.declarative.cert != null || cfg.declarative.key != null)
"+${pkgs.writers.writeBash "syncthing-copy-keys" ''
mkdir -p ${cfg.configDir}
chown ${cfg.user}:${cfg.group} ${cfg.configDir}
chmod 700 ${cfg.configDir}
${optionalString (cfg.declarative.cert != null) ''
cp ${toString cfg.declarative.cert} ${cfg.configDir}/cert.pem
chown ${cfg.user}:${cfg.group} ${cfg.configDir}/cert.pem
chmod 400 ${cfg.configDir}/cert.pem
''}
${optionalString (cfg.declarative.key != null) ''
cp ${toString cfg.declarative.key} ${cfg.configDir}/key.pem
chown ${cfg.user}:${cfg.group} ${cfg.configDir}/key.pem
chmod 400 ${cfg.configDir}/key.pem
''}
''}"
;
ExecStart = ''
${cfg.package}/bin/syncthing \
-no-browser \
-gui-address=${cfg.guiAddress} \
-home=${cfg.configDir}
'';
};
};
syncthing-init = {
after = [ "syncthing.service" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
User = cfg.user;
RemainAfterExit = true;
Type = "oneshot";
ExecStart = updateConfig;
};
};
syncthing-resume = {
wantedBy = [ "suspend.target" ];
};
};
};
}

View file

@ -2,7 +2,7 @@
with lib;
{
test.services.syncthing = {
services.syncthing = {
guiAddress = "${config.networking.hostName}.private:8384";
declarative = {
overrideDevices = true;
@ -48,10 +48,12 @@ with lib;
# needs to be on encrypted drives
# -------------------------------
desktop = {
enable = lib.mkDefault false;
watch = false;
devices = [ "workout" "workhorse" "pepe" "sterni" "porani" ];
};
finance = {
enable = lib.mkDefault false;
watch = false;
devices = [ "workout" "workhorse" "pepe" "sterni" "porani" ];
};
@ -59,65 +61,78 @@ with lib;
# no need to be stored on encrypted drives
# ----------------------------------------
book = {
enable = lib.mkDefault false;
id = "wwbvs-5lfbh";
watch = false;
devices = [ "workout" "pepe" "sterni" ];
};
lost-fotos = {
enable = lib.mkDefault false;
watch = false;
devices = [ "workout" "workhorse" "sterni" "porani" ];
};
movies = {
enable = lib.mkDefault false;
id = "vatmy-c2qf4";
watch = false;
devices = [ "workhorse" "porani" ];
};
porn = {
enable = lib.mkDefault false;
id = "vatmy-c2qf4";
watch = false;
devices = [ "workhorse" "porani" ];
};
music-library = {
enable = lib.mkDefault false;
id = "gytmq-r2zrx";
watch = false;
devices = [ "porani" "workout" "workhorse" "pepe" "sterni" ];
};
music-library-free = {
enable = lib.mkDefault false;
id = "mu9mn-zgvsw";
watch = false;
devices = [ "workout" "workhorse" "mors" ];
};
music-projects = {
enable = lib.mkDefault false;
id = "acfhu-r4t4f";
watch = false;
devices = [ "workout" "workhorse" "pepe" "sterni" ];
};
podcasts = {
enable = lib.mkDefault false;
id = "yvzmx-hcomd";
watch = false;
devices = [ "workhorse" "porani" ];
};
samples = {
enable = lib.mkDefault false;
id = "pcgkj-tjucd";
watch = false;
devices = [ "workout" "workhorse" "sterni" ];
};
series = {
enable = lib.mkDefault false;
id = "all-series";
watch = false;
devices = [ "workhorse" "porani" ];
};
smartphone-music = {
enable = lib.mkDefault false;
id = "0vjze-xvs8n";
watch = false;
devices = [ "workout" "smartphone" ];
};
smartphone-fotos = {
enable = lib.mkDefault false;
id = "e5823_jtbr-photos";
watch = false;
devices = [ "sterni" "workout" "pepe" "smartphone" ];
};
video-material = {
enable = lib.mkDefault false;
id = "wgkun-fec5h";
watch = false;
devices = [ "workout" "workhorse" ];
@ -125,19 +140,23 @@ with lib;
# one on one
porani-workout = {
enable = lib.mkDefault false;
devices = [ "porani" "workout" ];
watch = false;
};
porani-pepe = {
enable = lib.mkDefault false;
devices = [ "porani" "pepe" ];
watch = false;
};
windows-sync = {
enable = lib.mkDefault false;
id = "hcity-p5ikc";
watch = false;
devices = [ "bumba" "workout" ];
};
workout-pepe = {
enable = lib.mkDefault false;
devices = [ "pepe" "workout" ];
watch = false;
};
@ -150,7 +169,7 @@ with lib;
# convenience script to know which folder needs to be configured
environment.systemPackages =
let
folders = config.test.services.syncthing.declarative.folders;
folders = config.services.syncthing.declarative.folders;
computers = unique (flatten (mapAttrsToList (_: value: value.devices) folders));
isComputerInDeviceList = computer: deviceList: (unique deviceList) == (unique (deviceList ++ [ computer ]));
getFolderNames = computer: naturalSort (builtins.attrNames (filterAttrs (_: folder: isComputerInDeviceList computer folder.devices) folders));