#!/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}'