(define-module (tw services wireguard) #:use-module (ice-9 regex) #:use-module ((srfi srfi-1) #:select (append-map every)) #:use-module ((srfi srfi-26) #:select (cut)) #:use-module (gnu services) #:use-module (gnu services base) #:use-module (gnu services configuration) #:use-module (gnu services vpn) #:use-module (guix gexp) #:use-module ((guix records) #:select (match-record)) #:use-module ((guix utils) #:select (current-source-directory)) #:use-module (tw services secrets) #:export (%wireguard-peers tw-wireguard-configuration tw-wireguard-service-type)) (define %wireguard-peers `(("lap.twilken.net" . ,(wireguard-peer (name "lap.wg") (public-key "lap/DvCb8xXLUCqcaPEx8kCRcoeV4ScTMVZW5hvvNzA=") (preshared-key "/etc/wireguard/lap.psk") (allowed-ips '("10.0.0.1/32" "fc00::1/128")))) ("lud.twilken.net" . ,(wireguard-peer (name "lud.wg") (endpoint "lud.twilken.net:58921") (public-key "lud/9sbXVdOYXxOkRgAB+b/17QxbwllfJY/pbA3/MkE=") (preshared-key "/etc/wireguard/lud.psk") (allowed-ips '("10.0.0.2/32" "fc00::2/128")))) ("vin.twilken.net" . ,(wireguard-peer (name "vin.wg") (endpoint "vin.twilken.net:58921") (public-key "vin/Im+sOszZFE01UF1+QlyxLP1PsPXJgTz4KmgvL3Y=") (preshared-key "/etc/wireguard/vin.psk") (allowed-ips '("10.0.0.3/32" "fc00::3/128")))) ("fp4.twilken.net" . ,(wireguard-peer (name "fp4.wg") (public-key "fp4/aLAVBADTy+UGmNh011w1CFOOwq70Df6EWlZRkAs=") (preshared-key "/etc/wireguard/fp4.psk") (allowed-ips '("10.0.0.4/32" "fc00::4/128")))) ("pi3.twilken.net" . ,(wireguard-peer (name "pi3.wg") (endpoint "pi3.twilken.net:58922") (public-key "pi3/ThUH4qDTuyvNQIiiyy2dbziF/xLRTwO0+vcUoVY=") (preshared-key "/etc/wireguard/pi3.psk") (allowed-ips '("10.0.0.5/32" "fc00::5/128")))) ("frm.twilken.net" . ,(wireguard-peer (name "frm.wg") (public-key "frm/YGu1BfXUl4jrN0PTFMNdTQXWPSuY1wEpz5W9C2Y=") (preshared-key "/etc/wireguard/frm.psk") (allowed-ips '("10.0.0.6/32" "fc00::6/128")))))) (define (wireguard-peers-list? object) (and (list? object) (every (compose string? car) object) (every (compose wireguard-peer? cdr) object))) (define-configuration/no-serialization tw-wireguard-configuration (this-host (string) "The host name of the machine being configured.") (peers (wireguard-peers-list %wireguard-peers) "An alist of WireGuard peers to install.") (private-key-file (string "/etc/wireguard/private.key") "Where to store this host's private key.")) (define (other-peers this-host peers) (let ((own-peer (assoc-ref peers this-host))) (delq own-peer (map cdr peers)))) (define (tw-wireguard-service config) "Create a full WireGuard config from the personal network CONFIG." (match-record config (this-host peers private-key-file) (match-record (assoc-ref peers this-host) (@@ (gnu services vpn) ) (endpoint allowed-ips) (wireguard-configuration (addresses (map (lambda (cidr) (let ((ipv4 (string-match "/32$" cidr)) (ipv6 (string-match "/128$" cidr))) (cond (ipv4 (regexp-substitute #f ipv4 'pre "/24")) (ipv6 (regexp-substitute #f ipv6 'pre "/64")) (#t cidr)))) allowed-ips)) (port (if endpoint (string->number (cadr (string-split endpoint #\:))) 58921)) (private-key private-key-file) (peers (other-peers this-host peers)))))) (define (cut-string-at-char str char-pred) "Return the first part of STR up to the first occurrence of CHAR-PRED." (substring str 0 (string-index str char-pred))) (define (peer->ips peer) "Extract IP addresses assigned to the given `wireguard-peer' PEER." (map (cut cut-string-at-char <> #\/) (wireguard-peer-allowed-ips peer))) (define (tw-wireguard-hosts config) "Generate a hosts file entries from the personal WireGuard network CONFIG." (define (peer->entries peer) (map (cut host <> (wireguard-peer-name peer)) (peer->ips peer))) (append-map (compose peer->entries cdr) (tw-wireguard-configuration-peers config))) (define (tw-wireguard-secrets config) "Install secrets for the host's private key and preshared keys with peers." (define (local-file-here path) (local-file (canonicalize-path (string-append (current-source-directory) "/" path)))) (match-record config (this-host peers private-key-file) (define short-host (cut-string-at-char this-host #\.)) (define private-key (secret (encrypted-file (local-file-here (string-append "files/wireguard/" short-host ".key.enc"))) (destination private-key-file))) (define (peer->secret peer) (let ((short-peer (cut-string-at-char (wireguard-peer-name peer) #\.))) (secret (encrypted-file (local-file-here (string-append "files/wireguard/" short-host "-" short-peer ".psk.enc"))) (destination (string-append "/etc/wireguard/" short-peer ".psk"))))) (cons private-key (map peer->secret (other-peers this-host peers))))) (define tw-wireguard-service-type (service-type (name 'tw-wireguard) (description "Set up my personal WireGuard network.") (extensions (cons* (service-extension hosts-service-type tw-wireguard-hosts) (service-extension secrets-service-type tw-wireguard-secrets) ;; FIXME: `wireguard-service-type' cannot be extended, so copy its ;; service-extensions directly. (map (lambda (ext) (service-extension (service-extension-target ext) (compose (service-extension-compute ext) tw-wireguard-service))) (service-type-extensions wireguard-service-type))))))