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

86 lines
3.1 KiB
Nix
Raw Normal View History

2022-03-08 04:53:07 +00:00
{pkgs, ...}: let
2021-01-23 09:17:30 +00:00
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);
2022-03-08 01:42:46 +00:00
in {
2021-01-23 09:17:30 +00:00
config = {
2022-03-08 02:19:09 +00:00
systemd.services."${name}" = {
2022-03-08 01:42:46 +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 = {
2022-03-08 02:19:09 +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 = {
"/" = {
proxyPass = "http://[::1]:${toString config.port}";
proxyWebsockets = true;
2022-03-27 19:05:20 +00:00
extraConfig = ''
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
if ($query_string ~ "pw=([A-Za-z]*)") {
2022-03-27 21:37:06 +00:00
add_header Set-Cookie "password=$1; path=/; Max-Age=${toString (365 * 24 * 60 * 60)}; Secure";
2022-03-27 19:05:20 +00:00
return 303 /;
}
if ($http_cookie !~ "password=${pkgs.privateValue "" "foundry-pw"}") {
return 303 /logout;
}
'';
2022-02-06 23:52:09 +00:00
};
2022-03-27 19:05:20 +00:00
"/login".extraConfig = ''
2022-04-19 10:50:32 +00:00
more_set_headers 'Content-Type: text/html';
2022-03-27 19:05:20 +00:00
return 200 '<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body><form style="text-align: center; margin: 250px auto; width: 500px;" action="/" method="get"><label for="pw">Passwort: </label><input type="password" name="pw"><input type="submit" value="login"></form></html></body>';
'';
"/logout".extraConfig = ''
add_header Set-Cookie 'password=""; Max-Age=0';
return 303 /login;
'';
2021-01-23 09:17:30 +00:00
};
};
};
};
};
};
}