diff options
author | tv <tv@shackspace.de> | 2015-09-25 22:50:03 +0200 |
---|---|---|
committer | tv <tv@shackspace.de> | 2015-09-25 22:50:03 +0200 |
commit | a39d54aa2e28d8b15a5879024f64f3f41dee9f3b (patch) | |
tree | 9cd2583edef4e90bd33b1b5683dd36f8a8cc0f27 |
initial commit
-rwxr-xr-x | get | 91 |
1 files changed, 91 insertions, 0 deletions
@@ -0,0 +1,91 @@ +#! /bin/sh +# +# NAME +# get - evaluate ./default.nix +# +# SYNOPSIS +# get [-jlprs] [attrPath] +# +# DESCRIPTION +# The command nix evaluates the Nix expression in ./default.nix. +# +# OPTIONS +# +# -j Format output as JSON. Implies -s. +# +# -l List attrNames, one per line. +# +# -p Pretty-print output. (Only effectiv in conjunction with -j.) +# +# -r If result looks is a string, then it will be written directly to +# standard output rather than being formatted as JSON or Nix string +# with quotes. Refs jq(1)'s --raw-output. +# +# -s Recursively evaluate list elements and attributes. +# Refs nix-instantiate(1)'s --strict. +# +set -efu + +args=$* + +has_lopt() { + echo "$args" | grep -q '\(^\|\s\)--'"$1"'\(\s\|$\)' +} + +has_sopt() { + echo "$args" | grep -q '\(^\|\s\)-[a-z]*'"$1"'[a-z]*\(\s\|$\)' +} + +filter() { cat; } + +if has_sopt j; then + json=1 + strict=1 +fi + +if has_sopt l; then + unset json + unset strict + filter() { sed 's/\({ \)\?\(\S\+\) = <CODE>; }\?/\2\n/g' | grep .; } +fi + +if has_sopt p && has_sopt j; then + filter() { jq .; } +fi + +if has_sopt r; then + raw_output=1 +fi + +if has_sopt s; then + strict=1 +fi + + +if x=$(nix-instantiate --json --eval -A NIX_PATH 2>/dev/null); then + NIX_PATH=$(echo "$x" | jq -r .) + export NIX_PATH + unset x +fi + +result=$(nix-instantiate \ + --eval \ + --argstr user-name "$LOGNAME" \ + ${json+--json} \ + ${strict+--strict} \ + $( + for i; do + echo "$i" + done | sed -n ' + s/^[A-Za-z_][0-9A-Za-z_.-]*$/--attr &/p + s/^\([0-9A-Za-z-]\+\)=\([0-9A-Za-z-]*\)$/--argstr \1-name \2/p + ' + )) + +case ${raw_output-0} in 1) + case ${result:0:1} in \") + result=$(echo "$result" | jq -e -r .) + esac +esac + +echo "$result" | filter |