commit 9ce49984c996e478f57dafce16d3a2ed6a0cf2bd parent 77b983cf0847891752337c5a0992cb8b13b4919d Author: Luke Willis <lukejw@monastech.xyz> Date: Tue, 5 May 2026 20:58:48 -0400 services: Update minecraft-service-type Diffstat:
| M | mt/services/games.scm | | | 100 | +++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------- |
1 file changed, 65 insertions(+), 35 deletions(-)
diff --git a/mt/services/games.scm b/mt/services/games.scm @@ -10,13 +10,16 @@ #:use-module (guix packages) #:use-module (guix records) #:use-module (guix download) + #:use-module (ice-9 match) #:export (fabric-jar %default-fabric-jar minecraft-configuration minecraft-configuration? minecraft-service-type)) -(define-maybe/no-serialization file-like) +;;; +;;; Minecraft. +;;; (define* (fabric-jar hash #:key (minecraft "26.1.2") @@ -37,20 +40,23 @@ (define %default-fabric-jar (fabric-jar (base32 "1gax8i1risr0irgcmbwc0jdf78yhahplsqiyhiblrq7hkydx26z9"))) -(define (port? x) - (and (number? x) - (and (>= 0) (<= x 65535)))) - -(define-configuration/no-serialization minecraft-configuration - (java - (file-like openjdk) - "The java package to run the server with.") - (jar - (file-like %default-fabric-jar) - "The server jar to use.") - (port - (port 25565) - "The port the server should listen to.")) +;; TODO: Switch to define-configuration/no-serialization +(define-record-type* <minecraft-configuration> + minecraft-configuration + make-minecraft-configuration + minecraft-configuration? + (jdk minecraft-configuration-package + (default openjdk)) + (jar minecraft-configuration-jar + (default %default-fabric-jar)) + (memory minecraft-configuration-memory + (default 8)) + (log-file minecraft-configuration-log-file + (default "/var/log/minecraft.log")) + (home minecraft-configuration-home + (default "/var/lib/minecraft")) + (port minecraft-configuration-port + (default 25565))) (define %minecraft-account (list (user-account @@ -58,35 +64,59 @@ (group "minecraft") (system? #t) (comment "Minecraft server user") - (home-directory "/var/empty") + (home-directory "/var/lib/minecraft") (shell (file-append shadow "/sbin/nologin"))) (user-group (name "minecraft") (system? #t)))) -(define (minecraft-shepherd-service config) - "Return the <shepherd-service> object of Minecraft." - (match-record config <minecraft-configuration> - (java jar port) - (list (shepherd-service - (provision '(minecraft)) - (requirement '(user-processes networking)) - (start #~(make-forkexec-constructor - (list #$(file-append java "/bin/java") - "-Xmx8G" ;; TODO: Allow configuring this - "-jar" #$jar - "nogui") - #:user "minecraft" - #:group "minecraft")) - (stop #~(make-kill-destructor)))))) +(define minecraft-activation + (match-lambda + (($ <minecraft-configuration> jdk jar memory log-file home port) + (with-imported-modules '((guix build utils)) + #~(begin + (use-modules (guix build utils)) + + (let* ((user (getpw "minecraft")) + (eula #$(string-append home "/eula.txt"))) + ;; Agree to the EULA + (call-with-output-file eula + (lambda (port) + (display "eula=true\n" port))) + (chown eula (passwd:uid user) (passwd:gid user)) + (chmod eula #o755) + + ;; TODO: Setup server.properties + ;; TODO: Setup mods + )))))) + +(define minecraft-shepherd-service + (match-lambda + (($ <minecraft-configuration> jdk jar memory log-file home port) + (shepherd-service + (documentation "Minecraft server") + (provision '(minecraft)) + (requirement '(user-processes networking)) + (start #~(make-forkexec-constructor + (list #$(file-append jdk "/bin/java") + #$(format #f "-Xmx~aG" memory) + "-jar" #$jar + "nogui") + #:user "minecraft" + #:group "minecraft" + #:directory #$home + #:log-file #$log-file)) + (stop #~(make-kill-destructor)))))) (define minecraft-service-type (service-type (name 'minecraft) (extensions - (list (service-extension shepherd-root-service-type - minecraft-shepherd-service) - (service-extension account-service-type - (const %minecraft-account)))) + (list (service-extension account-service-type + (const %minecraft-account)) + (service-extension activation-service-type + minecraft-activation) + (service-extension shepherd-root-service-type + (compose list minecraft-shepherd-service)))) (default-value (minecraft-configuration)) (description "Run a Minecraft server.")))