From 7d5c7b9b44f52314c841e5e9934377c384ee82a1 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Thu, 9 Nov 2023 19:29:36 +0000 Subject: [PATCH] export evaluation from default.nix and use it for docs --- ci.nix | 51 ++++++++++++++++++++++++----------------- default.nix | 11 +++++++-- doc/extract-options.nix | 43 ++++++++-------------------------- doc/parse-options.fnl | 28 ++++++++++++---------- modules/all-modules.nix | 40 ++++++++++++++++++++++++++++++++ 5 files changed, 104 insertions(+), 69 deletions(-) create mode 100644 modules/all-modules.nix diff --git a/ci.nix b/ci.nix index 085c7fe..4e901ed 100644 --- a/ci.nix +++ b/ci.nix @@ -26,30 +26,39 @@ let tests // { buildEnv = (import liminix { - inherit nixpkgs borderVmConf; + inherit nixpkgs borderVmConf; device = import (liminix + "/devices/qemu"); liminix-config = vanilla; }).buildEnv; - doc = pkgs.stdenv.mkDerivation { - name = "liminix-doc"; - nativeBuildInputs = with pkgs; [ - gnumake sphinx - fennel luaPackages.lyaml - ]; - src = ./doc; - buildPhase = '' - cat ${(import ./doc/extract-options.nix).doc} > options.json - cat options.json | fennel --correlate parse-options.fnl > modules-generated.rst - cp ${(import ./doc/hardware.nix)} hardware.rst - make html - ''; - installPhase = '' - mkdir -p $out/nix-support $out/share/doc/ - cp modules.rst options.json $out - cp -a _build/html $out/share/doc/liminix - echo "file source-dist \"$out/share/doc/liminix\"" \ - > $out/nix-support/hydra-build-products - ''; + doc = + let json = + (import liminix { + inherit nixpkgs borderVmConf; + device = import (liminix + "/devices/qemu"); + liminix-config = {...} : { + imports = [ ./modules/all-modules.nix ]; + }; + }).outputs.optionsJson; + in pkgs.stdenv.mkDerivation { + name = "liminix-doc"; + nativeBuildInputs = with pkgs; [ + gnumake sphinx fennel luaPackages.lyaml + ]; + src = ./.; + buildPhase = '' + cat ${json} | fennel --correlate doc/parse-options.fnl > doc/modules-generated.rst + cp ${(import ./doc/hardware.nix)} doc/hardware.rst + make -C doc html + ''; + installPhase = '' + mkdir -p $out/nix-support $out/share/doc/ + cd doc + cp modules-generated.rst $out + ln -s ${json} $out/options.json + cp -a _build/html $out/share/doc/liminix + echo "file source-dist \"$out/share/doc/liminix\"" \ + > $out/nix-support/hydra-build-products + ''; }; with-unstable = (import liminix { nixpkgs = unstable; diff --git a/default.nix b/default.nix index 74d8636..f544d1d 100644 --- a/default.nix +++ b/default.nix @@ -17,7 +17,7 @@ let }; }); - config = (pkgs.lib.evalModules { + eval = pkgs.lib.evalModules { modules = [ { _module.args = { inherit pkgs; inherit (pkgs) lim; }; } ./modules/hardware.nix @@ -30,7 +30,8 @@ let ./modules/users.nix ./modules/outputs.nix ]; - }).config; + }; + config = eval.config; borderVm = ((import ) { system = builtins.currentSystem; @@ -43,6 +44,12 @@ let in { outputs = config.system.outputs // { default = config.system.outputs.${config.hardware.defaultOutput}; + optionsJson = + let o = import ./doc/extract-options.nix { + inherit pkgs eval; + lib = pkgs.lib; + }; + in pkgs.writeText "options.json" (builtins.toJSON o); }; # this is just here as a convenience, so that we can get a diff --git a/doc/extract-options.nix b/doc/extract-options.nix index f6b6da1..3b08e0a 100644 --- a/doc/extract-options.nix +++ b/doc/extract-options.nix @@ -1,31 +1,10 @@ +{ eval, lib, pkgs }: let - overlay = import ../overlay.nix; - pkgs = import ( { - overlays = [overlay]; - config = { - allowUnsupportedSystem = true; # mipsel - permittedInsecurePackages = [ - "python-2.7.18.6" # kernel backports needs python <3 - ]; - }; - }); - inherit (pkgs) lib; inherit (lib) types; - modulenames = - builtins.attrNames - (lib.filterAttrsRecursive - (n: t: - (n != "arch") && - ((t=="directory") || - ((t=="regular") && ((builtins.match ".*\\.nix$" n) != null)))) - (builtins.readDir ../modules)); - modulefiles = builtins.map (n: builtins.toPath "${../modules}/${n}") modulenames; - eval = (lib.evalModules { - modules = [ - { _module.args = { inherit pkgs; lib = pkgs.lib; }; } - ] ++ modulefiles; - }); conf = eval.config; + rootDir = builtins.toPath ./..; + stripAnyPrefixes = lib.flip (lib.fold lib.removePrefix) + ["${rootDir}/"]; optToDoc = name: opt : { inherit name; description = opt.description or null; @@ -42,16 +21,12 @@ let then let sd = lib.attrByPath item.loc ["not found"] conf; in item // { + declarations = map stripAnyPrefixes item.declarations; parameters = let x = lib.mapAttrsToList optToDoc sd.parameters; in x; } else - item; - o = builtins.map spliceServiceDefn - (pkgs.lib.optionAttrSetToDocList eval.options); -in { - doc = pkgs.writeText "options.yaml" '' - # ${./..} - ${builtins.toJSON o} - ''; -} + item // { declarations = map stripAnyPrefixes item.declarations; }; +in +builtins.map spliceServiceDefn + (pkgs.lib.optionAttrSetToDocList eval.options) diff --git a/doc/parse-options.fnl b/doc/parse-options.fnl index 8f2eb21..85bff55 100644 --- a/doc/parse-options.fnl +++ b/doc/parse-options.fnl @@ -2,21 +2,25 @@ (local { : view } (require :fennel)) +(fn basename [str ext] + (-> str + (string.gsub "(.*/)(.*)" "%2") + (string.gsub (.. ext "$") ""))) + (fn headline [name] - (let [(_ _ basename) (string.find name ".*/([^/].*).nix") - len (basename:len)] - (.. basename "\n" (string.rep "=" len)))) + (let [title (assert (basename name ".nix")) + len (title:len)] + (.. title "\n" (string.rep "=" len)))) (fn read-preamble [pathname] - (if (= (pathname:sub 1 1) "/") - (let [pathname (if (string.match pathname ".nix$") - pathname - (.. pathname "/default.nix"))] - (with-open [f (assert (io.open pathname :r))] - (accumulate [lines nil - l (f:lines) - :until (not (= (string.sub l 1 2) "##"))] - (.. (or lines "") (string.gsub l "^## *" "") "\n")))))) + (let [pathname (if (string.match pathname ".nix$") + pathname + (.. pathname "/default.nix"))] + (with-open [f (assert (io.open pathname :r))] + (accumulate [lines nil + l (f:lines) + :until (not (= (string.sub l 1 2) "##"))] + (.. (or lines "") (string.gsub l "^## *" "") "\n"))))) (fn relative-pathname [pathname] (let [pathname diff --git a/modules/all-modules.nix b/modules/all-modules.nix new file mode 100644 index 0000000..dce38af --- /dev/null +++ b/modules/all-modules.nix @@ -0,0 +1,40 @@ +# Import all of the modules, used in the documentation generator. Not +# currently expected to work in an actual configuration, but it would +# be nice if it did. + +{ + imports = [ + ./base.nix + ./bridge + ./busybox.nix + ./dhcp6c + ./dnsmasq + ./ext4fs.nix + ./firewall + ./flashimage.nix + ./hardware.nix + ./hostapd + ./hostname.nix + ./initramfs.nix + ./jffs2.nix + ./kernel.nix + ./kexecboot.nix + ./mount + ./network + ./ntp + ./outputs.nix + ./outputs/vmroot.nix + ./ppp + ./ramdisk.nix + ./squashfs.nix + ./ssh + ./standard.nix + ./tftpboot.nix + ./ubifs.nix + ./ubimage.nix + ./users.nix + ./vlan + ./watchdog + ./wlan.nix + ]; +}