colmena/manual/default.nix

101 lines
2.8 KiB
Nix
Raw Normal View History

2022-03-08 07:02:04 +01:00
{ lib, stdenv, nix-gitignore, mdbook, mdbook-linkcheck, python3, callPackage, writeScript
2022-01-02 01:41:35 +01:00
, deploymentOptionsMd ? null
, metaOptionsMd ? null
, colmena ? null
2021-11-18 22:15:20 +01:00
2021-11-23 22:17:05 +01:00
# Full version
, version ? if colmena != null then colmena.version else "unstable"
# Whether this build is unstable
, unstable ? version == "unstable" || lib.hasInfix "-" version
}:
2021-11-18 22:15:20 +01:00
let
2021-11-23 22:17:05 +01:00
apiVersion = builtins.concatStringsSep "." (lib.take 2 (lib.splitString "." version));
2021-11-18 22:15:20 +01:00
colorizedHelp = let
help = callPackage ./colorized-help.nix {
inherit colmena;
};
in if colmena != null then help else null;
2021-11-18 22:15:20 +01:00
redirectTemplate = lib.escapeShellArg ''
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Redirecting</title>
<meta http-equiv="refresh" content="0; URL=https://zhaofengli.github.io/colmena@path@">
</head>
<body>
Redirecting to <a href="https://zhaofengli.github.io/colmena@path@">https://zhaofengli.github.io/colmena@path@</a>
</body>
</html>
'';
in stdenv.mkDerivation {
inherit version deploymentOptionsMd metaOptionsMd colorizedHelp;
2021-11-23 22:17:05 +01:00
pname = "colmena-manual" + (if unstable then "-unstable" else "");
src = nix-gitignore.gitignoreSource [] ./.;
2022-03-08 07:02:04 +01:00
nativeBuildInputs = [ mdbook mdbook-linkcheck python3 ];
2021-11-18 22:15:20 +01:00
outputs = [ "out" "redirectFarm" ];
2021-11-23 22:17:05 +01:00
COLMENA_VERSION = version;
COLMENA_UNSTABLE = unstable;
patchPhase = ''
2021-11-23 22:17:05 +01:00
if [ -z "${toString unstable}" ]; then
2021-11-18 22:15:20 +01:00
sed "s|@apiVersion@|${apiVersion}|g" book.stable.toml > book.toml
fi
'';
buildPhase = ''
if [[ -n "$colorizedHelp" ]]; then
cat "$colorizedHelp" >> src/reference/cli.md
else
echo "Error: No colmena executable passed to the builder" >> src/reference/cli.md
fi
2022-01-02 01:41:35 +01:00
if [[ -n "$deploymentOptionsMd" ]]; then
cat "$deploymentOptionsMd" >> src/reference/deployment.md
else
echo "No deployment options text passed the the builder" >> src/reference/deployment.md
fi
if [[ -n "$metaOptionsMd" ]]; then
cat "$metaOptionsMd" >> src/reference/meta.md
else
echo "No meta options text passed the the builder" >> src/reference/meta.md
fi
mdbook build -d ./build
cp -r ./build/html $out
2021-11-18 22:15:20 +01:00
# Build the redirect farm
# GitHub Pages doesn't play well with directory symlinks. Default
# pages (index.html) don't work when a symlink is traversed.
mkdir -p $redirectFarm
subdir="/unstable"
2021-11-23 22:17:05 +01:00
if [ -z "${toString unstable}" ]; then
2021-11-18 22:15:20 +01:00
subdir="/${apiVersion}"
fi
pushd $redirectFarm
(cd $out; find . -name "*.html") | while read -r page; do
strippedPage=''${page#.}
target="$subdir$strippedPage"
mkdir -p $(dirname $page)
echo ${redirectTemplate} | sed "s|@path@|$target|g" > $page
done
popd
'';
installPhase = "true";
}