From a0a7df1b0e98f97724eb5f94537a3e052d313995 Mon Sep 17 00:00:00 2001 From: arcnmx Date: Thu, 30 May 2024 14:10:18 -0700 Subject: [PATCH] chore(monitoring): nf-fmt-nix --- .../nixos/monitoring/ingest/prometheus.nix | 18 +- .../nixos/monitoring/source/prometheus.nix | 14 +- modules/system/exports/monitoring.nix | 243 +++++++++--------- modules/system/exports/services.nix | 10 +- nixos/base/monitoring.nix | 8 +- nixos/monitoring.nix | 12 +- 6 files changed, 164 insertions(+), 141 deletions(-) diff --git a/modules/nixos/monitoring/ingest/prometheus.nix b/modules/nixos/monitoring/ingest/prometheus.nix index 8a53b683..6a5fff22 100644 --- a/modules/nixos/monitoring/ingest/prometheus.nix +++ b/modules/nixos/monitoring/ingest/prometheus.nix @@ -11,18 +11,24 @@ nodeExporterSystems = filter ( system: - system.config.access.online.enable && - system.config.exports.prometheus.exporter.services != [ ] + system.config.access.online.enable + && system.config.exports.prometheus.exporter.services != [] ) (attrValues systems); - mkPortTarget = { system, service, portName }: let + mkPortTarget = { + system, + service, + portName, + }: let port = service.ports.${portName}; in "${access.getAddressFor system.config.name "lan"}:${toString port.port}"; mkServiceConfig = system: serviceName: let service = system.config.exports.services.${serviceName}; - targets = map (portName: mkPortTarget { - inherit system service portName; - }) service.prometheus.exporter.ports; + targets = map (portName: + mkPortTarget { + inherit system service portName; + }) + service.prometheus.exporter.ports; in { job_name = "${system.config.name}-${service.id}"; static_configs = [ diff --git a/modules/nixos/monitoring/source/prometheus.nix b/modules/nixos/monitoring/source/prometheus.nix index 147a76b5..e549cf48 100644 --- a/modules/nixos/monitoring/source/prometheus.nix +++ b/modules/nixos/monitoring/source/prometheus.nix @@ -7,8 +7,9 @@ inherit (lib.attrsets) attrValues; inherit (lib.lists) concatMap toList; allExporters = let - exporters = removeAttrs config.services.prometheus.exporters [ "unifi-poller" ]; - in concatMap toList (attrValues exporters); + exporters = removeAttrs config.services.prometheus.exporters ["unifi-poller"]; + in + concatMap toList (attrValues exporters); in { config = { services.prometheus.exporters = { @@ -57,8 +58,11 @@ in { } ]; }; - networking.firewall.interfaces.lan.allowedTCPPorts = map (exporter: - mkIf (exporter.enable && !exporter.openFirewall) 999 - ) allExporters; + networking.firewall.interfaces.lan.allowedTCPPorts = + map ( + exporter: + mkIf (exporter.enable && !exporter.openFirewall) exporter.port + ) + allExporters; }; } diff --git a/modules/system/exports/monitoring.nix b/modules/system/exports/monitoring.nix index 36620d00..86ff57cc 100644 --- a/modules/system/exports/monitoring.nix +++ b/modules/system/exports/monitoring.nix @@ -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; + } diff --git a/modules/system/exports/services.nix b/modules/system/exports/services.nix index 41c3bd4e..fe036de7 100644 --- a/modules/system/exports/services.nix +++ b/modules/system/exports/services.nix @@ -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; }; diff --git a/nixos/base/monitoring.nix b/nixos/base/monitoring.nix index bf37f86b..35eed469 100644 --- a/nixos/base/monitoring.nix +++ b/nixos/base/monitoring.nix @@ -1,6 +1,6 @@ _: { - services = { - prometheus.exporters.node.enable = true; - promtail.enable = true; - }; + services = { + prometheus.exporters.node.enable = true; + promtail.enable = true; + }; } diff --git a/nixos/monitoring.nix b/nixos/monitoring.nix index fb96c452..bc93ae3a 100644 --- a/nixos/monitoring.nix +++ b/nixos/monitoring.nix @@ -1,7 +1,7 @@ _: { - services = { - grafana.enable = true; - loki.enable = true; - prometheus.enable = true; - }; -} \ No newline at end of file + services = { + grafana.enable = true; + loki.enable = true; + prometheus.enable = true; + }; +}