commit e4456928384fa0fa9d852c71ebb10a141fb0c9ff
parent 236d7966cbeac1b812bcf9128986b4e69b5b672e
Author: Luke Willis <lukejw@monastech.xyz>
Date:   Fri,  8 May 2026 20:20:29 -0400

services: Add bta-service-type

Diffstat:
Mmt/services/games.scm | 162++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 161 insertions(+), 1 deletion(-)

diff --git a/mt/services/games.scm b/mt/services/games.scm @@ -10,10 +10,14 @@ #:use-module (guix packages) #:use-module (guix records) #:use-module (guix download) + #:use-module (guix build-system copy) #:use-module (mt utils) #:use-module (mt packages games) #:use-module (ice-9 match) - #:export (fabric-jar + #:export (bta-configuration + bta-configuration? + bta-service-type + fabric-jar %default-fabric-jar modrinth-mod minecraft-configuration @@ -21,6 +25,162 @@ minecraft-service-type)) ;;; +;;; Better Than Adventure! +;;; + +;; TODO: Add justauth support + +(define %bta-account + (list (user-account + (name "bta") + (group "bta") + (system? #t) + (comment "BTA server user") + (home-directory "/var/lib/minecraft") + (shell (file-append shadow "/sbin/nologin"))) + (user-group + (name "bta") + (system? #t)))) + +;; TODO: Review. Is copy-build-system / package really necessary? +(define bta-fabric-server + (package + (name "bta-fabric-server") + (version "7.3_04") + (build-system copy-build-system) + (source + (origin + (method url-fetch/zipbomb) + (uri (string-append + "https://github.com/Turnip-Labs/bta-fabric-instance-repo/releases/download/v" + version "/bta_fabric_server_" version ".zip")) + (sha256 + (base32 + "18170krqhg61zlsvh7z9c8932qwa3waxalhh7lqdjj0qyp4lc4gr")))) + (home-page #f) + (synopsis #f) + (description #f) + (license #f))) + +(define-record-type* <bta-configuration> + bta-configuration + make-bta-configuration + bta-configuration? + (jdk bta-configuration-jdk + (default openjdk17)) + (memory bta-configuration-memory + (default 4)) + (properties bta-configuration-properties + (default '())) + (ops bta-configuration-ops + (default '())) + (mods bta-configuration-mods + (default '())) + (home bta-configuration-home + (default "/var/lib/bta")) + (log-file bta-configuration-log-file + (default "/var/log/bta.log"))) + +(define bta-activation + (match-lambda + (($ <bta-configuration> jdk memory properties ops mods home log-file) + (with-imported-modules '((guix build utils)) + #~(begin + (use-modules (guix build utils) + (ice-9 match)) + + (let ((user (getpwnam "bta")) + (server-launcher-properties + #$(string-append home "/fabric-server-launcher.properties")) + (server-properties #$(string-append home "/server.properties")) + (ops-txt #$(string-append home "/ops.txt")) + (mod-dir #$(string-append home "/mods/"))) + (match (primitive-fork) + (0 + (dynamic-wind + (const #t) + (lambda () + ;; Switch to the bta user + (setgid (passwd:gid user)) + (setuid (passwd:uid user)) + + ;; Write to fabric-server-launcher.properties + (call-with-output-file + server-launcher-properties + (lambda (port) + (format port "serverJar=~a\n" + #$(file-append bta-fabric-server + "/server.jar")))) + + ;; Write to server.properties + (call-with-output-file + serrver-properties + (lambda (port) + (for-each + (lambda (pair) + (format port "~a=~a\n" (car pair) (cdr pair))) + '#$properties))) + + ;; Initialize ops.txt + (unless (file-exists? ops-txt) + (call-with-output-file + properties + (lambda (port) + (for-each + (lambda (uuid) + (format port "~a\n" uuid)) + '#$ops)))) + + ;; Link all mods into the mods folder + (if (file-exists? mod-dir) + (delete-file-recursively mod-dir)) + (mkdir mod-dir) + (for-each + (lambda (mod) + (symlink mod (string-append mod-dir (basename mod)))) + '#$mods) + + ;; Return to main thread + (primitive-exit 0)) + (lambda () + (primitive-exit 1)))) + (pid (waitpid pid))))))))) + +(define bta-shepherd-service + (match-lambda + (($ <bta-configuration> jdk memory properties ops mods home log-file) + (shepherd-service + (documentation "BTA server") + (provision '(bta)) + (requirement '(user-processes networking)) + (start #~(make-forkexec-constructor + (list #$(file-append jdk "/bin/java") + "-Dfabric.runtimeMappingNamespace=official" + #$(format #f "-Xmx~aG" memory) + "-jar" #$(file-append bta-fabric-server + "fabric-server-launch.jar") + "nogui") + #:user "bta" + #:group "bta" + #:directory #$home + #:log-file #$log-file)) + (stop #~(make-kill-destructor SIGINT)))))) + +(define bta-service-type + (service-type + (name 'bta) + (extensions + (list (service-extension account-service-type + (const %bta-account)) + (service-extension activation-service-type + bta-activation) + (service-extension shepherd-root-service-type + (compose list bta-shepherd-service)))) + (default-value (bta-configuration)) + (description "Run a BTA server."))) + + +;;; ;;; Minecraft. ;;;