summaryrefslogtreecommitdiff
path: root/os/srv/syncthing.nix
blob: bcbf6652a7035e9029a7c1f166bcdfff68826633 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
{
  config,
  lib,
  username,
  ...
}:
let
  cfg = config.os.srv.syncthing;
  allFolders = {
    "openmw-config" = {
      path = "/home/${username}/.config/openmw";
      id = "openmw-config";
      devices = [ "oci" ];
      versioning = {
        type = "simple";
        params.keep = "3";
      };
      ignorePatterns = [
        "settings.cfg"
        "*.log"
      ];
    };

    "openmw-mods" = {
      path = "/home/${username}/games/openmw";
      id = "openmw-mods";
      devices = [ "oci" ];
      versioning = {
        type = "trashcan";
        params.cleanoutDays = "7";
      };
    };

    "game-saves" = {
      path = "/home/${username}/.saves";
      id = "game-saves";
      devices = [ "oci" ];
      versioning = {
        type = "staggered";
        params = {
          cleanInterval = "3600";
          maxAge = "2592000";
        };
      };
    };

    "keepass" = {
      path = "/home/${username}/.keepass";
      id = "keepass";
      devices = [
        {
          name = "oci";
          encryptionPasswordFile = config.sops.secrets."syncthing/encryption/keepass".path;
        }
      ];
      versioning = {
        type = "staggered";
        params = {
          cleanInterval = "3600";
          maxAge = "31536000";
        };
      };
    };

    "sync" = {
      path = "/home/${username}/sync";
      id = "sync";
      devices = [
        {
          name = "oci";
          encryptionPasswordFile = config.sops.secrets."syncthing/encryption/sync".path;
        }
      ];
      versioning = {
        type = "staggered";
        params = {
          cleanInterval = "3600";
          maxAge = "15552000";
        };
      };
    };

    "music" = {
      path = "/storage/music";
      id = "music";
      devices = [ "oci" ];
      versioning = {
        type = "trashcan";
        params.cleanoutDays = "14";
      };
    };
  };
  activeFoldersSet = lib.filterAttrs (name: _: builtins.elem name cfg.activeFolders) allFolders;

  syncDirs = lib.mapAttrsToList (_: folder: folder.path) activeFoldersSet;
in
{
  options.os.srv.syncthing = {
    enable = lib.mkEnableOption "enables syncthing syncing";

    activeFolders = lib.mkOption {
      type = lib.types.listOf (
        lib.types.enum [
          "openmw-config"
          "openmw-mods"
          "game-saves"
          "keepass"
          "sync"
          "music"
        ]
      );
      default = [
        "keepass"
        "sync"
      ];
      description = "List of Syncthing folders to enable and sync on this specific machine.";
    };
  };
  config = lib.mkIf cfg.enable {
    systemd.tmpfiles.rules = map (path: "d ${path} 0755 ${username} users -") syncDirs;

    services.syncthing = {
      enable = true;
      user = username;
      dataDir = "/home/${username}/.local/share/syncthing";
      configDir = "/home/${username}/.config/syncthing";
      guiPasswordFile = config.sops.secrets."syncthing/gui_password".path;

      settings = {
        devices."oci".id = "DQXGVDC-KGPM6RK-5NDEBJJ-R7PEWYZ-N6Z3WFZ-TSVJG5X-235SHG4-4BEJNQJ";

        folders = activeFoldersSet;
      };
    };
  };
}