2021-04-25 14:12:37 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -eu
|
|
|
|
|
|
|
|
NEW_REV=
|
2021-04-27 19:14:49 +02:00
|
|
|
PREVIOUS_REV="$(git rev-parse @~)"
|
2021-04-25 14:12:37 +02:00
|
|
|
OUTPUT_FILE=/dev/stdout
|
|
|
|
FLAKE_OUTPUTS=()
|
|
|
|
NIX_BUILD_ARGS=()
|
|
|
|
|
|
|
|
print_err() {
|
|
|
|
printf "%s\n" "$1" >&2
|
|
|
|
}
|
|
|
|
|
|
|
|
sanitize_output() {
|
|
|
|
if [ "$OUTPUT_FILE" != "/dev/stdout" ]; then
|
2021-04-27 19:14:49 +02:00
|
|
|
sed 's/\x1b\[[0-9;]*m//g'
|
2021-04-25 14:12:37 +02:00
|
|
|
else
|
2021-04-27 19:14:49 +02:00
|
|
|
cat
|
2021-04-25 14:12:37 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-04-28 20:17:41 +02:00
|
|
|
add_shell() {
|
|
|
|
local SYSTEM
|
|
|
|
if [ $# -gt 0 ] && [ -n "$1" ]; then
|
|
|
|
SYSTEM="$1"
|
|
|
|
else
|
|
|
|
SYSTEM="$(nix eval --raw --impure --expr 'builtins.currentSystem')"
|
|
|
|
fi
|
|
|
|
# Use 'inputDerivation' attribute to make sure that it is build-able
|
2022-05-31 13:42:57 +02:00
|
|
|
FLAKE_OUTPUTS+=("devShells.$SYSTEM.default.inputDerivation")
|
2021-04-28 20:17:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
add_host() {
|
|
|
|
FLAKE_OUTPUTS+=("nixosConfigurations.$1.config.system.build.toplevel")
|
|
|
|
}
|
|
|
|
|
2021-04-25 14:12:37 +02:00
|
|
|
usage() {
|
|
|
|
print_err "Usage: $0 [option]... [-- [nix build option]...]"
|
|
|
|
print_err ""
|
|
|
|
print_err " -h, --help"
|
|
|
|
print_err " print this usage screen and exit"
|
|
|
|
print_err " -f, --flake-output"
|
|
|
|
print_err " specify which flake output's closures should be diffed,"
|
|
|
|
print_err " can be used multiple times to specify multiple outputs"
|
|
|
|
print_err " -o, --output"
|
|
|
|
print_err " specify where to output the closure diffs,"
|
|
|
|
print_err " defaults to printing to standard output"
|
|
|
|
print_err " -n, --new-rev"
|
|
|
|
print_err " which git revision should be considered the 'new' state,"
|
|
|
|
print_err " defaults to current state"
|
|
|
|
print_err " -p, --previous-rev"
|
|
|
|
print_err " which git revision should be considered the 'previous' state,"
|
|
|
|
print_err " defaults to HEAD~"
|
2021-04-28 20:17:41 +02:00
|
|
|
print_err " --host [name]"
|
|
|
|
print_err " specify the name of a NixOS output configuration whose"
|
|
|
|
print_err " closure should be diffed, can be used multiple times"
|
|
|
|
print_err " if no host name is given, defaults to current hostname"
|
|
|
|
print_err " --shell [system]"
|
|
|
|
print_err " specify a specific system's devShell output whose closure"
|
|
|
|
print_err " should be diffed, can be used multiple times"
|
|
|
|
print_err " if no system is given, defaults to current system"
|
2021-04-25 14:12:37 +02:00
|
|
|
print_err ""
|
|
|
|
print_err "when no flake outputs are specified, automatically queries for"
|
|
|
|
print_err "all NixOS configurations, and devShell for current system"
|
|
|
|
}
|
|
|
|
|
2021-04-28 20:17:41 +02:00
|
|
|
is_option() {
|
|
|
|
[ $# -gt 0 ] && [[ $1 =~ ^(-.|--.*)$ ]]
|
|
|
|
}
|
|
|
|
|
2021-04-25 14:12:37 +02:00
|
|
|
parse_args() {
|
|
|
|
while [ $# -gt 0 ]; do
|
|
|
|
opt="$1"
|
|
|
|
shift
|
|
|
|
|
|
|
|
case "$opt" in
|
|
|
|
-h|--help)
|
|
|
|
usage
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
-f|--flake-output)
|
2021-04-27 18:43:44 +02:00
|
|
|
FLAKE_OUTPUTS+=("$1")
|
2021-04-25 14:12:37 +02:00
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-o|--output)
|
|
|
|
OUTPUT_FILE="$1"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-n|--new-rev)
|
2021-04-27 19:14:49 +02:00
|
|
|
NEW_REV="$(git rev-parse "$1")"
|
2021-04-25 14:12:37 +02:00
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-p|--previous-rev)
|
2021-04-27 19:14:49 +02:00
|
|
|
PREVIOUS_REV="$(git rev-parse "$1")"
|
2021-04-25 14:12:37 +02:00
|
|
|
shift
|
|
|
|
;;
|
2021-04-28 20:17:41 +02:00
|
|
|
--host)
|
|
|
|
if [ $# -gt 0 ] && ! is_option "$1"; then
|
|
|
|
add_host "$1"
|
|
|
|
shift
|
|
|
|
else
|
|
|
|
add_host "$(hostname)"
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
--shell)
|
|
|
|
if [ $# -gt 0 ] && ! is_option "$1"; then
|
|
|
|
add_shell "$1"
|
|
|
|
shift
|
|
|
|
else
|
|
|
|
add_shell
|
|
|
|
fi
|
|
|
|
;;
|
2021-04-25 14:12:37 +02:00
|
|
|
--)
|
|
|
|
NIX_BUILD_ARGS=("$@")
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
print_err "Unknown argument '$opt'"
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
list_nixos_configurations() {
|
|
|
|
nix eval '.#nixosConfigurations' \
|
|
|
|
--apply 'attrs: with builtins; concatStringsSep "\n" (attrNames attrs)' \
|
|
|
|
--raw
|
|
|
|
}
|
|
|
|
|
|
|
|
diff_output() {
|
|
|
|
local PREV NEW;
|
2021-04-27 19:14:49 +02:00
|
|
|
PREV="$(mktemp --dry-run)"
|
|
|
|
NEW="$(mktemp --dry-run)"
|
2021-04-25 14:12:37 +02:00
|
|
|
|
2021-04-27 18:51:50 +02:00
|
|
|
nix build "${NIX_BUILD_ARGS[@]}" ".?rev=${PREVIOUS_REV}#$1" -o "$PREV"
|
|
|
|
nix build "${NIX_BUILD_ARGS[@]}" ".${NEW_REV:+?rev=$NEW_REV}#$1" -o "$NEW"
|
2021-04-25 14:12:37 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
# shellcheck disable=SC2016
|
|
|
|
printf 'Closure diff for `%s`:\n```\n' "$1"
|
|
|
|
nix store diff-closures "$PREV" "$NEW" | sanitize_output
|
|
|
|
printf '```\n\n'
|
|
|
|
} >> "$OUTPUT_FILE"
|
|
|
|
}
|
|
|
|
|
|
|
|
parse_args "$@"
|
|
|
|
|
|
|
|
if [ "${#FLAKE_OUTPUTS[@]}" -eq 0 ]; then
|
2021-04-28 20:17:41 +02:00
|
|
|
for host in $(list_nixos_configurations); do
|
|
|
|
add_host "$host"
|
|
|
|
done
|
|
|
|
add_shell
|
2021-04-25 14:12:37 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
for out in "${FLAKE_OUTPUTS[@]}"; do
|
|
|
|
diff_output "$out"
|
|
|
|
done
|