Running a Valheim Dedicated Server on NixOS
Published: 2021-02-18 01:20:10

---
title: Running a Valheim Dedicated Server on NixOS
published: true
date: 2021-02-16 23:30:00 UTC
tags: 
canonical_url: https://kevincox.ca/2021/02/16/valheim-dedicated-server-nixos/
---

<main><p>I recently started playing <a href="https://www.valheimgame.com/">Valheim</a> 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.</p>

<p>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 <a href="https://nixos.org/">Nix</a> 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.</p>

<p>The only “weird” thing is that instead of using <a href="https://github.com/NixOS/patchelf"><code>patchelf</code></a> to fix up the server binary for NixOS I just called the ELF interpreter directly to launch the game.</p>

<div><pre><code><span>{</span><span>config</span><span>,</span> <span>pkgs</span><span>,</span> <span>lib</span><span>,</span> <span>...</span><span>}:</span> <span>{</span>
	<span>users</span><span>.</span><span>users</span><span>.</span><span>valheim</span> <span>=</span> <span>{</span>
		<span># Valheim puts save data in the home directory.</span>
		<span>home</span> <span>=</span> <span>"/var/lib/valheim"</span><span>;</span>
	<span>};</span>

	<span>systemd</span><span>.</span><span>services</span><span>.</span><span>valheim</span> <span>=</span> <span>{</span>
		<span>wantedBy</span> <span>=</span> <span>[</span> <span>"multi-user.target"</span> <span>];</span>
		<span>serviceConfig</span> <span>=</span> <span>{</span>
			<span>ExecStartPre</span> <span>=</span> <span>''</span><span>
</span><span> </span><span>${</span><span>pkgs</span><span>.</span><span>steamcmd</span><span>}</span><span>/bin/steamcmd \</span><span>
</span><span> +login anonymous \</span><span>
</span><span> +force_install_dir $STATE_DIRECTORY \</span><span>
</span><span> +app_update 896660 \</span><span>
</span><span> +quit</span><span>
</span><span> ''</span><span>;</span>
			<span>ExecStart</span> <span>=</span> <span>''</span><span>
</span><span> </span><span>${</span><span>pkgs</span><span>.</span><span>glibc</span><span>}</span><span>/lib/ld-linux-x86-64.so.2 ./valheim_server.x86_64 \</span><span>
</span><span> -name "YOUR SERVER NAME" \</span><span>
</span><span> -port 2456 \</span><span>
</span><span> -world "Dedicated" \</span><span>
</span><span> -password "YOUR PASSWORD HERE!!!" \</span><span>
</span><span> -public 1</span><span>
</span><span> ''</span><span>;</span>
			<span>Nice</span> <span>=</span> <span>"-5"</span><span>;</span>
			<span>Restart</span> <span>=</span> <span>"always"</span><span>;</span>
			<span>StateDirectory</span> <span>=</span> <span>"valheim"</span><span>;</span>
			<span>User</span> <span>=</span> <span>"valheim"</span><span>;</span>
			<span>WorkingDirectory</span> <span>=</span> <span>"/var/lib/valheim"</span><span>;</span>
		<span>};</span>
		<span>environment</span> <span>=</span> <span>{</span>
			<span># linux64 directory is required by Valheim.</span>
			<span>LD_LIBRARY_PATH</span> <span>=</span> <span>"linux64:</span><span>${</span><span>pkgs</span><span>.</span><span>glibc</span><span>}</span><span>/lib"</span><span>;</span>
		<span>};</span>
	<span>};</span>

    <span># This is my custom backup machinery. Substitute your own 🙂</span>
    <span>kevincox</span><span>.</span><span>backup</span><span>.</span><span>valheim</span> <span>=</span> <span>{</span>
		<span>paths</span> <span>=</span> <span>[</span>
			<span>"/var/lib/valheim/.config/unity3d/IronGate/Valheim/"</span>
		<span>];</span>
	<span>};</span>
<span>}</span>
</code></pre></div>
<p>After running that you will be able to search for <code>YOUR SERVER NAME</code> 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!</p>

<p>This is a simple implementation. The following things could be tweaked:</p>

<ul>
  <li>Put the game data in a different user and directory than the saves.
    <ul>
      <li>Allows running multiple servers.</li>
      <li>A compromised server can’t overwrite the game code.</li>
    </ul>
  </li>
  <li>Put the password out of line in a secret. Right now the password is publicly visible in the Nix store.</li>
  <li>Manage the <code>adminlist.txt</code>, <code>bannedlist.txt</code> and <code>permittedlist.txt</code>.</li>
</ul>