commit 5851278592c4b3cd6d8576740c291e5e0848cde8
parent da5a14b65de5c3f4f2284253e48a5862e3445cfc
Author: Luke Willis <lukejw@monastech.xyz>
Date: Wed, 6 May 2026 16:57:31 -0400
services: Add rcon shepherd action to minecraft-service-type
Diffstat:
3 files changed, 81 insertions(+), 3 deletions(-)
diff --git a/mt/packages/games.scm b/mt/packages/games.scm
@@ -22,11 +22,39 @@
#:use-module (gnu packages video)
#:use-module (gnu packages xdisorg)
#:use-module (guix build-system cmake)
+ #:use-module (guix build-system gnu)
#:use-module (guix build-system meson)
#:use-module (guix build-system qt)
#:use-module (guix download)
#:use-module (guix gexp))
+(define-public mcrcon
+ (package
+ (name "mcrcon")
+ (version "0.7.2")
+ (source (origin
+ (method git-fetch)
+ (uri (git-reference
+ (url "https://github.com/Tiiffi/mcrcon")
+ (commit (string-append "v" version))))
+ (file-name (git-file-name name version))
+ (sha256
+ (base32
+ "0vj8la4ygk3xq2270mf28wswb71hl0a90y53iss2n4v0qqm5xrvi"))))
+ (build-system gnu-build-system)
+ (arguments
+ (list
+ #:make-flags
+ #~(list (string-append "PREFIX=" #$output))
+ #:phases
+ #~(modify-phases %standard-phases
+ (delete 'configure)
+ (delete 'check))))
+ (home-page "https://github.com/Tiiffi/mcrcon")
+ (synopsis #f)
+ (description #f)
+ (license license:zlib)))
+
;; Controller patches for GLFW 3.4
;; Pulled from https://github.com/mdqinc/SDL_GameControllerDB
diff --git a/mt/services/games.scm b/mt/services/games.scm
@@ -11,6 +11,7 @@
#:use-module (guix records)
#:use-module (guix download)
#:use-module (mt utils)
+ #:use-module (mt packages games)
#:use-module (ice-9 match)
#:export (fabric-jar
%default-fabric-jar
@@ -66,6 +67,7 @@
(port minecraft-configuration-port
(default 25565)))
+
(define %minecraft-account
(list (user-account
(name "minecraft")
@@ -81,6 +83,9 @@
(define %minecraft-eula
(plain-file "eula.txt" "eula=true\n"))
+(define %rcon-password
+ (random-string 72))
+
(define minecraft-activation
(match-lambda
(($ <minecraft-configuration> jdk jar memory properties mods log-file home port)
@@ -112,6 +117,9 @@
(call-with-output-file
properties
(lambda (port)
+ (format port "server-port=~a\n" #$port)
+ (display "enable-rcon=true\n" port)
+ (format port "rcon.password=~a\n" #$%rcon-password)
(for-each
(lambda (pair)
(format port "~a=~a\n" (car pair) (cdr pair)))
@@ -133,8 +141,6 @@
(lambda ()
(primitive-exit 1))))
(pid (waitpid pid)))))
-
- ;; TODO: Setup server.properties
))))
(define minecraft-shepherd-service
@@ -153,7 +159,41 @@
#:group "minecraft"
#:directory #$home
#:log-file #$log-file))
- (stop #~(make-kill-destructor))))))
+ (stop #~(make-kill-destructor))
+ (actions (list (shepherd-action
+ (name 'rcon)
+ (documentation "Run server commands via rcon.")
+ (procedure #~(lambda (running . args)
+
+ (define (collect-lines port)
+ "Collect all lines from a port in a list"
+ (let loop ((line (read-line port))
+ (acc '()))
+ (if (eof-object? line)
+ (begin (close-port port)
+ acc)
+ (loop (read-line port)
+ (cons line acc)))))
+
+ (let ((output-pipe (pipe)))
+ (spawn #$(file-append mcrcon "/bin/mcrcon")
+ (cons "mcrcon" args)
+ #:output (cdr output-pipe)
+ #:error (cdr output-pipe)
+ #:environment
+ (list
+ #$(string-append "MCRCON_PASS="
+ %rcon-password))
+ #:search-path? #f)
+ (close-port (cdr output-pipe))
+ (while #t
+ (let ((line (read-line (car output-pipe))))
+ (if (eof-object? line)
+ (break))
+ (display line)
+ (newline))))
+ ;; TODO
+ )))))))))
(define minecraft-service-type
(service-type
diff --git a/mt/utils.scm b/mt/utils.scm
@@ -7,9 +7,18 @@
#:use-module (nongnu packages video)
#:use-module (nonguix utils)
#:use-module (nongnu packages nvidia)
+ #:use-module (srfi srfi-1)
#:export (remote-file
+ random-string
mt-transformation-nvidia-home-environment))
+(define (random-string n)
+ "Generate a random alphanumeric string of length N."
+ (let ((chars "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))
+ (list->string
+ (list-tabulate n (lambda (_)
+ (string-ref chars (random (string-length chars))))))))
+
(define (remote-file url hash)
"Download a file from a url into the store."
(origin
@@ -59,3 +68,4 @@ for NVIDIA drivers.\n"
(inherit home-env)
(packages packages)
(services services))))))
+