dotfiles/nixcfgs/users/js0ny/programs/pdf2zh/container.nix
2025-12-18 23:24:04 +00:00

113 lines
3.2 KiB
Nix
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Only use this method in NixOS or non-FHS environment
# For FHS environments, see ./uv.nix
# Configure the options via environment variables:
# PDF2ZH_API_BASE: The base URL of the OpenRouter-compatible API (default: https://openrouter.ai/api/v1)
# PDF2ZH_MODEL: The model to use (default: google/gemini-2.5-flash)
# PDF2ZH_API_KEY or OPENROUTER_API_KEY: The API key for authentication (one of them must be set)
{pkgs, ...}: let
imageTag = "ghcr.io/pdfmathtranslate/pdfmathtranslate-next";
# version = "2.6.4";
mkPdf2zh = {
name,
withEnv,
}:
pkgs.writeShellApplication {
inherit name;
runtimeInputs = [pkgs.podman];
text = ''
IMAGE_TAG="${imageTag}"
if ! podman image exists "$IMAGE_TAG"; then
echo "[pdf2zh] Pulling image $IMAGE_TAG ..."
podman pull "$IMAGE_TAG"
fi
PODMAN_ENV_ARGS=()
CMD_ARGS=()
${
if withEnv
then ''
API_BASE="''${PDF2ZH_API_BASE:-https://openrouter.ai/api/v1}"
MODEL="''${PDF2ZH_MODEL:-google/gemini-3-flash-preview}"
if [[ -n "''${PDF2ZH_API_KEY:-}" ]]; then
API_KEY="$PDF2ZH_API_KEY"
elif [[ -n "''${OPENROUTER_API_KEY:-}" ]]; then
API_KEY="$OPENROUTER_API_KEY"
else
echo "Error: Neither OPENROUTER_API_KEY nor PDF2ZH_API_KEY is set." >&2
echo "Please export one of them explicitly." >&2
exit 1
fi
echo "[pdf2zh] Using Model: $MODEL"
PODMAN_ENV_ARGS+=("-e" "OPENROUTER_API_KEY=$API_KEY")
CMD_ARGS+=(
"--openaicompatible"
"--openai-compatible-model" "$MODEL"
"--openai-compatible-base-url" "$API_BASE"
"--openai-compatible-api-key" "$API_KEY"
)
''
else ""
}
exec podman run \
--rm \
-it \
-p 7860:7860 \
-v "$(pwd):/data" \
-w /data \
"''${PODMAN_ENV_ARGS[@]}" \
"$IMAGE_TAG" \
pdf2zh \
"''${CMD_ARGS[@]}" \
"$@"
'';
};
pdf2zhRunner = mkPdf2zh {
name = "pdf2zh";
withEnv = true;
};
pdf2zhUnwrapped = mkPdf2zh {
name = "pdf2zh-unwrapped";
withEnv = false;
};
descEn = "PDF scientific paper translation with preserved formats";
descZh = " AI PDF ";
in {
services.podman.enable = true;
# Declare an image, do not instantiate as a container
services.podman.images.pdf2zh = {
image = imageTag;
description = " ${descEn} - ${descZh} Google/DeepL/Ollama/OpenAI CLI/GUI/Docker";
};
home.packages = [pdf2zhRunner pdf2zhUnwrapped];
home.file.".local/share/kio/servicemenus/pdf2zh.desktop" = {
enable = true; # TODO: Write a wrapper for status tracking
executable = true;
text = ''
[Desktop Entry]
Type=Service
MimeType=application/pdf;
Actions=translateToZh
Icon=translate
X-KDE-Priority=TopLevel
X-KDE-StartupNotify=false
[Desktop Action translateToZh]
Name=
Icon=translate
Exec=pdf2zh --openaicompatible "%f"
'';
};
}