Minecraft Server

From NixOS Wiki

Minecraft Server is a server for the blocky sandbox game Minecraft. Currently only servers for the Java Edition of the game are supported.

Setup

The minimum example to have a Minecraft server running on localhost at the default port of 25565. By setting the eula option to true, you are agreeing to the Minecraft EULA

Note: This package is unfree and requires extra steps to install.
 
/etc/nixos/configuration.nix
services.minecraft-server.enable = true;
services.minecraft-server.eula = true;

Configuration

This example is a more thorough declarative configuration that sets a few options including opening the firewall, restricting the server to only whitelisted users and setting the port to 43000

 
/etc/nixos/configuration.nix
services.minecraft-server = {
  enable = true;
  eula = true;
  openFirewall = true; # Opens the port the server is running on (by default 25565 but in this case 43000)
  declarative = true;
  whitelist = {
    # This is a mapping from Minecraft usernames to UUIDs. You can use https://mcuuid.net/ to get a Minecraft UUID for a username
    username1 = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
    username2 = "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy";
  };
  serverProperties = {
    server-port = 43000;
    difficulty = 3;
    gamemode = 1;
    max-players = 5;
    motd = "NixOS Minecraft server!";
    white-list = true;
    allow-cheats = true;
  };
  jvmOpts = "-Xms4092M -Xmx4092M -XX:+UseG1GC -XX:+CMSIncrementalPacing -XX:+CMSClassUnloadingEnabled -XX:ParallelGCThreads=2 -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=10";
};

Tips and tricks

Prefer IPv4

To use IPv4 by default, add -Djava.net.preferIPv4Stack=true to jvmOpts (the first two options are just the defaults)

jvmOpts = "-Xmx2048M -Xms2048M -Djava.net.preferIPv4Stack=true";