From c9dc8a0defb2ad64e8803219d40978b04d8fd571 Mon Sep 17 00:00:00 2001 From: blake Date: Thu, 10 Sep 2026 12:22:13 -0500 Subject: [PATCH] init valheim --- modules/gameservers/valheim/default.nix | 161 ++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 modules/gameservers/valheim/default.nix diff --git a/modules/gameservers/valheim/default.nix b/modules/gameservers/valheim/default.nix new file mode 100644 index 0000000..4242406 --- /dev/null +++ b/modules/gameservers/valheim/default.nix @@ -0,0 +1,161 @@ +{ + pkgs, + config, + lib, + inputs, + ... +}: let + nixservice = "podman-valheim"; + service = "valheim"; + cfg = config.homelab.${service}; + sec = config.sops.secrets; + homelab = config.homelab; +in { + options.homelab.${service} = { + enable = lib.mkEnableOption "enables ${service}"; + # set port options + port = lib.mkOption { + type = lib.types.int; + default = 7704; + description = "set port for ${service} (default: ${toString cfg.port}"; + }; + url = lib.mkOption { + type = lib.types.str; + default = "${service}.${homelab.base_domain}"; + description = "set domain for ${service}"; + }; + data_dir = lib.mkOption { + type = lib.types.str; + default = "/var/lib/${service}"; + description = "set data directory for ${service}"; + }; + ids = lib.mkOption { + type = lib.types.int; + default = cfg.port; + description = "set uid and pid of ${service} user (matches port by default)"; + }; + backup = lib.mkOption { + type = lib.types.bool; + default = false; + description = "enable backups for ${service}"; + }; + motd_gs = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = ["craftoria"]; + }; + backup_repo = lib.mkOption { + type = lib.types.path; + default = "/var/backup/gameservers/valheim"; + description = "path to take hourly backups to with borg!"; + }; + offsite_user = lib.mkOption { + type = lib.types.str; + default = "lifeboat"; + description = "user to connect to offsite host with"; + }; + offsite_target = lib.mkOption { + type = lib.types.str; + default = "lifeboat.holocron.blue"; + description = "ip or domain for offsite backups"; + }; + offsite_dir = lib.mkOption { + type = lib.types.str; + default = "lifeboat/holocron/archives/gameservers"; + description = "path to rsync offsite backups to"; + }; + }; + + config = lib.mkIf cfg.enable { + # declare ${service} group + users.groups.${service} = {gid = lib.mkForce cfg.ids;}; + + # declare ${service} user + users.users.${service} = { + description = "${service} server user"; + uid = lib.mkForce cfg.ids; + isSystemUser = true; + home = cfg.data_dir; + createHome = true; + group = service; + extraGroups = []; + }; + + virtualisation.oci-containers.containers = { + hass = { + image = "homeassistant/home-assistant:stable"; + autoStart = true; + extraOptions = [ + "--pull=newer" + "--network=host" + ]; + volumes = [ + "${cfg.data_dir}:/config" + ]; + environment = { + TZ = homelab.tz; + PUID = toString config.users.users.${service}.uid; + PGID = toString config.users.groups.${service}.gid; + }; + }; + }; + + # override umask to make permissions work out + systemd.services.${service}.serviceConfig = { + UMask = lib.mkForce "0007"; + }; + + # open firewall + networking.firewall.allowedTCPPorts = [cfg.port 8123 21063 21064]; + networking.firewall.allowedUDPPorts = [5353]; + + # add to caddy for reverse proxy + services.caddy.virtualHosts."${cfg.url}" = { + serverAliases = ["${service}.${homelab.public_domain}"]; + extraConfig = '' + tls internal + reverse_proxy 127.0.0.1:${toString cfg.port} { + } + ''; + }; + + # backups valhiem with borg! + services.borgbackup.jobs.${service} = { + archiveBaseName = service; + repo = cfg.backup_repo; + paths = lib.flatten ( + lib.attrValues ( + lib.mapAttrs (_: srv: + [srv.data_dir] + ++ ( + if builtins.hasAttr "db_dump" srv + then [srv.db_dump] + else [] + )) + servers + ) + ); + compression = "auto,zstd"; + #preHook = "systemctl start mysql-backup.service"; + postHook = '' + ${pkgs.sshpass}/bin/sshpass -f ${sec."lifeboat_rsync".path} \ + ${pkgs.rsync}/bin/rsync -aHAX --partial --numeric-ids --info=progress2,name1,stats2 --delete \ + -e "ssh -o StrictHostKeyChecking=no" \ + ${toString cfg.backup_repo} \ + ${cfg.offsite_user}@${cfg.offsite_target}::${cfg.offsite_dir} + ''; + startAt = "*-*-* *:00:00"; + group = "archives"; + encryption.mode = "repokey-blake2"; + encryption.passCommand = "cat ${config.sops.secrets."borg_passwd".path}"; + extraArgs = ["--verbose" "--show-rc" "--umask" "0007"]; + extraCreateArgs = ["--list" "--stats" "--filter" "AME"]; + prune.keep = { + within = "1d"; # Keep all archives from the last day + hourly = 24; + daily = 7; + weekly = 12; + monthly = -1; # Keep at least one archive for each month + }; + }; + }; +}