1
0
Fork 0
mirror of https://github.com/LnL7/nix-darwin.git synced 2024-12-15 17:51:01 +00:00
nix-darwin/pkgs/nix-tools/darwin-option.sh
Daiderd Jordan cb37c35e33
fix darwin-option descriptions
Also drops the eval hacks in favour of jq.
2023-06-25 13:03:52 +02:00

74 lines
1.4 KiB
Bash
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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