2010-09-03 19:10:50 +00:00
|
|
|
#! @shell@ -e
|
|
|
|
|
|
|
|
usage () {
|
2013-10-11 13:35:41 +00:00
|
|
|
exec man nixos-option
|
2013-10-11 12:12:36 +00:00
|
|
|
exit 1
|
2010-09-03 19:10:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#####################
|
|
|
|
# Process Arguments #
|
|
|
|
#####################
|
|
|
|
|
|
|
|
desc=false
|
|
|
|
defs=false
|
|
|
|
value=false
|
2011-04-24 15:30:25 +00:00
|
|
|
xml=false
|
2010-09-03 19:10:50 +00:00
|
|
|
verbose=false
|
|
|
|
|
|
|
|
option=""
|
|
|
|
|
|
|
|
argfun=""
|
|
|
|
for arg; do
|
|
|
|
if test -z "$argfun"; then
|
|
|
|
case $arg in
|
2010-09-25 22:29:50 +00:00
|
|
|
-*)
|
|
|
|
longarg=""
|
|
|
|
sarg="$arg"
|
|
|
|
while test "$sarg" != "-"; do
|
|
|
|
case $sarg in
|
2010-11-09 18:42:59 +00:00
|
|
|
--*) longarg=$arg; sarg="--";;
|
2010-09-25 22:29:50 +00:00
|
|
|
-d*) longarg="$longarg --description";;
|
|
|
|
-v*) longarg="$longarg --value";;
|
|
|
|
-l*) longarg="$longarg --lookup";;
|
|
|
|
-*) usage;;
|
|
|
|
esac
|
2010-11-09 18:42:59 +00:00
|
|
|
# remove the first letter option
|
2010-09-25 22:29:50 +00:00
|
|
|
sarg="-${sarg#??}"
|
|
|
|
done
|
|
|
|
;;
|
|
|
|
*) longarg=$arg;;
|
2010-09-03 19:10:50 +00:00
|
|
|
esac
|
2010-09-25 22:29:50 +00:00
|
|
|
for larg in $longarg; do
|
|
|
|
case $larg in
|
|
|
|
--description) desc=true;;
|
|
|
|
--value) value=true;;
|
|
|
|
--lookup) defs=true;;
|
2011-04-24 15:30:25 +00:00
|
|
|
--xml) xml=true;;
|
2010-09-25 22:29:50 +00:00
|
|
|
--verbose) verbose=true;;
|
|
|
|
--help) usage;;
|
|
|
|
-*) usage;;
|
|
|
|
*) if test -z "$option"; then
|
|
|
|
option="$larg"
|
|
|
|
else
|
|
|
|
usage
|
|
|
|
fi;;
|
|
|
|
esac
|
|
|
|
done
|
2010-09-03 19:10:50 +00:00
|
|
|
else
|
|
|
|
case $argfun in
|
|
|
|
set_*)
|
|
|
|
var=$(echo $argfun | sed 's,^set_,,')
|
|
|
|
eval $var=$arg
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
argfun=""
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2011-04-24 15:30:25 +00:00
|
|
|
if $xml; then
|
|
|
|
value=true
|
|
|
|
desc=true
|
|
|
|
defs=true
|
|
|
|
fi
|
|
|
|
|
2010-09-03 19:10:50 +00:00
|
|
|
if ! $defs && ! $desc; then
|
|
|
|
value=true
|
|
|
|
fi
|
|
|
|
|
|
|
|
if $verbose; then
|
|
|
|
set -x
|
|
|
|
else
|
|
|
|
set +x
|
|
|
|
fi
|
|
|
|
|
|
|
|
#############################
|
|
|
|
# Process the configuration #
|
|
|
|
#############################
|
|
|
|
|
2011-04-24 15:30:25 +00:00
|
|
|
evalNix(){
|
|
|
|
nix-instantiate - --eval-only "$@"
|
|
|
|
}
|
|
|
|
|
2010-09-03 19:10:50 +00:00
|
|
|
evalAttr(){
|
|
|
|
local prefix=$1
|
|
|
|
local suffix=$2
|
|
|
|
local strict=$3
|
2012-03-13 09:36:41 +00:00
|
|
|
echo "(import <nixos> {}).$prefix${option:+.$option}${suffix:+.$suffix}" |
|
2011-04-24 15:30:25 +00:00
|
|
|
evalNix ${strict:+--strict}
|
2010-09-03 19:10:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
evalOpt(){
|
|
|
|
evalAttr "eval.options" "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
evalCfg(){
|
|
|
|
evalAttr "config" "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
findSources(){
|
|
|
|
local suffix=$1
|
2012-03-13 09:36:41 +00:00
|
|
|
echo "builtins.map (f: f.source) (import <nixos> {}).eval.options${option:+.$option}.$suffix" |
|
2011-04-24 15:30:25 +00:00
|
|
|
evalNix --strict
|
2010-09-03 19:10:50 +00:00
|
|
|
}
|
|
|
|
|
2011-12-29 06:33:34 +00:00
|
|
|
# Given a result from nix-instantiate, recover the list of attributes it
|
|
|
|
# contains.
|
|
|
|
attrNames() {
|
|
|
|
local attributeset=$1
|
|
|
|
# sed is used to replace un-printable subset by 0s, and to remove most of
|
|
|
|
# the inner-attribute set, which reduce the likelyhood to encounter badly
|
|
|
|
# pre-processed input.
|
|
|
|
echo "builtins.attrNames $attributeset" | \
|
|
|
|
sed 's,<[A-Z]*>,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
|
|
|
|
}
|
|
|
|
|
2012-05-14 01:56:42 +00:00
|
|
|
# This duplicates the work made below, but it is useful for processing
|
|
|
|
# the output of nixos-option with other tools such as nixos-gui.
|
2011-04-24 15:30:25 +00:00
|
|
|
if $xml; then
|
|
|
|
evalNix --xml --no-location <<EOF
|
|
|
|
let
|
|
|
|
reach = attrs: attrs${option:+.$option};
|
2012-03-13 09:36:41 +00:00
|
|
|
nixos = import <nixos> {};
|
|
|
|
nixpkgs = import <nixpkgs> {};
|
2011-04-24 15:30:25 +00:00
|
|
|
sources = builtins.map (f: f.source);
|
|
|
|
opt = reach nixos.eval.options;
|
|
|
|
cfg = reach nixos.config;
|
|
|
|
in
|
|
|
|
|
|
|
|
with nixpkgs.lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
optStrict = v:
|
|
|
|
let
|
|
|
|
traverse = x :
|
|
|
|
if isAttrs x then
|
|
|
|
if x ? outPath then true
|
|
|
|
else all id (mapAttrsFlatten (n: traverseNoAttrs) x)
|
|
|
|
else traverseNoAttrs x;
|
|
|
|
traverseNoAttrs = x:
|
|
|
|
# do not continue in attribute sets
|
|
|
|
if isAttrs x then true
|
|
|
|
else if isList x then all id (map traverse x)
|
|
|
|
else true;
|
|
|
|
in assert traverse v; v;
|
|
|
|
in
|
|
|
|
|
|
|
|
if isOption opt then
|
|
|
|
optStrict ({}
|
|
|
|
// optionalAttrs (opt ? default) { inherit (opt) default; }
|
|
|
|
// optionalAttrs (opt ? example) { inherit (opt) example; }
|
|
|
|
// optionalAttrs (opt ? description) { inherit (opt) description; }
|
|
|
|
// optionalAttrs (opt ? type) { typename = opt.type.name; }
|
|
|
|
// optionalAttrs (opt ? options) { inherit (opt) options; }
|
|
|
|
// {
|
|
|
|
# to disambiguate the xml output.
|
|
|
|
_isOption = true;
|
|
|
|
declarations = sources opt.declarations;
|
|
|
|
definitions = sources opt.definitions;
|
|
|
|
value = cfg;
|
|
|
|
})
|
|
|
|
else
|
|
|
|
opt
|
|
|
|
EOF
|
|
|
|
exit $?
|
|
|
|
fi
|
|
|
|
|
2010-09-03 19:10:50 +00:00
|
|
|
if test "$(evalOpt "_type" 2> /dev/null)" = '"option"'; then
|
|
|
|
$value && evalCfg;
|
|
|
|
|
|
|
|
if $desc; then
|
|
|
|
$value && echo;
|
|
|
|
|
|
|
|
if default=$(evalOpt "default" - 2> /dev/null); then
|
|
|
|
echo "Default: $default"
|
|
|
|
else
|
|
|
|
echo "Default: <None>"
|
|
|
|
fi
|
|
|
|
if example=$(evalOpt "example" - 2> /dev/null); then
|
|
|
|
echo "Example: $example"
|
|
|
|
fi
|
|
|
|
echo "Description:"
|
|
|
|
eval printf $(evalOpt "description")
|
|
|
|
fi
|
|
|
|
|
|
|
|
if $defs; then
|
|
|
|
$desc || $value && echo;
|
|
|
|
|
2011-12-29 06:33:34 +00:00
|
|
|
printPath () { echo " $1"; }
|
|
|
|
|
2010-09-03 19:10:50 +00:00
|
|
|
echo "Declared by:"
|
2011-12-29 06:33:34 +00:00
|
|
|
nixMap printPath "$(findSources "declarations")"
|
2010-09-03 19:10:50 +00:00
|
|
|
echo ""
|
|
|
|
echo "Defined by:"
|
2011-12-29 06:33:34 +00:00
|
|
|
nixMap printPath "$(findSources "definitions")"
|
2010-09-03 19:10:50 +00:00
|
|
|
echo ""
|
|
|
|
fi
|
|
|
|
|
|
|
|
else
|
|
|
|
# echo 1>&2 "Warning: This value is not an option."
|
|
|
|
|
|
|
|
result=$(evalCfg)
|
2011-12-29 06:33:34 +00:00
|
|
|
if names=$(attrNames "$result" 2> /dev/null); then
|
|
|
|
echo 1>&2 "This attribute set contains:"
|
|
|
|
escapeQuotes () { eval echo "$1"; }
|
|
|
|
nixMap escapeQuotes "$names"
|
2010-09-03 19:10:50 +00:00
|
|
|
else
|
2011-12-29 06:33:34 +00:00
|
|
|
echo 1>&2 "An error occured while looking for attribute names."
|
2010-09-03 19:10:50 +00:00
|
|
|
echo $result
|
|
|
|
fi
|
|
|
|
fi
|