summaryrefslogtreecommitdiffstats
path: root/hipis/services/radicale.scm
blob: 41980955499696c4b8817ff502abd789fed45cb8 (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
(define-module (hipis services radicale)
  #:use-module (ice-9 match)

  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix packages)
  #:use-module (guix profiles)
  #:use-module (guix gexp)
  #:use-module (guix records)
  #:use-module (guix download)

  #:use-module (gnu packages)
  #:use-module (gnu packages web)
  #:use-module (gnu packages base)
  #:use-module (gnu packages dav)
  #:use-module (gnu packages admin)

  #:use-module (gnu services)
  #:use-module (gnu services web)
  #:use-module (gnu services guix)
  #:use-module (gnu services shepherd)

  #:use-module (gnu system shadow)

  #:export (radicale-service-type
	    radicale-configuration
	    radicale-nginx-server-configuration
	    %radicale-config-file
	    %radicale-nginx-server-configuration))

(define %radicale-config-file
  (plain-file "radicale.conf" "
[auth]
type = htpasswd
htpasswd_filename = /var/lib/radicale/users
htpasswd_encryption = md5

[server]
hosts = localhost:5232
"))


(define %radicale-nginx-server-configuration
  (nginx-server-configuration
   (locations
    (list
     (nginx-location-configuration
      (uri "/radicale/")
      (body '("proxy_pass        http://localhost:5232/;"
              "proxy_set_header  X-Script-Name /radicale;"
              "proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;"
              "proxy_set_header  Host $http_host;"
              "proxy_pass_header Authorization;")))))
   (root "")
   (try-files #nil)
   (index #nil)
   (listen '("80"))
   (ssl-certificate #f)
   (ssl-certificate-key #f)))

(define %radicale-accounts
  (list (user-group
         (name "radicale")
         (system? #t))
        (user-account
         (name "radicale")
         (group "radicale")
         (system? #t)
         (comment "Radicale Daemon")
         (home-directory "/var/empty")
         (shell (file-append shadow "/sbin/nologin")))))

(define-record-type* <radicale-configuration>
  radicale-configuration make-radicale-configuration
  radicale-configuration-configuration?
  (radicale-nginx-server-configuration    radicale-configuration-radicale-nginx-server-configuration
		                          (default %radicale-nginx-server-configuration))
  (package                                radicale-configuration-package
                                          (default radicale))
  (config-file                            radicale-configuration-config-file
                                          (default %radicale-config-file))
  (users                                  radicale-configuration-users
                                          (default '())))

(define (radicale-nginx-service config)
  (list (radicale-configuration-radicale-nginx-server-configuration
	 config)))

(define (radicale-activation config)
  (match-record config <radicale-configuration>
    (users)
    (with-imported-modules '((guix build utils))
      #~(begin
          (use-modules (guix build utils)
		       (ice-9 match))
          (let* ((uid (passwd:uid (getpw "radicale")))
		 (gid (group:gid (getgr "radicale")))
		 (radicale-lib "/var/lib/radicale")
		 (radicale-collections
		  (string-append radicale-lib "/collections"))
		 (radicale-users
		  (string-append radicale-lib "/users")))
            (mkdir-p radicale-collections)
	    (chown radicale-collections uid gid)
            (chown radicale-lib uid gid)
            (chmod radicale-lib #o700)
	    (when (file-exists? radicale-users)
	      (system* #$(file-append coreutils "/bin/rm")
		       radicale-users))
	    (system* #$(file-append coreutils "/bin/touch")
		     radicale-users)
	    (chown radicale-users uid gid)
	    (map (match-lambda
		   ((user . pass)
		    (system* #$(file-append httpd "/bin/htpasswd")
			     "-b" radicale-users user pass))
		   (_ (error "Expected list of cons lists with username and password.")))
		 '#$users))))))


(define (radicale-shepherd-service config)
  (match-record config <radicale-configuration>
    (package config-file)
    (list (shepherd-service
           (provision '(radicale))
           (documentation "Run the radicale daemon.")
           (requirement '(networking))
           (start #~(make-forkexec-constructor
                     (list #$(file-append package "/bin/radicale")
                           "-C" #$config-file)
                     #:user "radicale"
                     #:group "radicale"))
           (stop #~(make-kill-destructor))))))

(define radicale-service-type
  (service-type
   (name 'radicale)
   (extensions
    (list (service-extension nginx-service-type
                             radicale-nginx-service)
	  (service-extension shepherd-root-service-type
			     radicale-shepherd-service)
          (service-extension account-service-type
			     (const %radicale-accounts))
          (service-extension activation-service-type
			     radicale-activation)))
   (default-value (radicale-configuration))
   (description
    "Create static raports for various types of web server using goaccess.")))