···
1
-
{ config, lib, pkgs, ... }:
5
-
cfg = config.modules.syncthing;
7
-
# Helper function to create a serviceConfig entry if the condition is met
8
-
mkServiceConfigOption = name: value: mkIf (value != null) { "${name}" = value; };
10
-
# Construct the settings object for Syncthing
11
-
syncthingSettings = mkMerge [
13
-
(mkIf cfg.gui.enable {
15
-
(mkIf (cfg.gui.user != null) {
16
-
user = cfg.gui.user;
21
-
# Devices configuration
22
-
(mkIf (cfg.devices != {}) {
23
-
devices = mapAttrs (name: device: {
25
-
} // optionalAttrs (device.name != null) {
27
-
} // optionalAttrs (device.addresses != []) {
28
-
addresses = device.addresses;
32
-
# Folders configuration
33
-
(mkIf (cfg.folders != {}) {
34
-
folders = mapAttrs (name: folder: {
36
-
devices = folder.devices;
37
-
} // optionalAttrs (folder.ignorePerms != null) {
38
-
ignorePerms = folder.ignorePerms;
39
-
} // optionalAttrs (folder.type != null) {
41
-
} // optionalAttrs (folder.rescanIntervalS != null) {
42
-
rescanIntervalS = folder.rescanIntervalS;
43
-
} // optionalAttrs (folder.versioning != null) {
44
-
versioning = folder.versioning;
54
-
modules.syncthing = {
55
-
enable = mkEnableOption "Deploy syncthing";
57
-
openDefaultPorts = mkOption {
60
-
description = "Open ports in the firewall for Syncthing";
63
-
disableDefaultFolder = mkOption {
66
-
description = "Don't create default ~/Sync folder";
70
-
enable = mkEnableOption "Enable GUI configuration";
73
-
type = types.nullOr types.str;
75
-
description = "GUI username";
79
-
passwordFile = mkOption {
80
-
type = types.nullOr types.path;
82
-
description = "Path to file containing GUI password";
83
-
example = "config.age.secrets.syncthing-gui-password.path";
88
-
keyPath = mkOption {
89
-
type = types.nullOr types.path;
91
-
description = "Path to Syncthing private key for stable device ID";
92
-
example = "config.age.secrets.syncthing-key.path";
95
-
certPath = mkOption {
96
-
type = types.nullOr types.path;
98
-
description = "Path to Syncthing certificate for stable device ID";
99
-
example = "config.age.secrets.syncthing-cert.path";
103
-
devices = mkOption {
104
-
type = types.attrsOf (types.submodule {
108
-
description = "Device ID";
109
-
example = "DMWVMM6-MKEQVB4-I4UZTRH-5A6E24O-XHQTL3K-AAI5R5L-MXNMUGX-QTGRHQ2";
113
-
type = types.nullOr types.str;
115
-
description = "Device name (optional)";
118
-
addresses = mkOption {
119
-
type = types.listOf types.str;
121
-
description = "Device addresses";
122
-
example = [ "tcp://192.168.1.100:22000" ];
127
-
description = "Syncthing devices configuration";
130
-
id = "DMWVMM6-MKEQVB4-I4UZTRH-5A6E24O-XHQTL3K-AAI5R5L-MXNMUGX-QTGRHQ2";
133
-
id = "ANOTHER-DEVICE-ID-GOES-HERE";
134
-
addresses = [ "tcp://192.168.1.101:22000" ];
139
-
folders = mkOption {
140
-
type = types.attrsOf (types.submodule {
144
-
description = "Local folder path";
145
-
example = "/home/myuser/Documents";
148
-
devices = mkOption {
149
-
type = types.listOf (types.either types.str (types.submodule {
153
-
description = "Device name";
156
-
encryptionPasswordFile = mkOption {
158
-
description = "Path to file containing encryption password";
163
-
description = "List of devices that can access this folder";
164
-
example = [ "laptop" "phone" ];
167
-
ignorePerms = mkOption {
168
-
type = types.nullOr types.bool;
170
-
description = "Whether to ignore file permissions";
174
-
type = types.nullOr (types.enum [ "sendreceive" "sendonly" "receiveonly" ]);
176
-
description = "Folder type";
179
-
rescanIntervalS = mkOption {
180
-
type = types.nullOr types.int;
182
-
description = "Rescan interval in seconds";
185
-
versioning = mkOption {
186
-
type = types.nullOr (types.submodule {
189
-
type = types.enum [ "external" "simple" "staggered" "trashcan" ];
190
-
description = "Versioning type";
193
-
params = mkOption {
194
-
type = types.attrsOf types.str;
196
-
description = "Versioning parameters";
201
-
description = "Folder versioning configuration";
206
-
description = "Syncthing folders configuration";
209
-
path = "/home/myuser/Documents";
210
-
devices = [ "laptop" "phone" ];
211
-
ignorePerms = false;
214
-
path = "/home/myuser/Sensitive";
219
-
encryptionPasswordFile = "/run/secrets/syncthing-sensitive-password";
226
-
extraOptions = mkOption {
227
-
type = types.attrsOf types.anything;
229
-
description = "Additional Syncthing configuration options";
234
-
config = mkIf cfg.enable {
235
-
services.syncthing = {
237
-
openDefaultPorts = cfg.openDefaultPorts;
238
-
# Set stable identity if provided
239
-
key = mkIf (cfg.identity.keyPath != null) cfg.identity.keyPath;
240
-
cert = mkIf (cfg.identity.certPath != null) cfg.identity.certPath;
241
-
# Combine all settings
242
-
settings = syncthingSettings;
245
-
# Configure systemd service options collectively
246
-
systemd.services.syncthing = {
247
-
# Add environment variable to disable default folder creation
248
-
environment.STNODEFAULTFOLDER = mkIf cfg.disableDefaultFolder "true";
250
-
# Add supplementary groups for secret access
251
-
serviceConfig.SupplementaryGroups = [ "syncthing-secrets" ];
254
-
# Create a group for accessing secrets
255
-
users.groups.syncthing-secrets = {};