--- /dev/null
+Copyright (c) 2020 Scott Worley
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--- /dev/null
+# syncthing-set-id
+
+Modify in-place a nix module that configures syncthing device ids. This file can used as a normal `include` in nixos configs. This eliminates the need to copy-paste these device IDs around.
+
+ desktop$ syncthing-set-id syncthing-devices.nix
+ Creating /home/user/myNixOSConfigs/syncthing-devices.nix
+
+ desktop$ cat syncthing-devices.nix
+ {
+ "services" = {
+ "syncthing" = {
+ "declarative" = {
+ "devices" = {
+ "desktop" = {
+ "id" =
+ "1234567-89ABCDE-FGHIJKL-MNOPQRS-TUVWXYZ-0123456-789ABCD-EFGHIJK";
+ };
+ };
+ };
+ };
+ };
+ }
+
+Then, later, on another machine:
+
+ laptop$ syncthing-set-id syncthing-devices.nix
+
+ laptop$ cat syncthing-devices.nix
+ {
+ "services" = {
+ "syncthing" = {
+ "declarative" = {
+ "devices" = {
+ "desktop" = {
+ "id" =
+ "1234567-89ABCDE-FGHIJKL-MNOPQRS-TUVWXYZ-0123456-789ABCD-EFGHIJK";
+ };
+ "laptop" = {
+ "id" =
+ "1111111-2222222-3333333-4444444-5555555-6666666-7777777-8888888";
+ };
+ };
+ };
+ };
+ };
+ }
+
+Usage
+
+ $ syncthing-set-id
+
+ usage: syncthing-set-device-id file [name] [id]
+ example: syncthing-set-device-id /etc/nixos/modules/syncthing-devices.nix
+ example: syncthing-set-device-id /etc/nixos/modules/syncthing-devices.nix lappy
+ example: syncthing-set-device-id /etc/nixos/modules/syncthing-devices.nix lappy 1234567-89ABCDE-FGHIJKL-MNOPQRS-TUVWXYZ-0123456-789ABCD-EFGHIJK
+
+ If `name` is not specified, $HOSTNAME is used.
+ If `id` is not specified, the ID of the current system instance is used.
--- /dev/null
+{ pkgs ? import <nixpkgs> { }, }:
+
+pkgs.lib.makeScope pkgs.newScope (self:
+ with self; {
+
+ syncthing-set-id = callPackage ./syncthing-set-id.nix { };
+
+ })
--- /dev/null
+{ coreutils, jq, nix, nixfmt, syncthing, writeShellScriptBin, }:
+writeShellScriptBin "syncthing-set-id" ''
+ set -euo pipefail
+
+ PATH="${coreutils}/bin"
+
+ usage() {
+ cat <<< '
+ usage: syncthing-set-device-id file [name] [id]
+ example: syncthing-set-device-id /etc/nixos/modules/syncthing-devices.nix
+ example: syncthing-set-device-id /etc/nixos/modules/syncthing-devices.nix lappy
+ example: syncthing-set-device-id /etc/nixos/modules/syncthing-devices.nix lappy 1234567-89ABCDE-FGHIJKL-MNOPQRS-TUVWXYZ-0123456-789ABCD-EFGHIJK
+
+ If `name` is not specified, $HOSTNAME is used.
+ If `id` is not specified, the ID of the current system instance is used.
+ ' >&2
+ exit 1
+ }
+
+ [[ $# -lt 1 || "$1" == --help ]] && usage
+ file=$(realpath "$1")
+ name=''${2:-"$HOSTNAME"}
+ if (( $# < 3));then
+ configDir=$(${nix}/bin/nix eval --raw '(import <nixos/nixos> {}).config.services.syncthing.configDir')
+ id=$(${syncthing}/bin/syncthing -home="$configDir" -device-id)
+ else
+ id=$3
+ fi
+
+ if [[ ! -e "$file" ]];then
+ echo "Creating $file" >&2
+ echo '{}' > "$file"
+ fi
+
+ tmp=$(mktemp "$(dirname "$file")/.$(basename "$file").XXXXXXXXXX")
+
+ # Use `nix-instantiate --eval -E --json | jq -r` instead of `nix eval --raw` until
+ # https://github.com/NixOS/nix/issues/2678 is fixed.
+ ${nix}/bin/nix-instantiate --eval -E --json \
+ --argstr file "$file" \
+ --argstr name "$name" \
+ --argstr id "$id" \
+ '
+ { file, name, id }:
+ let pkgs = import <nixpkgs> { };
+ in pkgs.lib.generators.toPretty { } (pkgs.lib.recursiveUpdate (import file) {
+ services.syncthing.declarative.devices."''${name}".id = id;
+ })
+ ' | ${jq}/bin/jq -r | ${nixfmt}/bin/nixfmt > "$tmp"
+
+ mv "$tmp" "$file"
+''