1
0
Fork 0
mirror of https://github.com/LnL7/nix-darwin.git synced 2024-12-14 11:57:34 +00:00
nix-darwin/pkgs/nix-tools/darwin-option.sh

75 lines
1.4 KiB
Bash
Raw Permalink Normal View History

2016-12-12 16:34:43 +00:00
#! @shell@
set -e
set -o pipefail
export PATH=@path@:$PATH
2016-12-15 20:52:53 +00:00
evalNix() {
nix-instantiate --eval --strict "${extraEvalFlags[@]}" -E "with import <darwin> {}; $*" 2>/dev/null
2016-12-15 20:52:53 +00:00
}
evalOpt() {
evalNix "options.$option.$*"
2016-12-15 20:52:53 +00:00
}
evalOptAttrs() {
evalNix "builtins.concatStringsSep \"\\n\" (builtins.attrNames $*)" | jq -r .
}
evalOptText() {
evalNix "options.$option.$*" | jq -r .
2016-12-15 20:52:53 +00:00
}
showSyntax() {
2019-05-01 11:26:04 +00:00
echo "$0: [-I path] <option>" >&2
evalOptAttrs "options"
exit 1
}
2016-12-15 20:52:53 +00:00
# Parse the command line.
origArgs=("$@")
2019-05-01 11:26:04 +00:00
extraEvalFlags=()
2016-12-15 20:52:53 +00:00
option=
while [ "$#" -gt 0 ]; do
i="$1"; shift 1
case "$i" in
--help)
showSyntax
;;
2019-05-01 11:26:04 +00:00
-I)
if [ -z "$1" ]; then
echo "$0: $i requires an argument"
exit 1
fi
j="$1"; shift 1
extraEvalFlags+=("$i" "$j")
;;
2016-12-15 20:52:53 +00:00
*)
option="$i"
;;
esac
done
if [ -z "$option" ]; then showSyntax; fi
2016-12-15 21:03:06 +00:00
if [ "$(evalOpt "_type")" = '"option"' ]; then
2016-12-15 20:52:53 +00:00
echo "Value:"
2016-12-15 21:03:06 +00:00
evalOpt "value" || echo "no value"
2016-12-15 20:52:53 +00:00
echo
echo "Default:"
evalOpt "default" || evalOptText "defaultText" || echo "no default"
2016-12-15 20:52:53 +00:00
echo
echo "Example:"
if [ "$(evalOpt "example._type")" = '"literalExpression"' ]; then
evalOptText "example.text" || echo "no example"
else
evalOpt "example" || echo "no example"
fi
2016-12-15 20:52:53 +00:00
echo
echo "Description:"
evalOptText "description.text" || echo "no description"
2016-12-15 20:52:53 +00:00
echo
else
evalOptAttrs "options.$option"
2016-12-15 20:52:53 +00:00
fi