1
0
Fork 0
nixos-config/nixos/roles/foundryvtt.nix

80 lines
2.5 KiB
Nix
Raw Normal View History

2021-01-23 09:17:30 +00:00
{ pkgs, lib, config, ... }:
let
name = "foundryvtt";
stateDir = "/var/lib/${name}";
2021-06-05 14:08:19 +00:00
dataDir = "${stateDir}/data";
configFile = "${dataDir}/Config/options.json";
config = {
port = 3333;
upnp = false;
hostname = "rpg.maralorn.de";
routePrefix = null;
proxySSL = true;
proxyPort = null;
minifyStaticFiles = true;
updateChannel = "release";
};
declarativeConfigFile = builtins.toFile "foundry-options.json" (builtins.toJSON config);
2021-05-18 14:33:28 +00:00
in
{
2021-01-23 09:17:30 +00:00
config = {
systemd.services.${name} = {
2021-06-05 15:01:12 +00:00
wantedBy = [ "multi-user.target" ];
2021-01-23 09:17:30 +00:00
description = "Foundryvtt server";
2021-06-05 15:01:12 +00:00
preStart = ''
mkdir -p ${dataDir}
if [[ -f "${configFile}" ]]; then
tempfile=$(mktemp)
cp "${configFile}" "$tempfile"
${pkgs.jq}/bin/jq ".[0] * .[1]" -s "$tempfile" "${declarativeConfigFile}" > "${configFile}"
else
cp "${declarativeConfigFile}" "${configFile}"
fi
if [[ ! -f "${stateDir}/app/resources/app/main.js" ]]; then
echo "No ${name} app found. Please download zip from foundryvtt.com and extract to ${stateDir}/app"
fi
'';
2021-01-23 09:17:30 +00:00
serviceConfig = {
2021-06-05 14:08:19 +00:00
StateDirectory = "${name}";
2021-01-23 09:17:30 +00:00
WorkingDirectory = stateDir;
2021-06-05 14:08:19 +00:00
DynamicUser = true;
2021-06-05 11:09:47 +00:00
Restart = "always";
2021-07-13 13:59:49 +00:00
Environment = "HOME=${stateDir}";
2021-06-05 15:01:12 +00:00
ExecStart = "${pkgs.nodejs}/bin/node ${stateDir}/app/resources/app/main.js --dataPath=\"${dataDir}\"";
2021-01-23 09:17:30 +00:00
};
};
services = {
nginx = {
virtualHosts = {
2021-06-05 14:08:19 +00:00
${config.hostname} = {
2021-01-23 09:17:30 +00:00
extraConfig = ''
client_max_body_size 300M;
2021-06-05 14:08:19 +00:00
proxy_set_header Host $host;
2021-01-23 09:17:30 +00:00
'';
forceSSL = true;
enableACME = true;
2022-02-06 23:52:09 +00:00
locations = {
"/rules/" = {
2022-02-27 19:21:31 +00:00
alias = "${pkgs.fetchzip (import ./5etools-url.nix // { stripRoot = false;})}/";
2022-02-06 23:52:09 +00:00
index = "index.html";
};
"/" = {
proxyPass = "http://[::1]:${toString config.port}";
proxyWebsockets = true;
extraConfig = ''
2022-02-07 01:45:32 +00:00
if ($request_uri ~ ^/rules$) {
return 301 /rules/;
}
2022-02-06 23:52:09 +00:00
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
'';
};
2021-01-23 09:17:30 +00:00
};
};
};
};
};
};
}