commit 9940d01065e8d6dffb620c1164964d1b608ce597
parent 39f8206346506f1ef53c800228e3373b9a9c5a2c
Author: Luke Willis <lukejw@monastech.xyz>
Date:   Wed,  6 May 2026 12:31:31 -0400

services: Update minecraft-service-type with mod support

Diffstat:
Mmt/services/games.scm | 65+++++++++++++++++++++++++++++++++++++++++++++++++++--------------
Mmt/utils.scm | 13++++++++++++-
2 files changed, 63 insertions(+), 15 deletions(-)

diff --git a/mt/services/games.scm b/mt/services/games.scm @@ -10,9 +10,11 @@ #:use-module (guix packages) #:use-module (guix records) #:use-module (guix download) + #:use-module (mt utils) #:use-module (ice-9 match) #:export (fabric-jar %default-fabric-jar + modrinth-mod minecraft-configuration minecraft-configuration? minecraft-service-type)) @@ -21,6 +23,8 @@ ;;; Minecraft. ;;; +;; TODO: Modrinth API helper + (define* (fabric-jar hash #:key (minecraft "26.1.2") (loader "0.19.2") @@ -51,6 +55,8 @@ (default %default-fabric-jar)) (memory minecraft-configuration-memory (default 8)) + (mods minecraft-configuration-mods + (default '())) (log-file minecraft-configuration-log-file (default "/var/log/minecraft.log")) (home minecraft-configuration-home @@ -70,29 +76,60 @@ (name "minecraft") (system? #t)))) +(define %minecraft-eula + (plain-file "eula.txt" "eula=true\n")) + (define minecraft-activation (match-lambda - (($ <minecraft-configuration> jdk jar memory log-file home port) + (($ <minecraft-configuration> jdk jar memory mods log-file home port) (with-imported-modules '((guix build utils)) #~(begin - (use-modules (guix build utils)) + (use-modules (guix build utils) + (ice-9 match)) - (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) - + (let ((user (getpwnam "minecraft")) + (eula #$(string-append home "/eula.txt")) + (mod-dir #$(string-append home "/mods"))) + ;; TODO: Setup files as the root user + ;; Setup files as the git user. + (match (primitive-fork) + (0 + (dynamic-wind + (const #t) + (lambda () + ;; Switch to the minecraft user. + (setgid (passwd:gid user)) + (setuid (passwd:uid user)) + + ;; Placate the EULA requirement. + (if (not (file-exists? eula)) + (symlink #$%minecraft-eula eula)) + + ;; TODO: server.properties + + ;; Symlink mods to the mods folder. + (if (not (file-exists? mod-dir)) + (mkdir mod-dir)) + (for-each + (lambda (mod) + (let ((out-mod (string-append mod-dir (basename mod)))) + (if (file-exists? out-mod) + (delete-file out-mod)) + (symlink mod out-mod))) + (list #$@mods)) + + ;; Return to main thread. + (primitive-exit 0)) + (lambda () + (primitive-exit 1)))) + (pid (waitpid pid))))) + ;; TODO: Setup server.properties - ;; TODO: Setup mods - )))))) + )))) (define minecraft-shepherd-service (match-lambda - (($ <minecraft-configuration> jdk jar memory log-file home port) + (($ <minecraft-configuration> jdk jar memory mods log-file home port) (shepherd-service (documentation "Minecraft server") (provision '(minecraft)) diff --git a/mt/utils.scm b/mt/utils.scm @@ -1,11 +1,22 @@ (define-module (mt utils) #:use-module (gnu home) #:use-module (gnu packages video) + #:use-module (guix packages) + #:use-module (guix download) #:use-module (nongnu packages game-client) #:use-module (nongnu packages video) #:use-module (nonguix utils) #:use-module (nongnu packages nvidia) - #:export (mt-transformation-nvidia-home-environment)) + #:export (remote-file + mt-transformation-nvidia-home-environment)) + +(define (remote-file url hash) + "Download a file from a url into the store." + (origin + (method url-fetch) + (uri url) + (file-name (basename url)) + (sha256 hash))) ;; TODO: Master function that detects the type of variable passed ;; (operating-system or home-environment) and returns a transformation function