From 11e0992d9952ae22832623430aafa8ee4ab369d6 Mon Sep 17 00:00:00 2001 From: Noah Masur <7386960+nmasur@users.noreply.github.com> Date: Wed, 23 Apr 2025 19:20:45 +0000 Subject: [PATCH] semi-failed packaging of actual budget prometheus exporter --- pkgs/prometheus-actual-exporter/package.nix | 91 +++++++++++++++++++ .../prometheus-actual-exporter/tsconfig.patch | 12 +++ .../services/actualbudget/actualbudget.nix | 32 ++++++- .../modules/nmasur/presets/services/caddy.nix | 3 +- 4 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 pkgs/prometheus-actual-exporter/package.nix create mode 100644 pkgs/prometheus-actual-exporter/tsconfig.patch diff --git a/pkgs/prometheus-actual-exporter/package.nix b/pkgs/prometheus-actual-exporter/package.nix new file mode 100644 index 0000000..e56f38a --- /dev/null +++ b/pkgs/prometheus-actual-exporter/package.nix @@ -0,0 +1,91 @@ +{ + lib, + fetchFromGitHub, + nodejs_18, + buildNpmPackage, + nodePackages, + python3, + gcc, + gnumake, +}: +let + +in + +buildNpmPackage (finalAttrs: rec { + pname = "prometheus-actual-exporter"; + + version = "1.1.5"; + + src = fetchFromGitHub { + owner = "sakowicz"; + repo = "actual-budget-prometheus-exporter"; + tag = version; + hash = "sha256-DAmWr1HngxAjhOJW9OnMfDqpxBcZT+Tpew/w/YYJIYU="; + }; + + patches = [ ./tsconfig.patch ]; + + npmDepsHash = "sha256-N8xqRYFelolNGTEhG22M7KJ7B5U/uW7o+/XfLF8rHMg="; + + nativeBuildInputs = [ + nodejs_18 + nodePackages.typescript + python3 + nodePackages.node-gyp + gcc + gnumake + ]; + + postPatch = '' + echo "Removing better-sqlite3 install script before npm install" + sed -i '/"install"/d' node_modules/better-sqlite3/package.json || true + sed -i '/"install"/d' package.json || true + ''; + + preBuild = '' + echo "Disabling prebuilt install script from better-sqlite3" + find node_modules/better-sqlite3 -name package.json -exec sed -i '/"install"/d' {} + + rm -f node_modules/better-sqlite3/build/Release/better_sqlite3.node || true + ''; + + buildPhase = '' + # export npm_config_build_from_source=true + # export npm_config_unsafe_perm=true + # export BINARY_SITE=none + # export PATH=${nodePackages.node-gyp}/bin:$PATH + # export npm_config_node_gyp=${nodePackages.node-gyp}/bin/node-gyp + + # npm rebuild better-sqlite3 --build-from-source --verbose + + npm run build + ''; + + installPhase = '' + mkdir -p $out/{bin,lib} + cp -r . $out/lib/prometheus-actual-exporter + makeWrapper ${lib.getExe nodejs_18} $out/bin/prometheus-actual-exporter \ + --add-flags "$out/lib/prometheus-actual-exporter/dist/app.js" + ''; + + postInstall = '' + echo "Removing prebuilt .node and rebuilding better-sqlite3" + + export npm_config_build_from_source=true + export npm_config_unsafe_perm=true + export BINARY_SITE=none + export PATH=${nodePackages.node-gyp}/bin:$PATH + export npm_config_node_gyp=${nodePackages.node-gyp}/bin/node-gyp + + sed -i '/"install"/d' node_modules/better-sqlite3/package.json + rm -f node_modules/better-sqlite3/build/Release/better_sqlite3.node || true + + npm rebuild better-sqlite3 --build-from-source --verbose + ''; + + meta = { + description = "Prometheus exporter for Actual Budget"; + homepage = "https://github.com/sakowicz/actual-budget-prometheus-exporter"; + mainProgram = "prometheus-actual-exporter"; + }; +}) diff --git a/pkgs/prometheus-actual-exporter/tsconfig.patch b/pkgs/prometheus-actual-exporter/tsconfig.patch new file mode 100644 index 0000000..6e1fea1 --- /dev/null +++ b/pkgs/prometheus-actual-exporter/tsconfig.patch @@ -0,0 +1,12 @@ +diff --git a/tsconfig.json b/tsconfig.json +index 5106135..3a340f6 100644 +--- a/tsconfig.json ++++ b/tsconfig.json +@@ -8,5 +8,6 @@ + "skipLibCheck": true, + "lib": ["es2020"], + "outDir": "./dist" +- } ++ }, ++ "include": ["src/**/*", "app.ts"] + } diff --git a/platforms/nixos/modules/nmasur/presets/services/actualbudget/actualbudget.nix b/platforms/nixos/modules/nmasur/presets/services/actualbudget/actualbudget.nix index 10c5743..0060f80 100644 --- a/platforms/nixos/modules/nmasur/presets/services/actualbudget/actualbudget.nix +++ b/platforms/nixos/modules/nmasur/presets/services/actualbudget/actualbudget.nix @@ -1,5 +1,6 @@ { config, + pkgs, lib, ... }: @@ -18,6 +19,11 @@ in description = "Port to use for the localhost"; default = 5006; }; + prometheusPort = lib.mkOption { + type = lib.types.port; + description = "Port to use for prometheus actual exporter"; + default = 5007; + }; }; config = lib.mkIf cfg.enable { @@ -25,10 +31,30 @@ in services.actual = { enable = true; settings = { - port = 5006; + port = cfg.port; }; }; + # systemd.services.prometheus-actual-exporter = { + # enable = true; + # description = "Prometheus exporter for Actual budget"; + # serviceConfig = { + # DynamicUser = true; + # Environment = [ + # "ACTUAL_SERVER_URL=https://${hostnames.budget}:443" + # "PORT=${builtins.toString cfg.prometheusPort}" + # ]; + # EnvironmentFile = [ + # config.secrets.actualbudget-password.dest + # config.secrets.actualbudget-budget-id.dest + # ]; + # ExecStart = lib.getExe pkgs.nmasur.prometheus-actual-exporter; + # }; + # wantedBy = [ + # "multi-user.target" + # ]; + # }; + # Used for prometheus exporter virtualisation.podman.enable = true; @@ -66,7 +92,7 @@ in }; nmasur.presets.services.prometheus-exporters.scrapeTargets = [ - "127.0.0.1:5007" + "127.0.0.1:${builtins.toString cfg.prometheusPort}" ]; secrets.actualbudget-password = { @@ -99,7 +125,7 @@ in services.cloudflare-dyndns.domains = [ hostnames.budget ]; # Backups - services.restic.backups.default.paths = [ "/var/lib/actualbudget" ]; + services.restic.backups.default.paths = [ "/var/lib/actual" ]; }; diff --git a/platforms/nixos/modules/nmasur/presets/services/caddy.nix b/platforms/nixos/modules/nmasur/presets/services/caddy.nix index c954024..75a2e3b 100644 --- a/platforms/nixos/modules/nmasur/presets/services/caddy.nix +++ b/platforms/nixos/modules/nmasur/presets/services/caddy.nix @@ -103,8 +103,9 @@ in value = name; }) hostname_map; }; + + metrics = { }; # Enables Prometheus metrics }; - apps.http.servers.metrics = { }; # Enables Prometheus metrics apps.tls.automation.policies = cfg.tlsPolicies; # Setup logging to journal and files