From 1badb122cb5b8bc402b940cbcaff24d3200d92e3 Mon Sep 17 00:00:00 2001 From: Dmitry Voronin Date: Mon, 2 Dec 2024 02:29:53 +0300 Subject: [PATCH] Terraria: Add server container. --- container/Terraria.nix | 60 ++++++++++++++++++++++++++++ host/x86_64-linux/home/Container.nix | 1 + host/x86_64-linux/home/Network.nix | 7 +++- lib/Container.nix | 11 ++--- 4 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 container/Terraria.nix diff --git a/container/Terraria.nix b/container/Terraria.nix new file mode 100644 index 00000000..7636c5d2 --- /dev/null +++ b/container/Terraria.nix @@ -0,0 +1,60 @@ +{ + config, + container, + lib, + ... +}: let + cfg = config.container.module.terraria; +in { + options.container.module.terraria = { + enable = lib.mkEnableOption "the Terraria server."; + address = lib.mkOption { + default = "10.1.0.77"; + type = lib.types.str; + }; + port = lib.mkOption { + default = 22777; + type = lib.types.int; + }; + storage = lib.mkOption { + default = "${config.container.storage}/terraria"; + type = lib.types.str; + }; + }; + + config = lib.mkIf cfg.enable { + systemd.tmpfiles.rules = container.mkContainerDir cfg [ + "data" + ]; + + containers.terraria = container.mkContainer cfg { + bindMounts = { + "/var/lib/terraria" = { + hostPath = "${cfg.storage}/data"; + isReadOnly = false; + }; + }; + + config = { pkgs, ... }: container.mkContainerConfig cfg { + # NOTE: Admin with `tmux -S /var/lib/terraria/terraria.sock attach-session -t 0` + environment.systemPackages = with pkgs; [ tmux ]; + + services.terraria = let + dataDir = "/var/lib/terraria"; + in { + inherit (cfg) port; + inherit dataDir; + enable = true; + autoCreatedWorldSize = "large"; + maxPlayers = 4; + messageOfTheDay = "<3"; + noUPnP = false; + openFirewall = false; + password = "mishadima143"; + secure = false; + worldPath = "${dataDir}/.local/share/Terraria/Worlds/Together.wld"; + }; + }; + }; + }; +} diff --git a/host/x86_64-linux/home/Container.nix b/host/x86_64-linux/home/Container.nix index 1b64a9aa..b1876163 100644 --- a/host/x86_64-linux/home/Container.nix +++ b/host/x86_64-linux/home/Container.nix @@ -32,6 +32,7 @@ search.enable = true; status.enable = true; stock.enable = true; + terraria.enable = true; vpn.enable = true; watch.enable = true; yt.enable = true; diff --git a/host/x86_64-linux/home/Network.nix b/host/x86_64-linux/home/Network.nix index 49be7867..9e86fabf 100644 --- a/host/x86_64-linux/home/Network.nix +++ b/host/x86_64-linux/home/Network.nix @@ -216,10 +216,15 @@ in { + (mkForward internal cfg.git.portSsh cfg.git.address cfg.git.portSsh tcp) # Print serivce. - + (mkForward internal cfg.print.port cfg.print.address cfg.print.port tcp); + + (mkForward internal cfg.print.port cfg.print.address cfg.print.port tcp) + + # Terraria server. + + (mkForward external cfg.terraria.port cfg.terraria.address cfg.terraria.port tcp) + + (mkForward internal cfg.terraria.port cfg.terraria.address cfg.terraria.port tcp) # SSH access from WAN. # + (mkForward external 22143 config.container.host 22143 tcp) + ; }; }; } diff --git a/lib/Container.nix b/lib/Container.nix index 13892794..bfb04368 100644 --- a/lib/Container.nix +++ b/lib/Container.nix @@ -2,7 +2,6 @@ config, const, lib, - pkgs, util, ... }: { @@ -28,13 +27,15 @@ mkContainerConfig = cfg: extra: lib.recursiveUpdate { boot.isContainer = true; - # HACK: Do not evaluate nixpkgs inside the container. Use host's instead. - # nixpkgs.pkgs = lib.mkForce pkgs; - nixpkgs.pkgs = lib.mkForce pkgs; - # Release version. system.stateVersion = const.stateVersion; + # Nix is fucking annoying. + nixpkgs.config = { + allowUnfree = true; + allowInsecurePredicate = x: true; + }; + # Allow passwordless login as root. users = { users.root.password = "";