chore(monitoring): nf-fmt-nix

This commit is contained in:
arcnmx 2024-05-30 14:10:18 -07:00
parent 6fb140c7e6
commit a0a7df1b0e
6 changed files with 164 additions and 141 deletions

View file

@ -1,8 +1,5 @@
let
portModule = {
lib,
...
}: let
portModule = {lib, ...}: let
inherit (lib.options) mkEnableOption;
in {
options.prometheus = with lib.types; {
@ -27,7 +24,7 @@ let
};
labels = mkOption {
type = attrsOf str;
default = { };
default = {};
};
};
};
@ -41,120 +38,134 @@ let
exporter.ports = mkOptionDefault (attrNames exporterPorts);
};
};
in {
config,
gensokyo-zone,
lib,
...
}: let
inherit (gensokyo-zone.lib) mapListToAttrs mapAlmostOptionDefaults mkAlmostOptionDefault;
inherit (lib.options) mkOption;
inherit (lib.modules) mkIf mkOptionDefault;
inherit (lib.attrsets) attrNames filterAttrs nameValuePair;
mkExporter = { name, port }: nameValuePair "prometheus-exporters-${name}" ({config, ...}: {
nixos = {
serviceAttrPath = ["services" "prometheus" "exporters" name];
assertions = mkIf config.enable [
(nixosConfig: {
assertion = config.ports.default.port == nixosConfig.services.prometheus.exporters.${name}.port;
message = "port mismatch";
})
];
};
ports.default = mapAlmostOptionDefaults {
inherit port;
protocol = "http";
} // {
prometheus.exporter.enable = true;
};
});
exporters = mapListToAttrs mkExporter [
{ name = "node"; port = 9091; }
];
in {
options.exports = with lib.types; {
prometheus = {
exporter.services = mkOption {
type = listOf str;
};
};
services = mkOption {
type = attrsOf (submoduleWith {
modules = [serviceModule];
in
{
config,
gensokyo-zone,
lib,
...
}: let
inherit (gensokyo-zone.lib) mapListToAttrs mapAlmostOptionDefaults mkAlmostOptionDefault;
inherit (lib.options) mkOption;
inherit (lib.modules) mkIf mkOptionDefault;
inherit (lib.attrsets) attrNames filterAttrs nameValuePair;
mkExporter = {
name,
port,
}:
nameValuePair "prometheus-exporters-${name}" ({config, ...}: {
nixos = {
serviceAttrPath = ["services" "prometheus" "exporters" name];
assertions = mkIf config.enable [
(nixosConfig: {
assertion = config.ports.default.port == nixosConfig.services.prometheus.exporters.${name}.port;
message = "port mismatch";
})
];
};
ports.default =
mapAlmostOptionDefaults {
inherit port;
protocol = "http";
}
// {
prometheus.exporter.enable = true;
};
});
};
};
config.exports.prometheus = let
exporterServices = filterAttrs (_: service: service.enable && service.prometheus.exporter.ports != [ ]) config.exports.services;
exporters = mapListToAttrs mkExporter [
{
name = "node";
port = 9091;
}
];
in {
exporter.services = mkOptionDefault (attrNames exporterServices);
};
config.exports.services = {
prometheus = {config, ...}: {
id = mkAlmostOptionDefault "prometheus";
nixos = {
serviceAttr = "prometheus";
assertions = mkIf config.enable [
(nixosConfig: {
assertion = config.ports.default.port == nixosConfig.services.prometheus.port;
message = "port mismatch";
})
];
options.exports = with lib.types; {
prometheus = {
exporter.services = mkOption {
type = listOf str;
};
};
ports.default = mapAlmostOptionDefaults {
port = 9090;
protocol = "http";
services = mkOption {
type = attrsOf (submoduleWith {
modules = [serviceModule];
});
};
};
grafana = {config, ...}: {
id = mkAlmostOptionDefault "grafana";
nixos = {
serviceAttr = "grafana";
assertions = mkIf config.enable [
(nixosConfig: {
assertion = config.ports.default.port == nixosConfig.services.grafana.settings.server.http_port;
message = "port mismatch";
})
];
};
ports.default = mapAlmostOptionDefaults {
port = 9092;
protocol = "http";
};
config.exports.prometheus = let
exporterServices = filterAttrs (_: service: service.enable && service.prometheus.exporter.ports != []) config.exports.services;
in {
exporter.services = mkOptionDefault (attrNames exporterServices);
};
loki = {config, ...}: {
id = mkAlmostOptionDefault "loki";
nixos = {
serviceAttr = "loki";
assertions = mkIf config.enable [
(nixosConfig: {
assertion = config.ports.default.port == nixosConfig.services.loki.settings.httpListenPort;
message = "port mismatch";
})
];
};
ports.default = mapAlmostOptionDefaults {
port = 9093;
protocol = "http";
};
};
promtail = {config, ...}: {
id = mkAlmostOptionDefault "promtail";
nixos = {
serviceAttr = "promtail";
assertions = mkIf config.enable [
(nixosConfig: {
assertion = config.ports.default.port == nixosConfig.services.promtail.settings.httpListenPort;
message = "port mismatch";
})
];
};
ports.default = mapAlmostOptionDefaults {
port = 9094;
protocol = "http";
} // {
prometheus.exporter.enable = true;
};
};
} // exporters;
}
config.exports.services =
{
prometheus = {config, ...}: {
id = mkAlmostOptionDefault "prometheus";
nixos = {
serviceAttr = "prometheus";
assertions = mkIf config.enable [
(nixosConfig: {
assertion = config.ports.default.port == nixosConfig.services.prometheus.port;
message = "port mismatch";
})
];
};
ports.default = mapAlmostOptionDefaults {
port = 9090;
protocol = "http";
};
};
grafana = {config, ...}: {
id = mkAlmostOptionDefault "grafana";
nixos = {
serviceAttr = "grafana";
assertions = mkIf config.enable [
(nixosConfig: {
assertion = config.ports.default.port == nixosConfig.services.grafana.settings.server.http_port;
message = "port mismatch";
})
];
};
ports.default = mapAlmostOptionDefaults {
port = 9092;
protocol = "http";
};
};
loki = {config, ...}: {
id = mkAlmostOptionDefault "loki";
nixos = {
serviceAttr = "loki";
assertions = mkIf config.enable [
(nixosConfig: {
assertion = config.ports.default.port == nixosConfig.services.loki.settings.httpListenPort;
message = "port mismatch";
})
];
};
ports.default = mapAlmostOptionDefaults {
port = 9093;
protocol = "http";
};
};
promtail = {config, ...}: {
id = mkAlmostOptionDefault "promtail";
nixos = {
serviceAttr = "promtail";
assertions = mkIf config.enable [
(nixosConfig: {
assertion = config.ports.default.port == nixosConfig.services.promtail.settings.httpListenPort;
message = "port mismatch";
})
];
};
ports.default =
mapAlmostOptionDefaults {
port = 9094;
protocol = "http";
}
// {
prometheus.exporter.enable = true;
};
};
}
// exporters;
}

View file

@ -146,9 +146,11 @@
};
in {
options.exports = with lib.types; {
defaultServices = mkEnableOption "common base services" // {
default = config.type == "NixOS";
};
defaultServices =
mkEnableOption "common base services"
// {
default = config.type == "NixOS";
};
services = mkOption {
type = attrsOf (submoduleWith {
modules = [serviceModule];
@ -170,7 +172,7 @@ in {
"sshd"
"prometheus-exporters-node"
"promtail"
] (_: { enable = mkAlmostOptionDefault true; });
] (_: {enable = mkAlmostOptionDefault true;});
in {
services = mkIf cfg.defaultServices defaultServices;
};