nix-flake-outputs-size/create-report.sh
Jalil David Salamé Messina cd38cd76fd
fix(sh): ${VAR:-val} and ${VAR-val} are different
Who would've thought?

`:` means or empty, so:

```sh
VAR=
echo "${VAR-default}" # prints 'default'

VAR=''
echo "${VAR-default}" # prints ''

VAR=''
echo "${VAR:-default}" # prints 'default'
```

;-; nothing wants to work
2024-12-19 23:15:22 +01:00

63 lines
1.5 KiB
Bash
Executable file

#!/usr/bin/env bash
set -eu
util_path="${GITHUB_ACTION_PATH:-.}/utils.sh"
# shellcheck source=utils.sh
. "${util_path}"
group 'Retrieving Flake information'
flake_info=$(nix flake show --json)
endgroup
group 'Show Packages'
packages=$(
jq --raw-output '.packages."x86_64-linux" | select(. != null) | keys[]' <<-EOF
$flake_info
EOF
)
log "$packages"
endgroup
group 'Show NixOS Configurations'
configurations=$(
jq --raw-output '.nixosConfigurations | select(. != null) | keys[]' <<-EOF
$flake_info
EOF
)
log "$configurations"
endgroup
pkgs_json() {
group 'Building packages'
trap endgroup RETURN
for package in $packages; do
log "Building $package"
path=$(nix build --print-out-paths ".#$package")
log "Calculating size of $package"
nix path-info --closure-size --json "$path" |
jq --compact-output --arg pkg "$package" '.[] | {"name": $pkg, "size": .closureSize, "narSize": .narSize}'
done
endgroup
}
configs_json() {
group 'Building NixOS configurations'
trap endgroup RETURN
for config in $configurations; do
log "Building $config"
path=$(nix build --print-out-paths ".#nixosConfigurations.$config.config.system.build.toplevel")
log "Calculating size of $config"
nix path-info --closure-size --json "$path" |
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}'