From 7869d7168e6623e5efd574e18d5cdac6b6441afa Mon Sep 17 00:00:00 2001 From: Noam Yorav-Raphael Date: Mon, 8 Apr 2024 23:49:19 +0300 Subject: [PATCH] Build the book with Nix --- book.toml | 3 +- default.nix | 143 ++++++++++++++++++---------------------------------- release.nix | 6 +-- 3 files changed, 52 insertions(+), 100 deletions(-) diff --git a/book.toml b/book.toml index b0432db..055f340 100644 --- a/book.toml +++ b/book.toml @@ -13,4 +13,5 @@ git-repository-url = "https://github.com/NixOS/nix-pills" [output.linkcheck] follow-web-links = true -[output.epub] +# To build the epub version, run: +# mdbook-epub --standalone true diff --git a/default.nix b/default.nix index aa4aae2..1d88a20 100644 --- a/default.nix +++ b/default.nix @@ -1,87 +1,32 @@ -{ pkgs ? import {}, revCount, shortRev }: -let - lib = pkgs.lib; +{ pkgs ? import {} }: - sources = let - - # We want nix examples, but not the top level nix to build things - noTopLevelNix = path: type: let - relPath = lib.removePrefix (toString ./. + "/") (toString path); - in builtins.match "[^/]*\.nix" relPath == null; - - extensions = [ ".xml" ".txt" ".nix" ".bash" ]; - - in lib.cleanSourceWith { - filter = noTopLevelNix; - src = lib.sourceFilesBySuffices ./. extensions; - }; - - combined = pkgs.runCommand "nix-pills-combined" - { - buildInputs = [ pkgs.libxml2 ]; - meta.description = "Nix Pills with as a single docbook file"; - inherit revCount shortRev; - } - '' - cp -r ${sources} ./sources - chmod -R u+w ./sources - - cd sources - - printf "%s-%s" "$revCount" "$shortRev" > version - xmllint --xinclude --output "$out" ./book.xml - ''; - - toc = builtins.toFile "toc.xml" - '' - - - - - ''; - - manualXsltprocOptions = toString [ - "--param section.autolabel 1" - "--param section.label.includes.component.label 1" - "--stringparam html.stylesheet style.css" - "--param xref.with.number.and.title 1" - "--param toc.section.depth 3" - "--stringparam admon.style ''" - "--stringparam callout.graphics.extension .svg" - "--stringparam current.docid nix-pills" - "--param chunk.section.depth 0" - "--param chunk.first.sections 1" - "--param use.id.as.filename 1" - "--stringparam generate.toc 'book toc appendix toc'" - "--stringparam chunk.toc '${toc}'" - ]; - -in { - html-split = pkgs.stdenv.mkDerivation { +{ + html-split = pkgs.stdenvNoCC.mkDerivation { name = "nix-pills"; + src = ./.; - src = sources; - - buildInputs = with pkgs; [ - libxslt + nativeBuildInputs = with pkgs; [ + mdbook + mdbook-linkcheck ]; + buildPhase = '' + runHook preBuild + + # We can't check external links inside the sandbox, but it's good to check them outside the sandbox. + substituteInPlace book.toml --replace-fail 'follow-web-links = true' 'follow-web-links = false' + mdbook build + + runHook postBuild + ''; + installPhase = '' runHook preInstall - # Generate the HTML manual. + # The nix pills were originally built into this directory, and consumers of the nix pills expect to find it there. Do not change unless you also change other code that depends on this directory structure. dst=$out/share/doc/nix-pills mkdir -p "$dst" - xsltproc \ - ${manualXsltprocOptions} \ - --nonet --output "$dst/" \ - "${pkgs.docbook-xsl-ns}/xml/xsl/docbook/xhtml/chunk.xsl" \ - "${combined}" - - mkdir -p "$dst/images/callouts" - cp -r "${pkgs.docbook-xsl-ns}/xml/xsl/docbook/images/callouts"/*.svg "$dst/images/callouts" - - cp "${./style.css}" "$dst/style.css" + mv book/html/* "$dst"/ mkdir -p "$out/nix-support" echo "nix-build out $out" >> "$out/nix-support/hydra-build-products" @@ -91,13 +36,13 @@ in { ''; }; - epub = pkgs.stdenv.mkDerivation { + epub = pkgs.stdenvNoCC.mkDerivation { name = "nix-pills-epub"; + src = ./.; - src = sources; - - buildInputs = with pkgs; [ - libxslt + nativeBuildInputs = with pkgs; [ + mdbook-epub + unzip zip ]; @@ -107,29 +52,37 @@ in { doInstallCheck = true; + buildPhase = '' + runHook preBuild + + mdbook-epub --standalone${pkgs.lib.optionalString (pkgs.mdbook-epub.version != "unstable-2022-12-25") " true"} + + # Work around bugs in mdbook-epub. + mkdir nix-pills.epub-fix + ( cd nix-pills.epub-fix + unzip -q "../book/epub/Nix Pills.epub" + # Fix invalid ids. + sed -Ei 's/(id(ref)?=")([0-9])/\1p\3/g' OEBPS/content.opf + sed -Ei 's/(id="|href="#)([0-9])/\1fn\2/g' OEBPS/20-basic-dependencies-and-hooks.html + # Fix anchors. + sed -Ei 's~(.+) \{#([^\}]+)\}()~\1 id="\3"\2\4~g' OEBPS/*.html + # Fix broken links in body. + sed -Ei 's/("[0-9a-z-]+\.)md(["#])/\1html\2/g' OEBPS/*.html + zip -q "../book/epub/Nix Pills.epub" **/* + ) + + runHook postBuild + ''; + installPhase = '' runHook preInstall - # Generate the EPUB manual. + # The nix pills were originally built into this directory, and consumers of the nix pills expect to find it there. Do not change unless you also change other code that depends on this directory structure. dst=$out/share/doc/nix-pills mkdir -p "$dst" - xsltproc \ - ${manualXsltprocOptions} \ - --nonet --output "$dst/epub/" \ - "${pkgs.docbook-xsl-ns}/xml/xsl/docbook/epub3/chunk.xsl" \ - "${combined}" - mkdir -p "$dst/epub/OEBPS/images/callouts" - cp -r "${pkgs.docbook-xsl-ns}/xml/xsl/docbook/images/callouts"/*.svg "$dst/epub/OEBPS/images/callouts" - cp "${./style.css}" "$dst/epub/OEBPS/style.css" - - echo "application/epub+zip" > mimetype manual="$dst/nix-pills.epub" - zip -0Xq "$manual" mimetype - pushd "$dst/epub" && zip -Xr9D "$manual" * - popd - - rm -rf "$dst/epub" + mv "book/epub/Nix Pills.epub" "$manual" mkdir -p "$out/nix-support" echo "nix-build out $out" >> "$out/nix-support/hydra-build-products" diff --git a/release.nix b/release.nix index 7e2bf10..c1f81fc 100644 --- a/release.nix +++ b/release.nix @@ -1,5 +1,5 @@ -{ nix-pills ? { outPath = ./.; revCount = 1234; shortRev = "abcdef"; } -, nixpkgs ? { outPath = ; revCount = 1234; shortRev = "abcdef"; } +{ nix-pills ? { outPath = ./.; } +, nixpkgs ? { outPath = ; } , officialRelease ? false }: @@ -8,8 +8,6 @@ let pills = import ./default.nix { inherit pkgs; - - inherit (nix-pills) revCount shortRev; }; in rec { inherit (pills) html-split epub;