nixos/traccar: Rework configuration file creation. (#413653)

This commit is contained in:
Matthias Beyer 2025-08-04 10:26:00 +02:00 committed by GitHub
commit 2c06381907
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 19 deletions

View File

@ -133,6 +133,8 @@
- [`services.victorialogs.package`](#opt-services.victorialogs.package) now defaults to `victorialogs`, as `victoriametrics` no longer contains the VictoriaLogs binaries. - [`services.victorialogs.package`](#opt-services.victorialogs.package) now defaults to `victorialogs`, as `victoriametrics` no longer contains the VictoriaLogs binaries.
- The `services.traccar.settings` attribute has been reworked. Instead of the previous flat attribute set the new implementation uses nested attribute sets. You need to update you configuration manually. For instance, `services.traccar.settings.loggerConsole` becomes `services.traccar.settings.logger.console`.
- The `wstunnel` module was converted to RFC42-style settings, you will need to update your NixOS config if you make use of this module. - The `wstunnel` module was converted to RFC42-style settings, you will need to update your NixOS config if you make use of this module.
- [private-gpt](https://github.com/zylon-ai/private-gpt) service has been removed by lack of maintenance upstream. - [private-gpt](https://github.com/zylon-ai/private-gpt) service has been removed by lack of maintenance upstream.

View File

@ -8,44 +8,62 @@ let
cfg = config.services.traccar; cfg = config.services.traccar;
stateDirectory = "/var/lib/traccar"; stateDirectory = "/var/lib/traccar";
configFilePath = "${stateDirectory}/config.xml"; configFilePath = "${stateDirectory}/config.xml";
expandCamelCase = lib.replaceStrings lib.upperChars (map (s: ".${s}") lib.lowerChars);
mkConfigEntry = key: value: "<entry key='${expandCamelCase key}'>${value}</entry>"; # Map leafs to XML <entry> elements as expected by traccar, using
# dot-separated keys for nested attribute paths.
mapLeafs = lib.mapAttrsRecursive (
path: value: "<entry key='${lib.concatStringsSep "." path}'>${value}</entry>"
);
mkConfigEntry = config: lib.collect builtins.isString (mapLeafs config);
mkConfig = mkConfig =
configurationOptions: configurationOptions:
pkgs.writeText "traccar.xml" '' pkgs.writeText "traccar.xml" ''
<?xml version='1.0' encoding='UTF-8'?> <?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE properties SYSTEM 'http://java.sun.com/dtd/properties.dtd'> <!DOCTYPE properties SYSTEM 'http://java.sun.com/dtd/properties.dtd'>
<properties> <properties>
${builtins.concatStringsSep "\n" (lib.mapAttrsToList mkConfigEntry configurationOptions)} ${builtins.concatStringsSep "\n" (mkConfigEntry configurationOptions)}
</properties> </properties>
''; '';
defaultConfig = { defaultConfig = {
databaseDriver = "org.h2.Driver"; database = {
databasePassword = ""; driver = "org.h2.Driver";
databaseUrl = "jdbc:h2:${stateDirectory}/traccar"; password = "";
databaseUser = "sa"; url = "jdbc:h2:${stateDirectory}/traccar";
loggerConsole = "true"; user = "sa";
mediaPath = "${stateDirectory}/media"; };
templatesRoot = "${stateDirectory}/templates"; logger.console = "true";
media.path = "${stateDirectory}/media";
templates.root = "${stateDirectory}/templates";
}; };
in in
{ {
options.services.traccar = { options.services.traccar = {
enable = lib.mkEnableOption "Traccar, an open source GPS tracking system"; enable = lib.mkEnableOption "Traccar, an open source GPS tracking system";
settingsFile = lib.mkOption {
type = with lib.types; nullOr path;
default = null;
description = ''
File used as configuration for traccar. When specified, {option}`settings` is ignored.
'';
};
settings = lib.mkOption { settings = lib.mkOption {
apply = lib.recursiveUpdate defaultConfig; apply = lib.recursiveUpdate defaultConfig;
default = defaultConfig; default = defaultConfig;
description = '' description = ''
{file}`config.xml` configuration as a Nix attribute set. {file}`config.xml` configuration as a Nix attribute set.
Attribute names are translated from camelCase to dot-separated strings. For instance: This option is ignored if `settingsFile` is set.
{option}`mailSmtpPort = "25"`
would result in the following configuration property: Nested attributes get translated to a properties entry in the traccar configuration.
For instance: `mail.smtp.port = "25"` results in the following entry:
`<entry key='mail.smtp.port'>25</entry>` `<entry key='mail.smtp.port'>25</entry>`
Configuration options should match those described in
[Traccar - Configuration File](https://www.traccar.org/configuration-file/). Secrets should be specified using {option}`environmentFile`
Secret tokens should be specified using {option}`environmentFile`
instead of this world-readable attribute set. instead of this world-readable attribute set.
[Traccar - Configuration File](https://www.traccar.org/configuration-file/).
''; '';
}; };
environmentFile = lib.mkOption { environmentFile = lib.mkOption {
@ -56,7 +74,7 @@ in
Can be used for storing the secrets without making them available in the world-readable Nix store. Can be used for storing the secrets without making them available in the world-readable Nix store.
For example, you can set {option}`services.traccar.settings.databasePassword = "$TRACCAR_DB_PASSWORD"` For example, you can set {option}`services.traccar.settings.database.password = "$TRACCAR_DB_PASSWORD"`
and then specify `TRACCAR_DB_PASSWORD="<secret>"` in the environment file. and then specify `TRACCAR_DB_PASSWORD="<secret>"` in the environment file.
This value will get substituted in the configuration file. This value will get substituted in the configuration file.
''; '';
@ -65,7 +83,7 @@ in
config = config =
let let
configuration = mkConfig cfg.settings; configuration = if cfg.settingsFile != null then cfg.settingsFile else mkConfig cfg.settings;
in in
lib.mkIf cfg.enable { lib.mkIf cfg.enable {
systemd.services.traccar = { systemd.services.traccar = {
@ -92,7 +110,7 @@ in
serviceConfig = { serviceConfig = {
DynamicUser = true; DynamicUser = true;
EnvironmentFile = cfg.environmentFile; EnvironmentFile = lib.mkIf (cfg.environmentFile != null) cfg.environmentFile;
ExecStart = "${lib.getExe pkgs.traccar} ${configFilePath}"; ExecStart = "${lib.getExe pkgs.traccar} ${configFilePath}";
LockPersonality = true; LockPersonality = true;
NoNewPrivileges = true; NoNewPrivileges = true;