aboutsummaryrefslogtreecommitdiff
path: root/modules/services/muchsync.nix
blob: b7004418d35ed844c649c345cab57a758728b027 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
{ config, lib, pkgs, ... }:

with lib;

# Documentation was partially copied from the muchsync manual.
# See http://www.muchsync.org/muchsync.html

let
  cfg = config.services.muchsync;
  syncOptions = {
    options = {
      frequency = mkOption {
        type = types.str;
        default = "*:0/5";
        description = ''
          How often to run <command>muchsync</command>. This
          value is passed to the systemd timer configuration as the
          <literal>OnCalendar</literal> option. See
          <citerefentry>
            <refentrytitle>systemd.time</refentrytitle>
            <manvolnum>7</manvolnum>
          </citerefentry>
          for more information about the format.
        '';
      };

      sshCommand = mkOption {
        type = types.str;
        default = "${pkgs.openssh}/bin/ssh -CTaxq";
        defaultText = "ssh -CTaxq";
        description = ''
          Specifies a command line to pass to <command>/bin/sh</command>
          to execute a command on another machine.
          </para><para>
          Note that because this string is passed to the shell,
          special characters including spaces may need to be escaped.
        '';
      };

      upload = mkOption {
        type = types.bool;
        default = true;
        description = ''
          Whether to propagate local changes to the remote.
        '';
      };

      local = {
        checkForModifiedFiles = mkOption {
          type = types.bool;
          default = false;
          description = ''
            Check for locally modified files.
            Without this option, muchsync assumes that files in a maildir are
            never edited.
            </para><para>
            <option>checkForModifiedFiles</option> disables certain
            optimizations so as to make muchsync at least check the timestamp on
            every file, which will detect modified files at the cost of a longer
            startup time.
            </para><para>
            This option is useful if your software regularly modifies the
            contents of mail files (e.g., because you are running offlineimap
            with "synclabels = yes").
          '';
        };

        importNew = mkOption {
          type = types.bool;
          default = true;
          description = ''
            Whether to begin the synchronisation by running
            <command>notmuch new</command> locally.
          '';
        };
      };

      remote = {
        host = mkOption {
          type = types.str;
          description = ''
            Remote SSH host to synchronize with.
          '';
        };

        muchsyncPath = mkOption {
          type = types.str;
          default = "";
          defaultText = "$PATH/muchsync";
          description = ''
            Specifies the path to muchsync on the server.
            Ordinarily, muchsync should be in the default PATH on the server
            so this option is not required.
            However, this option is useful if you have to install muchsync in
            a non-standard place or wish to test development versions of the
            code.
          '';
        };

        checkForModifiedFiles = mkOption {
          type = types.bool;
          default = false;
          description = ''
            Check for modified files on the remote side.
            Without this option, muchsync assumes that files in a maildir are
            never edited.
            </para><para>
            <option>checkForModifiedFiles</option> disables certain
            optimizations so as to make muchsync at least check the timestamp on
            every file, which will detect modified files at the cost of a longer
            startup time.
            </para><para>
            This option is useful if your software regularly modifies the
            contents of mail files (e.g., because you are running offlineimap
            with "synclabels = yes").
          '';
        };

        importNew = mkOption {
          type = types.bool;
          default = true;
          description = ''
            Whether to begin the synchronisation by running
            <command>notmuch new</command> on the remote side.
          '';
        };
      };
    };
  };

in {
  meta.maintainers = with maintainers; [ pacien ];

  options.services.muchsync = {
    remotes = mkOption {
      type = with types; attrsOf (submodule syncOptions);
      default = { };
      example = literalExample ''
        {
          server = {
            frequency = "*:0/10";
            remote.host = "server.tld";
          };
        }
      '';
      description = ''
        Muchsync remotes to synchronise with.
      '';
    };
  };

  config = let
    mapRemotes = gen:
      with attrsets;
      mapAttrs'
      (name: remoteCfg: nameValuePair "muchsync-${name}" (gen name remoteCfg))
      cfg.remotes;
  in mkIf (cfg.remotes != { }) {
    assertions = [{
      assertion = config.programs.notmuch.enable;
      message = ''
        The muchsync module requires 'programs.notmuch.enable = true'.
      '';
    }];

    systemd.user.services = mapRemotes (name: remoteCfg: {
      Unit = { Description = "muchsync sync service (${name})"; };
      Service = {
        CPUSchedulingPolicy = "idle";
        IOSchedulingClass = "idle";
        Environment = [
          ''"PATH=${pkgs.notmuch}/bin"''
          ''"NOTMUCH_CONFIG=${config.home.sessionVariables.NOTMUCH_CONFIG}"''
          ''"NMBGIT=${config.home.sessionVariables.NMBGIT}"''
        ];
        ExecStart = concatStringsSep " " ([ "${pkgs.muchsync}/bin/muchsync" ]
          ++ [ "-s ${escapeShellArg remoteCfg.sshCommand}" ]
          ++ optional (!remoteCfg.upload) "--noup"

          # local configuration
          ++ optional remoteCfg.local.checkForModifiedFiles "-F"
          ++ optional (!remoteCfg.local.importNew) "--nonew"

          # remote configuration
          ++ [ (escapeShellArg remoteCfg.remote.host) ]
          ++ optional (remoteCfg.remote.muchsyncPath != "")
          "-r ${escapeShellArg remoteCfg.remote.muchsyncPath}"
          ++ optional remoteCfg.remote.checkForModifiedFiles "-F"
          ++ optional (!remoteCfg.remote.importNew) "--nonew");
      };
    });

    systemd.user.timers = mapRemotes (name: remoteCfg: {
      Unit = { Description = "muchsync periodic sync (${name})"; };
      Timer = {
        Unit = "muchsync-${name}.service";
        OnCalendar = remoteCfg.frequency;
        Persistent = true;
      };
      Install = { WantedBy = [ "timers.target" ]; };
    });
  };
}