summary refs log tree commit diff
path: root/services/minecraft.nix
blob: 29e8e0013a34e666f036097f56d6f3d2fc8011b8 (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
{ pkgs, auxiliaryPkgs, ... }:

let
  inherit (pkgs) dockerTools;
  inherit (auxiliaryPkgs) common;

  minecraft = pkgs.minecraft-server.overrideAttrs {
    version = "1.21.4";

    src = pkgs.fetchurl {
      url = "https://piston-data.mojang.com/v1/objects/4707d00eb834b446575d89a61a11b5d548d8c001/server.jar";
      hash = "sha256-EGaXCwnpxnGERXIpHEqHHMGsK4WDi/cAT6DneOEPE1g=";
    };
  };

  minecraftPort = 25565;
  minecraftDir = "/srv/mc";
  minecraftMemory = "8G";

  minecraftImage = dockerTools.buildLayeredImage {
    name = "minecraft";
    tag = minecraft.version;
    fromImage = common.alpine.base;
    contents = [ minecraft ];
  };

in
{
  networking.firewall.allowedTCPPorts = [ minecraftPort ];

  # TODO: put global ports and streamed images into foundation
  virtualisation.oci-containers.containers.minecraft = {
    imageFile = minecraftImage;
    image = "minecraft:${minecraft.version}";
    # expose minecraft service port globally.
    ports = [ "0.0.0.0:${toString minecraftPort}:${toString minecraftPort}" ];

    volumes = [
      "${minecraftDir}:/server"
    ];

    workdir = "/server";
    entrypoint = "${minecraft}/bin/minecraft-server";
    cmd = [
      "-Xms${minecraftMemory}"
      "-Xmx${minecraftMemory}"
    ];
  };
}