···
-
{ config, lib, pkgs, ... }:
-
cfg = config.modules.syncthing;
-
# Helper function to create a serviceConfig entry if the condition is met
-
mkServiceConfigOption = name: value: mkIf (value != null) { "${name}" = value; };
-
# Construct the settings object for Syncthing
-
syncthingSettings = mkMerge [
-
(mkIf (cfg.gui.user != null) {
-
# Devices configuration
-
(mkIf (cfg.devices != {}) {
-
devices = mapAttrs (name: device: {
-
} // optionalAttrs (device.name != null) {
-
} // optionalAttrs (device.addresses != []) {
-
addresses = device.addresses;
-
# Folders configuration
-
(mkIf (cfg.folders != {}) {
-
folders = mapAttrs (name: folder: {
-
devices = folder.devices;
-
} // optionalAttrs (folder.ignorePerms != null) {
-
ignorePerms = folder.ignorePerms;
-
} // optionalAttrs (folder.type != null) {
-
} // optionalAttrs (folder.rescanIntervalS != null) {
-
rescanIntervalS = folder.rescanIntervalS;
-
} // optionalAttrs (folder.versioning != null) {
-
versioning = folder.versioning;
-
enable = mkEnableOption "Deploy syncthing";
-
openDefaultPorts = mkOption {
-
description = "Open ports in the firewall for Syncthing";
-
disableDefaultFolder = mkOption {
-
description = "Don't create default ~/Sync folder";
-
enable = mkEnableOption "Enable GUI configuration";
-
type = types.nullOr types.str;
-
description = "GUI username";
-
passwordFile = mkOption {
-
type = types.nullOr types.path;
-
description = "Path to file containing GUI password";
-
example = "config.age.secrets.syncthing-gui-password.path";
-
type = types.nullOr types.path;
-
description = "Path to Syncthing private key for stable device ID";
-
example = "config.age.secrets.syncthing-key.path";
-
type = types.nullOr types.path;
-
description = "Path to Syncthing certificate for stable device ID";
-
example = "config.age.secrets.syncthing-cert.path";
-
type = types.attrsOf (types.submodule {
-
description = "Device ID";
-
example = "DMWVMM6-MKEQVB4-I4UZTRH-5A6E24O-XHQTL3K-AAI5R5L-MXNMUGX-QTGRHQ2";
-
type = types.nullOr types.str;
-
description = "Device name (optional)";
-
type = types.listOf types.str;
-
description = "Device addresses";
-
example = [ "tcp://192.168.1.100:22000" ];
-
description = "Syncthing devices configuration";
-
id = "DMWVMM6-MKEQVB4-I4UZTRH-5A6E24O-XHQTL3K-AAI5R5L-MXNMUGX-QTGRHQ2";
-
id = "ANOTHER-DEVICE-ID-GOES-HERE";
-
addresses = [ "tcp://192.168.1.101:22000" ];
-
type = types.attrsOf (types.submodule {
-
description = "Local folder path";
-
example = "/home/myuser/Documents";
-
type = types.listOf (types.either types.str (types.submodule {
-
description = "Device name";
-
encryptionPasswordFile = mkOption {
-
description = "Path to file containing encryption password";
-
description = "List of devices that can access this folder";
-
example = [ "laptop" "phone" ];
-
ignorePerms = mkOption {
-
type = types.nullOr types.bool;
-
description = "Whether to ignore file permissions";
-
type = types.nullOr (types.enum [ "sendreceive" "sendonly" "receiveonly" ]);
-
description = "Folder type";
-
rescanIntervalS = mkOption {
-
type = types.nullOr types.int;
-
description = "Rescan interval in seconds";
-
versioning = mkOption {
-
type = types.nullOr (types.submodule {
-
type = types.enum [ "external" "simple" "staggered" "trashcan" ];
-
description = "Versioning type";
-
type = types.attrsOf types.str;
-
description = "Versioning parameters";
-
description = "Folder versioning configuration";
-
description = "Syncthing folders configuration";
-
path = "/home/myuser/Documents";
-
devices = [ "laptop" "phone" ];
-
path = "/home/myuser/Sensitive";
-
encryptionPasswordFile = "/run/secrets/syncthing-sensitive-password";
-
extraOptions = mkOption {
-
type = types.attrsOf types.anything;
-
description = "Additional Syncthing configuration options";
-
config = mkIf cfg.enable {
-
openDefaultPorts = cfg.openDefaultPorts;
-
# Set stable identity if provided
-
key = mkIf (cfg.identity.keyPath != null) cfg.identity.keyPath;
-
cert = mkIf (cfg.identity.certPath != null) cfg.identity.certPath;
-
settings = syncthingSettings;
-
# Configure systemd service options collectively
-
systemd.services.syncthing = {
-
# Add environment variable to disable default folder creation
-
environment.STNODEFAULTFOLDER = mkIf cfg.disableDefaultFolder "true";
-
# Add supplementary groups for secret access
-
serviceConfig.SupplementaryGroups = [ "syncthing-secrets" ];
-
# Create a group for accessing secrets
-
users.groups.syncthing-secrets = {};