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."; description = "Base UDP port for the Valheim server.";
}; };
password = lib.mkOption { passwordSecret = lib.mkOption {
type = lib.types.str; 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 = { guests.valheim = {
interior.modules.services.games.valheim = { interior.modules.services.games.valheim = {
enable = true; enable = true;
inherit (cfg) serverName port password; inherit (cfg) serverName port;
inherit steamDir installDir stateDir; inherit steamDir installDir stateDir;
passwordFile = "/run/secrets/${cfg.passwordSecret}";
}; };
mounts = { mounts = {
@@ -92,6 +94,10 @@ in
group = "storage"; group = "storage";
mode = "0770"; mode = "0770";
}; };
${"/run/secrets/${cfg.passwordSecret}"} = {
file = config.sops.secrets.${cfg.passwordSecret}.path;
readOnly = true;
};
}; };
limits = { limits = {
@@ -100,5 +106,11 @@ in
tasksMax = lib.mkDefault 1024; 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"; installPath = "/pikachu/data/servers/valheim/main";
statePath = "/pikachu/media/games/servers/valheim/main"; statePath = "/pikachu/media/games/servers/valheim/main";
serverName = "main"; serverName = "main";
password = "foobarbaz";
reverseProxy = { reverseProxy = {
enable = true; enable = true;
route = { route = {

View File

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