Femtodata · Technology Consulting


syncthing-config: Centralized Config for Syncthing Nodes

One place to define syncthing devices and folders




I like syncthing for automatically syncing stuff between my laptops, servers, and phones. NixOS conveniently has a module for declaratively setting up devices and folders to share, but editing the configs by hand and keeping them in sync was getting to be a pain.

Enter syncthing-config, a module to declare your devices, folders, and which devices get which folders, all in one place.

To use, define a attribute set of syncthing data, this will be shared between all nixosConfiguration that use this module. Then, implement in each host by declaring its syncthing node name, importing the syncthing data attribute set.

Host config

We'll go backwards because it's simpler -- here's the syncthing-config.nix for my laptop, nyx:

# hosts/nyx/modules/syncthing-config.nix
{ ... }:
let
  syncthing-data = (import ../../../modules/syncthing-data.nix);
in
{
  # if you're copying the module to your own repo, otherwise you import this module via the nix-utils flake
  imports = [
    ../../../modules/syncthing-config.nix
  ];

  services.syncthing-config = {
    enable = true;
    devices = syncthing-data.devices;
    folders = syncthing-data.folders;
    thisDevice = "nyx";
  };
}

Data config

There's a lot going on in the example below, but the structure is pretty simple: define devices and folders. If nix-managed, devices include ID (bootstrapping instructions [below][#bootstrappin]), and other settings as defined by the NixOS module. Folders are pretty much just which devices should have access to them. Note that since I'm not using nix on my phone, you just specify an empty { } for those folder devices -- the path will be determined manually on the phone. You can also specify extraSettings for nix-managed devices, and versioning for folders on a per-device basis.

In the below example, boulder is a headless server, nyx is a laptop, and pixel-9p is a phone.

# syncthing-data.nix
{
  devices = {
    "boulder" = {
      id = "XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX";
      addresses = [
        "tcp://boulder"
        "dynamic"
      ];
      dataDir = "/mnt/encrypted/syncthing/data";
      configDir = "/mnt/encrypted/syncthing/config";
      user = "alexou";
      group = "users";
      extraSettings = {
        options = {
          urAccepted = 3;
        };
      };
    };
    "nyx" = {
      id = "XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX";
      addresses = [
        "tcp://nyx"
        "dynamic"
      ];
      dataDir = "/home/alexou/Documents/syncthing";
      configDir = "/home/alexou/.config/syncthing";
      user = "alexou";
      group = "users";
    };
    "pixel-9p" = {
      id = "XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX";
      addresses = [
        "tcp://pixel-9p"
        "dynamic"
      ];
    };
  };
  folders = {
    "keepassxc" = {
      "boulder" = {
        path = "/mnt/encrypted/syncthing/data/keepassxc";
        versioning = {
          "type" = "simple";
          "params.keep" = "10";
        };
      };
      "nyx" = {
        path = "/home/alexou/Documents/syncthing/keepassxc";
        versioning = {
          "type" = "simple";
          "params.keep" = "10";
        };
      };
      "pixel-9p" = { };
    };
    "obsidian-vault" = {
      "boulder" = {
        path = "/mnt/encrypted/syncthing/data/obsidian-vault";
        versioning = {
          "type" = "simple";
          "params.keep" = "10";
        };
      };
      "nyx" = {
        path = "/home/alexou/Documents/syncthing/obsidian-vault";
        versioning = {
          "type" = "simple";
          "params.keep" = "10";
        };
      };
      "pixel-9p" = { };
    };
    soundcloud = {
      boulder = {
        path = "/mnt/encrypted/syncthing/data/soundcloud";
      };
      nyx = {
        path = "/home/alexou/Documents/syncthing/soundcloud";
      };
    };
  };
}

Bootstrapping

You may be wondering, how do you get this started before you have a syncthing ID? The id attribute is only used by other hosts, so when you first bootstrap syncthing on a new computer, define the node with no id, and do not include it in any folder sharing definitions. When you nixos-rebuild switch, syncthing will start up and generate an ID. Once you have that, go back into syncthing-data.nix and add the id attribute for the new host, and add to the desired folders.

The NixOS wiki entry describes declaring the key.pem and cert.pem so they are stable, but I have not found that necesssary.

Good luck!