about summary refs log tree commit diff
path: root/nixos/doc
diff options
context:
space:
mode:
authorNicolas B. Pierron <nicolas.b.pierron@nbp.name>2014-09-07 10:39:03 -0700
committerNicolas B. Pierron <nicolas.b.pierron@nbp.name>2014-09-07 10:39:03 -0700
commita4e60ebacf95538ca56ebcd9ee1d8727cc475173 (patch)
tree431a16408b3292cde23dbe0c2688c751a3915a6a /nixos/doc
parent476b94a34dbdc37379e7f3d6fb1dd02d1d2852b5 (diff)
parent79411db08b86a606f3658c66f05e0508e933c8f5 (diff)
downloadnixlib-a4e60ebacf95538ca56ebcd9ee1d8727cc475173.tar
nixlib-a4e60ebacf95538ca56ebcd9ee1d8727cc475173.tar.gz
nixlib-a4e60ebacf95538ca56ebcd9ee1d8727cc475173.tar.bz2
nixlib-a4e60ebacf95538ca56ebcd9ee1d8727cc475173.tar.lz
nixlib-a4e60ebacf95538ca56ebcd9ee1d8727cc475173.tar.xz
nixlib-a4e60ebacf95538ca56ebcd9ee1d8727cc475173.tar.zst
nixlib-a4e60ebacf95538ca56ebcd9ee1d8727cc475173.zip
Merge pull request #3811 from nbp/options-json
Export the list of options to XML & JSON, such that external tools can use it.
Diffstat (limited to 'nixos/doc')
-rw-r--r--nixos/doc/manual/default.nix49
1 files changed, 43 insertions, 6 deletions
diff --git a/nixos/doc/manual/default.nix b/nixos/doc/manual/default.nix
index 47e01437ccc4..bd89ad4eb9cc 100644
--- a/nixos/doc/manual/default.nix
+++ b/nixos/doc/manual/default.nix
@@ -6,12 +6,21 @@ with pkgs.lib;
 let
 
   # Remove invisible and internal options.
-  options' = filter (opt: opt.visible && !opt.internal) (optionAttrSetToDocList options);
+  optionsList = filter (opt: opt.visible && !opt.internal) (optionAttrSetToDocList options);
+
+  # Replace functions by the string <function>
+  substFunction = x:
+    if builtins.isAttrs x then mapAttrs (name: substFunction) x
+    else if builtins.isList x then map substFunction x
+    else if builtins.isFunction x then "<function>"
+    else x;
 
   # Clean up declaration sites to not refer to the NixOS source tree.
-  options'' = flip map options' (opt: opt // {
+  optionsList' = flip map optionsList (opt: opt // {
     declarations = map (fn: stripPrefix fn) opt.declarations;
-  });
+  }
+  // optionalAttrs (opt ? example) { example = substFunction opt.example; }
+  // optionalAttrs (opt ? default) { default = substFunction opt.default; });
 
   prefix = toString ../../..;
 
@@ -21,10 +30,35 @@ let
     else
       fn;
 
-  optionsXML = builtins.toFile "options.xml" (builtins.unsafeDiscardStringContext (builtins.toXML options''));
+  # Convert the list of options into an XML file and a JSON file.  The builtin
+  # unsafeDiscardStringContext is used to prevent the realisation of the store
+  # paths which are used in options definitions.
+  optionsXML = builtins.toFile "options.xml" (builtins.unsafeDiscardStringContext (builtins.toXML optionsList'));
+  optionsJSON = builtins.toFile "options.json" (builtins.unsafeDiscardStringContext (builtins.toJSON optionsList'));
+
+  # Tools-friendly version of the list of NixOS options.
+  options' = stdenv.mkDerivation {
+    name = "options";
+
+    buildCommand = ''
+      # Export list of options in different format.
+      dst=$out/share/doc/nixos
+      mkdir -p $dst
+
+      cp ${optionsJSON} $dst/options.json
+      cp ${optionsXML} $dst/options.xml
+
+      mkdir -p $out/nix-support
+      echo "file json $dst/options.json" >> $out/nix-support/hydra-build-products
+      echo "file xml $dst/options.xml" >> $out/nix-support/hydra-build-products
+    ''; # */
+
+    meta.description = "List of NixOS options in various formats.";
+  };
 
   optionsDocBook = runCommand "options-db.xml" {} ''
-    if grep /nixpkgs/nixos/modules ${optionsXML}; then
+    optionsXML=${options'}/doc/share/nixos/options.xml
+    if grep /nixpkgs/nixos/modules $optionsXML; then
       echo "The manual appears to depend on the location of Nixpkgs, which is bad"
       echo "since this prevents sharing via the NixOS channel.  This is typically"
       echo "caused by an option default that refers to a relative path (see above"
@@ -33,7 +67,7 @@ let
     fi
     ${libxslt}/bin/xsltproc \
       --stringparam revision '${revision}' \
-      -o $out ${./options-to-docbook.xsl} ${optionsXML}
+      -o $out ${./options-to-docbook.xsl} $optionsXML
   '';
 
   sources = sourceFilesBySuffices ./. [".xml"];
@@ -49,6 +83,9 @@ let
 
 in rec {
 
+  # Tools-friendly version of the list of NixOS options.
+  options = options';
+
   # Generate the NixOS manual.
   manual = stdenv.mkDerivation {
     name = "nixos-manual";