From 92c6a14084e1c9413a96c22555063373109a0a3c Mon Sep 17 00:00:00 2001 From: Hilton Chain Date: Fri, 8 Aug 2025 13:35:45 +0800 Subject: [PATCH] services: Add sing-box-service-type. * modules/rosenthal/services/networking.scm (): New data type. (file-object?, sing-box-activation, sing-box-shepherd-service): New procedures. (sing-box-account, sing-box-service-type): New variables. --- modules/rosenthal/services/networking.scm | 79 +++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/modules/rosenthal/services/networking.scm b/modules/rosenthal/services/networking.scm index 65765c6..7aa36a1 100644 --- a/modules/rosenthal/services/networking.scm +++ b/modules/rosenthal/services/networking.scm @@ -15,9 +15,13 @@ #:use-module (gnu services configuration) #:use-module (gnu services dbus) #:use-module (gnu services shepherd) + #:use-module (gnu system shadow) #:export (iwd-configuration iwd-service-type + sing-box-service-type + sing-box-configuration + tailscale-configuration tailscale-service-type)) @@ -377,6 +381,81 @@ list, power save will be disabled.")) (default-value (iwd-configuration)) (description "Run iwd, the iNet wireless daemon."))) + +;;; +;;; sing-box +;;; + +(define (file-object? val) + (or (string? val) + (file-like? val))) + +(define-configuration/no-serialization sing-box-configuration + (sing-box + (file-like sing-box) + "") + (config-file + file-object + "") + (data-directory + (string "/var/lib/sing-box") + "") + ;; Shepherd + (shepherd-provision + (list-of-symbols '(sing-box)) + "") + (shepherd-requirement + (list-of-symbols '(networking)) + "") + (log-file + (string "/var/log/sing-box.log") + "") + (auto-start? + (boolean #t) + "")) + +(define sing-box-account + (list (user-group (name "sing-box") (system? #t)))) + +(define sing-box-activation + (match-record-lambda + (data-directory) + #~(begin + (use-modules (guix build utils)) + (mkdir-p #$data-directory)))) + +(define sing-box-shepherd-service + (match-record-lambda + (sing-box data-directory config-file + shepherd-provision shepherd-requirement log-file auto-start?) + (list (shepherd-service + (provision shepherd-provision) + (requirement `(user-processes ,@shepherd-requirement)) + (start + #~(make-forkexec-constructor + (list #$(file-append sing-box "/bin/sing-box") + "--config" #$config-file + "--directory" #$data-directory + "--disable-color" + "run") + #:log-file #$log-file)) + (stop #~(make-kill-destructor)) + (auto-start? auto-start?))))) + +(define sing-box-service-type + (service-type + (name 'sing-box) + (extensions + (list (service-extension account-service-type + (const sing-box-account)) + (service-extension activation-service-type + sing-box-activation) + (service-extension shepherd-root-service-type + sing-box-shepherd-service) + (service-extension log-rotation-service-type + (compose list sing-box-configuration-log-file)))) + (description ""))) + ;;; ;;; Tailscale