1 { coreutils, jq, nix, nixfmt, syncthing, writeShellScriptBin, }:
2 writeShellScriptBin "syncthing-set-id" ''
5 PATH="${coreutils}/bin"
9 usage: syncthing-set-device-id file [name] [id]
10 example: syncthing-set-device-id /etc/nixos/modules/syncthing-devices.nix
11 example: syncthing-set-device-id /etc/nixos/modules/syncthing-devices.nix lappy
12 example: syncthing-set-device-id /etc/nixos/modules/syncthing-devices.nix lappy 1234567-89ABCDE-FGHIJKL-MNOPQRS-TUVWXYZ-0123456-789ABCD-EFGHIJK
14 If `name` is not specified, $HOSTNAME is used.
15 If `id` is not specified, the ID of the current system instance is used.
20 [[ $# -lt 1 || "$1" == --help ]] && usage
22 name=''${2:-"$HOSTNAME"}
24 configDir=$(${nix}/bin/nix --experimental-features nix-command eval --impure --raw --expr '(import <nixos/nixos> {}).config.services.syncthing.configDir')
25 id=$(${syncthing}/bin/syncthing -home="$configDir" -device-id)
30 if [[ ! -e "$file" ]];then
31 echo "Creating $file" >&2
35 tmp=$(mktemp "$(dirname "$file")/.$(basename "$file").XXXXXXXXXX")
37 # Use `nix-instantiate --eval -E --json | jq -r` instead of `nix eval --raw` until
38 # https://github.com/NixOS/nix/issues/2678 is fixed.
39 ${nix}/bin/nix-instantiate --eval -E --json \
40 --argstr file "$file" \
41 --argstr name "$name" \
45 let pkgs = import <nixpkgs> { };
46 in pkgs.lib.generators.toPretty { } (pkgs.lib.recursiveUpdate (import file) {
47 services.syncthing.devices."''${name}".id = id;
49 ' | ${jq}/bin/jq -r | ${nixfmt}/bin/nixfmt > "$tmp"