1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.minetest-server;
7 flag = val: name: if val != null then "--${name} ${val} " else "";
8 flags = [
9 (flag cfg.gameId "gameid")
10 (flag cfg.world "world")
11 (flag cfg.configPath "config")
12 (flag cfg.logPath "logfile")
13 (flag cfg.port "port")
14 ];
15in
16{
17 options = {
18 services.minetest-server = {
19 enable = mkOption {
20 type = types.bool;
21 default = false;
22 description = "If enabled, starts a Minetest Server.";
23 };
24
25 gameId = mkOption {
26 type = types.nullOr types.str;
27 default = null;
28 description = ''
29 Id of the game to use. To list available games run
30 `minetestserver --gameid list`.
31
32 If only one game exists, this option can be null.
33 '';
34 };
35
36 world = mkOption {
37 type = types.nullOr types.path;
38 default = null;
39 description = ''
40 Name of the world to use. To list available worlds run
41 `minetestserver --world list`.
42
43 If only one world exists, this option can be null.
44 '';
45 };
46
47 configPath = mkOption {
48 type = types.nullOr types.path;
49 default = null;
50 description = ''
51 Path to the config to use.
52
53 If set to null, the config of the running user will be used:
54 `~/.minetest/minetest.conf`.
55 '';
56 };
57
58 logPath = mkOption {
59 type = types.nullOr types.path;
60 default = null;
61 description = ''
62 Path to logfile for logging.
63
64 If set to null, logging will be output to stdout which means
65 all output will be catched by systemd.
66 '';
67 };
68
69 port = mkOption {
70 type = types.nullOr types.int;
71 default = null;
72 description = ''
73 Port number to bind to.
74
75 If set to null, the default 30000 will be used.
76 '';
77 };
78 };
79 };
80
81 config = mkIf cfg.enable {
82 users.extraUsers.minetest = {
83 description = "Minetest Server Service user";
84 home = "/var/lib/minetest";
85 createHome = true;
86 uid = config.ids.uids.minetest;
87 };
88
89 systemd.services.minetest-server = {
90 description = "Minetest Server Service";
91 wantedBy = [ "multi-user.target" ];
92 after = [ "network.target" ];
93
94 serviceConfig.Restart = "always";
95 serviceConfig.User = "minetest";
96
97 script = ''
98 cd /var/lib/minetest
99
100 exec ${pkgs.minetest}/bin/minetest --server ${concatStrings flags}
101 '';
102 };
103 };
104}