diff options
author | Nicolas B. Pierron <nicolas.b.pierron@gmail.com> | 2014-12-21 15:10:46 -0800 |
---|---|---|
committer | Nicolas B. Pierron <nicolas.b.pierron@gmail.com> | 2014-12-21 15:10:46 -0800 |
commit | e4fb4168fce77044aa896834eb3fedd6e03ee795 (patch) | |
tree | 8651c6fb3a5c9bca286971d5f8299512bcdbbebe /nixos/modules/installer | |
parent | 77436079e9e07a4c8aa0bc9055207843fb7b330b (diff) | |
parent | 974edc5056616e3a6250277459f40974dfeba705 (diff) | |
download | nixlib-e4fb4168fce77044aa896834eb3fedd6e03ee795.tar nixlib-e4fb4168fce77044aa896834eb3fedd6e03ee795.tar.gz nixlib-e4fb4168fce77044aa896834eb3fedd6e03ee795.tar.bz2 nixlib-e4fb4168fce77044aa896834eb3fedd6e03ee795.tar.lz nixlib-e4fb4168fce77044aa896834eb3fedd6e03ee795.tar.xz nixlib-e4fb4168fce77044aa896834eb3fedd6e03ee795.tar.zst nixlib-e4fb4168fce77044aa896834eb3fedd6e03ee795.zip |
Merge pull request #5415 from nbp/nixos-options-submodules
nixos-option: Handle submodules
Diffstat (limited to 'nixos/modules/installer')
-rw-r--r-- | nixos/modules/installer/tools/nixos-option.sh | 88 |
1 files changed, 76 insertions, 12 deletions
diff --git a/nixos/modules/installer/tools/nixos-option.sh b/nixos/modules/installer/tools/nixos-option.sh index 4478059813d1..3016536ecddf 100644 --- a/nixos/modules/installer/tools/nixos-option.sh +++ b/nixos/modules/installer/tools/nixos-option.sh @@ -83,6 +83,68 @@ EOF fi } +header="let + nixos = import <nixpkgs/nixos> {}; + nixpkgs = import <nixpkgs> {}; +in with nixpkgs.lib; +" + +# This function is used for converting the option definition path given by +# the user into accessors for reaching the definition and the declaration +# corresponding to this option. +generateAccessors(){ + if result=$(evalNix --strict --show-trace <<EOF +$header + +let + path = "${option:+$option}"; + pathList = splitString "." path; + + walkOptions = attrsNames: result: + if attrsNames == [] then + result + else + let name = head attrsNames; rest = tail attrsNames; in + if isOption result.options then + walkOptions rest { + options = result.options.type.getSubOptions ""; + opt = ''(\${result.opt}.type.getSubOptions "")''; + cfg = ''\${result.cfg}."\${name}"''; + } + else + walkOptions rest { + options = result.options.\${name}; + opt = ''\${result.opt}."\${name}"''; + cfg = ''\${result.cfg}."\${name}"''; + } + ; + + walkResult = (if path == "" then x: x else walkOptions pathList) { + options = nixos.options; + opt = ''nixos.options''; + cfg = ''nixos.config''; + }; + +in + ''let option = \${walkResult.opt}; config = \${walkResult.cfg}; in'' +EOF +) + then + echo $result + else + # In case of error we want to ignore the error message roduced by the + # script above, as it is iterating over each attribute, which does not + # produce a nice error message. The following code is a fallback + # solution which is cause a nicer error message in the next + # evaluation. + echo "\"let option = nixos.options${option:+.$option}; config = nixos.config${option:+.$option}; in\"" + fi +} + +header="$header +$(eval echo $(generateAccessors)) +" + evalAttr(){ local prefix="$1" local strict="$2" @@ -92,10 +154,10 @@ evalAttr(){ test -n "$strict" && strict=true evalNix ${strict:+--strict} <<EOF +$header + let - reach = attrs: attrs${option:+.$option}${suffix:+.$suffix}; - nixos = import <nixos> {}; - nixpkgs = import <nixpkgs> {}; + value = $prefix${suffix:+.$suffix}; strict = ${strict:-false}; cleanOutput = x: with nixpkgs.lib; if isDerivation x then x.outPath @@ -106,12 +168,12 @@ let else x else x; in - cleanOutput (reach nixos.$prefix) + cleanOutput value EOF } evalOpt(){ - evalAttr "options" "" "$@" + evalAttr "option" "" "$@" } evalCfg(){ @@ -121,8 +183,11 @@ evalCfg(){ findSources(){ local suffix=$1 - echo "(import <nixos> {}).options${option:+.$option}.$suffix" | - evalNix --strict + evalNix --strict <<EOF +$header + +option.$suffix +EOF } # Given a result from nix-instantiate, recover the list of attributes it @@ -152,13 +217,12 @@ nixMap() { # the output of nixos-option with other tools such as nixos-gui. if $xml; then evalNix --xml --no-location <<EOF +$header + let - reach = attrs: attrs${option:+.$option}; - nixos = import <nixos> {}; - nixpkgs = import <nixpkgs> {}; sources = builtins.map (f: f.source); - opt = reach nixos.options; - cfg = reach nixos.config; + opt = option; + cfg = config; in with nixpkgs.lib; |