#! /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\+\) = ; }\?/\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" \ --argstr host-name "$HOSTNAME" \ ${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