Skip to content

Commit

Permalink
nixos/metis: add minecraft (MSD)
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Dec 29, 2023
1 parent ced0d7a commit 061e9bd
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions nixos/configurations/metis/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{
imports = [
./wireguard.nix
./minecraft
];
nixpkgs.hostPlatform = "x86_64-linux";
nix.registry.sys = {
Expand Down
13 changes: 13 additions & 0 deletions nixos/configurations/metis/minecraft/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{ pkgs, ... }:
{
imports = [
./msd.nix
];

lib.pkgs = {
authlib-injector = pkgs.fetchurl {
url = "https://github.com/yushijinhun/authlib-injector/releases/download/v1.2.4/authlib-injector-1.2.4.jar";
hash = "sha256-eVsnbLQIVe0E3H/KvCptFQ3kYAtUKHWmUi5l9rJbyp8=";
};
};
}
48 changes: 48 additions & 0 deletions nixos/configurations/metis/minecraft/msd.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{ config, pkgs, ... }:
let
name = "modernskyblock";
serviceName = "minecraft-${name}";
directory = "minecraft/${name}";
stdin = "/run/${directory}/stdin";

# https://github.com/NixOS/nixpkgs/blob/6e62521155cd3b4cdf6b49ecacf63db2a0cacc73/nixos/modules/services/games/minecraft-server.nix#L25C3-L34C1
stopScript = pkgs.writeShellScript "minecraft-server-stop" ''
echo stop > ${stdin}
# Wait for the PID of the minecraft server to disappear before
# returning, so systemd doesn't attempt to SIGKILL it.
while kill -0 "$1" 2> /dev/null; do
sleep 1s
done
'';

in
{
systemd.services.${serviceName} = {
after = [ "systemd-networkd-wait-online.service" ];
description = "Minecraft Server (Modern Skyblock 3: Departed)";
requires = [ "${serviceName}.socket" ];
serviceConfig = rec {
Type = "simple";
DynamicUser = true;
StateDirectory = directory;
WorkingDirectory = "%S/${directory}";
ExecStart = "${pkgs.jdk8}/bin/java -Xmx32G -Xms32G -javaagent:${config.lib.pkgs.authlib-injector}=https://hitmc.cc/api/yggdrasil -jar forge-1.12.2-14.23.5.2808-universal.jar nogui";
ExecStop = "${stopScript} $MAINPID";
Restart = "always";
StandardInput = "socket";
StandardOutput = "journal";
StandardError = "journal";
};
};

systemd.sockets.${serviceName} = {
description = "Socket for the Minecraft Server (Modern Skyblock 3: Departed)";
wantedBy = [ "sockets.target" ];
socketConfig = {
ListenFIFO = stdin;
RemoveOnStop = true;
FlushPending = true;
};
};
}

0 comments on commit 061e9bd

Please sign in to comment.