Files
nixpkgs/nixos/tests/kiwix-serve/default.nix
Adam Thompson-Sharpe 8c6fb9a752 nixos/kiwix-serve: init module
Adds a NixOS service module for kiwix-serve, which was requested quite a
while ago. kiwix-serve allows one to host ZIM files (such as for
archives of Wikipedia) over HTTP.

A NixOS VM test that generates and serves a basic ZIM file has also been
added. The ZIM file is generated as part of the test, since the output
file is relatively large (~60 KB) relative to the source content (~100
bytes).

See: <https://wiki.kiwix.org/wiki/Kiwix-serve>
2026-03-05 23:49:58 -05:00

76 lines
2.3 KiB
Nix

{ lib, pkgs, ... }:
let
mkTestZim =
name:
pkgs.runCommandLocal "${name}.zim"
{
nativeBuildInputs = [ pkgs.zim-tools ];
}
''
${lib.getExe' pkgs.zim-tools "zimwriterfs"} \
--name "${name}" \
--title 'NixOS kiwix-serve Test' \
--description 'NixOS test of kiwix-serve' \
--creator Nixpkgs \
--publisher Nixpkgs \
--language eng \
--welcome index.html \
--illustration icon.png \
${./html} \
$out
'';
# Test files must have different names or kiwix-serve will only serve one of them
testZimStore = mkTestZim "test-store";
testZimOutside = mkTestZim "test-outside";
in
{
name = "kiwix-serve";
meta.maintainers = with lib.maintainers; [ MysteryBlokHed ];
nodes = {
machine = {
systemd.services.copy-zim-file = {
description = "Copy test ZIM file to host system to test paths outside of store";
wantedBy = [ "multi-user.target" ];
before = [ "kiwix-serve.service" ];
requiredBy = [ "kiwix-serve.service" ];
serviceConfig = {
Type = "oneshot";
};
script = ''
mkdir -p /var/lib/kiwix-serve
cp ${testZimOutside} /var/lib/kiwix-serve/test-outside.zim
'';
};
services.kiwix-serve = {
enable = true;
port = 8080;
library = {
test-store = testZimStore;
test-outside = "/var/lib/kiwix-serve/test-outside.zim";
};
};
};
};
testScript = ''
machine.wait_for_unit("kiwix-serve.service")
machine.wait_for_open_port(8080)
machine.wait_until_succeeds("curl --fail --silent --head http://localhost:8080")
# ZIM file in store
test_content = machine.succeed("curl --fail --silent --location http://localhost:8080/content/test-store")
print(test_content)
assert "NixOS test of kiwix-serve" in test_content, "kiwix-serve did not provide the expected page for the store ZIM file"
# ZIM file outside of store
test_content = machine.succeed("curl --fail --silent --location http://localhost:8080/content/test-outside")
print(test_content)
assert "NixOS test of kiwix-serve" in test_content, "kiwix-serve did not provide the expected page for the out-of-store ZIM file"
'';
}