summaryrefslogtreecommitdiff
path: root/modules/valheim.nix
blob: f12a89471348076f9ed2f3f900ee86cdc97bc656 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{ config, lib, pkgs, ... }:

let
  cfg = config.orbekk.valheim-server;
  statePath = "/var/lib/${cfg.stateDir}";
in
{
  options = {
    orbekk.valheim-server = {
      enable = lib.mkEnableOption "Enable valheim server";
      programDir = lib.mkOption {
        type = lib.types.str;
        default = null;
      };
      stateDir = lib.mkOption {
        type = lib.types.str;
        default = "valheim";
      };
      statusPort = lib.mkOption {
        type = lib.types.int;
        default = 12817;
      };
    };
  };

  config = lib.mkIf cfg.enable {
    systemd.services.valheim-server = {
      description = "Valheim server";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];
      path = with pkgs; [ steam.run ];

      serviceConfig = {
        DynamicUser = true;
        StateDirectory = cfg.stateDir;
        KillSignal = "SIGINT";
      };

      environment = {
        HOME = statePath;
      };

      script = ''
        cd ${cfg.programDir}
        export templdpath=$LD_LIBRARY_PATH
        export LD_LIBRARY_PATH=./linux64:$LD_LIBRARY_PATH
        export SteamAppId=892970
        steam-run ./valheim_server.x86_64 -name "doomsday" -port 3400 -world "doomsday" -password "$(cat ${statePath}/password.txt)" -savedir ${statePath} -nographics -batchmode
      '';
    };

    systemd.services.valheim-server-prober = {
      description = "Collect metrics for valheim-server";
      startAt = "*:0/5";
      path = with pkgs; [ moreutils systemd gnugrep coreutils ];
      script = ''
        systemctl show valheim-server | \
        grep -E 'IPIngress|IPEgress|CPUUsage' | \
        tr '=' ' ' | {
          while read k v; do
            echo "# TYPE $k counter"
            echo "systemd_$k{unit=\"valheim-server.service\"} $v";
          done
        } | \
        sponge ${config.orbekk.monitoring-server.textFileDir}/valheim_server.prom
      '';
    };
  };
}