Skip to content

Instantly share code, notes, and snippets.

@shanemikel
Created January 23, 2017 21:46
Show Gist options
  • Save shanemikel/7e0c057a00906a66954f83f6ed876845 to your computer and use it in GitHub Desktop.
Save shanemikel/7e0c057a00906a66954f83f6ed876845 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
if [ $# -ne 2 ]; then
echo expecting two integer arguments... aborting >&2
exit 1
fi
profile=$(readlink ~/.nix-profile)
dirA="${profile}-$1-link"
dirB="${profile}-$2-link"
no_generation() {
local gen="$1"
echo no generation \`"$gen"\` found >&2
exit 1
}
if [[ ! -L "$dirA" ]]; then
no_generation "$dirA"
elif [[ ! -L "$dirB" ]]; then
no_generation "$dirB"
fi
arr=tmp_files
arr_all="\${$arr[@]}"
arr_len="\${#$arr[@]}"
arr_last="\${$arr[\$(($len-1))]}"
mk_temp() {
eval tmp=$arr_len
tmp_files[$tmp]=$(mktemp)
}
rm_temps() {
if [ -n "$DEBUG" ]; then
echo DEBUG: removing temporary files >&2
fi
eval tmp=$arr_all
rm -f $tmp
}
trap 'rm_temps' EXIT
mk_temp
eval versA=$arr_last
[ -n "$DEBUG" ] && echo DEBUG: "versA=$versA"
nix-store -q --references "$dirA" | sort > "$versA"
mk_temp
eval versB=$arr_last
[ -n "$DEBUG" ] && echo DEBUG: "versB=$versB"
nix-store -q --references "$dirB" | sort > "$versB"
in=$(comm -3 -1 "$versA" "$versB")
mk_temp
eval add=$arr_last
[ -n "$DEBUG" ] && echo DEBUG: "add=$add"
sed -r 's:^[^-]*-(.*)$:\1+:' <(echo "$in") \
| grep -v env-manifest.nix \
| sort -f \
> "$add"
un=$(comm -3 -2 "$versA" "$versB")
mk_temp
eval rem=$arr_last
[ -n "$DEBUG" ] && echo DEBUG: "rem=$rem"
sed -r 's:^[^-]*-(.*)$:\1-:' <(echo "$un") \
| grep -v env-manifest.nix \
| sort -f \
> "$rem"
cat "$rem" "$add" \
| sort -f \
| sed -r 's:(.*)-$:- \1:' \
| sed -r 's:(.*)\+$:\+ \1:'
[ -n "$DEBUG" ] && sleep 2s
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment