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: harden the opengym-mcp unit the same way as the API

gerrydorocea1f1a2dc54
diff --git a/docs/NIX.md b/docs/NIX.md
index d7caf8f1a..6abd1290e 100644
--- a/docs/NIX.md
+++ b/docs/NIX.md
@@ -336,7 +336,7 @@ variables, so entries there can still override).
| tmpfiles rules | always | `dataDir` (`0750`), `media.dataDir` (`0755`) |
| `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` |
+| `systemd.services.opengym-mcp` | only when `mcp.enable = true` | stdio bridge, runs as `opengym`, same sandbox as the API |
| `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 |
@@ -345,7 +345,9 @@ The API unit is restricted: `ProtectSystem=strict`, `ProtectHome`, `PrivateTmp`,
`PrivateDevices`, `ProtectKernelTunables/Modules/ControlGroups`, `RestrictNamespaces`,
`RestrictSUIDSGID`, `NoNewPrivileges`, `LockPersonality`, a system-call allowlist
(`@system-service`, `~@privileged`), and `ReadWritePaths` limited to `dataDir` — the API can only
-write to your data directory.
+write to your data directory. The MCP unit is a **stdio bridge that only reads** the same files, so
+it runs under the exact same sandbox (plus `StandardInput=null` / `StandardOutput=journal`, since a
+systemd-spawned MCP server has no client on stdin).
## 6. Module options reference
diff --git a/nix/opengym.nix b/nix/opengym.nix
index 08c47cca0..1f7323fcb 100644
--- a/nix/opengym.nix
+++ b/nix/opengym.nix
@@ -40,6 +40,26 @@ let
mediaGifRoot =
if cfg.media.fetchAtBuild then "${packages.opengym-media}/videos" else "${cfg.media.dataDir}/gif";
+ # Hardening shared by the API and MCP units. The MCP server reads the same data files,
+ # so it gets the same sandbox.
+ serviceHardening = {
+ 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"
+ ];
+ };
+
in
{
options.services.opengym = {
@@ -267,24 +287,9 @@ in
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 ];
}
+ serviceHardening
# systemd's StateDirectory= only lives under /var/lib, so use it for the default
# dataDir; a custom dataDir is created by the tmpfiles rule instead.
(lib.mkIf (builtins.toString cfg.dataDir == "/var/lib/opengym") {
@@ -327,14 +332,17 @@ in
environment = {
DATA_DIR = cfg.dataDir;
};
- serviceConfig = {
- ExecStart = "${packages.opengym-mcp}/bin/opengym-mcp";
- StandardInput = "null";
- StandardOutput = "journal";
- User = "opengym";
- Group = "opengym";
- Restart = "on-failure";
- };
+ serviceConfig = lib.mkMerge [
+ {
+ ExecStart = "${packages.opengym-mcp}/bin/opengym-mcp";
+ StandardInput = "null";
+ StandardOutput = "journal";
+ User = "opengym";
+ Group = "opengym";
+ Restart = "on-failure";
+ }
+ serviceHardening
+ ];
};
}
# Optional: let the module configure an nginx virtualHost for openGym itself.