]> git.scottworley.com Git - syncthing-autoregister/blobdiff - modules/syncthing-autoregister.nix
Follow renames in test data
[syncthing-autoregister] / modules / syncthing-autoregister.nix
index d1f0a7ce117f01bc2217bf93522490a6124f2d86..710c7a42fff96f05e0a026b52b8c0435df439291 100644 (file)
@@ -1,11 +1,39 @@
 { config, lib, pkgs, ... }:
 { config, lib, pkgs, ... }:
+with lib;
 let
 let
+  cfg = config.services.syncthing.autoRegister;
   localpkgs = import ../. { inherit pkgs; };
   register-script = pkgs.writeShellScript "syncthing-autoregister-script" ''
   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
+    ${localpkgs.syncthing-set-id}/bin/syncthing-set-id ${
+      escapeShellArg cfg.path
+    }
   '';
 in {
   '';
 in {
-  config = {
+  options = {
+    services.syncthing.autoRegister = {
+      enable = mkEnableOption ''
+        Automatically write the local syncthing device id to the nix module
+        file <option>services.syncthing.autoRegister.path</option>.
+      '';
+      path = mkOption {
+        type = types.path;
+        description = ''
+          Path of nix module file to write our syncthing
+          device id into.  It will be written into
+          services.syncthing.devices.<hostname>.id.  This file
+          can be imported as a NixOS module.
+
+          We recommend using a separate file just for this purpose.  I.e.,
+          don't point this at your main NixOS config.  Instead, import this
+          file from your main NixOS config.  (The current nix-configuration
+          read-modify-write implementation only supports attrsets of strings;
+          it does not support functions, which you probably use in your
+          main configuration.)
+        '';
+      };
+    };
+  };
+  config = mkIf cfg.enable {
     systemd.services.syncthing-autoregister = {
       after = [ "syncthing.service" "syncthing-init.service" ];
       wantedBy = [ "multi-user.target" ];
     systemd.services.syncthing-autoregister = {
       after = [ "syncthing.service" "syncthing-init.service" ];
       wantedBy = [ "multi-user.target" ];
@@ -17,6 +45,20 @@ in {
         ExecStart = register-script;
       };
     };
         ExecStart = register-script;
       };
     };
+    systemd.services.syncthing-reregister = {
+      after = [ "syncthing-autoregister.service" ];
+      script = ''
+        ${pkgs.coreutils}/bin/sleep 1
+        ${pkgs.systemd}/bin/systemctl restart syncthing-autoregister
+      '';
+      serviceConfig = {
+        Type = "oneshot";
+      };
+    };
+    systemd.paths.syncthing-reregister = {
+      pathConfig.PathChanged = cfg.path;
+      wantedBy = [ "multi-user.target" ];
+    };
   };
 }
 
   };
 }