aboutsummaryrefslogtreecommitdiff
path: root/modules/nixos/services/minecraft/default.nix
blob: ae8b4233316e81aa2b9780b34b6e233986377373 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{ options, config, lib, pkgs, namespace, ... }:

with lib; with lib.${namespace}; let
  cfg = config.${namespace}.services.minecraft;
in {
  options.${namespace}.services.minecraft = with types; {
    enable = mkEnableOption "minecraft server support";
  };

  config = mkIf cfg.enable {
    cxl.tools.tmux.enable = true;
    
    services.minecraft-servers = {
      enable = true;
      eula = true;
      openFirewall = true;
    };
    
    environment.systemPackages = with pkgs; [
      (writeShellScriptBin "mc-attach" ''
        file="/run/minecraft/$1.sock"

        if [[ -r "$file" && -w "$file" ]]; then
          tmux -S "$file" attach
        else
          sudo -u minecraft tmux -S "$file" attach
        fi
      '')
      
      (writeShellScriptBin "mc-shell" ''
        dir="/srv/minecraft"

        if [[ -v "$1" ]]; then
          dir="$1"
        fi

        cd "$dir"
        sudo -u minecraft "$SHELL"
      '')
    ];
  };
}