nix-flake-outputs-size/create-report.sh
Jalil David Salamé Messina 46291fc850
feat!(report): generate JSON instead of markdown
This allows us to analyze the report more easily. There is experimental
support for comparing against a base report, but we don't expose that
functionality to the action.
2024-12-17 23:17:48 +01:00

48 lines
1.4 KiB
Bash
Executable file

#!/bin/sh
set -eu
echo 'Retrieving Flake information' >&2
flake_info=$(nix flake show --json 2>/dev/null)
packages=$(
jq --raw-output '.packages."x86_64-linux" | select(. != null) | keys[]' <<-EOF
$flake_info
EOF
)
echo "Packages:" >&2
echo "$packages" >&2
configurations=$(
jq --raw-output '.nixosConfigurations | select(. != null) | keys[]' <<-EOF
$flake_info
EOF
)
echo "NixOS Configurations:" >&2
echo "$configurations" >&2
pkgs_json() {
for package in $packages; do
echo "Building $package" >&2
path=$(nix build --print-out-paths ".#$package" 2>/dev/null)
echo "Calculating size of $package" >&2
nix path-info --closure-size --json "$path" 2>/dev/null |
jq --compact-output --arg pkg "$package" '.[] | {"name": $pkg, "size": .closureSize, "narSize": .narSize}'
done
}
configs_json() {
for config in $configurations; do
echo "Building $config" >&2
path=$(nix build --print-out-paths ".#nixosConfigurations.$config.config.system.build.toplevel" 2>/dev/null)
echo "Calculating size of $config" >&2
nix path-info --closure-size --json "$path" 2>/dev/null |
jq --compact-output --arg pkg "$config" '.[] | {"name": $pkg, "size": .closureSize, "narSize": .narSize}'
done
}
pkgs=$(pkgs_json | jq --slurp '.')
configs=$(configs_json | jq --slurp '.')
echo "{}" | jq \
--argjson pkgs "$pkgs" \
--argjson configs "$configs" \
'{"packages": $pkgs, "nixosConfigurations": $configs}'