Skip to content

Instantly share code, notes, and snippets.

@cyber-murmel
Last active February 6, 2022 04:47
Show Gist options
  • Save cyber-murmel/4aeae3b5dafc72f12827b3284a9da481 to your computer and use it in GitHub Desktop.
Save cyber-murmel/4aeae3b5dafc72f12827b3284a9da481 to your computer and use it in GitHub Desktop.

This gist shows you how to install Minecraft Forge on a NixOS server. It is based on my NixOS on Hetzner Cloud gist.

  1. Add ./minecraft.nix to the imports in ~/repos/configuration/default.nix.
  2. 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> ];
}
  1. Rebuild the system.
sudo nixos-rebuild switch
  1. Switch to the newly created minecraft user.
sudo -u minecraft bash
cd
  1. 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
  1. 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
  1. Log out and restart the daemon.
exit
sudo systemctl restart minecraft
# Observe output.
journalctl --unit minecraft.service --follow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment