diff --git a/nix/pkgs/default.nix b/nix/pkgs/default.nix index b9e25bc9ff..ea3c666345 100644 --- a/nix/pkgs/default.nix +++ b/nix/pkgs/default.nix @@ -69,4 +69,5 @@ extraPkgs = pkgs: with pkgs; [ openssl gnome.zenity ]; }; vault-acme = pkgs.callPackage ./vault-acme { }; + nixos-size = pkgs.callPackage ./nixos-size { }; } // (import ./heptapod-runner args) diff --git a/nix/pkgs/nixos-size/combined-builder.sh b/nix/pkgs/nixos-size/combined-builder.sh new file mode 100755 index 0000000000..13a7d02b76 --- /dev/null +++ b/nix/pkgs/nixos-size/combined-builder.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +echo -ne "$sp1\n$sp2\n" > $out diff --git a/nix/pkgs/nixos-size/combined.nix b/nix/pkgs/nixos-size/combined.nix new file mode 100644 index 0000000000..b6b63fecbc --- /dev/null +++ b/nix/pkgs/nixos-size/combined.nix @@ -0,0 +1,13 @@ +{ builder ? ./combined-builder.sh }: + +derivation { + system = builtins.currentSystem; + name = "nixos-size-combined"; + inherit builder; + + sp1 = builtins.storePath /run/current-system; + sp2 = builtins.storePath /run/booted-system; + + preferLocalBuild = true; + allowSubstitutes = false; +} diff --git a/nix/pkgs/nixos-size/default.nix b/nix/pkgs/nixos-size/default.nix new file mode 100644 index 0000000000..faef6b4c6a --- /dev/null +++ b/nix/pkgs/nixos-size/default.nix @@ -0,0 +1,15 @@ +{ writeShellApplication, nix_2_3, gawk }: + +writeShellApplication { + name = "nixos-size"; + runtimeInputs = [ nix_2_3 gawk ]; + text = '' + test -e /run/booted-system || exit 0 + + combined_path="$(nix-build ${./combined.nix} --no-out-link --arg builder ${./combined-builder.sh})" + + echo "nixos_closure_size{type=\"booted-system\"} $(nix path-info -S /run/booted-system | awk '{ print $2 }')" + echo "nixos_closure_size{type=\"current-system\"} $(nix path-info -S /run/current-system | awk '{ print $2 }')" + echo "nixos_closure_size{type=\"combined\"} $(nix path-info -S "$combined_path" | awk '{ print $2 }')" + ''; +} diff --git a/ops/nixos/lib/common.nix b/ops/nixos/lib/common.nix index b32ce466b8..4f385987d4 100644 --- a/ops/nixos/lib/common.nix +++ b/ops/nixos/lib/common.nix @@ -243,23 +243,33 @@ in system.activationScripts.node-exporter = { text = '' test -d /run/prometheus-textfile-exports || mkdir /run/prometheus-textfile-exports + my_version_string="$(cat "$systemConfig/nixos-version")" my_hash_string="$(readlink -f "$systemConfig" | ${pkgs.gnugrep}/bin/grep -Eo '\b[0-9a-df-np-sv-z]{32}\b')" my_specialisation="$(cat "$systemConfig/specialisation-name" 2>/dev/null || true)" echo "nixos_running_system{version=\"$my_version_string\", hash=\"$my_hash_string\", specialisation=\"$my_specialisation\"} 1" > /run/prometheus-textfile-exports/running_system.prom - my_version_string="$(cat "/run/booted-system/nixos-version")" - my_hash_string="$(readlink -f "/run/booted-system" | ${pkgs.gnugrep}/bin/grep -Eo '\b[0-9a-df-np-sv-z]{32}\b')" - my_specialisation="$(cat "/run/booted-system/specialisation-name" 2>/dev/null || true)" - echo "nixos_booted_system{version=\"$my_version_string\", hash=\"$my_hash_string\", specialisation=\"$my_specialisation\"} 1" > /run/prometheus-textfile-exports/booted_system.prom + if test -e /run/booted-system; then + my_version_string="$(cat "/run/booted-system/nixos-version")" + my_hash_string="$(readlink -f "/run/booted-system" | ${pkgs.gnugrep}/bin/grep -Eo '\b[0-9a-df-np-sv-z]{32}\b')" + my_specialisation="$(cat "/run/booted-system/specialisation-name" 2>/dev/null || true)" + echo "nixos_booted_system{version=\"$my_version_string\", hash=\"$my_hash_string\", specialisation=\"$my_specialisation\"} 1" > /run/prometheus-textfile-exports/booted_system.prom + fi + + ${depot.nix.pkgs.nixos-size}/bin/nixos-size > /run/prometheus-textfile-exports/nixos_size.prom ''; }; boot.postBootCommands = lib.mkAfter '' test -d /run/prometheus-textfile-exports || mkdir /run/prometheus-textfile-exports - my_version_string="$(cat "/run/booted-system/nixos-version")" - my_hash_string="$(readlink -f "/run/booted-system" | ${pkgs.gnugrep}/bin/grep -Eo '\b[0-9a-df-np-sv-z]{32}\b')" - my_specialisation="$(cat "/run/booted-system/specialisation-name" 2>/dev/null || true)" - echo "nixos_booted_system{version=\"$my_version_string\", hash=\"$my_hash_string\", specialisation=\"$my_specialisation\"} 1" > /run/prometheus-textfile-exports/booted_system.prom + + if test -e /run/booted-system; then + my_version_string="$(cat "/run/booted-system/nixos-version")" + my_hash_string="$(readlink -f "/run/booted-system" | ${pkgs.gnugrep}/bin/grep -Eo '\b[0-9a-df-np-sv-z]{32}\b')" + my_specialisation="$(cat "/run/booted-system/specialisation-name" 2>/dev/null || true)" + echo "nixos_booted_system{version=\"$my_version_string\", hash=\"$my_hash_string\", specialisation=\"$my_specialisation\"} 1" > /run/prometheus-textfile-exports/booted_system.prom + fi + + ${depot.nix.pkgs.nixos-size}/bin/nixos-size > /run/prometheus-textfile-exports/nixos_size.prom ''; system.extraSystemBuilderCmds = lib.mkAfter '' echo "${if config.my.specialisationName == null then "" else config.my.specialisationName}" > $out/specialisation-name