Jump to content

Syncthing: Difference between revisions

From NixOS Wiki
Wrycode (talk | contribs)
m Add note about disabling default sync folder creation
Disable default sync folder: Simplify code by using command-line argument instead of env var
 
(27 intermediate revisions by 14 users not shown)
Line 1: Line 1:
Syncthing is a decentralized file sync service. You can use it to safely sync all files in a folder between different desktops/servers. In other Linux distribution you configure it via Web-GUI. In NixOS you can fully configure it using Nix.
<languages/>
== Install ==
[https://syncthing.net/ Syncthing] is a free and open-source decentralized file synchronization application that allows for secure, continuous, and private syncing of files between computers. Unlike cloud-based services, Syncthing operates peer-to-peer, so your data remains on your devices unless you choose to share it. It is cross-platform, offering native support for Linux, macOS, Windows, BSD, and mobile devices.<ref>https://syncthing.net/</ref>
Syncthing is available as a standalone package: <code>nix-env -iA nixos.syncthing</code>


It can also be enabled as a service. Example:
== Installation ==


<syntaxHighlight lang="nix">
==== Shell ====
services = {
 
     syncthing = {
To temporarily use Syncthing in a shell environment without modifying your system configuration, you can run:
        enable = true;
<syntaxhighlight lang="bash">
        user = "myusername";
nix-shell -p syncthing --run syncthing
         dataDir = "/home/myusername/Documents";   # Default folder for new synced folders
</syntaxhighlight>
         configDir = "/home/myusername/Documents/.config/syncthing";   # Folder for Syncthing's settings and keys
This provides Syncthing in your current shell without adding it to your system configuration. You can open the web interface at http://127.0.0.1:8384/ to configure and use it.
 
==== System setup ====
 
To install Syncthing as a system service that runs in the background and survives reboots, add the following to your <code>/etc/nixos/configuration.nix</code>:
<syntaxhighlight lang="nix">
# Example for /etc/nixos/configuration.nix
services.syncthing = {
  enable = true;
  openDefaultPorts = true; # Open ports in the firewall for Syncthing
};
</syntaxhighlight>
Once you've rebuilt your system, Syncthing will be available as a system service. You can visit http://127.0.0.1:8384/ to configure it through the web interface.
 
== Configuration ==
 
==== Basic ====
 
Basic Syncthing features can be configured directly within the <code>services.syncthing</code> attribute set:
<syntaxhighlight lang="nix">
services.syncthing = {
  enable = true;
  openDefaultPorts = true;
  # Optional: GUI credentials (can be set in the browser instead)
  settings.gui = {
     user = "myuser";
    password = "mypassword";
  };
};
</syntaxhighlight>
 
==== Advanced ====
 
For more advanced configuration with multiple devices and folders, you can declaratively configure devices and shared folders:<ref>https://docs.syncthing.net/users/config.html</ref>
<syntaxhighlight lang="nix">
services.syncthing = {
  enable = true;
  openDefaultPorts = true;
  settings = {
    gui = {
      user = "myuser";
      password = "mypassword";
    };
    devices = {
      "device1" = { id = "DEVICE-ID-GOES-HERE"; };
      "device2" = { id = "DEVICE-ID-GOES-HERE"; };
    };
    folders = {
      "Documents" = {
         path = "/home/myusername/Documents";
         devices = [ "device1" "device2" ];
      };
      "Example" = {
        path = "/home/myusername/Example";
        devices = [ "device1" ];
        ignorePerms = false; # Enable file permission syncing
      };
     };
     };
  };
};
};
</syntaxHighlight>
</syntaxhighlight>


You can confirm Syncthing runs by visiting http://127.0.0.1:8384/ and following the official Getting Started guide: https://docs.syncthing.net/intro/getting-started.html
== Tips and tricks ==


== Declarative configuration ==
=== Sync folders and remote hosts ===
You can declaratively set your Syncthing folders by using the <code>services.syncthing.devices</code> and <code>services.syncthing.folders</code> options:


(Note: Before NixOS 21.11, declarative configuration was done in the <code>services.syncthing.declarative</code> option, such as <code>services.syncthing.declarative.folders = {};</code>)
The following configuration will trust the remote hosts <code>device1</code> and <code>device2</code> by adding their <code>id</code>s. The shares <code>Documents</code> and <code>Example</code> are added to the local node, defined by their local file paths and list of allowed devices.


<syntaxHighlight lang="nix">
The share <code>Sensitive</code> is shared unencrypted with <code>device1</code>, and encrypted with <code>device2</code>:
services = {
<syntaxhighlight lang="nix">
   syncthing = {
services.syncthing = {
     enable = true;
   settings = {
    user = "myusername";
     devices = {
     dataDir = "/home/myusername/Documents";
      "device1" = { id = "DEVICE-ID-GOES-HERE"; };
    configDir = "/home/myusername/Documents/.config/syncthing";
      "device2" = { id = "DEVICE-ID-GOES-HERE"; };
    overrideDevices = true;     # overrides any devices added or deleted through the WebUI
    };
    overrideFolders = true;     # overrides any folders added or deleted through the WebUI
     folders = {
    settings = {
      "Documents" = {
       devices = {
        path = "/home/myusername/Documents";
         "device1" = { id = "DEVICE-ID-GOES-HERE"; };
        devices = [ "device1" "device2" ];
         "device2" = { id = "DEVICE-ID-GOES-HERE"; };
      };
       "Example" = {
         path = "/home/myusername/Example";
         devices = [ "device1" ];
        # By default, Syncthing doesn't sync file permissions. This line enables it for this folder.
        ignorePerms = false;
       };
       };
       folders = {
       "Sensitive" = {
        "Documents" = {        # Name of folder in Syncthing, also the folder ID
         path = "/home/myusername/Sensitive";
          path = "/home/myusername/Documents";   # Which folder to add to Syncthing
        devices = [
           devices = [ "device1" "device2" ];      # Which devices to share the folder with
          # We trust this device to have access
        };
           # to the decrypted contents of this folder.
        "Example" = {
          "device1"
           path = "/home/myusername/Example";
           # We do not trust this device, but we want to have another
           devices = [ "device1" ];
          # (encrypted) copy of the data for redundancy/backup/sync purposes.
          ignorePerms = false;  # By default, Syncthing doesn't sync file permissions. This line enables it for this folder.
           {
         };
            name = "device2";
            # encryptionPasswordFile is a path to a file containing the encryption password.
            # See below for information about managing secrets on NixOS.
            encryptionPasswordFile = "/run/secrets/st-sensitive-password";
          }
         ];
       };
       };
     };
     };
   };
   };
};
};
</syntaxHighlight>
</syntaxhighlight>
 
=== Declarative node IDs ===
 
If you set up Syncthing with the above configuration, you will still need to manually accept the connection from your other devices. If you want to make this automatic, you must also set the key.pem and cert.pem options:
<syntaxhighlight lang="nix">
services.syncthing = {
  key = "/run/secrets/path/to/key.pem";
  cert = "/run/secrets/path/to/cert.pem";
  # ... other configuration
};
</syntaxhighlight>
This will ensure your node has a stable ID. You can optionally include the key.pem and cert.pem files in the NixOS configuration using a tool like sops-nix. See [[Comparison of secret managing schemes]].
 
To generate a new key.cert and key.pem for a deployment, you can use the -generate argument:
<syntaxhighlight lang="bash">
$ nix-shell -p syncthing --run "syncthing generate --config myconfig/"
2024/04/23 11:41:17 INFO: Generating ECDSA key and certificate for syncthing...
2024/04/23 11:41:17 INFO: Device ID: DMWVMM6-MKEQVB4-I4UZTRH-5A6E24O-XHQTL3K-AAI5R5L-MXNMUGX-QTGRHQ2
2024/04/23 11:41:17 INFO: Default folder created and/or linked to new config
$ ls myconfig/
cert.pem  config.xml  key.pem
</syntaxhighlight>


If running a headless server, you should also change guiAddress to a publicly visible one (or just 0.0.0.0:8384, for example).
=== Disable default sync folder ===


You will also probably have to open a few ports in the firewall:
Syncthing creates a 'Sync' folder in your home directory every time it regenerates a configuration, even if your declarative configuration does not have this folder. You can disable that by using the <code>--no-default-folder</code> command-line option<ref>https://docs.syncthing.net/users/syncthing.html#cmdoption-no-default-folder</ref>:
<syntaxHighlight lang="nix">
<syntaxhighlight lang="nix">
  # Syncthing ports: 8384 for remote access to GUI
services.syncthing.extraFlags = [ "--no-default-folder" ]; # Don't create default ~/Sync folder
  # 22000 TCP and/or UDP for sync traffic
</syntaxhighlight>
  # 21027/UDP for discovery
  # source: https://docs.syncthing.net/users/firewall.html
  networking.firewall.allowedTCPPorts = [ 8384 22000 ];
  networking.firewall.allowedUDPPorts = [ 22000 21027 ];
</syntaxHighlight>


It is also a good idea to protect the web GUI with a username and password combination:
== Troubleshooting ==
<syntaxHighlight lang="nix">
 
services.syncthing.settings.gui = {
== See also ==
    user = "username";
    password = "password";
};
</syntaxHighlight>


== Disable default sync folder ==
* [[Home Manager]] – Use Syncthing declaratively at the user level: [https://github.com/nix-community/home-manager/blob/master/modules/services/syncthing.nix Syncthing module in Home Manager]
Syncthing creates a ‘Sync’ folder in your home directory every time it regenerates a configuration (even if your declarative configuration does not have this folder). You can disable that by setting the STNODEFAULTFOLDER environment variable:
* [[Comparison of secret managing schemes]] – Compare different ways to manage secrets declaratively on NixOS, including for use with Syncthing.
<code>systemd'''.'''services'''.'''syncthing'''.'''environment'''.'''STNODEFAULTFOLDER = "true"''';''' ''# Don't create default ~/Sync folder''</code>
* [https://nixos.org/manual/nixos/stable/index.html#sec-services Syncthing in NixOS Manual] – Official documentation for configuring services like Syncthing.
* [https://docs.syncthing.net Syncthing User Documentation] – In-depth official guide on Syncthing features, configuration, and troubleshooting.
* [https://discourse.nixos.org/search?q=syncthing Syncthing discussions on Discourse] – Community tips, troubleshooting, and advanced use cases.


== Home-manager service ==
== References ==


https://github.com/nix-community/home-manager/blob/master/modules/services/syncthing.nix
[[Category:Applications]]
[[Category: Applications]]
[[Category:File synchronization]]
[[Category:Web Applications]]

Latest revision as of 15:33, 3 July 2025

Syncthing is a free and open-source decentralized file synchronization application that allows for secure, continuous, and private syncing of files between computers. Unlike cloud-based services, Syncthing operates peer-to-peer, so your data remains on your devices unless you choose to share it. It is cross-platform, offering native support for Linux, macOS, Windows, BSD, and mobile devices.[1]

Installation

Shell

To temporarily use Syncthing in a shell environment without modifying your system configuration, you can run:

nix-shell -p syncthing --run syncthing

This provides Syncthing in your current shell without adding it to your system configuration. You can open the web interface at http://127.0.0.1:8384/ to configure and use it.

System setup

To install Syncthing as a system service that runs in the background and survives reboots, add the following to your /etc/nixos/configuration.nix:

# Example for /etc/nixos/configuration.nix
services.syncthing = {
  enable = true;
  openDefaultPorts = true; # Open ports in the firewall for Syncthing
};

Once you've rebuilt your system, Syncthing will be available as a system service. You can visit http://127.0.0.1:8384/ to configure it through the web interface.

Configuration

Basic

Basic Syncthing features can be configured directly within the services.syncthing attribute set:

services.syncthing = {
  enable = true;
  openDefaultPorts = true;
  # Optional: GUI credentials (can be set in the browser instead)
  settings.gui = {
    user = "myuser";
    password = "mypassword";
  };
};

Advanced

For more advanced configuration with multiple devices and folders, you can declaratively configure devices and shared folders:[2]

services.syncthing = {
  enable = true;
  openDefaultPorts = true;
  settings = {
    gui = {
      user = "myuser";
      password = "mypassword";
    };
    devices = {
      "device1" = { id = "DEVICE-ID-GOES-HERE"; };
      "device2" = { id = "DEVICE-ID-GOES-HERE"; };
    };
    folders = {
      "Documents" = {
        path = "/home/myusername/Documents";
        devices = [ "device1" "device2" ];
      };
      "Example" = {
        path = "/home/myusername/Example";
        devices = [ "device1" ];
        ignorePerms = false; # Enable file permission syncing
      };
    };
  };
};

Tips and tricks

Sync folders and remote hosts

The following configuration will trust the remote hosts device1 and device2 by adding their ids. The shares Documents and Example are added to the local node, defined by their local file paths and list of allowed devices.

The share Sensitive is shared unencrypted with device1, and encrypted with device2:

services.syncthing = {
  settings = {
    devices = {
      "device1" = { id = "DEVICE-ID-GOES-HERE"; };
      "device2" = { id = "DEVICE-ID-GOES-HERE"; };
    };
    folders = {
      "Documents" = {
        path = "/home/myusername/Documents";
        devices = [ "device1" "device2" ];
      };
      "Example" = {
        path = "/home/myusername/Example";
        devices = [ "device1" ];
        # By default, Syncthing doesn't sync file permissions. This line enables it for this folder.
        ignorePerms = false;
      };
      "Sensitive" = {
        path = "/home/myusername/Sensitive";
        devices = [
          # We trust this device to have access
          # to the decrypted contents of this folder.
          "device1"
          # We do not trust this device, but we want to have another
          # (encrypted) copy of the data for redundancy/backup/sync purposes.
          {
            name = "device2";
            # encryptionPasswordFile is a path to a file containing the encryption password.
            # See below for information about managing secrets on NixOS.
            encryptionPasswordFile = "/run/secrets/st-sensitive-password";
          }
        ];
      };
    };
  };
};

Declarative node IDs

If you set up Syncthing with the above configuration, you will still need to manually accept the connection from your other devices. If you want to make this automatic, you must also set the key.pem and cert.pem options:

services.syncthing = {
  key = "/run/secrets/path/to/key.pem";
  cert = "/run/secrets/path/to/cert.pem";
  # ... other configuration
};

This will ensure your node has a stable ID. You can optionally include the key.pem and cert.pem files in the NixOS configuration using a tool like sops-nix. See Comparison of secret managing schemes.

To generate a new key.cert and key.pem for a deployment, you can use the -generate argument:

$ nix-shell -p syncthing --run "syncthing generate --config myconfig/"
2024/04/23 11:41:17 INFO: Generating ECDSA key and certificate for syncthing...
2024/04/23 11:41:17 INFO: Device ID: DMWVMM6-MKEQVB4-I4UZTRH-5A6E24O-XHQTL3K-AAI5R5L-MXNMUGX-QTGRHQ2
2024/04/23 11:41:17 INFO: Default folder created and/or linked to new config
$ ls myconfig/
cert.pem  config.xml  key.pem

Disable default sync folder

Syncthing creates a 'Sync' folder in your home directory every time it regenerates a configuration, even if your declarative configuration does not have this folder. You can disable that by using the --no-default-folder command-line option[3]:

services.syncthing.extraFlags = [ "--no-default-folder" ]; # Don't create default ~/Sync folder

Troubleshooting

See also

References