aboutsummaryrefslogtreecommitdiff
path: root/tw/services/games.scm
blob: 7b7d8a4b256912f041626d04ba4c039481fc4142 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
(define-module (tw services games)
  #:use-module ((gnu packages admin) #:select (shadow))
  #:use-module (gnu services)
  #:use-module (gnu services configuration)
  #:use-module (gnu services shepherd)
  #:use-module (gnu system shadow)
  #:use-module (guix gexp)
  #:use-module (guix records)
  #:use-module (tw packages games)
  #:export (minecraft-server-service-type
            minecraft-server-configuration))

;; TODO: backups of Minecraft data

(define-configuration/no-serialization minecraft-server-configuration
  (data-location (string "/var/lib/minecraft-server") "Path on disk where
server data will be stored.")
  (port (integer 25565) "The port to bind the main server to.")
  (user-name (string "minecraft") "UNIX username to create for the server.")
  (group-name (string "minecraft") "UNIX group to create for the server."))

(define (minecraft-server-shepherd config)
  (match-record config <minecraft-server-configuration> (data-location port user-name group-name)
    (list (shepherd-service
           (provision '(minecraft-server))
           (requirement '(networking))
           (documentation "Run a Minecraft server.")
           (start #~(make-forkexec-constructor
                     (list #$(file-append minecraft-server "/bin/minecraft-server")
                           "--nogui" "--port" #$(number->string port))
                     #:user #$user-name #:group #$group-name
                     #:directory #$data-location))
           (stop #~(make-kill-destructor))))))

(define (minecraft-server-activation config)
  (match-record config <minecraft-server-configuration> (data-location)
    #~(with-output-to-file (string-append #$data-location "/eula.txt")
        (lambda () (display "eula=true") (newline)))))

(define (minecraft-server-accounts config)
  (match-record config <minecraft-server-configuration> (data-location user-name group-name)
    (list (user-account
           (name user-name)
           (group group-name)
           (comment "Minecraft server user")
           (system? #t)
           (home-directory data-location)
           (shell (file-append shadow "/sbin/nologin")))
          (user-group
           (name group-name)
           (system? #t)))))

(define minecraft-server-service-type
  (service-type
   (name 'minecraft-server)
   (extensions (list (service-extension shepherd-root-service-type minecraft-server-shepherd)
                     (service-extension activation-service-type minecraft-server-activation)
                     (service-extension account-service-type minecraft-server-accounts)))
   (default-value (minecraft-server-configuration))
   (description "Run a Minecraft server.")))