Files
millerson-overlay.nix/packages/voicestudio/default.nix
T
alex 66c64572d2
CI / check (push) Waiting to run
feat(voicestudio): force the GGUF TTS runtime onto Vulkan
PyTorch in the Nix env is CPU-only, so the bundled omnivoice.cpp
runtime (ggml Vulkan) is the only GPU path.

Put vulkan-tools on the backend PATH: the app finds the adapter by
running `vulkaninfo` and otherwise reports none, since NixOS has no
FHS library directories for its fallback probe.

Default OMNIVOICE_GGUF_BACKEND=vulkan so the runtime opens the GPU or
fails loudly instead of falling back to the CPU. Overridable.

Refs: nix-overlay-ltr
2026-11-20 21:20:37 +03:00

95 lines
4.0 KiB
Nix

{
pkgs,
inputs,
...
}:
let
system = pkgs.stdenv.hostPlatform.system;
app = inputs.voicestudio.packages.${system}.default or null;
in
if app == null then
# Upstream builds the desktop package for x86_64-linux only.
{
meta.platforms = [ "x86_64-linux" ];
}
else
let
# Python and its libraries come from the nixpkgs the app itself is built against, so
# the closure holds one glibc and one libstdc++: upstream's launcher already puts its
# gcc libstdc++ on LD_LIBRARY_PATH, which the backend inherits.
upstream = inputs.voicestudio.inputs.nixpkgs.legacyPackages.${system};
python = upstream.callPackage ./runtime.nix { src = inputs.voicestudio; };
resources = "${app}/libexec/electron/resources";
# The manylinux wheels dlopen libstdc++ and libz, which NixOS has no /usr/lib for.
libraryPath = upstream.lib.makeLibraryPath [
upstream.stdenv.cc.cc.lib
upstream.zlib
];
# The packaged backend command from electron/src/main/backend.ts, run from the store
# environment. The shell passes the port through OMNIVOICE_PORT.
# shortcut: no bytecode cache (-B, the store is read-only), so the heavy imports (torch and
# co.) take about 15 s longer on every launch; drop -B and set PYTHONPYCACHEPREFIX to a user
# cache dir if that hurts.
backend = upstream.writeShellApplication {
name = "voicestudio-backend";
# Opt-in engines build their own venvs with `uv venv --python 3.11`; ffmpeg and uv
# are already on PATH from upstream's launcher. The app finds the GPU by running
# `vulkaninfo`; without it on PATH it reports no adapter and offers no GPU engine.
runtimeInputs = [
upstream.python311
upstream.vulkan-tools
];
text = ''
export LD_LIBRARY_PATH="${libraryPath}''${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
# Never fetch an interpreter: uv's own CPython cannot run here.
export UV_PYTHON_DOWNLOADS=never UV_PYTHON_PREFERENCE=only-system
# Torch is CPU-only here, so the bundled omnivoice.cpp runtime (GGUF engine) is the
# only GPU path. Pin it to Vulkan rather than let ggml fall back to the CPU quietly;
# a missing driver then fails loudly. Override: OMNIVOICE_GGUF_BACKEND=auto|cpu, and
# OMNIVOICE_GGUF_DEVICE=<n> to pick another adapter.
export OMNIVOICE_GGUF_BACKEND="''${OMNIVOICE_GGUF_BACKEND:-vulkan}"
cd ${resources}
# -s -B: no user site, and the sources live in the read-only store.
exec ${python}/bin/python -s -B -m uvicorn main:app \
--app-dir backend --host 127.0.0.1 --port "''${OMNIVOICE_PORT:-3900}"
'';
};
# The app's own import probe (electron/src/main/runtime-import-probe.ts), run in the
# backend's environment: fails when a wheel cannot find a system library.
importProbe = upstream.runCommand "voicestudio-imports" { } ''
export HOME=$TMPDIR LD_LIBRARY_PATH="${libraryPath}"
cd ${resources}
${python}/bin/python -s -B -c "import fastapi, uvicorn, omnivoice, faster_whisper, sentencepiece, torch, torchaudio, torchvision"
touch $out
'';
in
# With OMNIVOICE_BACKEND_CMD set the shell treats the backend as external: it neither
# stages sources nor runs `uv sync` into ~/.config/VoiceStudio. --set-default keeps the
# variable usable as an override.
upstream.runCommand "voicestudio"
{
nativeBuildInputs = [ upstream.makeWrapper ];
meta = app.meta;
passthru = {
category = "AI Audio";
inherit backend python;
tests.imports = importProbe;
};
}
''
makeWrapper ${app}/bin/voicestudio $out/bin/voicestudio \
--set-default OMNIVOICE_BACKEND_CMD ${backend}/bin/voicestudio-backend
# The menu entry has to start this launcher, not the bare app.
cp -r ${app}/share $out/share
chmod -R u+w $out/share
substituteInPlace $out/share/applications/VoiceStudio.desktop \
--replace-fail "Exec=${app}/bin/voicestudio" "Exec=$out/bin/voicestudio"
''