fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
6.2 KiB204 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5# Documentation was partially copied from the muchsync manual.
6# See http://www.muchsync.org/muchsync.html
7
8let
9  cfg = config.services.muchsync;
10  syncOptions = {
11    options = {
12      frequency = mkOption {
13        type = types.str;
14        default = "*:0/5";
15        description = ''
16          How often to run {command}`muchsync`. This
17          value is passed to the systemd timer configuration as the
18          `OnCalendar` option. See
19          {manpage}`systemd.time(7)`
20          for more information about the format.
21        '';
22      };
23
24      sshCommand = mkOption {
25        type = types.str;
26        default = "${pkgs.openssh}/bin/ssh -CTaxq";
27        defaultText = "ssh -CTaxq";
28        description = ''
29          Specifies a command line to pass to {command}`/bin/sh`
30          to execute a command on another machine.
31
32          Note that because this string is passed to the shell,
33          special characters including spaces may need to be escaped.
34        '';
35      };
36
37      upload = mkOption {
38        type = types.bool;
39        default = true;
40        description = ''
41          Whether to propagate local changes to the remote.
42        '';
43      };
44
45      local = {
46        checkForModifiedFiles = mkOption {
47          type = types.bool;
48          default = false;
49          description = ''
50            Check for locally modified files.
51            Without this option, muchsync assumes that files in a maildir are
52            never edited.
53
54            {option}`checkForModifiedFiles` disables certain
55            optimizations so as to make muchsync at least check the timestamp on
56            every file, which will detect modified files at the cost of a longer
57            startup time.
58
59            This option is useful if your software regularly modifies the
60            contents of mail files (e.g., because you are running offlineimap
61            with "synclabels = yes").
62          '';
63        };
64
65        importNew = mkOption {
66          type = types.bool;
67          default = true;
68          description = ''
69            Whether to begin the synchronisation by running
70            {command}`notmuch new` locally.
71          '';
72        };
73      };
74
75      remote = {
76        host = mkOption {
77          type = types.str;
78          description = ''
79            Remote SSH host to synchronize with.
80          '';
81        };
82
83        muchsyncPath = mkOption {
84          type = types.str;
85          default = "";
86          defaultText = "$PATH/muchsync";
87          description = ''
88            Specifies the path to muchsync on the server.
89            Ordinarily, muchsync should be in the default PATH on the server
90            so this option is not required.
91            However, this option is useful if you have to install muchsync in
92            a non-standard place or wish to test development versions of the
93            code.
94          '';
95        };
96
97        checkForModifiedFiles = mkOption {
98          type = types.bool;
99          default = false;
100          description = ''
101            Check for modified files on the remote side.
102            Without this option, muchsync assumes that files in a maildir are
103            never edited.
104
105            {option}`checkForModifiedFiles` disables certain
106            optimizations so as to make muchsync at least check the timestamp on
107            every file, which will detect modified files at the cost of a longer
108            startup time.
109
110            This option is useful if your software regularly modifies the
111            contents of mail files (e.g., because you are running offlineimap
112            with "synclabels = yes").
113          '';
114        };
115
116        importNew = mkOption {
117          type = types.bool;
118          default = true;
119          description = ''
120            Whether to begin the synchronisation by running
121            {command}`notmuch new` on the remote side.
122          '';
123        };
124      };
125    };
126  };
127
128in {
129  meta.maintainers = with maintainers; [ pacien ];
130
131  options.services.muchsync = {
132    remotes = mkOption {
133      type = with types; attrsOf (submodule syncOptions);
134      default = { };
135      example = literalExpression ''
136        {
137          server = {
138            frequency = "*:0/10";
139            remote.host = "server.tld";
140          };
141        }
142      '';
143      description = ''
144        Muchsync remotes to synchronise with.
145      '';
146    };
147  };
148
149  config = let
150    mapRemotes = gen:
151      with attrsets;
152      mapAttrs'
153      (name: remoteCfg: nameValuePair "muchsync-${name}" (gen name remoteCfg))
154      cfg.remotes;
155  in mkIf (cfg.remotes != { }) {
156    assertions = [
157      (hm.assertions.assertPlatform "services.muchsync" pkgs platforms.linux)
158
159      {
160        assertion = config.programs.notmuch.enable;
161        message = ''
162          The muchsync module requires 'programs.notmuch.enable = true'.
163        '';
164      }
165    ];
166
167    systemd.user.services = mapRemotes (name: remoteCfg: {
168      Unit = { Description = "muchsync sync service (${name})"; };
169      Service = {
170        CPUSchedulingPolicy = "idle";
171        IOSchedulingClass = "idle";
172        Environment = [
173          ''"PATH=${pkgs.notmuch}/bin"''
174          ''"NOTMUCH_CONFIG=${config.home.sessionVariables.NOTMUCH_CONFIG}"''
175          ''"NMBGIT=${config.home.sessionVariables.NMBGIT}"''
176        ];
177        ExecStart = concatStringsSep " " ([ "${pkgs.muchsync}/bin/muchsync" ]
178          ++ [ "-s ${escapeShellArg remoteCfg.sshCommand}" ]
179          ++ optional (!remoteCfg.upload) "--noup"
180
181          # local configuration
182          ++ optional remoteCfg.local.checkForModifiedFiles "-F"
183          ++ optional (!remoteCfg.local.importNew) "--nonew"
184
185          # remote configuration
186          ++ [ (escapeShellArg remoteCfg.remote.host) ]
187          ++ optional (remoteCfg.remote.muchsyncPath != "")
188          "-r ${escapeShellArg remoteCfg.remote.muchsyncPath}"
189          ++ optional remoteCfg.remote.checkForModifiedFiles "-F"
190          ++ optional (!remoteCfg.remote.importNew) "--nonew");
191      };
192    });
193
194    systemd.user.timers = mapRemotes (name: remoteCfg: {
195      Unit = { Description = "muchsync periodic sync (${name})"; };
196      Timer = {
197        Unit = "muchsync-${name}.service";
198        OnCalendar = remoteCfg.frequency;
199        Persistent = true;
200      };
201      Install = { WantedBy = [ "timers.target" ]; };
202    });
203  };
204}