Files
puppy-tracker/module.nix
T
Alexander Heldt 15211e32b0 Add a read-only integration feed of walks and meals
GET /api/integration/events?since=<unix ms> returns one account's walk
starts and meals that happened or changed since then, tombstones
included, for other services on the same host. It is authenticated
with a bearer token rather than a session and cannot write anything.

Enabled with -integration-token-file (services.puppy-tracker
.integration.tokenFile); -integration-user picks the account, and
defaults to the only one.
2026-09-29 19:13:28 +00:00

157 lines
5.6 KiB
Nix

self: { config, lib, pkgs, ... }:
let
cfg = config.services.puppy-tracker;
staticPkg = self.packages.${pkgs.system}.static;
serverPkg = self.packages.${pkgs.system}.server;
in
{
options.services.puppy-tracker = {
enable = lib.mkEnableOption "Puppy Tracker (offline-first puppy tracking app with sync server)";
address = lib.mkOption {
type = lib.types.str;
default = "0.0.0.0";
description = "Address the server listens on.";
};
port = lib.mkOption {
type = lib.types.port;
default = 8080;
description = "TCP port the server listens on.";
};
openFirewall = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Whether to open the configured port in the firewall.";
};
inviteCodeFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
example = "/run/secrets/puppy-invite-code";
description = ''
Path to an EnvironmentFile containing the shared registration secret as
`PUPPY_INVITE_CODE=...`. Kept out of the Nix store so the code stays
secret. When null, registration is disabled (existing accounts can still
log in).
'';
};
vapidKeyFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
example = "/run/secrets/puppy-vapid-key";
description = ''
Path to an EnvironmentFile containing the Web Push signing key as
`PUPPY_VAPID_KEY=...` (a base64url P-256 private scalar). When null the
server generates one on first start and keeps it in its state directory,
which is fine for a single host. Note that browsers pin this key when
they subscribe: replacing it silently breaks every existing reminder
subscription until each device re-enables notifications.
'';
};
secureCookies = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
Mark session cookies Secure. Enable once the service is reached over
HTTPS (e.g. behind a TLS-terminating reverse proxy); leave off for plain
HTTP on a LAN, or browsers will drop the cookie and logins won't stick.
'';
};
integration = {
tokenFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
example = "/run/agenix/schema-puppy-token";
description = ''
File holding a bearer token (just the token) that unlocks the
read-only GET /api/integration/events feed of walks and meals, used by
other services on this host. When null the endpoint doesn't exist.
'';
};
user = lib.mkOption {
type = lib.types.str;
default = "";
description = "Email of the account the feed exposes. Empty means the only account.";
};
};
package = lib.mkOption {
type = lib.types.package;
default = serverPkg;
defaultText = lib.literalExpression "puppy-tracker.packages.\${system}.server";
description = "The puppy-tracker server package.";
};
staticPackage = lib.mkOption {
type = lib.types.package;
default = staticPkg;
defaultText = lib.literalExpression "puppy-tracker.packages.\${system}.static";
description = "The puppy-tracker static-site package (HTML/CSS/JS).";
};
};
config = lib.mkIf cfg.enable {
systemd.services.puppy-tracker = {
description = "Puppy Tracker server";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
serviceConfig = {
ExecStart = lib.concatStringsSep " " ([
"${cfg.package}/bin/puppy-tracker-server"
"-addr ${cfg.address}:${toString cfg.port}"
"-static ${cfg.staticPackage}/share/puppy-tracker"
"-data /var/lib/puppy-tracker/puppy.db"
] ++ lib.optional cfg.secureCookies "-secure-cookies"
# %d is the unit's credentials directory (see LoadCredential below).
++ lib.optionals (cfg.integration.tokenFile != null) [
"-integration-token-file %d/integration-token"
"-integration-user ${lib.escapeShellArg cfg.integration.user}"
]);
# DynamicUser can't own the secret, so systemd reads it as root and
# hands the service a private copy.
LoadCredential = lib.optional (cfg.integration.tokenFile != null)
"integration-token:${toString cfg.integration.tokenFile}";
# Secrets (registration code, Web Push key) are read from env files kept
# out of the store, exposed to the server as PUPPY_INVITE_CODE and
# PUPPY_VAPID_KEY.
EnvironmentFile = lib.filter (f: f != null) [ cfg.inviteCodeFile cfg.vapidKeyFile ];
DynamicUser = true;
StateDirectory = "puppy-tracker";
StateDirectoryMode = "0750";
Restart = "on-failure";
RestartSec = "2s";
# Hardening
ProtectSystem = "strict";
ProtectHome = true;
PrivateTmp = true;
PrivateDevices = true;
NoNewPrivileges = true;
ProtectKernelTunables = true;
ProtectKernelModules = true;
ProtectKernelLogs = true;
ProtectControlGroups = true;
RestrictSUIDSGID = true;
RestrictRealtime = true;
LockPersonality = true;
MemoryDenyWriteExecute = true;
SystemCallArchitectures = "native";
SystemCallFilter = [ "@system-service" "~@privileged @resources" ];
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" ];
};
};
networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [ cfg.port ];
};
}