101 lines
2.7 KiB
Nix
101 lines
2.7 KiB
Nix
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
|
cfg = config.services.nightlight;
|
|
yamlFormat = pkgs.formats.yaml { };
|
|
generatedConfig = {
|
|
server = {
|
|
grpc_addr = "0.0.0.0:${toString cfg.grpcPort}";
|
|
http_addr = "0.0.0.0:${toString cfg.httpPort}";
|
|
};
|
|
storage = {
|
|
data_dir = toString cfg.dataDir;
|
|
retention_days = cfg.retentionDays;
|
|
};
|
|
};
|
|
configFile = yamlFormat.generate "nightlight.yaml" (lib.recursiveUpdate generatedConfig cfg.settings);
|
|
configPath = "/etc/nightlight/nightlight.yaml";
|
|
in
|
|
{
|
|
options.services.nightlight = {
|
|
enable = lib.mkEnableOption "nightlight service";
|
|
|
|
httpPort = lib.mkOption {
|
|
type = lib.types.port;
|
|
default = 9101;
|
|
description = "Port for Prometheus-compatible HTTP API (remote_write and query)";
|
|
};
|
|
|
|
grpcPort = lib.mkOption {
|
|
type = lib.types.port;
|
|
default = 9091;
|
|
description = "Port for gRPC API";
|
|
};
|
|
|
|
dataDir = lib.mkOption {
|
|
type = lib.types.path;
|
|
default = "/var/lib/nightlight";
|
|
description = "Data directory for nightlight";
|
|
};
|
|
|
|
retentionDays = lib.mkOption {
|
|
type = lib.types.int;
|
|
default = 15;
|
|
description = "Number of days to retain metrics data";
|
|
};
|
|
|
|
settings = lib.mkOption {
|
|
type = lib.types.attrs;
|
|
default = {};
|
|
description = "Additional configuration settings";
|
|
};
|
|
|
|
package = lib.mkOption {
|
|
type = lib.types.package;
|
|
default = pkgs.nightlight-server or (throw "nightlight-server package not found");
|
|
description = "Package to use for nightlight";
|
|
};
|
|
};
|
|
|
|
config = lib.mkIf cfg.enable {
|
|
# Create system user
|
|
users.users.nightlight = {
|
|
isSystemUser = true;
|
|
group = "nightlight";
|
|
description = "Nightlight service user";
|
|
home = cfg.dataDir;
|
|
};
|
|
|
|
users.groups.nightlight = {};
|
|
|
|
# Create systemd service
|
|
systemd.services.nightlight = {
|
|
description = "Nightlight Prometheus-Compatible Metrics Storage";
|
|
wantedBy = [ "multi-user.target" ];
|
|
after = [ "network.target" ];
|
|
|
|
serviceConfig = {
|
|
Type = "simple";
|
|
User = "nightlight";
|
|
Group = "nightlight";
|
|
Restart = "on-failure";
|
|
RestartSec = "10s";
|
|
|
|
# State directory management
|
|
StateDirectory = "nightlight";
|
|
StateDirectoryMode = "0750";
|
|
|
|
# Security hardening
|
|
NoNewPrivileges = true;
|
|
PrivateTmp = true;
|
|
ProtectSystem = "strict";
|
|
ProtectHome = true;
|
|
ReadWritePaths = [ cfg.dataDir ];
|
|
|
|
ExecStart = "${cfg.package}/bin/nightlight-server --config ${configPath}";
|
|
};
|
|
};
|
|
|
|
environment.etc."nightlight/nightlight.yaml".source = configFile;
|
|
};
|
|
}
|