blob: fe7143f326a2830c2877ca4fbe18775638e87966 (
plain) (
blame)
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
(use-modules (guix gexp)
(gnu)
(guix)
((guix build utils) #:prefix utils:)
((guix modules) #:prefix modules:)
((srfi srfi-1) #:prefix srfi-1:)
(srfi srfi-26) ; cut utility
((ice-9 textual-ports) #:prefix textual-ports:)
((ice-9 ftw) #:prefix ftw:)
((ice-9 popen) #:prefix popen:)
((ice-9 rdelim) #:prefix rdelim:)
((ice-9 string-fun) #:prefix string-fun:))
(use-package-modules ssh
backup
version-control)
(use-service-modules networking
ssh
mcron
admin
mail
web
certbot
cgit)
;; Missing functions from stdlib
(define* (partial f #:rest args1)
(lambda* (#:rest args2)
(apply f (append args1 args2))))
(define (%every-pred-fn ps x)
(if (null? ps)
#t
(and ((car ps) x)
(%every-pred-fn (cdr ps) x))))
(define (every-pred . ps)
(partial %every-pred-fn ps))
(define (complement f)
(lambda* (#:rest args)
(not (apply f args))))
;; custom extensions
(define (slurp f)
(string-trim-both
(call-with-input-file f textual-ports:get-string-all)))
(define (ignored? path)
(or (equal? "." path)
(equal? ".." path)))
(define (dotfile? path)
(equal? #\. (string-ref path 0)))
(define (list-directory path)
(ftw:scandir path
(every-pred (complement ignored?)
(complement dotfile?))))
(define (hash-double-quote-reader _char port)
"Taken almost verbatim from let-over-lambda."
(let ((chars '()))
(do ((prev (read-char port) curr)
(curr (read-char port) (read-char port)))
((and (equal? #\" prev)
(equal? #\# curr)))
(set! chars (cons prev chars)))
(list->string (reverse! chars))))
(read-hash-extend #\" hash-double-quote-reader)
(define (heredoc-reader _char port)
"Taken almost verbatim from let-over-lambda."
(let ((chars '()))
(do ((curr (read-char port)
(read-char port)))
((equal? #\newline curr))
(set! chars (cons curr chars)))
(let* ((pattern (reverse! chars))
(pointer pattern)
(output '()))
(do ((curr (read-char port)
(when (not (null? pointer))
(read-char port))))
((null? pointer))
(set! output (cons curr output))
(set! pointer
(if (equal? (car pointer) curr)
(cdr pointer)
pattern)))
(list->string
(reverse!
(srfi-1:drop output
(length pattern)))))))
(read-hash-extend #\> heredoc-reader)
(define (read-delimited-list char port)
(let ((sexp-string (string-append
"("
(rdelim:read-delimited (string char) port)
")")))
(call-with-input-string sexp-string read)))
(define (lambda-shorthand-reader _char port)
`(lambda (%)
,(read-delimited-list #\] port)))
(define (cmd . args)
(let* ((port (apply popen:open-pipe* OPEN_READ args))
(out (string-trim-right (rdelim:read-string port))))
(popen:close-pipe port)
out))
(read-hash-extend #\[ lambda-shorthand-reader)
(define (tick-cmd-reader _char port)
(cons 'cmd
(map (lambda (s)
(if (equal? #\$ (string-ref s 0))
(string->symbol (substring s 1))
s))
(string-split (rdelim:read-delimited "´" port)
#\space))))
(read-hash-extend #\´ tick-cmd-reader)
(define-macro (-> x . sexps)
(srfi-1:fold
(lambda (el acc)
(if (list? el)
(append (list (car el)
acc)
(cdr el))
(list el acc)))
x
sexps))
(define-macro (->> x . sexps)
(srfi-1:fold
(lambda (el acc)
(if (list? el)
(append el (list acc))
(list el acc)))
x
sexps))
;; DNS constants
(define user "andreh")
(define tld (slurp "shared/tld.txt"))
(define host-name (slurp "shared/hostname.txt"))
(define mail-domain-prefix "mail")
(define mail-domain (string-append mail-domain-prefix "." tld))
(define git-domain-prefix "git")
(define git-domain (string-append git-domain-prefix "." tld))
;; OS configuration
(define sudoers "\
root ALL=(ALL) ALL
%wheel ALL=NOPASSWD: ALL\n")
(define certbot-alias
"certbot")
(define letsencrypt-prefix
"/etc/letsencrypt/live/")
(define (tls-pub-for domain)
(string-append letsencrypt-prefix domain "/fullchain.pem"))
(define (tls-priv-for domain)
(string-append letsencrypt-prefix domain "/privkey.pem"))
(define opensmtpd-config
(plain-file "euandreh-smtpd.conf" (string-append "
listen on eth0
# File comes from mail-aliases-service-type
table aliases file:/etc/aliases
accept from any domain " mail-domain " alias <aliases> deliver to maildir
accept for local alias <aliases> deliver to maildir
accept for any relay
pki " mail-domain " cert \"" (tls-pub-for mail-domain) "\"
pki " mail-domain " key \"" (tls-priv-for mail-domain) "\"")))
(define tls-prefixes
(list mail-domain-prefix
git-domain-prefix
"chat"))
(define static-projects
'("guile-pds" "boneco"))
(define tls-domains
(cons tld
(append
(map #[string-append % "." tld]
tls-prefixes)
(map (cut string-append <> "." tld) static-projects))))
(define (static-nginx-config domains root)
(nginx-server-configuration
(server-name domains)
(listen '("443 ssl"))
(ssl-certificate (tls-pub-for tld))
(ssl-certificate-key (tls-priv-for tld))
(root root)))
(define static-projects-nginx-config
(map #[static-nginx-config (list (string-append % "." tld))
(string-append "/srv/http/" %)]
static-projects))
(define cgit-with-custom-about-formatting
(package
(inherit cgit)
(arguments
(substitute-keyword-arguments (package-arguments cgit)
((#:phases phases)
`(modify-phases ,phases
(add-after unpack 'patch-about-formatting
(lambda _
(substitute* "lib/cgit/filters/about-formatting.sh"
(("txt2html") "md2html"))))))))))
(define my-system
(operating-system
(timezone "America/Sao_Paulo")
(host-name host-name)
(users (cons* (user-account
(name user)
(group "users")
(home-directory (string-append "/home/" user))
(supplementary-groups '("wheel")))
%base-user-accounts))
(sudoers-file (plain-file "sudoers" sudoers))
(packages
(append (map (compose list specification->package+output symbol->string)
;; required for guix pull
'(nss-certs))
%base-packages))
(services
(append
(list (service openssh-service-type
(openssh-configuration
(openssh openssh-sans-x)
(password-authentication? #false)
(authorized-keys
`((,user ,(local-file "id_rsa.pub"))))))
(simple-service 'automatic-services-restart
activation-service-type
(with-imported-modules '((gnu services herd))
#~(begin
(use-modules (gnu services herd))
(restart-service 'mcron)
(restart-service 'nginx))))
(service unattended-upgrade-service-type)
(service dhcp-client-service-type)
#;
(service opensmtpd-service-type
(opensmtpd-configuration
(config-file opensmtpd-config)))
#;
(service mail-aliases-service-type
`(("postmaster" "root")
("webmaster" "root")
("abuse" "root")
(,certbot-alias "root")))
(service nginx-service-type
(nginx-configuration
(server-blocks
(append (list)
static-projects-nginx-config))))
(service certbot-service-type
(certbot-configuration
(email (string-append certbot-alias "@" tld))
(certificates
(list
(certificate-configuration
(domains tls-domains))))))
(simple-service 'automatic-certbot-renewal
activation-service-type
(with-imported-modules '((gnu services herd))
#~(begin
(use-modules (gnu services herd))
#;
(execl "/var/lib/certbot/renew-certificates")
(restart-service 'nginx))))
(service cgit-service-type
(cgit-configuration
(package cgit-with-custom-about-formatting)
(remove-suffix? #t)
(root-title "EuAndreh's repositories")
(root-desc "Patches welcome!")
(snapshots '("tar.gz" "zip"))
(clone-prefix (list (string-append "https://" git-domain)))
(source-filter (file-append cgit "/lib/cgit/filters/syntax-highlighting.py"))
(about-filter (file-append cgit "/lib/cgit/filters/about-formatting.sh"))
(favicon (file-append cgit "/share/cgit/favicon.ico"))
(enable-commit-graph? #t)
(enable-follow-links? #t)
(enable-index-owner? #f)
(enable-log-filecount? #t)
(enable-log-linecount? #t)
(enable-subject-links? #t)
(enable-html-serving? #t)
(enable-git-config? #t)
(max-repodesc-length 120)
(max-stats "year")
(nocache? #t)
(include
(plain-file "euandreh-cgitrc"
(string-join (map (partial string-append "readme=:README")
'("" ".txt" ".md" ".rst"))
"\n")))
#;
(nginx-server-configuration-list
(list
(nginx-server-configuration
(server-name (list git-domain)))))))
#;
(simple-service 'init-srv-directories
activation-service-type
#~(for-each (lambda (p)
(mkdir-p p)
(chmod p #o755))
'("/srv/http"
"/srv/git"))))
(modify-services
%base-services
(guix-service-type
config => (guix-configuration
(inherit config)
(authorized-keys
(append
(list (local-file "/etc/guix/signing-key.pub"))
%default-authorized-guix-keys)))))))
(bootloader
(bootloader-configuration
(bootloader grub-bootloader)
(target "/dev/vda")
(terminal-outputs '(console))))
(swap-devices
(list (uuid "79a91c82-f3e1-4ed7-8c4e-23569f1ae0ca")))
(file-systems
(cons* (file-system
(mount-point "/")
(device
(uuid "fddb6a4c-8b8c-4f57-b274-5d6d33200f28"
'ext4))
(type "ext4"))
%base-file-systems))))
(define my-machine
(machine
(operating-system my-system)
(environment managed-host-environment-type)
(configuration (machine-ssh-configuration
(host-name tld)
(system "x86_64-linux")
(user user)
(host-key "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOwnnw/u8ub+kcQhnVSyNWarYGH8aesUwIy4SIprufKf")))))
(list my-machine)
|