This gist shows you how to install Minecraft Forge on a NixOS server. It is based on my NixOS on Hetzner Cloud gist.
- Add
./minecraft.nix
to theimports
in~/repos/configuration/default.nix
. - Paste the following in
~/repos/configuration/minecraft.nix
.
{ config, pkgs, ... }:
{
users = {
groups.minecraft = {};
extraUsers.minecraft = {
isSystemUser = true;
group = "minecraft";
home = "/var/minecraft";
createHome = true;
packages = with pkgs; [
jre8
];
};
};
systemd.services.minecraft = {
enable = true;
description = "Forge Minecraft Server";
serviceConfig = {
ExecStart = "${pkgs.jre8}/bin/java -Xms1G -Xmx7G -jar ./forge.jar nogui";
WorkingDirectory = "${config.users.extraUsers.minecraft.home}/forge";
Restart = "always";
RestartSec = 60;
};
after = [ "network.target" ];
wantedBy = [ "default.target" ];
};
networking.firewall.allowedTCPPorts = [ <portA> <portB> ];
networking.firewall.allowedUDPPorts = [ <portA> <portB> ];
}
- Rebuild the system.
sudo nixos-rebuild switch
- Switch to the newly created minecraft user.
sudo -u minecraft bash
cd
- Download and extract the forge version you want.
mkdir forge
cd forge
wget https://maven.minecraftforge.net/net/minecraftforge/forge/1.16.5-36.2.20/forge-1.16.5-36.2.20-installer.jar
java -jar forge-1.16.5-36.2.20-installer.jar --installServer
# You accept the EULA by doing this
echo 'eula=true' > eula.txt
# Symlink the current version to the generic name.
ln -s forge-1.16.5-36.2.20.jar forge.jar
# Test running the server.
nix-shell -p jre8 --run 'java -Xms1G -Xmx7G -jar forge.jar --nogui'
# When the server has started, type `stop` to stop.
stop
- Change the ports in the
server.properties
file to same as in~/repos/configuration/minecraft.nix
.
sed -e 's/^query\.port=.*$/query.port=<portA>/g' -i server.properties
sed -e 's/^server-port=.*$/server-port=<portA>/g' -i server.properties
sed -e 's/^rcon\.port=.*$/rcon.port=<portB>/g' -i server.properties
- Log out and restart the daemon.
exit
sudo systemctl restart minecraft
# Observe output.
journalctl --unit minecraft.service --follow