Running a Valheim Dedicated Server on NixOS
Posted
I recently started playing Valheim with some friends. It is a cool game where you start in the wilderness and have to upgrade your abilities and equipment to take on bosses. We decided to set up a dedicated server so that the group could keep playing even if the host wasn’t online.
The dedicated server is distributed via Steam with no authentication required making it very easy to get up and running. I didn’t properly package it for Nix because frequent updates would probably make it more work then it was worth. The following snippet is enough to get the server running. This could also be used as a template for any steam-distributed server.
The only “weird” thing is that instead of using patchelf
to fix up the server binary for NixOS I just called the ELF interpreter directly to launch the game.
{config, pkgs, lib, ...}: {
users.users.valheim = {
# Valheim puts save data in the home directory.
home = "/var/lib/valheim";
};
systemd.services.valheim = {
wantedBy = [ "multi-user.target" ];
serviceConfig = {
ExecStartPre = ''
${pkgs.steamcmd}/bin/steamcmd \
+login anonymous \
+force_install_dir $STATE_DIRECTORY \
+app_update 896660 \
+quit
'';
ExecStart = ''
${pkgs.glibc}/lib/ld-linux-x86-64.so.2 ./valheim_server.x86_64 \
-name "YOUR SERVER NAME" \
-port 2456 \
-world "Dedicated" \
-password "YOUR PASSWORD HERE!!!" \
-public 1
'';
Nice = "-5";
Restart = "always";
StateDirectory = "valheim";
User = "valheim";
WorkingDirectory = "/var/lib/valheim";
};
environment = {
# linux64 directory is required by Valheim.
LD_LIBRARY_PATH = "linux64:${pkgs.glibc}/lib";
};
};
# This is my custom backup machinery. Substitute your own 🙂
kevincox.backup.valheim = {
paths = [
"/var/lib/valheim/.config/unity3d/IronGate/Valheim/"
];
};
}
After that you have a couple of ways to join the game:
- Use “Join IP” from in-game with your IP or hostname and port 2456.
- Steam → View → Servers → Add Server → Enter your IP or hostname and port 2457 this is one higher than the port passed in the command line.
- Search for “YOUR SERVER NAME” in the “Community” server list in-game and log in with your password.
This is a simple implementation. The following things could be tweaked:
- Put the game data in a different user and directory than the saves.
- Allows running multiple servers.
- A compromised server can’t overwrite the game code.
- Put the password out of line in a secret. Right now the password is publicly visible in the Nix store.
- Manage the
adminlist.txt
,bannedlist.txt
andpermittedlist.txt
.