server-configs/machines/gerd/services/wger/default.nix

193 lines
5.5 KiB
Nix
Raw Normal View History

2024-12-03 07:45:11 +00:00
{ config, pkgs, ... }:
let
svc_domain = "wger.${config.mine.shared.settings.domain}";
port = 8000;
wger_user = "wger";
statedir = config.mine.zfsMounts."rpool/safe/svcs/wger";
wgerpkgs = pkgs.callPackage ./wgerpkg/default.nix {};
wger_settings = {
2024-12-03 21:08:25 +00:00
EMAIL_FROM = "wger Workout Manager <wger@${config.mine.shared.settings.domain}>";
2024-12-03 07:45:11 +00:00
ALLOW_REGISTRATION = true;
ALLOW_GUEST_USERS = true;
ALLOW_UPLOAD_VIDEOS = false;
MIN_ACCOUNT_AGE_TO_TRUST = 21;
EXERCISE_CACHE_TTL = 3600;
};
django_settings = rec {
# enable debug for now, otherwise it tries
# to create a CACHE folder/file in the CWD.
# and if I fix that, then static content no
# longer wants to load.
DEBUG = true;
2024-12-03 07:45:11 +00:00
DATABASES.default = {
ENGINE = "django.db.backends.postgresql";
NAME = "wger";
USER = "wger";
PASSWORD = "";
HOST = "/run/postgresql";
PORT = "";
};
2024-12-03 21:08:25 +00:00
ADMINS = [["admin" "admin@${config.mine.shared.settings.domain}"]];
2024-12-03 07:45:11 +00:00
MANAGERS = ADMINS;
2024-12-03 21:07:03 +00:00
TIME_ZONE = "Europe/Copenhagen";
2024-12-03 07:45:11 +00:00
2024-12-03 21:07:03 +00:00
SECRET_KEY = "$SECRET_KEY";
2024-12-03 07:45:11 +00:00
2024-12-03 21:07:03 +00:00
SITE_URL = "https://${svc_domain}";
2024-12-03 07:45:11 +00:00
MEDIA_ROOT = "${statedir}/media";
MEDIA_URL = "/media/";
2024-12-03 21:07:03 +00:00
# EMAIL
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend";
EMAIL_HOST = config.mine.shared.settings.mail.domain_smtp;
EMAIL_PORT = config.mine.shared.settings.mail.ports.submissions;
EMAIL_USE_SSL = true;
EMAIL_HOST_USER = "wger";
EMAIL_HOST_PASSWORD = "$EMAIL_HOST_PASSWORD";
EMAIL_FROM_ADDRESS = wger_settings.EMAIL_FROM;
2024-12-03 07:45:11 +00:00
EMAIL_PAGE_DOMAIN = SITE_URL;
2024-12-03 21:07:03 +00:00
# setup allowed hosts
2024-12-03 07:45:11 +00:00
CSRF_TRUSTED_ORIGINS = [ "https://${svc_domain}" ];
ALLOWED_HOSTS = [ svc_domain ];
2024-12-03 21:07:03 +00:00
# disable recaptcha
2024-12-03 07:45:11 +00:00
RECAPTCHA_PUBLIC_KEY = "";
RECAPTCHA_PRIVATE_KEY = "";
USE_RECAPTCHA = false;
};
wger_settings_file = pkgs.writeText "settings.json" (builtins.toJSON wger_settings);
django_settings_file = pkgs.writeText "settings.json" (builtins.toJSON django_settings);
settingsFile = pkgs.writeText "settings.py" ''
from wger.settings_global import *
import json
2024-12-03 21:07:03 +00:00
import os
2024-12-03 07:45:11 +00:00
with open("${django_settings_file}") as f:
2024-12-03 21:07:03 +00:00
for k, v in json.load(f).items():
if isinstance(v, str) and v.startswith("$"):
v = os.environ[v[1:]]
globals()[k] = v
2024-12-03 07:45:11 +00:00
with open("${wger_settings_file}") as f:
2024-12-03 21:07:03 +00:00
for k, v in json.load(f).items():
if isinstance(v, str) and v.startswith("$"):
v = os.environ[v[1:]]
WGER_SETTINGS[k] = v
2024-12-03 07:45:11 +00:00
'';
in {
2024-12-03 21:07:03 +00:00
# main service
2024-12-03 07:45:11 +00:00
systemd.services.wger = {
description = "wger fitness";
wantedBy = [ "multi-user.target" ];
after = [ "networking.target" ];
2024-12-03 21:07:03 +00:00
2024-12-03 07:45:11 +00:00
script = ''
2024-12-03 21:07:03 +00:00
# initial setup
2024-12-03 07:45:11 +00:00
${wgerpkgs}/bin/wger migrate-db -s ${settingsFile} || true
2024-12-03 21:07:03 +00:00
${wgerpkgs}/bin/wger load-fixtures -s ${settingsFile} || true
2024-12-03 07:45:11 +00:00
# run server
${wgerpkgs}/bin/wger start -s ${settingsFile}
'';
serviceConfig = {
2024-12-03 21:07:03 +00:00
EnvironmentFile = config.age.secrets.wger-env.path;
# ensure it does not try to create `/CACHE`
PrivateTmp = "yes";
WorkingDirectory = "/tmp";
2024-12-03 07:45:11 +00:00
User = "wger";
Group = "wger";
};
};
2024-12-03 21:07:03 +00:00
# periodic keep up-to-date
systemd.timers."wger-housekeeping" = {
wantedBy = [ "timers.target" ];
timerConfig.OnCalendar = "daily";
};
systemd.services."wger-housekeeping" = {
after = [ "wger.service" ];
requires = [ "wger.service" ];
script = ''
WGER_SETTINGS=${settingsFile} ${wgerpkgs}/bin/manage sync-exercises || true
WGER_SETTINGS=${settingsFile} ${wgerpkgs}/bin/manage download-exercise-images || true
WGER_SETTINGS=${settingsFile} ${wgerpkgs}/bin/manage download-exercise-videos || true
# WGER_SETTINGS=${settingsFile} ${wgerpkgs}/bin/manage sync-ingredients || true
2024-12-03 21:07:03 +00:00
${wgerpkgs}/bin/wger load-online-fixtures -s ${settingsFile} || true
WGER_SETTINGS=${settingsFile} ${wgerpkgs}/bin/manage exercises-health-check || true
'';
serviceConfig = {
EnvironmentFile = config.age.secrets.wger-env.path;
# Type = "oneshot";
User = "wger";
Group = "wger";
};
2024-12-03 07:45:11 +00:00
};
services.postgresql = {
ensureDatabases = [ wger_user ];
ensureUsers = [{
name = wger_user;
ensureDBOwnership = true;
}];
};
2024-12-03 21:07:03 +00:00
# setup users
users.users."${wger_user}"= {
uid = 738;
isSystemUser = true;
group = wger_user;
};
users.groups."${wger_user}".gid = 738;
# nginx
services.nginx.virtualHosts."${svc_domain}" = config.mine.shared.lib.authelia.mkProtectedWebsite {
2024-12-03 07:45:11 +00:00
forceSSL = true;
enableACME = true;
locations."/" = config.mine.shared.lib.authelia.mkProtectedLocation {
proxyPass = "http://localhost:${builtins.toString port}";
};
locations."/api/v2/register" = config.mine.shared.lib.authelia.mkProtectedLocation {
proxyPass = "http://localhost:${builtins.toString port}";
};
locations."/static".proxyPass = "http://localhost:${builtins.toString port}";
locations."/media".proxyPass = "http://localhost:${builtins.toString port}";
locations."/api".proxyPass = "http://localhost:${builtins.toString port}";
};
2024-12-03 21:07:03 +00:00
# metadata
mine.shared.meta.wger = {
name = "Wger";
description = "We host Wger, which is a FLOSS fitness/workout/nutrition and weight tracker, with FLOSS apps, read more [here](https://wger.de/).";
url = "https://${svc_domain}";
package = let
pkg = wgerpkgs;
in {
name = pkg.pname;
version = pkg.version;
meta = pkg.meta;
};
};
2024-12-03 07:45:11 +00:00
}