docs: refactor serve-docs; add nix run .#docs

Refactor `serve-docs` using `makeWrapper`, make it available as a
`serve-docs` package and as the `docs` "app".

This means `nix build .#docs` will build the docs while `nix run .#docs`
will run the server.
This commit is contained in:
Matt Sturgeon 2025-05-21 17:27:28 +01:00
parent 5c49988a7c
commit f3342bdbd4
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299
5 changed files with 28 additions and 8 deletions

View file

@ -132,5 +132,9 @@ lib.fix (
inherit evaledModules transformOptions;
inherit (self) search lib-docs;
};
serve-docs = pkgs.callPackage ./server {
inherit (self) docs;
};
}
)

19
docs/server/default.nix Normal file
View file

@ -0,0 +1,19 @@
{
lib,
runCommand,
makeBinaryWrapper,
python3,
docs,
}:
runCommand "serve-docs"
{
nativeBuildInputs = [ makeBinaryWrapper ];
meta.mainProgram = "server";
}
''
mkdir -p $out/bin
makeWrapper ${lib.getExe python3} \
$out/bin/server \
--add-flags ${./server.py} \
--chdir ${docs}
''

16
docs/server/server.py Normal file
View file

@ -0,0 +1,16 @@
import http.server
PORT = 8000
class UncachedHTTPHandler(http.server.SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header("Cache-Control", "no-cache, no-store, must-revalidate")
self.send_header("Pragma", "no-cache")
self.send_header("Expires", "0")
super().end_headers()
with http.server.HTTPServer(("", PORT), UncachedHTTPHandler) as httpd:
print(f"Serving documentation at http://localhost:{PORT}")
httpd.serve_forever()