From 5e86139ee4af27f84228708fd32903bb0c4230f0 Mon Sep 17 00:00:00 2001 From: clerie Date: Tue, 9 May 2023 11:45:33 +0200 Subject: [PATCH] Bump version and add modules --- .gitignore | 1 + Cargo.lock | 2 +- flake.nix | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 132 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index ea8c4bf..07c12f2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +result diff --git a/Cargo.lock b/Cargo.lock index c659fb0..6224217 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -450,7 +450,7 @@ dependencies = [ [[package]] name = "nixos-exporter" -version = "0.5.0" +version = "0.6.0" dependencies = [ "axum", "reqwest", diff --git a/flake.nix b/flake.nix index f7ae95f..56ed915 100644 --- a/flake.nix +++ b/flake.nix @@ -10,7 +10,7 @@ in { nixos-exporter = pkgs.rustPlatform.buildRustPackage rec { pname = "nixos-exporter"; - version = "0.5.0"; + version = "0.6.0"; src = ./.; @@ -33,12 +33,140 @@ type = "app"; program = self.packages.x86_64-linux.nixos-exporter + "/bin/nixos-exporter"; }; + nixos-validator = { + type = "app"; + program = self.packages.x86_64-linux.nixos-exporter + "/bin/nixos-validator"; + }; default = self.apps.x86_64-linux.nixos-exporter; }; + + nixosModules = { + nixos-exporter = { config, pkgs, lib, ... }: + with lib; + let + cfg = config.services.nixos-exporter; + in { + options = { + services.nixos-exporter = { + enable = mkEnableOption "Export NixOS status metrics"; + listen = mkOption { + type = with types; nullOr str; + default = null; + description = "Interface for metrics"; + example = "[::]:2345"; + }; + }; + }; + + config = mkIf cfg.enable { + users.users."nixos-exporter" = { + isSystemUser = true; + group = "nixos-exporter"; + }; + + users.groups."nixos-exporter" = {}; + + systemd.services."nixos-exporter" = { + wantedBy = [ "multi-user.target" ]; + after = [ "network.target" ]; + serviceConfig = { + Restart = "always"; + PrivateTmp = true; + WorkingDirectory = "/tmp"; + RuntimeDirectory = "nixos-exporter"; + User = "nixos-exporter"; + Group = "nixos-exporter"; + ExecStart = '' + ${self.packages."${config.nixpkgs.system}".nixos-exporter}/bin/nixos-exporter ${optionalString (cfg.listen != null) "--listen ${escapeShellArg cfg.listen}"} + ''; + }; + }; + }; + }; + + nixos-validator = { config, pkgs, lib, ... }: + with lib; + let + cfg = config.services.nixos-validator; + in { + options = { + services.nixos-validator = { + enable = mkEnableOption "Validate NixOS metrics"; + listen = mkOption { + type = with types; nullOr str; + default = null; + description = "Interface for metrics"; + example = "[::]:2345"; + }; + prometheusUrl = mkOption { + type = types.str; + description = "Url for Prometheus"; + example = "https://prometheus.monitoring.clerie.de"; + }; + prometheusQueryTagTemplate = mkOption { + type = with types; nullOr str; + default = null; + description = "Template for Prometheus Query"; + example = "instance=\"{}\""; + }; + hydraUrl = mkOption { + type = types.str; + description = "Url for Hydra"; + example = "https://hydra.clerie.de"; + }; + hydraJobTemplate = mkOption { + type = with types; nullOr str; + default = null; + description = "Template for Hydra Job Url"; + example = "nixfiles/nixfiles/nixosConfigurations.{}"; + }; + }; + }; + + config = mkIf cfg.enable { + users.users."nixos-validator" = { + isSystemUser = true; + group = "nixos-validator"; + }; + + users.groups."nixos-validator" = {}; + + systemd.services."nixos-validator" = { + wantedBy = [ "multi-user.target" ]; + after = [ "network.target" ]; + serviceConfig = { + Restart = "always"; + PrivateTmp = true; + WorkingDirectory = "/tmp"; + RuntimeDirectory = "nixos-validator"; + User = "nixos-validator"; + Group = "nixos-validator"; + ExecStart = '' + ${self.packages."${config.nixpkgs.system}".nixos-exporter}/bin/nixos-validator ${concatStringsSep " " [ + (optionalString (cfg.listen != null) "--listen ${escapeShellArg cfg.listen}") + "--prometheus-url ${escapeShellArg cfg.prometheusUrl}" + (optionalString (cfg.prometheusQueryTagTemplate != null) "--prometheus-query-tag-template ${escapeShellArg cfg.prometheusQueryTagTemplate}") + "--hydra-url ${escapeShellArg cfg.hydraUrl}" + (optionalString (cfg.hydraJobTemplate != null) "--hydra-job-template ${escapeShellArg cfg.hydraJobTemplate}") + ]} + ''; + }; + }; + }; + }; + + default = { ... }: + { + imports = [ + self.nixosModules.nixos-exporter + self.nixosModules.nixos-validator + ]; + }; + }; + hydraJobs = { packages.x86_64-linux.nixos-exporter = self.packages.x86_64-linux.nixos-exporter; }; }; } -