;;; TollGate as a Guix service: its OpenWrt files, run as OpenWrt would. (define-module (tollgate-services) #:use-module (gnu services) #:use-module (guix gexp) #:use-module (guix records) #:use-module ((gnu packages web) #:select (jq)) #:use-module (tollgate) #:use-module (openwrt-services) #:export (tollgate-configuration tollgate-configuration? tollgate-service-type)) ;; What TollGate's package does on OpenWrt, mapped onto openwrt-services: ;; - its procd init script runs as an openwrt-init-script (needs ;; nodogsplash, which TollGate's setup enables); ;; - its uci-defaults scripts (portal symlink, 99-tollgate-setup: uhttpd on ;; :8080, the private network and SSIDs, firewall rules, nodogsplash) run ;; once, as on first boot -- services come up with the result, which is ;; what its postinst's restarts achieve; ;; - /etc/tollgate (config, ecash wallet, portal site) is mutable state, ;; seeded once; /etc/config/firewall-tollgate is seeded like other UCI ;; configs; its iface hotplug script restarts it once the WAN is up. (define-record-type* tollgate-configuration make-tollgate-configuration tollgate-configuration? (tollgate tollgate-configuration-tollgate (default tollgate-wrt))) (define (tollgate-file config file) (file-append (tollgate-configuration-tollgate config) "/share/tollgate-wrt/" file)) (define (tollgate-activation config) #~(begin (use-modules (guix build utils)) (mkdir-p "/etc/tollgate/ecash") (unless (file-exists? "/etc/tollgate/tollgate-captive-portal-site") (copy-recursively #$(tollgate-file config "tollgate-captive-portal-site") "/etc/tollgate/tollgate-captive-portal-site") (for-each (lambda (f) (chmod f #o644)) (find-files "/etc/tollgate/tollgate-captive-portal-site"))))) (define tollgate-service-type (service-type (name 'tollgate) (extensions (list (service-extension openwrt-init-script-service-type (lambda (config) (list (openwrt-init-script (name "tollgate-wrt") (script (tollgate-file config "etc/init.d/tollgate-wrt")) (requirement '(ubus networking nodogsplash)) (start-priority 95) (environment '("TOLLGATE_DEBUG=1")))))) (service-extension uci-service-type (lambda (config) (uci-extension (seeds `(("firewall-tollgate" ,(tollgate-file config "etc/config/firewall-tollgate")))) (defaults `(("90-tollgate-captive-portal-symlink" ,(tollgate-file config "etc/uci-defaults/90-tollgate-captive-portal-symlink")))) ;; It configures the radios' APs, so it ;; must run once /etc/config/wireless ;; exists. (late-defaults `(("99-tollgate-setup" ,(tollgate-file config "etc/uci-defaults/99-tollgate-setup"))))))) (service-extension activation-service-type tollgate-activation) (service-extension etc-service-type (lambda (config) `(("hotplug.d/iface/95-tollgate-restart" ,(tollgate-file config "etc/hotplug.d/iface/95-tollgate-restart"))))) ;; tollgate, tollgate-wrt and its helper scripts; jq for them. (service-extension profile-service-type (lambda (config) (list (tollgate-configuration-tollgate config) jq))) ;; It needs the captive portal running. (service-extension nodogsplash-service-type (const #t)))) (default-value (tollgate-configuration)) (description "Run TollGate, which sells network access paid with Cashu ecash and announced over Nostr, through the nodogsplash captive portal.")))