nix: seralize TOML config in the NixOS module
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: Ia6171de7832659075918879c31420e376a6a6964
This commit is contained in:
parent
bfd9fa485e
commit
f96592a7cd
1 changed files with 88 additions and 4 deletions
|
|
@ -6,33 +6,117 @@ self: {
|
||||||
}: let
|
}: let
|
||||||
inherit (lib.modules) mkIf;
|
inherit (lib.modules) mkIf;
|
||||||
inherit (lib.options) mkOption mkEnableOption;
|
inherit (lib.options) mkOption mkEnableOption;
|
||||||
inherit (lib.types) package;
|
inherit (lib.types) package bool str listOf attrsOf submodule int nullOr anything;
|
||||||
|
|
||||||
cfg = config.services.pscand;
|
cfg = config.services.pscand;
|
||||||
|
|
||||||
|
settingsFormat = pkgs.formats.toml {};
|
||||||
|
|
||||||
|
configFile = settingsFormat.generate "pscand.toml" {
|
||||||
|
scanner_dirs = cfg.scannerDirs;
|
||||||
|
log_dir = cfg.logDir;
|
||||||
|
ring_buffer_size = cfg.ringBufferSize;
|
||||||
|
journal_enabled = cfg.journalEnabled;
|
||||||
|
file_enabled = cfg.fileEnabled;
|
||||||
|
scanners =
|
||||||
|
lib.mapAttrs (_: scanner: {
|
||||||
|
enabled = scanner.enabled;
|
||||||
|
interval_secs = scanner.interval;
|
||||||
|
extra = scanner.extra;
|
||||||
|
})
|
||||||
|
cfg.scanners;
|
||||||
|
};
|
||||||
in {
|
in {
|
||||||
options.services.pscand = {
|
options.services.pscand = {
|
||||||
enable = mkEnableOption "Pluggable System Condition Monitoring Daemon";
|
enable = mkEnableOption "Pluggable System Condition Monitoring Daemon";
|
||||||
|
|
||||||
package = mkOption {
|
package = mkOption {
|
||||||
type = package;
|
type = package;
|
||||||
default = self.packages.${pkgs.hostPlatform.system.pscand};
|
default = self.packages.${pkgs.hostPlatform.system.pscand};
|
||||||
defaultText = "self.packages.$${pkgs.hostPlatform.system}.pscand";
|
defaultText = "self.packages.$${pkgs.hostPlatform.system}.pscand";
|
||||||
description = "The pscand package to use";
|
description = "The pscand package to use";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
scannerDirs = mkOption {
|
||||||
|
type = listOf str;
|
||||||
|
default = ["${cfg.package}/lib/pscand/scanners"];
|
||||||
|
description = "Directories to load scanner plugins from";
|
||||||
|
};
|
||||||
|
|
||||||
|
logDir = mkOption {
|
||||||
|
type = str;
|
||||||
|
default = "/var/log/pscand";
|
||||||
|
description = "Directory for log files and heartbeat";
|
||||||
|
};
|
||||||
|
|
||||||
|
ringBufferSize = mkOption {
|
||||||
|
type = int;
|
||||||
|
default = 1000;
|
||||||
|
description = "Number of log entries to keep in memory for crash recovery";
|
||||||
|
};
|
||||||
|
|
||||||
|
journal.enable = mkEnableOption "logging to Systemd journal";
|
||||||
|
file.enable = mkEnableOption "logging to file";
|
||||||
|
|
||||||
|
scanners = mkOption {
|
||||||
|
type = attrsOf (submodule {
|
||||||
|
options = {
|
||||||
|
enabled = mkOption {
|
||||||
|
type = bool;
|
||||||
|
default = true;
|
||||||
|
description = "Whether this scanner is enabled";
|
||||||
|
};
|
||||||
|
|
||||||
|
interval = mkOption {
|
||||||
|
type = nullOr int;
|
||||||
|
default = null;
|
||||||
|
description = "Collection interval in seconds (null = use scanner default)";
|
||||||
|
};
|
||||||
|
|
||||||
|
extra = mkOption {
|
||||||
|
type = attrsOf anything;
|
||||||
|
default = {};
|
||||||
|
description = "Scanner-specific configuration options";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
});
|
||||||
|
default = {};
|
||||||
|
description = "Per-scanner configuration settings";
|
||||||
|
example = {
|
||||||
|
system = {
|
||||||
|
enabled = true;
|
||||||
|
interval = 5;
|
||||||
|
};
|
||||||
|
sensor = {
|
||||||
|
enabled = true;
|
||||||
|
interval = 10;
|
||||||
|
extra = {sensors = ["coretemp" "nvme"];};
|
||||||
|
};
|
||||||
|
power = {
|
||||||
|
enabled = true;
|
||||||
|
interval = 30;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
config = mkIf cfg.enable {
|
config = mkIf cfg.enable {
|
||||||
systemd.packages = [cfg.package];
|
environment.etc."pscand/pscand.toml".source = configFile;
|
||||||
|
|
||||||
systemd.services.pscand = {
|
systemd.services.pscand = {
|
||||||
wantedBy = ["multi-user.target"];
|
wantedBy = ["multi-user.target"];
|
||||||
after = ["network.target"];
|
after = ["network.target"];
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
Type = "simple";
|
Type = "simple";
|
||||||
ExecStart = "${cfg.package}/bin/pscand";
|
ExecStart = "${cfg.package}/bin/pscand run --config /etc/pscand/pscand.toml";
|
||||||
Restart = "on-failure";
|
Restart = "on-failure";
|
||||||
RestartSec = "5s";
|
RestartSec = "5s";
|
||||||
Environment = ["${cfg.package}/lib/pscand/scanners"]; # FIXME: make this configurable
|
Environment = "PSCAND_SCANNER_DIRS=${lib.concatStringsSep ":" cfg.scannerDirs}";
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
systemd.tmpfiles.rules = [
|
||||||
|
"d ${cfg.logDir} 0755 root root -"
|
||||||
|
];
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue