From b00b144bd77fdaca30cb490e78e06e41617a79ad Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Mon, 27 Jul 2020 13:51:13 -0700 Subject: [PATCH] Syncthing device-id autoregistration --- modules/syncthing-autoregister.nix | 22 ++++++++++++++++++++ tests/automatic-enrollment.nix | 32 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 modules/syncthing-autoregister.nix create mode 100644 tests/automatic-enrollment.nix diff --git a/modules/syncthing-autoregister.nix b/modules/syncthing-autoregister.nix new file mode 100644 index 0000000..d1f0a7c --- /dev/null +++ b/modules/syncthing-autoregister.nix @@ -0,0 +1,22 @@ +{ config, lib, pkgs, ... }: +let + localpkgs = import ../. { inherit pkgs; }; + register-script = pkgs.writeShellScript "syncthing-autoregister-script" '' + ${localpkgs.syncthing-set-id}/bin/syncthing-set-id /tmp/syncthing-auto-register-test-device-ids.nix + ''; +in { + config = { + systemd.services.syncthing-autoregister = { + after = [ "syncthing.service" "syncthing-init.service" ]; + wantedBy = [ "multi-user.target" ]; + environment.NIX_PATH = config.environment.variables.NIX_PATH; + serviceConfig = { + User = config.services.syncthing.user; + RemainAfterExit = true; + Type = "oneshot"; + ExecStart = register-script; + }; + }; + }; +} + diff --git a/tests/automatic-enrollment.nix b/tests/automatic-enrollment.nix new file mode 100644 index 0000000..cac1b28 --- /dev/null +++ b/tests/automatic-enrollment.nix @@ -0,0 +1,32 @@ +import ({ pkgs, lib, ... }: + let + configuration = pkgs.writeText "configuration.nix" '' + { pkgs, ... }: { + imports = [ + ${../.}/modules/syncthing-autoregister.nix + + ]; + services.syncthing = { + enable = true; + openDefaultPorts = true; + }; + environment.systemPackages = with pkgs; [ nix ]; + } + ''; + in { + + name = "syncthing"; + + machine = "${configuration}"; + + testScript = '' + machine.copy_from_host( + "${configuration}", + "/etc/nixos/configuration.nix", + ) + machine.wait_for_unit("syncthing-autoregister.service") + machine.succeed( + '(( "$(nix eval --raw -f /tmp/syncthing-auto-register-test-device-ids.nix services.syncthing.declarative.devices.machine.id | wc -c)" == 63 ))' + ) + ''; + }) -- 2.44.1