aboutsummaryrefslogtreecommitdiff
path: root/tw/services/games.scm
blob: d239b248eb61d98736aec0bf7b8d197a0c10e9f5 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
(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)
  #:use-module (tw services restic)
  #:use-module (tw services secrets)
  #:export (minecraft-server-service-type
            minecraft-server-configuration))

(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-backup-repository
  (restic-local-repository
   (path "/var/backups/minecraft")))

(define minecraft-backup-password-file
  "/etc/restic/lud-minecraft")

(define minecraft-backup-password
  (restic-password-source
   (type 'file)
   (name minecraft-backup-password-file)))

(define (minecraft-backup-secrets config)
  (list (secret
         (encrypted-file (local-file "../system/files/restic/lud-minecraft.enc"))
         (destination minecraft-backup-password-file))))

(define (minecraft-restic-backups config)
  (match-record config <minecraft-server-configuration> (data-location)
    (list (restic-scheduled-backup
           (schedule #~"30 3 * * *")
           (paths (list data-location))
           (repo minecraft-backup-repository)
           (password minecraft-backup-password)))))

(define (minecraft-backup-cleanups config)
  (list (restic-scheduled-cleanup
         (schedule #~"30 4 * * *")
         (repo minecraft-backup-repository)
         (password minecraft-backup-password)
         (keep-daily 30)
         (keep-monthly -1))))

(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)
                     (service-extension secrets-service-type minecraft-backup-secrets)
                     (service-extension restic-backup-service-type minecraft-restic-backups)
                     (service-extension restic-cleanup-service-type minecraft-backup-cleanups)))
   (default-value (minecraft-server-configuration))
   (description "Run a Minecraft server.")))