#! @shell@ -e # FIXME: rewrite this in a more suitable language. usage () { exec man nixos-option exit 1 } ##################### # Process Arguments # ##################### xml=false verbose=false nixPath="" option="" exit_code=0 argfun="" for arg; do if test -z "$argfun"; then case $arg in -*) sarg="$arg" longarg="" while test "$sarg" != "-"; do case $sarg in --*) longarg=$arg; sarg="--";; -I) argfun="include_nixpath";; -*) usage;; esac # remove the first letter option sarg="-${sarg#??}" done ;; *) longarg=$arg;; esac for larg in $longarg; do case $larg in --xml) xml=true;; --verbose) verbose=true;; --help) usage;; -*) usage;; *) if test -z "$option"; then option="$larg" else usage fi;; esac done else case $argfun in set_*) var=$(echo $argfun | sed 's,^set_,,') eval $var=$arg ;; include_nixpath) nixPath="-I $arg $nixPath" ;; esac argfun="" fi done if $verbose; then set -x else set +x fi ############################# # Process the configuration # ############################# evalNix(){ # disable `-e` flag, it's possible that the evaluation of `nix-instantiate` fails (e.g. due to broken pkgs) set +e result=$(nix-instantiate ${nixPath:+$nixPath} - --eval-only "$@" 2>&1) exit_code=$? set -e if test $exit_code -eq 0; then cat <" else if strict then if isAttrs x then mapAttrs (n: cleanOutput) x else if isList x then map cleanOutput x else x else x; in cleanOutput value EOF } evalOpt(){ evalAttr "option" "" "$@" } evalCfg(){ local strict="$1" evalAttr "config" "$strict" } findSources(){ local suffix=$1 evalNix --strict <,0,g; :inner; s/{[^\{\}]*};/0;/g; t inner;' | \ evalNix --strict } # map a simple list which contains strings or paths. nixMap() { local fun="$1" local list="$2" local elem for elem in $list; do test $elem = '[' -o $elem = ']' && continue; $fun $elem done } # This duplicates the work made below, but it is useful for processing # the output of nixos-option with other tools such as nixos-gui. if $xml; then evalNix --xml --no-location < /dev/null)" = '"option"'; then echo "Value:" evalCfg 1 echo echo "Default:" if default=$(evalOpt "default" - 2> /dev/null); then echo "$default" else echo "" fi echo if example=$(evalOpt "example" - 2> /dev/null); then echo "Example:" echo "$example" echo fi echo "Description:" echo echo $(evalOpt "description") echo $desc; printPath () { echo " $1"; } echo "Declared by:" nixMap printPath "$(findSources "declarations")" echo echo "Defined by:" nixMap printPath "$(findSources "files")" echo else # echo 1>&2 "Warning: This value is not an option." result=$(evalCfg "") if names=$(attrNames "$result" 2> /dev/null); then echo 1>&2 "This attribute set contains:" escapeQuotes () { eval echo "$1"; } nixMap escapeQuotes "$names" else echo 1>&2 "An error occurred while looking for attribute names." echo $result fi fi exit $exit_code