]> git.scottworley.com Git - syncthing-autoregister/commitdiff
Modify syncthing config in-place to add device id
authorScott Worley <scottworley@scottworley.com>
Tue, 21 Jul 2020 18:10:21 +0000 (11:10 -0700)
committerScott Worley <scottworley@scottworley.com>
Tue, 21 Jul 2020 18:36:15 +0000 (11:36 -0700)
COPYING [new file with mode: 0644]
README [new file with mode: 0644]
default.nix [new file with mode: 0644]
syncthing-set-id.nix [new file with mode: 0644]

diff --git a/COPYING b/COPYING
new file mode 100644 (file)
index 0000000..59ce2a3
--- /dev/null
+++ b/COPYING
@@ -0,0 +1,20 @@
+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.
diff --git a/README b/README
new file mode 100644 (file)
index 0000000..d462866
--- /dev/null
+++ b/README
@@ -0,0 +1,58 @@
+# 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.
diff --git a/default.nix b/default.nix
new file mode 100644 (file)
index 0000000..320a65f
--- /dev/null
@@ -0,0 +1,8 @@
+{ pkgs ? import <nixpkgs> { }, }:
+
+pkgs.lib.makeScope pkgs.newScope (self:
+  with self; {
+
+    syncthing-set-id = callPackage ./syncthing-set-id.nix { };
+
+  })
diff --git a/syncthing-set-id.nix b/syncthing-set-id.nix
new file mode 100644 (file)
index 0000000..0277983
--- /dev/null
@@ -0,0 +1,52 @@
+{ 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"
+''