NixOS flake for running a Valheim dedicated server
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-09-18 02:00:43 -05:00
nixos-modules bump version, format, flake update, update nixpkgs src 2026-09-18 01:59:38 -05:00
pkgs bump version, format, flake update, update nixpkgs src 2026-09-18 01:59:38 -05:00
.gitignore Actually building package 2023-04-13 12:39:36 +12:00
flake.lock bump version, format, flake update, update nixpkgs src 2026-09-18 01:59:38 -05:00
flake.nix bump version, format, flake update, update nixpkgs src 2026-09-18 01:59:38 -05:00
LICENSE Initial commit 2023-04-13 09:12:02 +12:00
README.md add server update instruction to Readme 2026-09-18 02:00:43 -05:00

Copy Notice

This repository was originally forked from https://github.com/aidalgol/valheim-server-flake

Valheim Server Flake

A Nix flake for the Valheim dedicated server, providing both an overlay and a NixOS module.

Usage

(Your NixOS system configuration must already be a flake.)

Add this flake as an input, and add the NixOS module. Your config should look something like this.

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.11";
    valheim-server = {
      url = "git+https://codeberg.org/JU12000/valheim-server-flake";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };
  outputs = {
    self,
    nixpkgs,
    valheim-server,
  }: let
    system = "x86_64-linux";
    pkgs = import nixpkgs {inherit system;};
  in {
    nixosConfigurations.my-server= nixpkgs.lib.nixosSystem {
      inherit system;
      modules = [
        ./configuration.nix
        valheim-server.nixosModules.default
      ];
    };
  };
}

Then in your configuration.nix,

{
  config,
  pkgs,
  ...  
}: {
  # ...
  nixpkgs.config.allowUnfreePredicate = pkg:
    builtins.elem (lib.getName pkg) [
      "valheim-server"
      "steamworks-sdk-redist"
    ];
  # ...
  services.valheim = {
    enable = true;
    serverName = "Some cozy server";
    worldName = "Midgard";
    openFirewall = true;
    password = "sekkritpasswd";
    # If you want to use BepInEx mods.
    bepinexMods = [
      # This does NOT fetch mod dependencies.  You need to add those manually,
      # if there are any (besides BepInEx).
      (pkgs.fetchValheimThunderstoreMod {
        owner = "Somebody";
        name = "SomeMod";
        version = "x.y.z";
        hash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
      })
      # ...
    ];
    bepinexConfigs = [
      ./some_mod.cfg
      # ...
    ];
  };
  # ...
}

Managing the server

The NixOS module in this flake runs Valheim server as a systemd service, which you can manage this service using systemctl and journalctl.

# Start, stop, or restart the server (requires superuser).
$ systemctl <start|stop|restart> valheim
# Show the runtime status with most recent log data. 
$ systemctl status valheim

Note that nixos-rebuild switch automatically restarts the service if any attributes under services.valheim are changed. Also be aware that stopping a service does not disable it, and the service will be started again on next boot, nixos-rebuild switch, etc.

# Show live log from the service.
$ journalctl -u valheim -f

Updating the server

Valheim updates its server depots regularly, and each build pins an exact Steam manifest. Steam stops serving old manifests to anonymous accounts, so the pin must be refreshed every time the server updates. If a build fails with something like Encountered 401 for depot manifest ... Aborting, this is why.

  1. Look up the current public manifest for depot 896661.

    $ curl -s https://api.steamcmd.net/v1/info/896660 \
        | jq -r '.data."896660".depots."896661".manifests.public.gid'
    579930472478445891
    

    Also grab the new game version from the patch notes to use as version in the next step.

  2. In pkgs/valheim-server/default.nix, set version to the new version, set manifestId to the value from step 1, and set hash = lib.fakeHash; as a deliberate placeholder.

  3. Build once to make Nix report the real hash, then copy it out of the error.

    $ NIXPKGS_ALLOW_UNFREE=1 nix build --impure .#valheim-server
    error: hash mismatch in fixed-output derivation '...valheim-server-depot.drv':
             specified: sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
                got:    sha256-RzHVowrb6Pp298tGeeXK8IjLl0WdXsy73oe2hpCtsKo=
    
  4. Put the got: value into hash and build again to confirm it succeeds.

Note that the hash depends on both the manifest and the depotdownloader version provided by nixpkgs, so always recompute it against the same nixpkgs revision that your systems build with.

Notes on using mods

Because BepInEx (the mod framework used by just about every Valheim mod) must both be installed in-tree with Valheim, and to be able to write to various files in the directory tree, we cannot run the modded Valheim server from the Nix store. To work around this without completely giving up on immutability, we copy the files out of the Nix store to a directory under /var/lib/valheim and run from there, but wipe and rebuild this directory on each launch.