feat(nixos): load Valheim password from sops

This commit is contained in:
2026-09-10 09:24:53 -04:00
parent eea7dbef40
commit 31c2369d1f
3 changed files with 26 additions and 10 deletions

View File

@@ -44,9 +44,10 @@ in
description = "Base UDP port for the Valheim server.";
};
password = lib.mkOption {
passwordSecret = lib.mkOption {
type = lib.types.str;
description = "Valheim server password.";
default = "services/valheim/password";
description = "SOPS secret name containing the Valheim server password.";
};
};
@@ -69,8 +70,9 @@ in
guests.valheim = {
interior.modules.services.games.valheim = {
enable = true;
inherit (cfg) serverName port password;
inherit (cfg) serverName port;
inherit steamDir installDir stateDir;
passwordFile = "/run/secrets/${cfg.passwordSecret}";
};
mounts = {
@@ -92,6 +94,10 @@ in
group = "storage";
mode = "0770";
};
${"/run/secrets/${cfg.passwordSecret}"} = {
file = config.sops.secrets.${cfg.passwordSecret}.path;
readOnly = true;
};
};
limits = {
@@ -100,5 +106,11 @@ in
tasksMax = lib.mkDefault 1024;
};
};
sops.secrets.${cfg.passwordSecret} = {
owner = "root";
group = "storage";
mode = "0440";
};
};
}

View File

@@ -74,7 +74,6 @@
installPath = "/pikachu/data/servers/valheim/main";
statePath = "/pikachu/media/games/servers/valheim/main";
serverName = "main";
password = "foobarbaz";
reverseProxy = {
enable = true;
route = {

View File

@@ -1,6 +1,7 @@
{
config,
lib,
pkgs,
...
}:
{
@@ -37,9 +38,9 @@
description = "Base UDP port for the Valheim server.";
};
password = lib.mkOption {
type = lib.types.str;
description = "Valheim server password.";
passwordFile = lib.mkOption {
type = lib.types.path;
description = "Runtime file containing the Valheim server password.";
};
};
@@ -47,6 +48,12 @@
config = lib.mkIf config.modules.services.games.valheim.enable (
let
service = config.modules.services.games.valheim;
startScript = pkgs.writeShellScript "valheim-start" ''
set -eu
password="$(${pkgs.coreutils}/bin/tr -d '\n' < ${lib.escapeShellArg service.passwordFile})"
exec ./valheim_server.x86_64 "$@" -password "$password"
'';
in
{
modules.services.steamcmd = {
@@ -54,7 +61,7 @@
inherit (service) steamDir installDir stateDir;
serviceName = "valheim";
appId = 896660;
executable = "./valheim_server.x86_64";
executable = "${startScript}";
arguments = [
"-nographics"
"-batchmode"
@@ -66,8 +73,6 @@
service.serverName
"-savedir"
service.stateDir
"-password"
service.password
"-public"
"0"
];