git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #5415 from nbp/nixos-options-submodules

nixos-option: Handle submodules

commite4fb4168fce77044aa896834eb3fedd6e03ee795 authorNicolas B. Pierron <nicolas.b.pierron@gmail.com> dateSun 21 Dec 2014, 15:10 (12 years ago) parent7743607974edc5
1 file changed+76−12
nixos/modules/installer/tools/nixos-option.sh+76−12
nixos/modules/installer/tools/nixos-option.sh+76−12
@@ -83,6 +83,68 @@ EOF
8383 fi
8484}
8585
86+header="let
87+ nixos = import <nixpkgs/nixos> {};
88+ nixpkgs = import <nixpkgs> {};
89+in with nixpkgs.lib;
90+"
91+
92+# This function is used for converting the option definition path given by
93+# the user into accessors for reaching the definition and the declaration
94+# corresponding to this option.
95+generateAccessors(){
96+ if result=$(evalNix --strict --show-trace <<EOF
97+$header
98+
99+let
100+ path = "${option:+$option}";
101+ pathList = splitString "." path;
102+
103+ walkOptions = attrsNames: result:
104+ if attrsNames == [] then
105+ result
106+ else
107+ let name = head attrsNames; rest = tail attrsNames; in
108+ if isOption result.options then
109+ walkOptions rest {
110+ options = result.options.type.getSubOptions "";
111+ opt = ''(\${result.opt}.type.getSubOptions "")'';
112+ cfg = ''\${result.cfg}."\${name}"'';
113+ }
114+ else
115+ walkOptions rest {
116+ options = result.options.\${name};
117+ opt = ''\${result.opt}."\${name}"'';
118+ cfg = ''\${result.cfg}."\${name}"'';
119+ }
120+ ;
121+
122+ walkResult = (if path == "" then x: x else walkOptions pathList) {
123+ options = nixos.options;
124+ opt = ''nixos.options'';
125+ cfg = ''nixos.config'';
126+ };
127+
128+in
129+ ''let option = \${walkResult.opt}; config = \${walkResult.cfg}; in''
130+EOF
131+)
132+ then
133+ echo $result
134+ else
135+ # In case of error we want to ignore the error message roduced by the
136+ # script above, as it is iterating over each attribute, which does not
137+ # produce a nice error message. The following code is a fallback
138+ # solution which is cause a nicer error message in the next
139+ # evaluation.
140+ echo "\"let option = nixos.options${option:+.$option}; config = nixos.config${option:+.$option}; in\""
141+ fi
142+}
143+
144+header="$header
145+$(eval echo $(generateAccessors))
146+"
147+
86148evalAttr(){
87149 local prefix="$1"
88150 local strict="$2"
@@ -92,10 +154,10 @@ evalAttr(){
92154 test -n "$strict" && strict=true
93155
94156 evalNix ${strict:+--strict} <<EOF
157+$header
158+
95159let
96− reach = attrs: attrs${option:+.$option}${suffix:+.$suffix};
97− nixos = import <nixos> {};
98− nixpkgs = import <nixpkgs> {};
160+ value = $prefix${suffix:+.$suffix};
99161 strict = ${strict:-false};
100162 cleanOutput = x: with nixpkgs.lib;
101163 if isDerivation x then x.outPath
@@ -106,12 +168,12 @@ let
106168 else x
107169 else x;
108170in
109− cleanOutput (reach nixos.$prefix)
171+ cleanOutput value
110172EOF
111173}
112174
113175evalOpt(){
114− evalAttr "options" "" "$@"
176+ evalAttr "option" "" "$@"
115177}
116178
117179evalCfg(){
@@ -121,8 +183,11 @@ evalCfg(){
121183
122184findSources(){
123185 local suffix=$1
124− echo "(import <nixos> {}).options${option:+.$option}.$suffix" |
125− evalNix --strict
186+ evalNix --strict <<EOF
187+$header
188+
189+option.$suffix
190+EOF
126191}
127192
128193# Given a result from nix-instantiate, recover the list of attributes it
@@ -152,13 +217,12 @@ nixMap() {
152217# the output of nixos-option with other tools such as nixos-gui.
153218if $xml; then
154219 evalNix --xml --no-location <<EOF
220+$header
221+
155222let
156− reach = attrs: attrs${option:+.$option};
157− nixos = import <nixos> {};
158− nixpkgs = import <nixpkgs> {};
159223 sources = builtins.map (f: f.source);
160− opt = reach nixos.options;
161− cfg = reach nixos.config;
224+ opt = option;
225+ cfg = config;
162226in
163227
164228with nixpkgs.lib;