- MQTT client with configurable broker connection - Periodic system stats publishing (30s interval) - Command listening on MQTT topic with logging - Systemd service configuration - NixOS module for declarative deployment 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
97 lines
2.6 KiB
Nix
97 lines
2.6 KiB
Nix
{ config, lib, pkgs, ... }:
|
|
|
|
with lib;
|
|
|
|
let
|
|
cfg = config.services.system-stats-daemon;
|
|
in
|
|
{
|
|
options.services.system-stats-daemon = {
|
|
enable = mkEnableOption "System Stats MQTT Daemon";
|
|
|
|
package = mkOption {
|
|
type = types.package;
|
|
description = "The system-stats-daemon package to use";
|
|
};
|
|
|
|
mqttHost = mkOption {
|
|
type = types.str;
|
|
default = "localhost";
|
|
description = "MQTT broker hostname";
|
|
};
|
|
|
|
mqttPort = mkOption {
|
|
type = types.int;
|
|
default = 1883;
|
|
description = "MQTT broker port";
|
|
};
|
|
|
|
mqttUsername = mkOption {
|
|
type = types.nullOr types.str;
|
|
default = null;
|
|
description = "MQTT username (null for no auth)";
|
|
};
|
|
|
|
mqttPassword = mkOption {
|
|
type = types.nullOr types.str;
|
|
default = null;
|
|
description = "MQTT password (null for no auth)";
|
|
};
|
|
|
|
statsTopic = mkOption {
|
|
type = types.str;
|
|
default = "system/stats";
|
|
description = "MQTT topic for publishing stats";
|
|
};
|
|
|
|
commandTopic = mkOption {
|
|
type = types.str;
|
|
default = "system/commands";
|
|
description = "MQTT topic for receiving commands";
|
|
};
|
|
|
|
publishInterval = mkOption {
|
|
type = types.int;
|
|
default = 30000;
|
|
description = "Interval between stats publications (milliseconds)";
|
|
};
|
|
};
|
|
|
|
config = mkIf cfg.enable {
|
|
systemd.services.system-stats-daemon = {
|
|
description = "System Stats MQTT Daemon";
|
|
after = [ "network.target" ];
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
environment = {
|
|
SYSTEM_STATS_MQTT_HOST = cfg.mqttHost;
|
|
SYSTEM_STATS_MQTT_PORT = toString cfg.mqttPort;
|
|
SYSTEM_STATS_MQTT_USERNAME = mkIf (cfg.mqttUsername != null) cfg.mqttUsername;
|
|
SYSTEM_STATS_MQTT_PASSWORD = mkIf (cfg.mqttPassword != null) cfg.mqttPassword;
|
|
SYSTEM_STATS_STATS_TOPIC = cfg.statsTopic;
|
|
SYSTEM_STATS_COMMAND_TOPIC = cfg.commandTopic;
|
|
SYSTEM_STATS_PUBLISH_INTERVAL = toString cfg.publishInterval;
|
|
};
|
|
|
|
serviceConfig = {
|
|
Type = "exec";
|
|
User = "root";
|
|
Group = "root";
|
|
ExecStart = "${cfg.package}/bin/system_stats_daemon start";
|
|
ExecStop = "${cfg.package}/bin/system_stats_daemon stop";
|
|
Restart = "always";
|
|
RestartSec = 5;
|
|
StandardOutput = "journal";
|
|
StandardError = "journal";
|
|
SyslogIdentifier = "system_stats_daemon";
|
|
WorkingDirectory = "${cfg.package}";
|
|
|
|
# Security settings
|
|
NoNewPrivileges = true;
|
|
PrivateTmp = true;
|
|
ProtectHome = true;
|
|
ProtectSystem = false; # Need access to system stats
|
|
};
|
|
};
|
|
};
|
|
} |