aboutsummaryrefslogtreecommitdiff
path: root/src/guix/system.scm
blob: 31900f1ce0d06e2b1786a81b857443de7567e057 (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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
(use-modules
  ((ice-9 textual-ports) #:prefix textual-ports:)
  ((srfi srfi-1) #:prefix s1:)
  ((xyz euandreh heredoc) #:prefix heredoc:)
  ((org euandre packages) #:prefix pkg:)
  ((org euandre queue) #:prefix q:)
  (gnu)
  (guix build-system trivial)
  (guix build utils)
  (guix packages)
  (guix records))
(use-package-modules
  admin
  ssh
  version-control)
(use-service-modules
  admin
  certbot
  cgit
  dns
  mail
  mcron
  networking
  security
  shepherd
  ssh
  version-control
  vpn
  web)
(heredoc:enable-syntax)


(define ipv4 "216.238.68.100")
(define ipv6 "2001:19f0:b400:1f0c:5400:04ff:fe35:8c89")
(define tld "euandre.org")
(define +tld+ tld)


(define (path s)
  ;; src/guix/system.scm + ../../../ = ./
  (string-append (dirname (dirname (dirname (current-filename)))) "/" s))

(define +users+
  `(("andreh" "EuAndreh" ("wheel") ,(path "src/keys/SSH/andreh.pub.txt"))))


(define working-dir
  (dirname (dirname (dirname (current-filename)))))


(define (str . rest)
  (apply string-append rest))

(define (fmt . rest)
  (apply format #f rest))

(define (path s)
  (str working-dir "/" s))

(define (slurp s)
  (call-with-input-file
   s
   textual-ports:get-string-all))

(define (file s)
  (slurp (path s)))

(define (script name content)
  (package
    (name name)
    (version "latest")
    (source #f)
    (build-system trivial-build-system)
    (arguments
      (list
       #:modules '((guix build utils))
       #:builder
       #~(begin
           (use-modules (guix build utils))
           (let* ((bin  (string-append %output "/bin"))
                  (prog (string-append bin "/" #$name)))
             (mkdir-p bin)
             (call-with-output-file prog
              (lambda (port)
                (display #$content port)))
             (chmod prog #o755)))))
    (home-page #f)
    (synopsis #f)
    (description #f)
    (license #f)))


(define ns (fmt "ns1.~a." tld))
(define mail (fmt "hostmaster.~a." tld))
(define dkim-selector "dkimproxyout")
(define dkim-public-key-path "/var/lib/dkimproxyout/public.key")

(define dkim-name (str dkim-selector "._domainkey"))
(define dkim-public-key
  (if (file-exists? dkim-public-key-path)
    (string-join (reverse
                  (cdr
                   (reverse
                    (cdr
                     (string-split (slurp dkim-public-key-path)
                                   #\newline)))))
                 "")
    "stub-public-key-for-building"))

(define ipv4-reverse-domain
  (str
   (string-join (reverse
                 (string-split ipv4
                               #\.))
                ".")
   ".in-addr.arpa"))

(define ipv6-reverse-domain
  (str
   (string-join (reverse
                 (map (lambda (s) (fmt "~a" s))
                      (string->list
                       (string-delete #\: ipv6))))
                ".")
   ".ip6.arpa"))

(define-zone-entries tld-zone
  ("@"         ""   "IN"   "NS"    (fmt "ns1.~a." tld))
  ("@"         ""   "IN"   "NS"    (fmt "ns2.~a." tld))
  ("ns1"       ""   "IN"   "A"     ipv4)
  ("ns1"       ""   "IN"   "AAAA"  ipv6)
  ("ns2"       ""   "IN"   "A"     ipv4)
  ("ns2"       ""   "IN"   "AAAA"  ipv6)

  ("@"         ""   "IN"   "A"     ipv4)
  ("@"         ""   "IN"   "AAAA"  ipv6)

  ("@"         ""   "IN"   "CAA"   "0 issue \"letsencrypt.org\"")
  ("@"         ""   "IN"   "CAA"   "0 issuewild \";\"")
  ("@"         ""   "IN"   "CAA"   "0 iodef \"mailto:eu@euandre.org\"")

  ("mta-sts"   ""   "IN"   "A"     ipv4)
  ("mta-sts"   ""   "IN"   "AAAA"  ipv6)
  ("_mta-sts"  ""   "IN"   "TXT"   "\"v=STSv1; id=20230314\"")
  ("@"         ""   "IN"   "MX"    (fmt "10 ~a." tld))
  ("_dmarc"    ""   "IN"   "TXT"   "\"v=DMARC1; p=quarantine\"")
  ("@"         ""   "IN"   "TXT"   (fmt "\"v=spf1 a:~a -all\"" tld))
  (dkim-name   ""   "IN"   "TXT"   (fmt "\"v=DKIM1; k=rsa; t=s; p=~a\"" dkim-public-key)))

(define-zone-entries ipv4-reverse-domain-zone
  ("@"         ""   "IN"   "PTR"   (str tld "."))
  ("@"         ""   "IN"   "NS"    (fmt "ns1.~a." tld))
  ("@"         ""   "IN"   "NS"    (fmt "ns2.~a." tld)))

(define-zone-entries ipv6-reverse-domain-zone
  ("@"         ""   "IN"   "PTR"   (str tld "."))
  ("@"         ""   "IN"   "NS"    (fmt "ns1.~a." tld))
  ("@"         ""   "IN"   "NS"    (fmt "ns2.~a." tld)))

(define zones
  (list
   (knot-zone-configuration
     (domain tld)
     (semantic-checks? #t)
     (zone
       (zone-file
         (origin tld)
         (ns ns)
         (mail mail)
         (entries tld-zone))))
   (knot-zone-configuration
     (domain ipv4-reverse-domain)
     (semantic-checks? #t)
     (zone
       (zone-file
         (origin ipv4-reverse-domain)
         (ns ns)
         (mail mail)
         (entries ipv4-reverse-domain-zone))))
   (knot-zone-configuration
     (domain ipv6-reverse-domain)
     (semantic-checks? #t)
     (zone
       (zone-file
         (origin ipv6-reverse-domain)
         (ns ns)
         (mail mail)
         (entries ipv6-reverse-domain-zone))))))


(define-record-type* <git-configuration>
  git-configuration
  make-git-configuration
  git-configuration?
  (package           git-configuration-package           (default git))
  (user              git-configuration-user              (default "git"))
  (group             git-configuration-group             (default "git"))
  (export-all?       git-configuration-export-all?       (default #f))
  (base-path         git-configuration-base-path         (default "/srv/git"))
  (user-path         git-configuration-user-path         (default #f))
  (run-in-container? git-configuration-run-in-container? (default #f))
  (container-name    git-configuration-container-name    (default "git-contaner")))

(define (git-command config)
  (match-record config <git-configuration>
      (package user group base-path run-in-container? container-name)
    (let ((bin (file-append package "/bin/git")))
      (if (not run-in-container?)
        bin
        (least-authority-wrapper
         bin
         #:user      user
         #:group     group
         #:name      container-name
         #:directory base-path
         #:preserved-environment-variables
         '()
         #:mappings
         (list
          (file-system-mapping
            (source base-path)
            (target source)
            (writable? #t))))))))

(define (git-shepherd-services config)
  (match-record config <git-configuration>
      (user group export-all? base-path user-path)
    (list
     (shepherd-service
       (provision '(git))
       (requirement '(networking))
       (start
         #~(make-forkexec-constructor
            (list #$(git-command config)
                  "daemon" "--syslog" "--reuseaddr"
                  #$@(pkg:mklist (and export-all? "--export-all"))
                  #$@(pkg:mklist (and base-path (string-append "--base-path=" base-path)))
                  #$@(pkg:mklist (and user-path (string-append "--user-path=" user-path))))
            #:user  #$user
            #:group #$group))
       (stop #~(make-kill-destructor SIGKILL))
       (documentation "")))))

(define (git-accounts config)
  (match-record config <git-configuration>
      (user group)
    (list
     (user-group
       (name group)
       (system? #t))
     (user-account
       (name user)
       (group group)
       (system? #t)
       (comment "External SSH Git service user")
       (home-directory "/srv/git")
       (shell
         (file-append git "/bin/git-shell"))))))


(define git-service-type
  (service-type
    (name 'git)
    (extensions
      (list
       (service-extension shepherd-root-service-type
                          git-shepherd-services)
       (service-extension account-service-type
                          git-accounts)
       (service-extension profile-service-type
                          (compose list git-configuration-package))))
    (default-value (git-configuration))
    (description "Better git:// service.")))


(define package-symbols
  '())

(define package-records
  (list))



(operating-system
  (locale "fr_FR.UTF-8")
  (timezone "America/Sao_Paulo")
  (host-name +tld+)
  (skeletons pkg:skeletons)
  (users (append (pkg:user-accounts +users+) %base-user-accounts))
  (packages (pkg:package-set package-symbols package-records))
  (services
    (append
     (list
      (service knot-service-type
        (knot-configuration
          (zones zones)))
      (service ntp-service-type)
      (service dhcp-client-service-type)
      (service fail2ban-service-type)
      (service openssh-service-type (q:openssh-default-configuration (pkg:users->keys +users+)))
      (service certbot-service-type (q:tld-certbot-configuration +tld+))
      (service nginx-service-type
        (nginx-configuration
          (server-blocks
            (list
             (nginx-server-configuration
               (server-name (list tld))
               (listen '("[::]:443 ssl http2" "443 ssl http2"))
               (root "/srv/www")
               (ssl-certificate     (fmt "/etc/letsencrypt/live/~a/fullchain.pem" tld))
               (ssl-certificate-key (fmt "/etc/letsencrypt/live/~a/privkey.pem"   tld))
               (locations
                 (list
                  (nginx-location-configuration
                    (uri "/git/static/")
                    (body
                      (list
                       (list "alias " cgit "/share/cgit/;"))))
                  (nginx-location-configuration
                    (uri "/git/")
                    (body
                      (list
                       (list "fastcgi_param SCRIPT_FILENAME " cgit "/lib/cgit/cgit.cgi;")
                       #"-
                         fastcgi_param PATH_INFO $uri;
                         fastcgi_param QUERY_STRING $args;
                         fastcgi_param HTTP_HOST $server_name;
                         fastcgi_pass localhost:9000;
                         rewrite /git(.*) $1 break;
                         "#)))))
               (raw-content
                 '(#"-
                     ssl_protocols TLSv1.3;
                     ssl_ciphers EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH;
                     ssl_prefer_server_ciphers on;
                     gzip off;  # Disable compression altogether due to BREACH
                     charset utf-8;
                     autoindex on;
                     add_header Strict-Transport-Security 'max-age=31536000; includeSubdomains' always;
                     "#)))))))
      (service cgit-service-type q:cgit-pre-configuration)
      (service pkg:syskeep-service-type)
      (service git-service-type
        (git-configuration
          (export-all? #t)))
      (simple-service 'add-wireguard-aliases hosts-service-type
        (list
         (host "10.0.0.0" "toph")
         (host "10.0.0.1" "velhinho")
         (host "10.0.0.2" "azula")))
      (service wireguard-service-type
        (wireguard-configuration
          (addresses '("10.0.0.0/32"))
          (peers
            (list
             (wireguard-peer
               (name "velhinho")
               (public-key "Mhv8KxB/QXQpNKNtqD57PoFv43TXJ1lg52PJd6TmtwI=")
               (allowed-ips '("10.0.0.1/32"))
               (keep-alive 25))
             (wireguard-peer
               (name "azula")
               (public-key "8IxSFlJoFuTzLtIkoKZH4CkUbIxd6++E0lBOin/7rT8=")
               (allowed-ips '("10.0.0.2/32"))
               (keep-alive 25))))))
      (service q:shadow-group-service-type)
      (service q:dkimproxyout-service-type)
      (service q:cyrus-sasl-service-type)
      (service q:dovecot-service-type)
      (service q:internet-postfix-service-type)
      (service mail-aliases-service-type
        '(("root"         "andreh")
          ("eu"           "andreh")
          ("mailing-list" "andreh"))))
     pkg:base-services))
  (bootloader
    (bootloader-configuration
      (bootloader grub-bootloader)
      (targets '("/dev/vda"))))
  (swap-devices
    (list
     (swap-space
       (target
         (uuid "94b47d91-3542-438a-84a9-859fe347ce09")))))
  (file-systems
    (append
     (list
      (file-system
        (mount-point "/")
        (device
          (uuid "4c36d5ad-f996-413e-a55c-c05b7e1876f2" 'btrfs))
        (type "btrfs"))
      (file-system
        (mount-point "/mnt/production")
        (needed-for-boot? #t)
        (device
          (uuid "b1a7e4a1-a8ea-48a4-ab8b-884a1b6a9c11" 'btrfs))
        (type "btrfs"))
      (file-system
        (mount-point "/mnt/backup")
        (device
          (uuid "6632849d-f180-4740-86e6-a519d43ab75a" 'btrfs))
        (type "btrfs")))
     %base-file-systems)))