~abcdw/rde

ref: 48dd30a50fb4377f7aa9a35bd92d5a97b4166d78 rde/gnu/home-services/mail.scm -rw-r--r-- 12.1 KiB
48dd30a5Xinglu Chen gnu: home-services: mail: Make some fields for L2md optional. 5 months ago
                                                                                
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
(define-module (gnu home-services mail)
  #:use-module (gnu home-services)
  #:use-module (gnu home-services-utils)
  #:use-module (gnu home-services files)
  #:use-module (gnu home-services shepherd)
  #:use-module (gnu packages mail)
  #:use-module (gnu services configuration)

  #:use-module (srfi srfi-1)
  #:use-module (ice-9 match)

  #:use-module (guix packages)
  #:use-module (guix gexp)
  #:use-module (guix diagnostics)
  #:use-module (guix i18n)
  #:use-module ((guix import utils) #:select (flatten))

  #:export (home-isync-service-type
	    home-isync-configuration

            home-notmuch-service-type
	    home-notmuch-configuration
            home-notmuch-extension

            home-l2md-service-type
            home-l2md-configuration
            l2md-repo))

(define (serialize-isync-config field-name val)
  (define (serialize-term term)
    (match term
      ((? symbol? e) (symbol->string e))
      ((? number? e) (format #f "~a" e))
      ((? string? e) (format #f "~s" e))
      (e e)))
  (define (serialize-item entry)
    (match entry
      ((? gexp? e) e)
      ((? list lst)
       #~(string-join '#$(map serialize-term lst)))))

  #~(string-append #$@(interpose (map serialize-item val) "\n" 'suffix)))

(define-configuration/no-serialization home-isync-configuration
  (package
   (package isync)
   "isync package to use.")
  (xdg-flavor?
   (boolean #t)
   "Whether to use the {$XDG_CONFIG_HOME/isync/mbsyncrc} configuration
file or not.  If @code{#t} creates a wrapper for mbsync binary.")
  (config
   (list '())
   "AList of pairs, each pair is a String and String or Gexp."))

(define (add-isync-package config)
  (if (home-isync-configuration-xdg-flavor? config)
      (list
       (home-isync-configuration-package config)
       (wrap-package
        (home-isync-configuration-package config)
        "mbsync"
        #~(system
           (string-join
            (cons
             #$(file-append (home-isync-configuration-package config)
                            "/bin/mbsync")
             (if (or (member "-c" (command-line))
                     (member "--config" (command-line)))
                 (cdr (command-line))
                 (append
                  (list "--config"
                        "${XDG_CONFIG_HOME:-$HOME/.config}/isync/mbsyncrc")
                  (cdr (command-line)))))))))
      (list (home-isync-configuration-package config))))

(define (add-isync-configuration config)
  `((,(if (home-isync-configuration-xdg-flavor? config)
          "config/isync/mbsyncrc"
          "mbsyncrc")
     ,(mixed-text-file
       "mbsyncrc"
       (serialize-isync-config #f (home-isync-configuration-config config))))))

(define (home-isync-extensions cfg extensions)
  (home-isync-configuration
   (inherit cfg)
   (config (append (home-isync-configuration-config cfg) extensions))))

(define home-isync-service-type
  (service-type (name 'home-isync)
                (extensions
                 (list (service-extension
			home-profile-service-type
			add-isync-package)
		       (service-extension
                        home-files-service-type
                        add-isync-configuration)))
		(compose concatenate)
		(extend home-isync-extensions)
                (default-value (home-isync-configuration))
                (description "Install and configure isync.")))

(define (list-of-gexps? lst)
  (and (list? lst) (every gexp? lst)))

(define-configuration/no-serialization home-notmuch-configuration
  (package
   (package notmuch)
   "notmuch package to use.")
  (xdg-flavor?
   (boolean #t)
   "Whether to use the {$XDG_CONFIG_HOME/notmuch/default/config}
configuration file or not.")
  (config
   (ini-config '())
   "AList of pairs, each pair is a String and String or Gexp.")
  (pre-new
   (list-of-gexps '())
   "List of gexp to add in @file{pre-new} hook. Read @code{man
notmuch-hooks} for more information.")
  (post-new
   (list-of-gexps '())
   "List of gexp to add in @file{post-new} hook. Read @code{man
notmuch-hooks} for more information.")
  (post-insert
   (list-of-gexps '())
   "List of gexp to add in @file{post-insert} hook. Read @code{man
notmuch-hooks} for more information."))

(define-configuration/no-serialization home-notmuch-extension
  (config
   (ini-config '())
   "AList of pairs, each pair is a String and String or Gexp.")
  (pre-new
   (list-of-gexps '())
   "List of gexp to add in @file{pre-new} hook. Read @code{man
notmuch-hooks} for more information.")
  (post-new
   (list-of-gexps '())
   "List of gexp to add in @file{post-new} hook. Read @code{man
notmuch-hooks} for more information.")
  (post-insert
   (list-of-gexps '())
   "List of gexp to add in @file{post-insert} hook. Read @code{man
notmuch-hooks} for more information."))

(define (add-notmuch-package config)
  (list (home-notmuch-configuration-package config)))

(define (add-notmuch-configuration config)
  (define (serialize-field key val)
    (let ((val (cond
                ((list? val) (string-join (map maybe-object->string val) ";"))
                (else val))))
      (format #f "~a=~a\n" key val)))

  (define (filter-fields field)
    (filter-configuration-fields home-notmuch-configuration-fields
				 (list field)))

  (define (hook-file hook gexps)
    (list (string-append "config/notmuch/default/hooks/" hook)
          (program-file (string-append "notmuch-" hook) #~(begin #$@gexps))))

  (define (get-hook hook)
    (let* ((field-obj (car (filter-fields (string->symbol hook))))
           (gexps ((configuration-field-getter field-obj) config)))
      (if (not (null? gexps))
          (hook-file hook gexps)
          '())))

  (remove null?
  `(,@(map get-hook '("pre-new" "post-new" "post-insert"))
    (,(if (home-notmuch-configuration-xdg-flavor? config)
          "config/notmuch/default/config"
          "notmuch-config")
     ,(mixed-text-file
       "notmuch-config"
       (generic-serialize-ini-config
        #:serialize-field serialize-field
        #:fields (home-notmuch-configuration-config config)))))))

(define (home-notmuch-extensions cfg extensions)
  (home-notmuch-configuration
   (inherit cfg)
   (config
    (append (home-notmuch-configuration-config cfg)
            (append-map home-notmuch-extension-config extensions)))
   (pre-new
    (append (home-notmuch-configuration-pre-new cfg)
            (append-map home-notmuch-extension-pre-new extensions)))
   (post-new
    (append (home-notmuch-configuration-post-new cfg)
            (append-map home-notmuch-extension-post-new extensions)))
   (post-insert
    (append (home-notmuch-configuration-post-insert cfg)
            (append-map home-notmuch-extension-post-insert extensions)))))

(define home-notmuch-service-type
  (service-type (name 'home-notmuch)
                (extensions
                 (list (service-extension
			home-profile-service-type
			add-notmuch-package)
		       (service-extension
                        home-files-service-type
                        add-notmuch-configuration)))
		(compose identity)
		(extend home-notmuch-extensions)
                (default-value (home-notmuch-configuration))
                (description "Install and configure notmuch.")))



;;;
;;; L2md.
;;;

(define (string-or-list-of-strings? val)
  (or (string? val) (list-of-strings? val)))

(define-maybe/no-serialization string)
(define-maybe/no-serialization string-or-gexp)

(define-configuration/no-serialization l2md-repo
  (name
   (string)
   "The name of the public-inbox repository.")
  (urls
   (string-or-list-of-strings)
   "A list of URLs to fetch the public-inbox repository from.")
  (maildir
   (maybe-string 'disabled)
   "The maildir corresponding to the public-inbox repository.  This is
optional, an external MDA like Procmail can be used instead to filter
the messages, see the @code{pipe} field.")
  (pipe
   (maybe-string-or-gexp 'disabled)
   "A command to pipe the messages to for further filtering.  This is
mutually exclusive with the @code{maildir} field.")
  (initial-import
   (integer 0)
   "The number of messages to import initially, if @code{0}, import all
the messages.")
  (sync-enabled?
   (boolean #t)
   "Whether to sync this repository or not."))

(define list-of-l2md-repos? (list-of l2md-repo?))

(define-configuration/no-serialization home-l2md-configuration
  (package
    (package l2md)
    "The L2md package to use.")
  (autostart?
   (boolean #f)
   "Whether to autostart L2md on login.")
  (period
   (integer 180)
   "The number of seconds between each round of fetching Git
repositories.")
  (maildir
   (maybe-string 'disabled)
   "The maildir to which messages should be delivered.  This can also be
set on a per-list basis using the using the @code{maildir} field in
the @code{<l2md-repo>} record.")
  (pipe
   (maybe-string-or-gexp 'disabled)
   "A command to pipe the messages to for further filtering.  This is
mutually exclusive with the @code{maildir} field.  This can also be
set on a per-list basis using the @code{<l2md-repo>} record.")
  (base
   (string "${XDG_STATE_HOME:-$HOME}/l2md")
   "The directory where L2md stores Git repositories and other
metadata.")
  (repos
   (list-of-l2md-repos '())
   "List of @code{l2md-repo} records, representing the configuration for
a particular public-inbox repository."))

(define (serialize-l2md-configuration config)
  (define (serialize-field field-name val)
    (let ((val (cond
                ((boolean? val) (if val "1" "0"))
                (else (maybe-object->string val)))))
      (if (string= val "")
          '()
          (list "\t" (object->snake-case-string field-name) " = " val "\n"))))

  (define (l2md-repo->alist repos)
    (match repos
      (($ <l2md-repo> _ name urls maildir pipe initial-import sync-enabled?)
       (begin
         `(repo ,name
                (,@(map (lambda (url)
                          `(url . ,url))
                        (maybe-list urls))
                 (maildir . ,maildir)
                 (pipe . ,pipe)
                 (initial-import . ,initial-import)
                 (sync-enabled . ,sync-enabled?)))))))

  (match config
    (($ <home-l2md-configuration> _ package autostart? period maildir pipe base repos)
     (begin
       (generic-serialize-git-ini-config
        #:combine-ini (compose flatten list)
        #:combine-alist append
        #:combine-section-alist cons*
        #:serialize-field serialize-field
        #:fields
        `((general
           ((period . ,period)
            ,@(optional (not (eq? maildir 'disabled)) `((maildir . ,maildir)))
            ,@(optional (not (eq? pipe 'disabled)) `((pipe . ,pipe)))
            (base . ,base)))
          ,@(map l2md-repo->alist repos)))))))

(define (l2md-files-service config)
  `(("l2mdconfig"
     ,(apply mixed-text-file
             "l2md-config"
             (serialize-l2md-configuration config)))))

(define l2md-shepherd-service
  (match-lambda
    (($ <home-l2md-configuration> _ package autostart?)
     (optional autostart?
               (list (shepherd-service
                      (documentation
                       "L2md service for downloading public-inbox archives.")
                      (provision '(l2md))
                      (start #~(make-forkexec-constructor
                                (list #$(file-append package "/bin/l2md"))
                                #:log-file (string-append
                                            (or (getenv "XDG_LOG_HOME")
                                                (string-append
                                                 (getenv "HOME")
                                                 "/.local/var/log"))
                                            "/l2md.log")))
                      (stop #~(make-kill-destructor))))))))

(define (l2md-profile-service config)
  (list (home-l2md-configuration-package config)))

(define home-l2md-service-type
  (service-type (name 'home-l2md)
                (extensions
                 (list (service-extension
                        home-files-service-type
                        l2md-files-service)
                       (service-extension
                        home-shepherd-service-type
                        l2md-shepherd-service)
                       (service-extension
                        home-profile-service-type
                        l2md-profile-service)))
                (description "Install and configure L2md.")))