1
1
Fork 0

Compare commits

...

4 Commits

1 changed files with 424 additions and 433 deletions

View File

@ -3,8 +3,9 @@
with lib; with lib;
let let
cfg = config.services.syncthing; cfg = config.services.syncthings;
opt = options.services.syncthing; enabledInstances = filterAttrs (n: v: v.enable) cfg.instances;
opt = options.services.syncthings;
defaultUser = "syncthing"; defaultUser = "syncthing";
defaultGroup = defaultUser; defaultGroup = defaultUser;
settingsFormat = pkgs.formats.json { }; settingsFormat = pkgs.formats.json { };
@ -145,7 +146,12 @@ let
in { in {
###### interface ###### interface
options = { options = {
services.syncthing = { services.syncthings = {
instances = mkOption {
description = mdDoc "Syncthing instance definitions";
default = {};
type = types.attrsOf (types.submodule {
options = {
enable = mkEnableOption enable = mkEnableOption
(lib.mdDoc "Syncthing, a self-hosted open-source alternative to Dropbox and Bittorrent Sync"); (lib.mdDoc "Syncthing, a self-hosted open-source alternative to Dropbox and Bittorrent Sync");
@ -491,14 +497,6 @@ in {
''; '';
}; };
systemService = mkOption {
type = types.bool;
default = true;
description = lib.mdDoc ''
Whether to auto-launch Syncthing as a system service.
'';
};
user = mkOption { user = mkOption {
type = types.str; type = types.str;
default = defaultUser; default = defaultUser;
@ -577,19 +575,23 @@ in {
''; '';
}; };
openDefaultPorts = mkOption { openPorts = mkOption {
type = types.bool; type = types.bool;
default = false; default = false;
example = true; example = true;
description = lib.mdDoc '' description = mdDoc "Whether to open the transfer port and discovery port in the firewall.";
Whether to open the default ports in the firewall: TCP/UDP 22000 for transfers };
and UDP 21027 for discovery.
If multiple users are running Syncthing on this machine, you will need transferPort = mkOption {
to manually open a set of ports for each instance and leave this disabled. type = types.port;
Alternatively, if you are running only a single instance on this machine example = 22000;
using the default ports, enable this. description = mdDoc "The TCP/UDP port for transfers.";
''; };
discoveryPort = mkOption {
type = types.port;
example = 21027;
description = mdDoc "The UDP port for discovery.";
}; };
package = mkOption { package = mkOption {
@ -601,33 +603,22 @@ in {
''; '';
}; };
}; };
});
};
};
}; };
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 web interface.
'')
(mkRenamedOptionModule [ "services" "syncthing" "extraOptions" ] [ "services" "syncthing" "settings" ])
(mkRenamedOptionModule [ "services" "syncthing" "folders" ] [ "services" "syncthing" "settings" "folders" ])
(mkRenamedOptionModule [ "services" "syncthing" "devices" ] [ "services" "syncthing" "settings" "devices" ])
(mkRenamedOptionModule [ "services" "syncthing" "options" ] [ "services" "syncthing" "settings" "options" ])
] ++ map (o:
mkRenamedOptionModule [ "services" "syncthing" "declarative" o ] [ "services" "syncthing" o ]
) [ "cert" "key" "devices" "folders" "overrideDevices" "overrideFolders" "extraOptions"];
###### implementation ###### implementation
config = mkIf cfg.enable { config = mkIf (enabledInstances != {}) {
networking.firewall = {
networking.firewall = mkIf cfg.openDefaultPorts {
allowedTCPPorts = [ 22000 ]; allowedTCPPorts = [ 22000 ];
allowedUDPPorts = [ 21027 22000 ]; allowedUDPPorts = [ 21027 22000 ];
}; };
systemd.packages = [ pkgs.syncthing ]; systemd.packages = [ pkgs.syncthing ];
users.users = mkIf (cfg.systemService && cfg.user == defaultUser) { users.users = mkIf (cfg.user == defaultUser) {
${defaultUser} = ${defaultUser} =
{ group = cfg.group; { group = cfg.group;
home = cfg.dataDir; home = cfg.dataDir;
@ -637,7 +628,7 @@ in {
}; };
}; };
users.groups = mkIf (cfg.systemService && cfg.group == defaultGroup) { users.groups = mkIf (cfg.group == defaultGroup) {
${defaultGroup}.gid = ${defaultGroup}.gid =
config.ids.gids.syncthing; config.ids.gids.syncthing;
}; };
@ -645,7 +636,7 @@ in {
systemd.services = { systemd.services = {
# upstream reference: # upstream reference:
# https://github.com/syncthing/syncthing/blob/main/etc/linux-systemd/system/syncthing%40.service # https://github.com/syncthing/syncthing/blob/main/etc/linux-systemd/system/syncthing%40.service
syncthing = mkIf cfg.systemService { syncthing = {
description = "Syncthing service"; description = "Syncthing service";
after = [ "network.target" ]; after = [ "network.target" ];
environment = { environment = {