Skip to content

Instantly share code, notes, and snippets.

@jorsn
Created June 27, 2019 22:59
Show Gist options
  • Save jorsn/9cb109b3192b03016e6a0914cbcb1bd8 to your computer and use it in GitHub Desktop.
Save jorsn/9cb109b3192b03016e6a0914cbcb1bd8 to your computer and use it in GitHub Desktop.
A convenience shell wrapper around the nix function unsafeGetAttrPos.
#!/bin/sh
pname="$(basename "$0")"
read -d '' usage <<EOF
Usage: $pname -h|--help
$pname [-n <number of context lines>] [attrset (default: nixpkgs)] <attr name>
EOF
attrset="import <nixpkgs> {}"
n=3
case "$1" in
-h | --help ) echo "$usage" >&2; exit;;
-n ) n="$2"; shift 2;;
esac
if [ "$#" -gt 1 ]; then
attrset="$1"
shift
fi
attr="$1"
read -d '' expr <<EOF
let
inherit (builtins.unsafeGetAttrPos "${attr}" (${attrset})) file line;
in "\${file}:\${builtins.toString line}"
EOF
attrPos="$(nix eval --raw "($expr)")"
file="${attrPos%:*}"
line="${attrPos##*:}"
if [ $n -gt 0 ]; then
context="-e $((line-n)),$((line-1))p -e $((line+1)),$((line+n))p"
fi
echo "${attrPos}:"
sed -n $context -e ${line}'s/^.\+$/&/gp' "$file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment