Skip to content

Instantly share code, notes, and snippets.

Created April 12, 2020 03:12
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save gnprice/1864b871656773f78f419201e8b5cda3 to your computer and use it in GitHub Desktop.
set -eu
usage() {
cat >&2 <<'EOF'
usage: ./difflog $out1 $out2
Compare two Nix build logs, normalizing out boring differences.
This can be useful to test the effect of a change to build scripts,
especially a refactoring that's intended to have no effect.
Each argument can be either:
* the store path of a build output, or
* the store path of a derivation for the build.
Each argument will be passed to `nix log` to get the log,
and also used to find the output hash to normalize out.
Normalization includes:
* replacing the output hash with zeroes
* stripping ECMA-48(-style) terminal controls, like colors
* replacing dates, timestamps, and timings with constants
* sorting runs of certain common messages, to reduce
nondeterminism from concurrency
extract_hash() {
local b="${1##*/}"
echo "${b%%-*}"
find_hash() {
local outpath
if [[ "$1" = *.drv ]]; then
outpath=$(nix show-derivation "$1" \
| jq -r '
to_entries | .[0].value.outputs.out.path
extract_hash "$outpath"
striplog() {
hash=$(find_hash "$1")
# `expand` is because in `make` trace lines, I see
# continuation lines indented with tabs locally
# but spaces in cached builds.
nix log "$1" \
| expand \
| hash="$hash" \
perl -0pe '
# Normalize away the output hash.
# I see these locally but not in cached builds.
s<^\@nix \{.*\n><>gm;
# ECMA-48 control sequences. I see color in gcc warnings
# locally, but not in cached builds.
# And some other kinds of ECMA-48(-style) terminal magic.
# Fix some dates that sneak in impurely.
s<^configure: autobuild timestamp\.\.\. \K\d+-\d+>
# And timestamps, and times.
s<^\d?\K ?\d:\d\d\.\d+ >
< 0:00.00 >gm;
s<\b(time: |elapsed: |in )\K\d+(\.\d+)?s>
# Sort runs of adjacent lines of each of these types.
s<(^(libtool: compile: |/nix/store/\S+/bin/bash \S+/libtool ).*\n)+>
< join("", sort(split /^/, $&)) >egm;
s<(^(libtool: link: |/nix/store/\S+/bin/bash \S+/libtool ).*\n)+>
< join("", sort(split /^/, $&)) >egm;
s<(^shrinking /nix/store/.*\n)+>
< join("", sort(split /^/, $&)) >egm;
s<(^/nix/store/\S+: interpreter directive changed .*\n)+>
< join("", sort(split /^/, $&)) >egm;
d=$(mktemp -d)
if ! (( $# == 2 )); then
exit 2
striplog "$1" >"$d"/a.log
striplog "$2" >"$d"/b.log
git diff --no-index "$d"/{a,b}.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment