nix-flake-outputs-size/create-report.sh

70 lines
1.7 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
package_size_table() {
table='| Installable | NAR Size | Closure Size |
|-------------|---------:|-------------:|
'
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
row=$(nix path-info --size --closure-size --human-readable "$path" 2>/dev/null |
sed "s/^\(\S\+\)\(\s\+\)\(\S\+\)\(\s\+\)\(\S\+\)$/| \`$package\` | \3 | \5 |/")
table="$table$row
"
done
printf '%s' "$table"
}
configuration_size_table() {
table='| NixOS Configuration | NAR Size | Closure Size |
|-------------|---------:|-------------:|
'
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
row=$(nix path-info --size --closure-size --human-readable "$path" 2>/dev/null |
sed "s/^\(\S\+\)\(\s\+\)\(\S\+\)\(\s\+\)\(\S\+\)$/| \`$config\` | \3 | \5 |/")
table="$table$row
"
done
printf '%s' "$table"
}
markdown() {
cat <<-EOF
## Outputs' size
### NixOS Configurations sizes
$(configuration_size_table)
### Package sizes
$(package_size_table)
EOF
}
markdown >size-report.md