1 import <nixpkgs/nixos/tests/make-test-python.nix> ({ pkgs, lib, ... }:
3 deviceIDFilename = "/tmp/syncthing-auto-register-test-device-ids.nix";
4 vmConfiguration = pkgs.writeText "vm-configuration.nix" ''
7 ${../.}/lib/test-nixos-rebuild-switch-config.nix
8 ${../.}/modules/syncthing-autoregister.nix
10 services.syncthing = {
12 openDefaultPorts = true;
15 path = "${deviceIDFilename}";
19 # For verifyDeviceIDSet()
20 environment.systemPackages = with pkgs; [ nix ];
24 evaluatedVMConfiguration = import <nixos/nixos/lib/eval-config.nix> {
25 system = builtins.currentSystem;
26 modules = [ "${vmConfiguration}" ];
29 # Also include a syncthing configuration with a device id
30 # because using this feature pulls in additional dependencies.
31 evaluatedSyncthingDeclarativeConfiguration = import <nixos/nixos/lib/eval-config.nix> {
32 system = builtins.currentSystem;
34 ../lib/test-nixos-rebuild-switch-config.nix
36 services.syncthing = {
38 settings.folders.forDeps.path = "/nope";
44 initialConfiguration = {
45 imports = [ "${vmConfiguration}" ];
46 virtualisation.memorySize = 2048;
47 system.activationScripts.installInitialConfiguration = {
50 cp "${vmConfiguration}" /etc/nixos/configuration.nix
54 system.extraDependencies = [
55 evaluatedVMConfiguration.config.system.build.toplevel
56 evaluatedSyncthingDeclarativeConfiguration.config.system.build.toplevel
60 configurationWithDeviceIDs =
61 pkgs.writeText "configuration-with-device-ids.nix" ''
70 configurationWithFolder =
71 pkgs.writeText "configuration-with-folder.nix" ''
73 imports = [ ${configurationWithDeviceIDs} ];
74 services.syncthing.settings.folders.foo = {
75 devices = [ "a" "b" ];
76 path = "''${config.services.syncthing.dataDir}/foo";
86 a = initialConfiguration;
87 b = initialConfiguration;
94 def setConfig(machine, config):
95 machine.copy_from_host(config, "/etc/nixos/configuration.nix")
98 def verifyDeviceIDSet(machine, expectedDevice):
99 machine.wait_until_succeeds(
100 '(( "$(nix --experimental-features nix-command eval --impure --raw --expr "(import <nixos/nixos> {}).config.services.syncthing.settings.devices.%s.id" | wc -c)" == 63 ))'
105 def getDeviceFile(machine):
106 machine.copy_from_vm("${deviceIDFilename}")
109 def putDeviceFile(machine):
110 hostDeviceIDFilename = os.path.join(
112 os.path.basename("${deviceIDFilename}"),
114 machine.copy_from_host(
115 hostDeviceIDFilename, "${deviceIDFilename}"
117 machine.succeed("chown syncthing ${deviceIDFilename}")
120 configurationWithDeviceIDs = (
121 "${configurationWithDeviceIDs}"
123 configurationWithFolder = (
124 "${configurationWithFolder}"
128 a.wait_for_unit("syncthing-autoregister.service")
129 setConfig(a, configurationWithDeviceIDs)
130 verifyDeviceIDSet(a, "a")
133 b.wait_for_unit("syncthing-autoregister.service")
135 setConfig(b, configurationWithDeviceIDs)
136 verifyDeviceIDSet(b, "a")
137 verifyDeviceIDSet(b, "b")
141 setConfig(a, configurationWithFolder)
142 setConfig(b, configurationWithFolder)
143 a.succeed("nixos-rebuild switch")
144 b.succeed("nixos-rebuild switch")
145 a.wait_for_file("/var/lib/syncthing/foo")
146 b.wait_for_file("/var/lib/syncthing/foo")
147 a.succeed("echo a2b > /var/lib/syncthing/foo/a2b")
148 b.succeed("echo b2a > /var/lib/syncthing/foo/b2a")
149 a.wait_for_file("/var/lib/syncthing/foo/b2a")
150 b.wait_for_file("/var/lib/syncthing/foo/a2b")