Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

feat: optional module-managed nginx virtualHost

gerrydoro23052e9d2d1e
diff --git a/docs/NIX.md b/docs/NIX.md
index a0c961e95..9b1f2fcee 100644
--- a/docs/NIX.md
+++ b/docs/NIX.md
@@ -33,15 +33,17 @@ managed by your NixOS configuration. This document covers both.
- `systemd.services.opengym-media` — a one-shot media downloader (only in runtime-media mode),
- `systemd.services.opengym-mcp` — the MCP server, **only if `mcp.enable` is set**,
- read-only options exposing the built frontend and media directories so *your* web server can
- serve them,
+ serve them (or `nginx.enable = true` for the module to manage one — see §4),
- `environment` / `environmentFile` — escape hatches to pass extra environment variables to the
API (for variables openGym doesn't know about yet). See [Module options reference](#6-module-options-reference).
-The module deliberately **does not own a web server**: it forces `services.nginx.enable = false`
-and hands you the `web.root`, `media.imageRoot` and `media.gifRoot` store paths to wire into your
-own reverse proxy (Caddy, nginx, Traefik, …). openGym needs everything on **one origin** (passkeys
-require it), and NixOS deployments already run a proxy — so the module integrates openGym into
-*yours* instead of re-implementing nginx itself. See §4.
+The module **does not own a web server by default**: it hands you the `web.root`, `media.imageRoot`
+and `media.gifRoot` store paths to wire into your own reverse proxy (Caddy, nginx, Traefik, …).
+openGym needs everything on **one origin** (passkeys require it), and NixOS deployments already run
+a proxy — so the module integrates openGym into *yours* instead of re-implementing a web server
+itself. See §4. If you'd rather have openGym's web server managed for you, set
+`services.opengym.nginx.enable = true` and the module configures an nginx virtualHost for openGym
+(only then is `services.nginx` enabled; on the Caddy-native default it is force-disabled).
### Configuration flow
@@ -156,37 +158,38 @@ catch-all `handle` that serves the SPA.
### nginx
-```nginx
-server {
- listen 443 ssl;
- server_name gym.example.com;
- root /nix/store/…-opengym-frontend/share/opengym; # ← config.services.opengym.web.root
-
- location / {
- try_files $uri $uri/ /index.html;
- }
-
- location /api/ {
- proxy_pass http://127.0.0.1:3000; # ← the apiPort you configured
- proxy_http_version 1.1;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $remote_addr;
- proxy_set_header X-Forwarded-Proto $scheme;
- }
-
- location /img/ { alias /nix/store/…-opengym-media/images/; } # or /var/lib/opengym/media/img/
- location /gif/ { alias /nix/store/…-opengym-media/videos/; } # or /var/lib/opengym/media/gif/
+Two ways to use nginx. If you don't already run an nginx config you care about, let the module
+manage one:
+
+```nix
+{
+ services.opengym = {
+ enable = true;
+ rpId = "gym.example.com";
+ origin = "https://gym.example.com";
+ nginx = {
+ enable = true; # module writes the virtualHost below for you
+ # hostName = "gym.example.com"; # defaults to rpId
+ # enableACME = true; # Let's Encrypt + force TLS (requires security.acme.acceptTerms = true)
+ };
+ };
+ security.acme.acceptTerms = true; # only needed with enableACME = true
}
```
-Remember: because the module forces `services.nginx.enable = false`, if you want *NixOS's* nginx
-to serve openGym you must re-enable it after importing the module:
+That renders a `services.nginx.virtualHosts."gym.example.com"` with the SPA root + fallback, the
+`/api` proxy, `/img` and `/gif` aliases, gzip/optimisation settings, and — when `enableACME` — TLS.
+
+Otherwise, write the virtualHost yourself (e.g. to mix with your existing sites). Remember the
+module's Caddy-native default is `services.nginx.enable = false`; you must switch that back on
+*before* adding your own vhost — the module will do it for you if you set `nginx.enable = true`:
```nix
-{ services.nginx.enable = lib.mkForce true; }
+{ services.nginx.enable = lib.mkTrue; } # or set services.opengym.nginx.enable = true
```
+Then a minimal handwritten config:
+
### Passkey constraint (read before choosing a hostname)
WebAuthn passkeys are bound to an exact hostname and require HTTPS. Configure the module with the
@@ -331,7 +334,8 @@ variables, so entries there can still override).
| `systemd.services.opengym-api` | always | `Restart=on-failure`, runs as `opengym`, hardened |
| `systemd.services.opengym-media` | only when `media.fetchAtBuild = false` | one-shot (`Type=oneshot`, `RemainAfterExit`) media downloader |
| `systemd.services.opengym-mcp` | only when `mcp.enable = true` | stdio bridge, runs as `opengym` |
-| `services.nginx.enable = false` | always | force-disabled — bring your own web server |
+| `services.nginx` vhost | only when `nginx.enable = true` | managed virtualHost (SPA + `/api` + `/img` + `/gif`) |
+| `services.nginx.enable = false` | only when `nginx.enable = false` | force-disabled — bring your own web server |
<span id="systemd-hardening"></span>
The API unit is restricted: `ProtectSystem=strict`, `ProtectHome`, `PrivateTmp`,
@@ -368,6 +372,14 @@ for other modules to consume — setting them yourself is an error.
| `environment` | `attrsOf str` | `{ }` | Extra environment variables set on the `opengym-api` unit — the escape hatch for variables the module doesn't manage yet (newly added to the project). On a conflict with a variable the module derives from its own options, the module's value wins. Mutually exclusive with `environmentFile`. |
| `environmentFile` | `str` | `""` | Path to an env file (`KEY=VALUE` per line) read by the `opengym-api` unit via systemd `EnvironmentFile=`. systemd applies it *after* the declarative environment, so entries in the file may override module-managed variables. Mutually exclusive with `environment`. |
+### `nginx.*` suboptions
+
+| Option | Type | Default | Description |
+| --- | --- | --- | --- |
+| `nginx.enable` | `bool` | `false` | Have the module configure an nginx virtualHost for openGym via `services.nginx` (SPA root + fallback, `/api` proxy, `/img` + `/gif` aliases, gzip/optimisation). When `false` (Caddy-native default) the module force-disables `services.nginx`. |
+| `nginx.hostName` | `str` | `"${rpId}"` | Server name the virtualHost listens on. |
+| `nginx.enableACME` | `bool` | `false` | Serve over TLS: `enableACME` + `forceSSL` on the vhost. Requires `security.acme.acceptTerms = true`. |
+
### `media.*` suboptions
| Option | Type | Default | Description |
@@ -436,7 +448,7 @@ routine update — see the passkey caveat in §4.
| No passkey prompt / "verification failed" | `rpId`/`origin` don't match the address bar. Ask the server what it loaded: `journalctl -u opengym-api -b \| grep 'gym-api on'`. See §4 and `docs/SELF_HOSTING.md`. |
| App loads but exercises show no image/animation | The `/img` and `/gif` routes aren't mapped to `imageRoot`/`gifRoot` in your web server, or runtime media hasn't downloaded yet — check `systemctl status opengym-media` / `journalctl -u opengym-media`. |
| `/api` returns 502/404 | Your proxy's `apiPort` (`services.opengym.apiPort`) differs from what `reverse_proxy`/`proxy_pass` targets. |
-| `nginx` is silently gone after enabling the module | Expected — the module sets `services.nginx.enable = false`. Re-enable with `lib.mkForce true` if you manage openGym via nginx. |
+| `nginx` is silently gone after enabling the module | Expected on the Caddy-native default (`nginx.enable = false`): the module sets `services.nginx.enable = false`. Set `services.opengym.nginx.enable = true` to have the module configure nginx for openGym (or `lib.mkForce true` to manage it yourself). |
| Service won't start | `journalctl -u opengym-api -xe`. Common: custom `dataDir` not created/owned correctly (the tmpfiles rule should handle it; check `systemd-tmpfiles --create`). |
| Eval fails: `environment and environmentFile are mutually exclusive` | Pick one. Set extra vars either as a Nix attrset (`environment`) or in a file (`environmentFile`), never both. |
| You expected a binary cache | The flake builds from source; override `services.opengym.package` (or `packages.*` via a cache/overlay) to substitute binaries. |
diff --git a/flake.nix b/flake.nix
index 913f60c35..1b9aaa4ad 100644
--- a/flake.nix
+++ b/flake.nix
@@ -44,6 +44,21 @@
nginxEnabled = testConfig.config.services.nginx.enable;
+ # Variant with the optional managed nginx vhost switched on.
+ nginxTestConfig = nixpkgs.lib.nixosSystem {
+ inherit system;
+ modules = [
+ opengymNixOSModule
+ {
+ services.opengym = {
+ enable = true;
+ nginx.enable = true;
+ };
+ system.stateVersion = "25.05";
+ }
+ ];
+ };
+
check = pkgs.runCommand "opengym-module-check" { } ''
set -euo pipefail
# Verify the module evaluated correctly
@@ -58,8 +73,14 @@
# Verify extra environment is applied to the API unit
echo "opengym-api env MY_FLAG = ${if testConfig.config.systemd.services.opengym-api.environment ? MY_FLAG then testConfig.config.systemd.services.opengym-api.environment.MY_FLAG else "missing"}"
- # Verify nginx is disabled (system uses Caddy)
- echo "services.nginx.enable = ${if nginxEnabled then "true" else "false"}"
+ # Verify nginx is disabled by default (system uses Caddy)
+ echo "services.nginx.enable (default) = ${if nginxEnabled then "true" else "false"}"
+
+ # Verify the optional managed nginx vhost gets configured
+ echo "services.nginx.enable (nginx.enable) = ${if nginxTestConfig.config.services.nginx.enable then "true" else "false"}"
+ echo "nginx vhost for 'localhost' = ${if nginxTestConfig.config.services.nginx.virtualHosts ? localhost then "true" else "false"}"
+ echo "nginx /api proxyPass = ${nginxTestConfig.config.services.nginx.virtualHosts."localhost".locations."/api/".proxyPass or "missing"}"
+ echo "nginx /img alias = ${nginxTestConfig.config.services.nginx.virtualHosts."localhost".locations."/img/".alias or "missing"}"
# Verify frontend/media store paths are exposed for a web server
echo "services.opengym.web.root = ${cfg.web.root}"
diff --git a/nix/opengym.nix b/nix/opengym.nix
index 04276cc72..d632a5e54 100644
--- a/nix/opengym.nix
+++ b/nix/opengym.nix
@@ -158,6 +158,25 @@ in
'';
};
+ nginx = {
+ enable = lib.mkEnableOption "an nginx virtualHost for openGym, managed via services.nginx";
+
+ hostName = lib.mkOption {
+ type = lib.types.str;
+ default = cfg.rpId;
+ description = "Server name the nginx virtualHost listens on (defaults to rpId).";
+ };
+
+ enableACME = lib.mkOption {
+ type = lib.types.bool;
+ default = false;
+ description = ''
+ Serve openGym over TLS with a Let's Encrypt certificate (services.nginx enableACME +
+ forceSSL). Also requires security.acme.acceptTerms = true in your configuration.
+ '';
+ };
+ };
+
media = {
fetchAtBuild = lib.mkOption {
type = lib.types.bool;
@@ -209,97 +228,147 @@ in
assertion = cfg.environment == { } || cfg.environmentFile == "";
message = "services.opengym.environment and services.opengym.environmentFile are mutually exclusive; use only one.";
}
+ {
+ assertion = !cfg.nginx.enable || cfg.nginx.hostName != "";
+ message = "services.opengym.nginx.hostName must be set when services.opengym.nginx.enable is true.";
+ }
+ {
+ assertion = !cfg.nginx.enableACME || cfg.nginx.enable;
+ message = "services.opengym.nginx.enable must be true to use services.opengym.nginx.enableACME.";
+ }
];
}
- (lib.mkIf cfg.enable {
-
- users.users.opengym = {
- isSystemUser = true;
- group = "opengym";
- description = "openGym service user";
- };
-
- users.groups.opengym = { };
-
- systemd.tmpfiles.rules = [
- "d ${cfg.dataDir} 0750 opengym opengym - -"
- "d ${cfg.media.dataDir} 0755 opengym opengym - -"
- ];
-
- systemd.services.opengym-api = {
- description = "openGym API server";
- after = [ "network.target" ];
- wantedBy = [ "multi-user.target" ];
- environment = cfg.environment // apiEnv;
- serviceConfig = {
- ExecStart = "${packages.opengym-api}/bin/opengym-api";
- User = "opengym";
- Group = "opengym";
- StateDirectory = "opengym";
- StateDirectoryMode = "0750";
- Restart = "on-failure";
- RestartSec = 5;
- EnvironmentFile = lib.mkIf (cfg.environmentFile != "") cfg.environmentFile;
-
- NoNewPrivileges = true;
- ProtectSystem = "strict";
- ProtectHome = true;
- PrivateTmp = true;
- PrivateDevices = true;
- ProtectKernelTunables = true;
- ProtectKernelModules = true;
- ProtectControlGroups = true;
- RestrictNamespaces = true;
- RestrictSUIDSGID = true;
- LockPersonality = true;
- SystemCallFilter = [
- "@system-service"
- "~@privileged"
- ];
- ReadWritePaths = [ cfg.dataDir ];
- };
- };
-
- systemd.services.opengym-media = lib.mkIf (!cfg.media.fetchAtBuild) {
- description = "Download exercise media for openGym";
- after = [ "network-online.target" ];
- wants = [ "network-online.target" ];
- wantedBy = [ "multi-user.target" ];
- serviceConfig = {
- ExecStart = "${packages.opengym-fetch-media}/bin/opengym-fetch-media ${cfg.media.dataDir}";
- Type = "oneshot";
- RemainAfterExit = true;
- User = "opengym";
- Group = "opengym";
- StateDirectory = "opengym";
- };
- };
-
- services.nginx.enable = lib.mkForce false;
-
- services.opengym.media = {
- imageRoot = mediaRoot;
- gifRoot = mediaGifRoot;
- };
-
- services.opengym.web.root = "${packages.opengym-frontend}/share/opengym";
-
- systemd.services.opengym-mcp = lib.mkIf cfg.mcp.enable {
- description = "openGym MCP server";
- after = [ "network.target" ];
- wantedBy = [ "multi-user.target" ];
- environment = {
- DATA_DIR = cfg.dataDir;
- };
- serviceConfig = {
- ExecStart = "${packages.opengym-mcp}/bin/opengym-mcp";
- StandardInput = "null";
- StandardOutput = "journal";
- User = "opengym";
- Group = "opengym";
- Restart = "on-failure";
- };
- };
- })
+ (lib.mkIf cfg.enable (
+ lib.mkMerge [
+ {
+ users.users.opengym = {
+ isSystemUser = true;
+ group = "opengym";
+ description = "openGym service user";
+ };
+
+ users.groups.opengym = { };
+
+ systemd.tmpfiles.rules = [
+ "d ${cfg.dataDir} 0750 opengym opengym - -"
+ "d ${cfg.media.dataDir} 0755 opengym opengym - -"
+ ];
+
+ systemd.services.opengym-api = {
+ description = "openGym API server";
+ after = [ "network.target" ];
+ wantedBy = [ "multi-user.target" ];
+ environment = cfg.environment // apiEnv;
+ serviceConfig = {
+ ExecStart = "${packages.opengym-api}/bin/opengym-api";
+ User = "opengym";
+ Group = "opengym";
+ StateDirectory = "opengym";
+ StateDirectoryMode = "0750";
+ Restart = "on-failure";
+ RestartSec = 5;
+ EnvironmentFile = lib.mkIf (cfg.environmentFile != "") cfg.environmentFile;
+
+ NoNewPrivileges = true;
+ ProtectSystem = "strict";
+ ProtectHome = true;
+ PrivateTmp = true;
+ PrivateDevices = true;
+ ProtectKernelTunables = true;
+ ProtectKernelModules = true;
+ ProtectControlGroups = true;
+ RestrictNamespaces = true;
+ RestrictSUIDSGID = true;
+ LockPersonality = true;
+ SystemCallFilter = [
+ "@system-service"
+ "~@privileged"
+ ];
+ ReadWritePaths = [ cfg.dataDir ];
+ };
+ };
+
+ systemd.services.opengym-media = lib.mkIf (!cfg.media.fetchAtBuild) {
+ description = "Download exercise media for openGym";
+ after = [ "network-online.target" ];
+ wants = [ "network-online.target" ];
+ wantedBy = [ "multi-user.target" ];
+ serviceConfig = {
+ ExecStart = "${packages.opengym-fetch-media}/bin/opengym-fetch-media ${cfg.media.dataDir}";
+ Type = "oneshot";
+ RemainAfterExit = true;
+ User = "opengym";
+ Group = "opengym";
+ StateDirectory = "opengym";
+ };
+ };
+
+ services.opengym.media = {
+ imageRoot = mediaRoot;
+ gifRoot = mediaGifRoot;
+ };
+
+ services.opengym.web.root = "${packages.opengym-frontend}/share/opengym";
+
+ systemd.services.opengym-mcp = lib.mkIf cfg.mcp.enable {
+ description = "openGym MCP server";
+ after = [ "network.target" ];
+ wantedBy = [ "multi-user.target" ];
+ environment = {
+ DATA_DIR = cfg.dataDir;
+ };
+ serviceConfig = {
+ ExecStart = "${packages.opengym-mcp}/bin/opengym-mcp";
+ StandardInput = "null";
+ StandardOutput = "journal";
+ User = "opengym";
+ Group = "opengym";
+ Restart = "on-failure";
+ };
+ };
+ }
+ # Optional: let the module configure an nginx virtualHost for openGym itself.
+ (lib.mkIf cfg.nginx.enable {
+ services.nginx = {
+ enable = true;
+ recommendedOptimisation = true;
+ recommendedGzipSettings = true;
+ virtualHosts.${cfg.nginx.hostName} =
+ {
+ root = cfg.web.root;
+ locations = {
+ "/" = {
+ tryFiles = "$uri $uri/ /index.html";
+ };
+ "/api/" = {
+ proxyPass = "http://127.0.0.1:${toString cfg.apiPort};";
+ extraConfig = ''
+ proxy_http_version 1.1;
+ proxy_set_header Host $host;
+ proxy_set_header X-Real-IP $remote_addr;
+ proxy_set_header X-Forwarded-For $remote_addr;
+ proxy_set_header X-Forwarded-Proto $scheme;
+ '';
+ };
+ "/img/" = {
+ alias = "${cfg.media.imageRoot}/";
+ };
+ "/gif/" = {
+ alias = "${cfg.media.gifRoot}/";
+ };
+ };
+ }
+ // lib.optionalAttrs cfg.nginx.enableACME {
+ enableACME = true;
+ forceSSL = true;
+ };
+ };
+ })
+ # Caddy-native default: this module owns no web server of its own.
+ (lib.mkIf (!cfg.nginx.enable) {
+ services.nginx.enable = lib.mkForce false;
+ })
+ ]
+ ))
];
}