Minecraft Server: Difference between revisions

Removed flags from example, they were old and broken (causing minecraft-server to fail upon using them); instead recommended user to find their own flags. Also added a tip on changing server version/package
Voklen (talk | contribs)
m Add syntax highlighting to code examples
 
(16 intermediate revisions by one other user not shown)
Line 2: Line 2:
{{tip/unfree}}
{{tip/unfree}}


[https://minecraft.wiki/w/Server Minecraft Server] is a server for the blocky sandbox game [[Minecraft]]. Currently only servers for the [https://www.minecraft.net/en-us/article/java-or-bedrock-edition Java Edition] of the game are supported.
[https://minecraft.wiki/w/Server Minecraft Server] is a server for the sandbox game [[Minecraft]]. Currently, only servers for the [https://www.minecraft.net/en-us/article/java-or-bedrock-edition Java Edition] of Minecraft are supported.


== Setup ==
== Setup ==
Line 15: Line 15:
== Configuration ==
== 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 <code>43000</code>
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 <code>43000</code>.


{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
Line 24: Line 24:
   declarative = true;
   declarative = true;
   whitelist = {
   whitelist = {
     # This is a mapping from Minecraft usernames to UUIDs. You can use https://mcuuid.net/ to get a Minecraft UUID for a username
     # This is a mapping of Minecraft usernames to to the players' UUIDs
     username1 = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
     username1 = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
     username2 = "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy";
     username2 = "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy";
Line 37: Line 37:
     allow-cheats = true;
     allow-cheats = true;
   };
   };
   jvmOpts = "-Xms2048M -Xmx4096M";
   jvmOpts = "-Xms2048M -Xmx2048M";  
};
};
</nowiki>}}
</nowiki>}}
You might want to look at using using [https://docs.papermc.io/paper/aikars-flags/ Aikar's flags] or the [https://github.com/brucethemoose/Minecraft-Performance-Flags-Benchmarks Minecraft performance flags benchmark].
You might want to view the [https://minecraft.wiki/w/Server.properties#Keys list of all available server properties for the vanilla server].


See [[#See also]] for recommended JVM flags for the <code>jvmOpts</code> option. These primarily depend on your [[Java]] version.
== Tips and tricks ==
== Tips and tricks ==


=== Prefer IPv4 ===
=== Accessing the Minecraft server console ===
 
The Minecraft server console allows you to view server logs and issue [https://minecraft.wiki/w/Commands commands] to the server interactively. The Minecraft server console is <strong>not</strong> directly accessible on NixOS—unlike on non-declarative systems, where running the server through a shell command provides the interactive console to the current terminal.
 
==== Accessing logs ====
 
Since the Minecraft server runs as a systemd service, you can access its stdout through the systemd journal:
 
<code>journalctl -eu minecraft-server.service</code>
 
The logs are also available in the <code>logs</code> subdirectory of the server's data directory, which is configured via <code>services.minecraft-server.dataDir</code>. The default value for this option is <code>/var/lib/minecraft</code>.
 
==== Issuing commands ====
 
There are two ways to issue commands to the Minecraft server:
 
1. Writing to the server’s stdin via its named pipe at <code>/run/minecraft-server.stdin</code>:
 
<code>echo "say Removed Herobrine" > /run/minecraft-server.stdin</code>
 
2. Using the [https://minecraft.wiki/w/RCON server's provided RCON feature].


To use IPv4 by default, add <code>-Djava.net.preferIPv4Stack=true</code> to <code>jvmOpts</code>.
Example minimal configuration:
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
services.minecraft-server.serverProperties = {
    enable-rcon = true;
    "rcon.password" = "your password";
};
</nowiki>}}


=== Use a different server version/package ===
=== Use a different server ===


To use a specific server version, or another Minecraft server—such as [https://papermc.io/ PaperMC]—change <code>services.minecraft-server.package</code> to a nix package that represents your desired server.
To use a specific server version, or another Minecraft server—such as [https://papermc.io/ PaperMC]—change <code>services.minecraft-server.package</code> to a nix package that represents your desired server.
Line 54: Line 81:
For example:
For example:


<code>services.minecraft-server.package = pkgs.minecraftServers.vanilla-1-12;</code>
<syntaxhighlight lang="nix">
services.minecraft-server.package = pkgs.minecraftServers.vanilla-1-12;
</syntaxhighlight>


or
or


<code>services.minecraft-server.package = pkgs.papermc;</code>
<syntaxhighlight lang="nix">
services.minecraft-server.package = pkgs.papermc;
</syntaxhighlight>
 
=== Prefer IPv4 ===
 
To use IPv4 by default, add <code>-Djava.net.preferIPv4Stack=true</code> to <code>jvmOpts</code>.
 
== See also ==


* [https://github.com/Infinidoge/nix-minecraft nix-minecraft], a [[flake]] based attempt to better support Minecraft related content for the Nix ecosystem. It can be used for more complex server setups, including mods and plugins.
* https://exa.y2k.diy/garden/jvm-args for setting additional JVM flags in the <code>jvmOpts</code> option. Some server-related software—like the Velocity proxy—have their own recommended JVM flags list.
* https://mcuuid.net to get a player's UUID from their current username or vice versa.
[[Category: Applications]]
[[Category: Applications]]
[[Category: Gaming]]
[[Category: Gaming]]