;;; genenetwork-machines --- Guix configuration for genenetwork machines ;;; Copyright © 2023–2026 Arun Isaac (guix-forge) ;;; Copyright © 2021 Bonface K. M. ;;; Copyright © 2024 Pjotr Prins ;;; ;;; 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 ;;; . ;;; 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 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 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 (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 //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 , 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 (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 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 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 (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))))))