From 05f69100c34039b1cccaefbe3d0698c59493138c Mon Sep 17 00:00:00 2001 From: Hilton Chain Date: Sat, 30 May 2026 19:03:32 +0800 Subject: [PATCH] utils: Add file-object-or-file-config?. --- modules/rosenthal/services/child-error.scm | 7 ++- modules/rosenthal/services/games.scm | 9 ++- modules/rosenthal/services/mail.scm | 8 ++- modules/rosenthal/services/messaging.scm | 72 ++++++++++++---------- modules/rosenthal/services/monitoring.scm | 28 ++++++--- modules/rosenthal/services/web.scm | 45 +++++++++----- modules/rosenthal/utils/predicates.scm | 13 +++- 7 files changed, 120 insertions(+), 62 deletions(-) diff --git a/modules/rosenthal/services/child-error.scm b/modules/rosenthal/services/child-error.scm index 3e03cd6..457ecda 100644 --- a/modules/rosenthal/services/child-error.scm +++ b/modules/rosenthal/services/child-error.scm @@ -274,14 +274,17 @@ headers. This can expose sensitive information in your logs.") (file-like wakapi-bin) "The wakapi package.") (config - gexp + file-object-or-file-config "Association list of Wakapi configurations.") (no-serialization)) (define home-wakapi-shepherd-service (match-record-lambda (wakapi config) - (let ((config-file (yaml-file "wakapi.yaml" config))) + (let ((config-file + (if (file-config? config) + (yaml-file "wakapi.yaml" config) + config))) (list (shepherd-service (documentation "Run wakapi.") (provision '(wakapi)) diff --git a/modules/rosenthal/services/games.scm b/modules/rosenthal/services/games.scm index 157c6b4..74dc85c 100644 --- a/modules/rosenthal/services/games.scm +++ b/modules/rosenthal/services/games.scm @@ -7,6 +7,7 @@ #:use-module (guix packages) #:use-module (guix records) #:use-module (rosenthal utils file) + #:use-module (rosenthal utils predicates) ;; Guix System #:use-module (gnu system accounts) #:use-module (gnu system shadow) @@ -19,14 +20,14 @@ #:export (gamemode-service-type gamemode-configuration)) -(define-maybe gexp) +(define-maybe file-object-or-file-config) (define-configuration/no-serialization gamemode-configuration (gamemode (file-like gamemode) "@code{gamemode} package to use.") (config - maybe-gexp + maybe-file-object-or-file-config "")) (define (gamemode-account-service config) @@ -39,7 +40,9 @@ (gamemode config) `(("gamemode.ini" ,(if (maybe-value-set? config) - (ini-file "gamemode.ini" config) + (if (file-config? config) + (ini-file "gamemode.ini" config) + config) (file-append (package-source gamemode) "/example/gamemode.ini"))) ("security/limits.d/10-gamemode.conf" ,(file-append gamemode "/etc/security/limits.d/10-gamemode.conf"))))) diff --git a/modules/rosenthal/services/mail.scm b/modules/rosenthal/services/mail.scm index 4805769..0a511f6 100644 --- a/modules/rosenthal/services/mail.scm +++ b/modules/rosenthal/services/mail.scm @@ -8,6 +8,7 @@ #:use-module (guix gexp) #:use-module (guix records) #:use-module (rosenthal utils file) + #:use-module (rosenthal utils predicates) ;; Guix System - services #:use-module (gnu services) #:use-module (gnu services admin) @@ -91,7 +92,7 @@ (file-like goimapnotify) "") (config - gexp + file-object-or-file-config "") (wait (integer 1) @@ -109,7 +110,10 @@ (define home-goimapnotify-shepherd (match-record-lambda (goimapnotify config wait shepherd-provision shepherd-requirement auto-start?) - (let ((config-file (yaml-file "goimapnotify.yaml" config))) + (let ((config-file + (if (file-config? config) + (yaml-file "goimapnotify.yaml" config) + config))) (list (shepherd-service (provision shepherd-provision) (requirement shepherd-requirement) diff --git a/modules/rosenthal/services/messaging.scm b/modules/rosenthal/services/messaging.scm index 8ff2823..487fea6 100644 --- a/modules/rosenthal/services/messaging.scm +++ b/modules/rosenthal/services/messaging.scm @@ -34,7 +34,7 @@ (string "http://localhost:8008") "") (config - file-like + file-object-or-file-config "") (group-id (user-and-group-id #f) @@ -69,22 +69,26 @@ (define heisenbridge-shepherd (match-record-lambda (heisenbridge config homeserver shepherd-provision shepherd-requirement auto-start?) - (list (shepherd-service - (provision shepherd-provision) - (requirement `(user-processes networking ,@shepherd-requirement)) - (start - #~(make-forkexec-constructor - (list #$(file-append heisenbridge "/bin/heisenbridge") - "--config" #$config #$homeserver) - #:user "heisenbridge" - #:group "heisenbridge" - #:environment-variables - '("PATH=/run/current-system/profile/bin" - "SSL_CERT_DIR=/run/current-system/profile/etc/ssl/certs" - "SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt"))) - (stop #~(make-kill-destructor)) - (auto-start? auto-start?) - (actions (list (shepherd-configuration-action config))))))) + (let ((config-file + (if (file-config? config) + (yaml-file "heisenbridge.yaml" config) + config))) + (list (shepherd-service + (provision shepherd-provision) + (requirement `(user-processes networking ,@shepherd-requirement)) + (start + #~(make-forkexec-constructor + (list #$(file-append heisenbridge "/bin/heisenbridge") + "--config" #$config-file #$homeserver) + #:user "heisenbridge" + #:group "heisenbridge" + #:environment-variables + '("PATH=/run/current-system/profile/bin" + "SSL_CERT_DIR=/run/current-system/profile/etc/ssl/certs" + "SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt"))) + (stop #~(make-kill-destructor)) + (auto-start? auto-start?) + (actions (list (shepherd-configuration-action config-file)))))))) (define heisenbridge-service-type (service-type @@ -106,7 +110,7 @@ (file-like mautrix-telegram) "") (config - file-like + file-object-or-file-config "") (group-id (user-and-group-id #f) @@ -153,20 +157,24 @@ (define mautrix-telegram-shepherd (match-record-lambda (mautrix-telegram config shepherd-provision shepherd-requirement auto-start?) - (list (shepherd-service - (provision shepherd-provision) - (requirement - `(user-processes networking postgresql ,@shepherd-requirement)) - (start - #~(make-forkexec-constructor - (list #$(file-append mautrix-telegram "/bin/mautrix-telegram") - "--no-update" "--config" #$config) - #:user "mautrix-telegram" - #:group "mautrix-telegram" - #:directory "/var/lib/mautrix-telegram")) - (stop #~(make-kill-destructor)) - (auto-start? auto-start?) - (actions (list (shepherd-configuration-action config))))))) + (let ((config-file + (if (file-config? config) + (yaml-file "mautrix-telegram.yaml" config) + config))) + (list (shepherd-service + (provision shepherd-provision) + (requirement + `(user-processes networking postgresql ,@shepherd-requirement)) + (start + #~(make-forkexec-constructor + (list #$(file-append mautrix-telegram "/bin/mautrix-telegram") + "--no-update" "--config" #$config-file) + #:user "mautrix-telegram" + #:group "mautrix-telegram" + #:directory "/var/lib/mautrix-telegram")) + (stop #~(make-kill-destructor)) + (auto-start? auto-start?) + (actions (list (shepherd-configuration-action config-file)))))))) (define mautrix-telegram-service-type (service-type diff --git a/modules/rosenthal/services/monitoring.scm b/modules/rosenthal/services/monitoring.scm index 281289a..3296771 100644 --- a/modules/rosenthal/services/monitoring.scm +++ b/modules/rosenthal/services/monitoring.scm @@ -98,7 +98,7 @@ (file-like grafana-bin) "") (config - gexp + file-object-or-file-config "") (postgresql-password-file string @@ -157,7 +157,10 @@ (define grafana-shepherd (match-record-lambda (grafana config shepherd-provision shepherd-requirement auto-start?) - (let ((config-file (ini-file "grafana.ini" config))) + (let ((config-file + (if (file-config? config) + (ini-file "grafana.ini" config) + config))) (list (shepherd-service (provision shepherd-provision) (requirement `(loopback postgresql user-processes @@ -197,7 +200,7 @@ (file-like loki-bin) "") (config - gexp + file-object-or-file-config "") (group-id (user-and-group-id #f) @@ -244,7 +247,10 @@ (define loki-shepherd (match-record-lambda (loki config shepherd-provision shepherd-requirement auto-start?) - (let ((config-file (yaml-file "loki.yaml" config))) + (let ((config-file + (if (file-config? config) + (yaml-file "loki.yaml" config) + config))) (list (shepherd-service (provision shepherd-provision) (requirement `(loopback user-processes ,@shepherd-requirement)) @@ -281,7 +287,7 @@ (file-like mimir-bin) "") (config - gexp + file-object-or-file-config "") (group-id (user-and-group-id #f) @@ -328,7 +334,10 @@ (define mimir-shepherd (match-record-lambda (mimir config shepherd-provision shepherd-requirement auto-start?) - (let ((config-file (yaml-file "mimir.yaml" config))) + (let ((config-file + (if (file-config? config) + (yaml-file "mimir.yaml" config) + config))) (list (shepherd-service (provision shepherd-provision) (requirement `(loopback user-processes ,@shepherd-requirement)) @@ -368,7 +377,7 @@ (string "0.0.0.0:9090") "") (config - gexp + file-object-or-file-config "") (group-id (user-and-group-id #f) @@ -415,7 +424,10 @@ (define prometheus-shepherd (match-record-lambda (prometheus listen-address config shepherd-provision shepherd-requirement auto-start?) - (let ((config-file (yaml-file "prometheus.yml" config))) + (let ((config-file + (if (file-config? config) + (yaml-file "prometheus.yml" config) + config))) (list (shepherd-service (provision shepherd-provision) (requirement `(loopback user-processes ,@shepherd-requirement)) diff --git a/modules/rosenthal/services/web.scm b/modules/rosenthal/services/web.scm index 660c21c..4fa39b9 100644 --- a/modules/rosenthal/services/web.scm +++ b/modules/rosenthal/services/web.scm @@ -178,7 +178,7 @@ (list-of-file-likes (list git git-lfs)) "@code{git} and extension packages to install.") (config - gexp + file-object-or-file-config "") (postgresql-password-file string @@ -228,7 +228,10 @@ (define forgejo-shepherd-service (match-record-lambda (forgejo config) - (let ((config-file (ini-file "forgejo.ini" config))) + (let ((config-file + (if (file-config? config) + (ini-file "forgejo.ini" config) + config))) (list (shepherd-service (documentation "Run Forgejo.") (provision '(forgejo)) @@ -282,7 +285,7 @@ (file-like iocaine) "") (config - gexp + file-object-or-file-config "") (log-file (string "/var/log/iocaine.log") @@ -323,7 +326,10 @@ (define iocaine-etc (match-record-lambda (config) - `(("iocaine/iocaine.toml" ,(toml-file "iocaine.toml" config))))) + `(("iocaine/iocaine.toml" + ,(if (file-config? config) + (toml-file "iocaine.toml" config) + config))))) (define iocaine-shepherd-service (match-record-lambda @@ -541,7 +547,7 @@ test its configuration file.")) (string "misskey/misskey:latest") "Misskey docker image to use.") (config - gexp + file-object-or-file-config "Alist of Misskey configuration, to be serialized to YAML format.") (data-directory (string "/var/lib/misskey") @@ -589,7 +595,10 @@ test its configuration file.")) (define misskey-oci (match-record-lambda (image config data-directory log-file ) - (let ((config-file (yaml-file "misskey.yaml" config))) + (let ((config-file + (if (file-config? config) + (yaml-file "misskey.yaml" config) + config))) (oci-extension (containers (list (oci-container-configuration @@ -642,7 +651,7 @@ test its configuration file.")) (boolean #t) "") (config - (gexp #~'()) + (file-object-or-file-config #~'()) "") (no-serialization)) @@ -665,11 +674,16 @@ test its configuration file.")) (match-record-lambda (navidrome ffmpeg auto-start? config) (let ((config-file - (toml-file "navidrome.toml" - #~`(("DataFolder" . "/var/lib/navidrome") - ("CacheFolder" . "/var/lib/navidrome/cache") - ("EnableInsightsCollector" . #f) - ,@#$config)))) + (if (file-config? config) + (toml-file "navidrome.toml" + (let ((default-settings + '(("DataFolder" . "/var/lib/navidrome") + ("CacheFolder" . "/var/lib/navidrome/cache") + ("EnableInsightsCollector" . #f)))) + (if (gexp? config) + #~(append #$default-settings #$config) + (append default-settings config)))) + config))) (list (shepherd-service (documentation "Run Navidrome.") (provision '(navidrome)) @@ -714,7 +728,7 @@ test its configuration file.")) (string "/var/lib/tuwunel") "Directory to create for @code{tuwunel} user.") (config - gexp + file-object-or-file-config "Configuration file in @code{toml-file} format.") ;; Account (group-id @@ -758,7 +772,10 @@ test its configuration file.")) (define tuwunel-shepherd (match-record-lambda (tuwunel config auto-start? shepherd-requirement) - (let ((config-file (toml-file "tuwunel.toml" config))) + (let ((config-file + (if (file-config? config) + (toml-file "tuwunel.toml" config) + config))) (list (shepherd-service (provision '(tuwunel)) (requirement `(networking user-processes ,@shepherd-requirement)) diff --git a/modules/rosenthal/utils/predicates.scm b/modules/rosenthal/utils/predicates.scm index 3f250b7..7d1a8b4 100644 --- a/modules/rosenthal/utils/predicates.scm +++ b/modules/rosenthal/utils/predicates.scm @@ -5,12 +5,23 @@ ;; Utilities #:use-module (guix gexp) #:export (file-object? + file-config? + file-object-or-file-config? user-and-group-id?)) (define (file-object? val) - (or (string? val) + (or (and (string? val) + (string-prefix? "/" val)) (file-like? val))) +(define (file-config? val) + (or (gexp? val) + (pair? val))) + +(define (file-object-or-file-config? val) + (or (file-object? val) + (file-config? val))) + (define (user-and-group-id? val) (or (integer? val) (eqv? val #f)))