This repository has been archived on 2026-04-18. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
server-config/services/traccar.nix
Simon Gardling f2ca84ab53
Some checks failed
Build and Deploy / deploy (push) Failing after 1m6s
traccar: fix jdbc url escaping for envsubst and xml
2026-04-12 20:47:20 -04:00

86 lines
2.5 KiB
Nix

{
pkgs,
service_configs,
lib,
...
}:
{
imports = [
(lib.serviceMountWithZpool "traccar" service_configs.zpool_ssds [
"/var/lib/traccar"
])
(lib.serviceFilePerms "traccar" [
"Z /var/lib/traccar 0700 traccar traccar"
])
(lib.mkCaddyReverseProxy {
subdomain = "traccar";
port = service_configs.ports.private.traccar_web.port;
})
];
users.users.traccar = {
isSystemUser = true;
group = "traccar";
home = "/var/lib/traccar";
description = "Traccar GPS Tracking";
};
users.groups.traccar = { };
# PostgreSQL database (auto-created, peer auth via Unix socket)
services.postgresql = {
ensureDatabases = [ "traccar" ];
ensureUsers = [
{
name = "traccar";
ensureDBOwnership = true;
}
];
};
services.traccar = {
enable = true;
# The JDBC URL contains '$' (Java inner class) and '&' (query param
# separator) which break the NixOS module's XML generator + envsubst.
# Route it through environmentFile so envsubst replaces $TRACCAR_DB_URL
# with the literal value, and use & for valid XML (the XML parser
# decodes it back to & for JDBC).
environmentFile = pkgs.writeText "traccar-db-env" ''
TRACCAR_DB_URL=jdbc:postgresql:///traccar?socketFactory=org.newsclub.net.unix.AFUNIXSocketFactory$FactoryArg&socketFactoryArg=${service_configs.postgres.socket}/.s.PGSQL.5432
'';
settings = {
web.port = toString service_configs.ports.private.traccar_web.port;
# PostgreSQL via Unix socket (peer auth, junixsocket is bundled)
database = {
driver = "org.postgresql.Driver";
url = "$TRACCAR_DB_URL";
user = "traccar";
password = "";
};
# Only enable OsmAnd protocol (phone app). Prevents Traccar from
# opening 200+ default protocol ports that conflict with other services.
protocols.enable = "osmand";
osmand.port = toString service_configs.ports.public.traccar_tracking.port;
};
};
# Disable DynamicUser so we can use peer auth with PostgreSQL
systemd.services.traccar = {
after = [ "postgresql.service" ];
requires = [ "postgresql.service" ];
serviceConfig = {
DynamicUser = lib.mkForce false;
User = "traccar";
Group = "traccar";
};
};
# OsmAnd tracking port must be reachable from the internet for the phone app
networking.firewall.allowedTCPPorts = [
service_configs.ports.public.traccar_tracking.port
];
}