]> git.scottworley.com Git - syncthing-autoregister/blame - modules/syncthing-autoregister.nix
Prefer modulesPath over <nixos/nixos/modules>
[syncthing-autoregister] / modules / syncthing-autoregister.nix
CommitLineData
b00b144b 1{ config, lib, pkgs, ... }:
7fb0951b 2with lib;
b00b144b 3let
7fb0951b 4 cfg = config.services.syncthing.autoRegister;
b00b144b
SW
5 localpkgs = import ../. { inherit pkgs; };
6 register-script = pkgs.writeShellScript "syncthing-autoregister-script" ''
320df276
SW
7 ${localpkgs.syncthing-set-id}/bin/syncthing-set-id ${
8 escapeShellArg cfg.path
9 }
b00b144b
SW
10 '';
11in {
7fb0951b
SW
12 options = {
13 services.syncthing.autoRegister = {
14 enable = mkEnableOption ''
320df276
SW
15 Automatically write the local syncthing device id to the nix module
16 file <option>services.syncthing.autoRegister.path</option>.
7fb0951b 17 '';
320df276
SW
18 path = mkOption {
19 type = types.path;
320df276
SW
20 description = ''
21 Path of nix module file to write our syncthing
22 device id into. It will be written into
22596d75 23 services.syncthing.devices.<hostname>.id. This file
320df276
SW
24 can be imported as a NixOS module.
25
26 We recommend using a separate file just for this purpose. I.e.,
27 don't point this at your main NixOS config. Instead, import this
28 file from your main NixOS config. (The current nix-configuration
29 read-modify-write implementation only supports attrsets of strings;
30 it does not support functions, which you probably use in your
31 main configuration.)
32 '';
33 };
7fb0951b
SW
34 };
35 };
36 config = mkIf cfg.enable {
b00b144b
SW
37 systemd.services.syncthing-autoregister = {
38 after = [ "syncthing.service" "syncthing-init.service" ];
39 wantedBy = [ "multi-user.target" ];
40 environment.NIX_PATH = config.environment.variables.NIX_PATH;
41 serviceConfig = {
42 User = config.services.syncthing.user;
43 RemainAfterExit = true;
44 Type = "oneshot";
45 ExecStart = register-script;
46 };
47 };
4ac49460
SW
48 systemd.services.syncthing-reregister = {
49 after = [ "syncthing-autoregister.service" ];
50 script = ''
51 ${pkgs.coreutils}/bin/sleep 1
52 ${pkgs.systemd}/bin/systemctl restart syncthing-autoregister
53 '';
54 serviceConfig = {
55 Type = "oneshot";
56 };
57 };
58 systemd.paths.syncthing-reregister = {
59 pathConfig.PathChanged = cfg.path;
60 wantedBy = [ "multi-user.target" ];
61 };
b00b144b
SW
62 };
63}
64