- Nix 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| nixos-modules | ||
| pkgs | ||
| .gitignore | ||
| flake.lock | ||
| flake.nix | ||
| LICENSE | ||
| README.md | ||
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.
-
Look up the current
publicmanifest for depot896661.$ curl -s https://api.steamcmd.net/v1/info/896660 \ | jq -r '.data."896660".depots."896661".manifests.public.gid' 579930472478445891Also grab the new game version from the patch notes to use as
versionin the next step. -
In
pkgs/valheim-server/default.nix, setversionto the new version, setmanifestIdto the value from step 1, and sethash = lib.fakeHash;as a deliberate placeholder. -
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= -
Put the
got:value intohashand 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.