DEV Community

Kevin Cox
Kevin Cox

Posted on • Originally published at kevincox.ca on

Running a Valheim Dedicated Server on NixOS

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 running that you will be able to search for YOUR SERVER NAME in the “Community” server list and log in with your password. Unfortunately there appears to be no way to direct connect, so make sure the name you pick is unique enough to find and be sure to choose a secure 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 and permittedlist.txt.

Top comments (1)

Collapse
 
nicolasguilloux profile image
Guilloux Nicolas • Edited

Hey ! Thanks a lot for your piece of code, it helped me deploy the server. I just want to point out that I had to add 2 lines to make it work, and some tweaks about the group and user.
In the preStart, after the steamcmd thing, I had to symlink the library in the linux64 folder to the .steam/sdk64 since it failed to get it from that folder.

Since it would be not convenient to put some code here, this is a link to my working module I created for those who wants to quickly setup a server.

gitlab.com/NicolasGuilloux/nixos-c...

The best approach would be to package the server itself with the possiblity to give a save folder. This would fix the multi-instance thing, and fix some security concerns as well.