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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
|
;;; genenetwork-machines --- Guix configuration for genenetwork machines
;;; Copyright © 2023–2026 Arun Isaac <arunisaac@systemreboot.net> (guix-forge)
;;; Copyright © 2021 Bonface K. M. <bonfacemunyuki@gmail.com>
;;; Copyright © 2024 Pjotr Prins <pjotr.prins@thebird.nl>
;;;
;;; This file is part of genenetwork-machines.
;;;
;;; genenetwork-machines is free software: you can redistribute it
;;; and/or modify it under the terms of the GNU General Public License
;;; as published by the Free Software Foundation, either version 3 of
;;; the License, or (at your option) any later version.
;;;
;;; genenetwork-machines is distributed in the hope that it will be
;;; useful, but WITHOUT ANY WARRANTY; without even the implied
;;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
;;; See the GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with genenetwork-machines. If not, see
;;; <https://www.gnu.org/licenses/>.
;;; CGIT web services, shared by the cgit system container
;;; (cgit-container.scm) and reusable elsewhere.
;;;
;;; Adapted from guix-forge's (forge cgit), (forge fcgiwrap), (forge
;;; nginx) and (forge anubis). The differences from guix-forge:
;;;
;;; * nginx is the STOCK nginx-service-type from (gnu services
;;; web): cgit-nginx-service-type contributes its sites as plain
;;; nginx-server-configuration records -- the Guix way, no
;;; hand-rolled conf strings. The OS instantiates
;;; nginx-service-type itself, passing the list returned by
;;; (cgit-http-directives CONFIG) as the http-context
;;; extra-content (the client-address recovery and the rate-limit
;;; zones the cgit locations reference).
;;; * fcgiwrap runs without the least-authority wrapper (nested
;;; sandboxes inside a system container are fragile); it still runs
;;; as the nginx user on a unix socket.
;;; * Anubis (AI bot firewall) is optional and disabled by default:
;;; the test container cannot pass challenges, and the package needs
;;; a heavy Go build. Turn on the anubis? field for public
;;; deployments.
(define-module (genenetwork services cgit)
#:export (cgit-web-configuration
cgit-web-service-type
cgit-nginx-service-type
cgit-http-directives
%cgit-rate-limit-http-directives
cgit-demo-repository-service)
#:use-module (gnu)
#:use-module ((forge git) #:select (git-without-safe-directory-check))
#:use-module ((forge anubis) #:select (anubis-ai-firewall))
#:use-module ((genenetwork services anubis)
#:select (%anubis-unix-socket
anubis-configuration
anubis-service-type))
#:use-module ((genenetwork services nginx)
#:select (fastcgi-parameter
fastcgi-parameter->nginx
fastcgi-pass
proxy-header
proxy-header->nginx
proxy-pass
unix-socket-proxy-pass
try-files
nginx-include
limit-request
limit-request-status
limit-request-log-level
limit-request-zone
limit-connection
limit-connection-status
limit-connection-log-level
limit-connection-zone
set-real-ip-from
real-ip-header
real-ip-recursive))
#:use-module ((gnu packages mail) #:select (mailcap))
#:use-module ((gnu packages version-control)
#:select (cgit-pink git))
#:use-module (gnu services base)
#:use-module (gnu services shepherd)
#:use-module (guix gexp)
#:use-module (guix records)
#:use-module (ice-9 match)
#:use-module (srfi srfi-1))
(use-package-modules admin web)
(use-service-modules web)
(define %cgit-readme
(append (list "README.md" "README.markdown" "README.mdown" "README.mkd"
"README.org" "README.rst")
;; man page READMEs
(map (lambda (n)
(string-append "README." (number->string n)))
(iota 9 1))
(list "README.htm" "README.html"
"README.txt"
"README")))
(define-record-type* <cgit-web-configuration>
cgit-web-configuration
make-cgit-web-configuration
cgit-web-configuration?
this-cgit-web-configuration
(cgit cgit-web-configuration-cgit ; package
(default cgit-pink))
;; git without the safe.directory check: the CGI runs as an
;; unprivileged user while the repositories may be owned by root.
(git cgit-web-configuration-git ; package
(default git-without-safe-directory-check))
(port cgit-web-configuration-port ; string
(default "8883"))
(server-name cgit-web-configuration-server-name ; string
(default "cgit"))
(repository-directory cgit-web-configuration-repository-directory ; string
(default "/srv/git"))
(root-title cgit-web-configuration-root-title ; string
(default "cgit"))
(root-desc cgit-web-configuration-root-desc ; string
(default "a fast webinterface for git"))
(readme cgit-web-configuration-readme ; list of strings
(default %cgit-readme))
(snapshots cgit-web-configuration-snapshots ; list of strings
(default (list "tar.gz")))
(mimetype-file cgit-web-configuration-mimetype-file ; file-like or #f
(default (file-append mailcap "/etc/mime.types")))
(repository-sort cgit-web-configuration-repository-sort ; 'age or 'name
(default 'age))
(plain-email? cgit-web-configuration-plain-email? ; boolean
(default #f))
(cache-root cgit-web-configuration-cache-root ; string
(default "/var/cache/cgit"))
(extra-options cgit-web-configuration-extra-options
; list of (KEY . VALUE) pairs; VALUE #f or omitted
; means KEY=1
(default '()))
(fcgiwrap-processes cgit-web-configuration-fcgiwrap-processes ; integer
(default 4))
;; Unix socket the fcgiwrap instance serves the cgit and
;; git-http-backend CGIs on.
(socket cgit-web-configuration-socket ; string
(default "/var/run/fcgiwrap/cgit/socket"))
;; Protect the diff and tree URIs behind the Anubis AI bot firewall
;; (see anubis-service-type below). Off by default.
(anubis? cgit-web-configuration-anubis? ; boolean
(default #f))
;; Addresses (IP or CIDR block) of the reverse proxies in front of
;; this instance, whose client-address header is believed. This is
;; what keeps the rate limiters keyed on $binary_remote_addr
;; PER-CLIENT: without it every request carrying a proxy in front
;; arrives from the proxy's address, so all clients share -- and
;; exhaust -- one budget (the 429 storm this default fixes). The
;; proxy must be configured to send the header, e.g. in the host
;; nginx in front of the container:
;; proxy_set_header X-Real-IP $remote_addr;
;; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
;; Set to '() when the instance is exposed directly: then no client
;; can choose its own bucket by sending a forged header.
(trusted-proxies cgit-web-configuration-trusted-proxies
; list of strings
(default (list "127.0.0.1")))
;; Request header carrying the client address, as set by the
;; trusted proxies above.
(real-ip-header cgit-web-configuration-real-ip-header ; string
(default "X-Forwarded-For"))
;; ADDRESS:PORT of the INTERNAL cgit server (anubis? #t only). It
;; is reachable from localhost alone and carries the cgit CGI; the
;; public listener proxies to it through Anubis. Change it when 8081 is taken
;; on the host, since a `guix system container --network' shares the
;; host network namespace.
(internal-listen cgit-web-configuration-internal-listen ; string
(default "127.0.0.1:8081")))
(define (cgit-about-filter cgit)
;; about-filter for the README formats cgit's own filters support;
;; like guix-forge's, minus the org-mode variant (which would pull
;; in emacs). The filter scripts live inside the cgit package, so
;; the package is closed over at build time.
(program-file "cgit-about-filter"
(with-imported-modules '((guix build utils))
#~(begin
(use-modules (guix build utils) (ice-9 match))
(match (command-line)
((_ about-file)
(cond
((or (string-suffix-ci? ".markdown" about-file)
(string-suffix-ci? ".mdown" about-file)
(string-suffix-ci? ".md" about-file)
(string-suffix-ci? ".mkd" about-file))
(invoke #$(file-append cgit "/lib/cgit/filters/html-converters/md2html")
about-file))
((string-suffix-ci? ".rst" about-file)
(invoke #$(file-append cgit "/lib/cgit/filters/html-converters/rst2html")
about-file))
((or (string-suffix-ci? ".htm" about-file)
(string-suffix-ci? ".html" about-file))
(call-with-input-file about-file
(lambda (port)
(let loop ((line (read-line port 'concat)))
(unless (eof-object? line)
(display line)
(loop (read-line port 'concat)))))))
(else
(invoke #$(file-append cgit "/lib/cgit/filters/html-converters/txt2html")
about-file)))))))))
(define (cgit-rc-file config)
"Return the cgitrc as a file-like object. It is passed to cgit
through the CGIT_CONFIG environment variable (as guix-forge does),
not through /etc/cgitrc."
(match-record config <cgit-web-configuration>
(cgit repository-directory root-title root-desc readme snapshots
mimetype-file repository-sort plain-email? cache-root
extra-options)
(mixed-text-file "cgitrc"
"css=/cgit.css\n"
"logo=/cgit.png\n"
"virtual-root=/\n"
"root-title=" root-title "\n"
"root-desc=" root-desc "\n"
(string-append
(fold-right (lambda (file acc)
(string-append "readme=:" file "\n" acc))
"" readme)
"")
"cache-root=" cache-root "\n"
;; About page rendering for the README formats
;; cgit's own filters support.
"about-filter=" (cgit-about-filter cgit) "\n"
(if mimetype-file
#~(string-append "mimetype-file=" #$mimetype-file "\n")
"")
"snapshots=" (string-join snapshots) "\n"
"repository-sort="
(match repository-sort
('age "age") ('name "name")) "\n"
"noplainemail=" (if plain-email? "1" "0") "\n"
"enable-index-owner=0\n"
(fold-right (lambda (opt acc)
(string-append
(match opt
((key . #f) (string-append key "=1\n"))
((key . value)
(string-append key "=" value "\n"))
(key (string-append key "=1\n")))
acc))
"" extra-options)
"scan-path=" repository-directory "\n")))
(define (cgit-activation config)
(match-record config <cgit-web-configuration>
(socket)
#~(begin
(use-modules (guix build utils))
(mkdir-p #$(cgit-web-configuration-cache-root config))
(mkdir-p #$(cgit-web-configuration-repository-directory config))
(let ((dir (dirname #$socket)))
(mkdir-p dir)
;; The socket is created by fcgiwrap, which runs as the nginx
;; user; give it the directory.
(let ((user (getpw "nginx")))
(chown dir (passwd:uid user) (group:gid user)))))))
(define cgit-web-service-type
(service-type
(name 'cgit-web)
(extensions
(list
(service-extension activation-service-type
cgit-activation)
;; Keep cgit in the system profile (its filters shell out to
;; store binaries by absolute path, but the CGI itself benefits
;; from a stable location).
(service-extension profile-service-type
(lambda (config)
(list (cgit-web-configuration-cgit config))))))
(default-value (cgit-web-configuration))
(description
"Prepare the repository directory and cache for cgit. The cgitrc
is passed to the CGI through CGIT_CONFIG (see cgit-nginx-service-type,
which runs the fcgiwrap instance)." )))
;;; Rate limiting comes in two flavours, both keyed on the client IP
;;; ($binary_remote_addr), so one abusive client cannot spend another's
;;; budget:
;;;
;;; * a GENERAL limit applies to every request: at most %cgit-perip-rate
;;; requests per second per IP (nginx's r/s notation), with a small
;;; burst queue, and at most %cgit-connperip-limit concurrent
;;; connections per IP. Bots that keep one connection open are not
;;; caught by the connection limit, so limit_req is the one that
;;; matters most for them. The burst is sized for a page view that
;;; pulls several assets at once: 5 r/s sustained, but a short spike
;;; of %cgit-perip-burst requests is allowed through without delay.
;;; * the EXPENSIVE cgit endpoints (/diff/, /commit/, /log/ -- see
;;; below) additionally get their own, much stricter limit_req zone:
;;; RATE is expressed in r/m notation; 60r/m means one request per
;;; second per client IP, with a small burst queue. Exceeding it
;;; answers 429 Too Many Requests instead of running the CGI. It is
;;; deliberately generous: a human clicking through log -> commit ->
;;; diff, or opening several commits in tabs, is normal traffic -- an
;;; earlier 5r/m burst 3 rejected that.
;;;
;;; Both reject with 429 and log at warn level, so fail2ban-style
;;; tooling can follow up.
;;;
;;; The /plain/ view (raw file content) is NOT special-cased here: it
;;; goes through Anubis like everything else, and the Anubis policy
;;; allows curl/wget on /<repo>/plain/ without the challenge (see
;;; %anubis-challenge-all-policy in (genenetwork services anubis)).
;;; One gate instead of two: browsers are challenged there and can
;;; solve it, tools are let through, and a spoofed curl user agent from
;;; a browser still gets the challenge because browsers send
;;; Sec-Fetch-*.
;;;
;;; "Keyed on the client IP" only holds up if the client IP is really
;;; there: behind a TLS-terminating reverse proxy every request arrives
;;; from the proxy's address, so the keys collapse into ONE global
;;; budget -- 5 r/s for the whole internet, 20 connections for the
;;; whole internet -- and legitimate users get 429s whenever somebody
;;; else is busy. (Observed live: 9 of 12 requests rejected at a rate
;;; of one request every 2 seconds.) cgit-http-directives therefore
;;; emits set_real_ip_from for every address in the configuration's
;;; trusted-proxies field plus real_ip_header/real_ip_recursive, which
;;; makes $binary_remote_addr the forwarded client address again; the
;;; proxy in front has to send that header (see the trusted-proxies
;;; field). Keep trusted-proxies as narrow as the deployment allows.
(define %cgit-perip-zone "perip")
(define %cgit-perip-rate "5r/s")
(define %cgit-perip-burst 30)
(define %cgit-connperip-zone "connperip")
(define %cgit-connperip-limit 20)
(define %cgit-expensive-rate "60r/m")
(define %cgit-expensive-burst 20)
(define %cgit-expensive-zone "cgit-expensive")
(define (cgit-general-limit-directives)
"The server-level nginx directives applying the general per-client-IP
request and connection limits to every location of a server."
(list
(limit-request %cgit-perip-zone %cgit-perip-burst)
(limit-connection %cgit-connperip-zone %cgit-connperip-limit)))
(define (cgit-expensive-location uri rate burst)
"A rate-limited nginx location for an expensive cgit URI."
(nginx-location-configuration
(uri uri)
(body
(list
(limit-request %cgit-expensive-zone burst)
(limit-request-status 429)
(try-files "$uri" "@cgit")))))
(define (cgit-expensive-locations rate burst)
"The rate-limited nginx locations for the expensive cgit endpoints."
;; NOTE: regex locations are evaluated in order and the FIRST match
;; wins in nginx (they do not cascade), so these are listed before
;; the generic prefix location in the server blocks below.
(list
(cgit-expensive-location "~ ^/[^/]+/diff/" rate burst)
(cgit-expensive-location "~ ^/[^/]+/commit/" rate burst)
(cgit-expensive-location "~ ^/[^/]+/log/" rate burst)))
(define (cgit-http-directives config)
"The http-context nginx directives the cgit sites need: client
address recovery for requests arriving through a trusted proxy, the
rate-limiting zones keyed on the recovered address, and the reject
status/log level. Pass the result as the @code{extra-content} of the
nginx-configuration that instantiates the stock nginx-service-type."
;; Not match-record: the record field real-ip-header would shadow the
;; nginx serializer of the same name from (genenetwork services
;; nginx).
(let ((proxies (cgit-web-configuration-trusted-proxies config))
(header (cgit-web-configuration-real-ip-header config)))
(append
;; Client address recovery, first: limit_req_zone is expanded at
;; configuration load time and the zones key on
;; $binary_remote_addr, which real_ip rewrites per request. The
;; directives are still required in the http context, and their
;; presence here is what makes the limits per-client instead of
;; per-proxy.
(if (null? proxies)
'()
(append (map set-real-ip-from proxies)
(list (real-ip-header header)
(real-ip-recursive))))
;; General per-client-IP request rate and concurrent connections.
(list
(limit-request-zone "$binary_remote_addr"
%cgit-perip-zone "10m" %cgit-perip-rate)
(limit-connection-zone "$binary_remote_addr"
%cgit-connperip-zone "10m")
;; Expensive-endpoint zone (same key, separate, stricter rate).
(limit-request-zone "$binary_remote_addr"
%cgit-expensive-zone "10m" %cgit-expensive-rate)
;; Answer 429 and log at warn so fail2ban can pick rejections up.
(limit-request-status 429)
(limit-connection-status 429)
(limit-request-log-level 'warn)
(limit-connection-log-level 'warn)))))
(define %cgit-rate-limit-http-directives
;; cgit-http-directives for a default <cgit-web-configuration>, i.e.
;; trusting 127.0.0.1 (the host proxy / Anubis) for the client
;; address. Kept for out-of-tree users of the old name; prefer
;; (cgit-http-directives config) so the trusted-proxies and
;; real-ip-header fields are honoured.
(cgit-http-directives (cgit-web-configuration)))
;; FastCGI parameters and the other nginx directives the locations
;; below are built from are typed records and serializer procedures
;; from (genenetwork services nginx), so no directive is built by
;; hand and values are quoted correctly -- including the empty
;; string of the httpoxy mitigation and repository paths with
;; spaces. The parameters every FastCGI program expects
;; (QUERY_STRING, REQUEST_METHOD, CONTENT_TYPE, REMOTE_ADDR, ...)
;; are not listed by hand at all: they come from the standard
;; fastcgi_params file the nginx service installs into /etc/nginx,
;; included the standard way.
(define (cgit-git-http-location config)
"The nginx location handing the git smart HTTP protocol
(clone, fetch, push) to git-http-backend -- direct, not behind
Anubis, so non-browser git clients keep working."
(match-record config <cgit-web-configuration>
(git repository-directory socket)
(nginx-location-configuration
(uri "~ ^/.*/(HEAD|info/refs|git-receive-pack|git-upload-pack).*$")
(body
(append
;; Standard FastCGI parameters, from the file the nginx
;; service installs into /etc/nginx.
(list (nginx-include "fastcgi_params"))
(map fastcgi-parameter->nginx
(list
(fastcgi-parameter
(name "SCRIPT_FILENAME")
(value (file-append git "/libexec/git-core/git-http-backend")))
(fastcgi-parameter
(name "GIT_PROJECT_ROOT") (value repository-directory))
(fastcgi-parameter
(name "GIT_HTTP_EXPORT_ALL") (value "yes"))
(fastcgi-parameter
(name "PATH_INFO") (value "$uri"))
;; Mitigate https://httpoxy.org/ vulnerabilities
(fastcgi-parameter (name "HTTP_PROXY") (value ""))))
(list (fastcgi-pass socket)))))))
(define (cgit-cgi-location config)
"The named nginx location running the cgit CGI through fcgiwrap."
(match-record config <cgit-web-configuration>
(cgit socket)
(nginx-named-location-configuration
(name "cgit")
(body
(append
;; Standard FastCGI parameters, from the file the nginx
;; service installs into /etc/nginx.
(list (nginx-include "fastcgi_params"))
(map fastcgi-parameter->nginx
(list
(fastcgi-parameter
(name "SCRIPT_FILENAME")
(value (file-append cgit "/lib/cgit/cgit.cgi")))
(fastcgi-parameter (name "PATH_INFO") (value "$uri"))
(fastcgi-parameter (name "HTTP_HOST") (value "$server_name"))
;; Mitigate https://httpoxy.org/ vulnerabilities
(fastcgi-parameter (name "HTTP_PROXY") (value ""))))
(list (fastcgi-pass socket)))))))
(define (cgit-nginx-server-blocks config)
"The cgit nginx sites as nginx-server-configuration records, to be
contributed to the stock nginx-service-type. Without Anubis: one
server block with the cgit CGI through fcgiwrap and the
git-http-backend location for the smart HTTP protocol. With
Anubis: the PUBLIC server block proxies to Anubis, which challenges
AI bots and forwards to the INTERNAL server block (localhost only);
the git smart-HTTP endpoints stay directly exposed so clones keep
working. The expensive endpoints are rate limited in both
variants -- with Anubis, inside the internal server, so crawlers
that PASS the challenge are stopped too."
(match-record config <cgit-web-configuration>
(cgit port server-name anubis?)
(let (;; The `root' field holds a file-like; the
;; nginx-configuration renderer splices its store path into
;; the conf (mixed-text-file).
(root (file-append cgit "/share/cgit"))
(git-http (cgit-git-http-location config))
(expensive (cgit-expensive-locations %cgit-expensive-rate
%cgit-expensive-burst))
(cgi (cgit-cgi-location config))
(fallback (nginx-location-configuration
(uri "/")
(body (list (try-files "$uri" "@cgit"))))))
(if anubis?
(list
;; PUBLIC: everything else goes through Anubis. The
;; general per-IP request/connection limits sit here, on
;; the listener that sees the real client address (Anubis
;; itself connects from 127.0.0.1, so limiting the internal
;; server instead would lump every client together).
(nginx-server-configuration
(listen (list port))
(server-name (list server-name))
(raw-content (cgit-general-limit-directives))
(locations
(list git-http
(nginx-location-configuration
(uri "/")
(body
(cons (unix-socket-proxy-pass %anubis-unix-socket)
(map proxy-header->nginx
(list
(proxy-header (name "X-Real-IP")
(value "$remote_addr"))
(proxy-header (name "X-Forwarded-For")
(value "$proxy_add_x_forwarded_for"))
(proxy-header (name "X-Forwarded-Proto")
(value "$scheme"))
(proxy-header (name "Host")
(value "$http_host"))))))))))
;; INTERNAL: the plain cgit server, reachable only via
;; Anubis and from localhost.
(nginx-server-configuration
(listen (list (cgit-web-configuration-internal-listen config)))
(server-name (list server-name))
(root root)
(locations (append expensive (list cgi fallback)))))
(list
(nginx-server-configuration
(listen (list port))
(server-name (list server-name))
(root root)
(raw-content (cgit-general-limit-directives))
(locations (append (list git-http) expensive
(list cgi fallback)))))))))
(define cgit-nginx-activation
;; Only the fcgiwrap log directory: log, run and temp directories
;; for nginx itself are created by the stock nginx-service-type.
#~(begin
(use-modules (guix build utils))
(mkdir-p "/var/log/fcgiwrap")))
(define (cgit-nginx-shepherd-services config)
;; fcgiwrap only: the nginx shepherd service comes from the stock
;; nginx-service-type the OS instantiates. fcgiwrap runs as the
;; nginx user, on a unix socket, with the cgitrc passed through
;; CGIT_CONFIG (as guix-forge does); the stock
;; fcgiwrap-service-type cannot pass environment variables or a
;; worker count, so it stays a small local service.
(match-record config <cgit-web-configuration>
(fcgiwrap-processes socket)
(list
(shepherd-service
(provision '(fcgiwrap))
(documentation "Run fcgiwrap for the cgit CGI.")
(requirement '(user-processes loopback))
(start #~(make-forkexec-constructor
(list #$(file-append fcgiwrap "/sbin/fcgiwrap")
"-s" (string-append "unix:" #$socket)
"-c" (number->string #$fcgiwrap-processes)
"-f")
#:user "nginx" #:group "nginx"
#:environment-variables
(list (string-append "CGIT_CONFIG="
#$(cgit-rc-file config))
;; cgit runs its filters (about-filter, source
;; filter, ...) through /bin/sh with PATH
;; lookups; make sure the system profile's
;; binaries (e.g. python3) are reachable.
"PATH=/run/current-system/profile/bin:/run/current-system/profile/sbin")
#:log-file "/var/log/fcgiwrap/cgit.log"))
(stop #~(make-kill-destructor))))))
(define cgit-nginx-service-type
(service-type
(name 'cgit-nginx)
(extensions
(list
;; Contribute the cgit sites to the OS's nginx: plain
;; nginx-server-configuration records consumed by the stock
;; nginx-service-type (which the OS instantiates, with
;; (cgit-http-directives config) as http extra-content).
(service-extension nginx-service-type cgit-nginx-server-blocks)
(service-extension shepherd-root-service-type
cgit-nginx-shepherd-services)
(service-extension activation-service-type
(const cgit-nginx-activation))
;; Keep fcgiwrap in the system profile (nginx itself is added by
;; the stock nginx-service-type).
(service-extension profile-service-type
(const (list fcgiwrap)))
;; Instantiate Anubis when the anubis? field is set; the
;; extension compute returning an empty list disables it. Its
;; target is the internal cgit listener, so internal-listen moves
;; both ends together (they used to coincide by construction).
(service-extension anubis-service-type
(lambda (config)
(if (cgit-web-configuration-anubis? config)
(list (anubis-configuration
(target (string-append
"http://"
(cgit-web-configuration-internal-listen
config)))))
(list))))))
(default-value (cgit-web-configuration))
(description
"Contribute the cgit sites to the stock nginx-service-type, and
run fcgiwrap (as the nginx user, on a unix socket, with the cgitrc
passed via CGIT_CONFIG) with a git-http-backend location for the
smart HTTP protocol. Optionally protect the web URIs with
Anubis.")))
;;; A demo repository seeded into the repository directory at first
;;; boot, so the web interface has something to show. cgit's
;;; scan-path expects BARE repositories (a non-bare repo is picked up
;;; as repo/.git), so we build a temporary worktree repo and clone it
;;; bare into place. Git identity is passed with -c so no global
;;; config is needed; the seeding is skipped when the repository
;;; already exists.
(define %cgit-demo-readme
(mixed-text-file "README.md"
"# Demo repository\n\n"
"This repository was seeded by the cgit container "
"activation so the web interface has something to "
"show. Hello from the cgit container!\n"))
(define %cgit-demo-readme
(mixed-text-file "README.md"
"# Demo repository\n\n"
"This repository was seeded by the cgit container "
"activation so the web interface has something to "
"show. Hello from the cgit container!\n"))
;; NOTE: this must be a SHEPHERD service, not an activation snippet:
;; during the container's pre-shepherd activation SIGCHLD is ignored,
;; so every child process (git!) is auto-reaped and `system*`/`invoke`
;; die with ECHILD ("No child processes"). A forkexec'd one-shot runs
;; as its own process and reaps its own children.
(define %cgit-seed-script
(program-file
"cgit-seed-repository"
(with-imported-modules '((guix build utils))
#~(begin
(use-modules (guix build utils))
(define git-bin #$(file-append git "/bin/git"))
(define (log text)
(call-with-output-file "/srv/git/seed-log.txt"
(lambda (port) (display text port))))
(mkdir-p "/srv/git")
(log (string-append
"seed: entered\n"
(if (file-exists? "/srv/git/demo/HEAD")
"guard: repo exists, skipping\n"
"guard: repo missing, seeding\n")))
(unless (file-exists? "/srv/git/demo/HEAD")
(let ((seed "/tmp/cgit-seed-demo"))
(mkdir-p seed)
(copy-file #$%cgit-demo-readme
(string-append seed "/README.md"))
(with-directory-excursion seed
(invoke git-bin "init" "-q" "-b" "main" ".")
(invoke git-bin "add" "README.md")
(invoke git-bin
"-c" "user.name=GeneNetwork Demo"
"-c" "user.email=demo@genenetwork.org"
"commit" "-q"
"-m" "Initial demo commit")
(invoke git-bin "clone" "-q" "--bare"
"." "/srv/git/demo"))
(call-with-output-file "/srv/git/demo/description"
(lambda (p)
(display "Demo repository for the cgit container/VM" p)))
(delete-file-recursively "/tmp/cgit-seed-demo")
(log "seed: done\n")))))))
(define cgit-demo-repository-service
(simple-service 'cgit-demo-repository
shepherd-root-service-type
(list (shepherd-service
(provision '(cgit-demo-seed))
(one-shot? #t)
(requirement '(user-processes))
(start #~(make-forkexec-constructor
(list #$%cgit-seed-script)
#:log-file "/var/log/cgit-seed.log"))
(stop #~(make-kill-destructor))))))
|