fix(voicestudio): run the backend from a Nix-built Python env
The app's first-run setup downloads uv's generic CPython into ~/.config/VoiceStudio/runtime, which NixOS cannot execute (stub-ld). Do the same frozen `uv sync` at build time against upstream's nixpkgs python, in a fixed-output derivation, and point the app at it through OMNIVOICE_BACKEND_CMD so it no longer manages a runtime of its own. The ctranslate2 executable-stack flag is cleared at build time with upstream's own repair, since the store is read-only. CPU torch only. Adds an import-probe check. Refs: nix-overlay-2r3
This commit is contained in:
@@ -3,21 +3,83 @@
|
||||
inputs,
|
||||
...
|
||||
}:
|
||||
# Upstream's flake builds the app against its own pinned nixpkgs (see the
|
||||
# comment in flake.nix), so there is nothing to re-derive here. Only the first
|
||||
# run touches the network: the backend bootstraps itself with uv into
|
||||
# ~/.local/share, exactly like the published AppImage.
|
||||
# Upstream builds the desktop package for x86_64-linux only.
|
||||
let
|
||||
package = inputs.voicestudio.packages.${pkgs.stdenv.hostPlatform.system}.default or null;
|
||||
system = pkgs.stdenv.hostPlatform.system;
|
||||
app = inputs.voicestudio.packages.${system}.default or null;
|
||||
in
|
||||
if package == null then
|
||||
if app == null then
|
||||
# Upstream builds the desktop package for x86_64-linux only.
|
||||
{
|
||||
meta.platforms = [ "x86_64-linux" ];
|
||||
}
|
||||
else
|
||||
package.overrideAttrs (_: {
|
||||
passthru = (package.passthru or { }) // {
|
||||
category = "AI Audio";
|
||||
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.
|
||||
runtimeInputs = [ upstream.python311 ];
|
||||
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
|
||||
|
||||
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"
|
||||
''
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
{
|
||||
lib,
|
||||
runCommand,
|
||||
cacert,
|
||||
python311,
|
||||
uv,
|
||||
src,
|
||||
}:
|
||||
# The app's own setup runs `uv sync` into ~/.config/VoiceStudio with a CPython that uv
|
||||
# downloads, and NixOS cannot execute that interpreter (stub-ld). This is the same frozen
|
||||
# sync done at build time against nixpkgs' CPython, so the backend environment is a store
|
||||
# path and the running app downloads no interpreter and no packages.
|
||||
#
|
||||
# The torch build is CPU: uv.lock keeps the "cpu" and "cuda" groups as conflicting
|
||||
# alternatives, and the app itself picks CPU on a host without an NVIDIA driver.
|
||||
let
|
||||
version = (lib.importJSON "${src}/package.json").version;
|
||||
|
||||
# A fixed-output derivation may not reference store paths, so it holds site-packages
|
||||
# only: no console scripts (their shebangs name the build directory) and no pyvenv.cfg
|
||||
# (it names the interpreter). The venv below supplies those.
|
||||
sitePackages =
|
||||
runCommand "voicestudio-site-packages-${version}"
|
||||
{
|
||||
nativeBuildInputs = [
|
||||
uv
|
||||
python311
|
||||
];
|
||||
# Nix empties SSL_CERT_FILE in the sandbox and uv then trusts nothing.
|
||||
SSL_CERT_FILE = "${cacert}/etc/ssl/certs/ca-bundle.crt";
|
||||
outputHashMode = "recursive";
|
||||
# Changes with uv.lock: build once, copy the hash from the mismatch error.
|
||||
outputHash = "sha256-qnRSxapQBCuJa8DYtyrAywS/9US5Or54N7dcN3sdL8s=";
|
||||
}
|
||||
''
|
||||
mkdir project
|
||||
cd project
|
||||
cp ${src}/pyproject.toml ${src}/uv.lock ${src}/README.md .
|
||||
|
||||
export HOME=$TMPDIR
|
||||
export UV_CACHE_DIR=$TMPDIR/uv-cache
|
||||
export UV_PROJECT_ENVIRONMENT=$TMPDIR/venv
|
||||
export UV_PYTHON_DOWNLOADS=never
|
||||
export UV_PYTHON_PREFERENCE=only-system
|
||||
export UV_LINK_MODE=copy
|
||||
|
||||
# Nine locked packages are sdists, and uv fetches their build backends unpinned:
|
||||
# a new setuptools release would change the wheels built from them, and this hash.
|
||||
# The cutoff pins those backends. The project itself is not installed, the app
|
||||
# ships its own sources.
|
||||
uv sync --frozen --no-dev --no-default-groups --group cpu --no-install-project \
|
||||
--python ${python311}/bin/python3.11 --exclude-newer 2026-11-12T00:00:00Z
|
||||
|
||||
mkdir -p $out
|
||||
cp -r $TMPDIR/venv/lib/python3.11/site-packages/. $out/
|
||||
|
||||
# CTranslate2 4.4.0, the last release whisperx allows on Python 3.11, asks for an
|
||||
# executable stack and current glibc refuses to dlopen it ("cannot enable executable
|
||||
# stack"). The app repairs this in place at run time, which a read-only store cannot
|
||||
# allow, so apply its own repair (backend/core/execstack.py) here. Fails the build
|
||||
# if the libraries move.
|
||||
python3.11 -B - $out <<'EOF'
|
||||
import glob, sys
|
||||
sys.path.insert(0, "${src}/backend")
|
||||
from core.execstack import clear_execstack, has_execstack
|
||||
|
||||
libs = glob.glob(sys.argv[1] + "/ctranslate2*/libctranslate2*.so*")
|
||||
assert libs, "no CTranslate2 libraries found"
|
||||
for lib in libs:
|
||||
clear_execstack(lib)
|
||||
assert has_execstack(lib) is False, lib
|
||||
EOF
|
||||
|
||||
# RECORD still lists the console scripts left behind, and their hashes depend on
|
||||
# the build directory named in each shebang. Drop those lines so a different
|
||||
# `sandbox-build-dir` cannot change this output.
|
||||
find $out -name RECORD -exec sed -i '/^\.\.\/\.\.\/\.\.\/bin\//d' {} +
|
||||
'';
|
||||
in
|
||||
runCommand "voicestudio-python-${version}" { passthru = { inherit sitePackages; }; } ''
|
||||
${python311}/bin/python3.11 -m venv --without-pip $out
|
||||
rm -r $out/lib/python3.11/site-packages
|
||||
ln -s ${sitePackages} $out/lib/python3.11/site-packages
|
||||
''
|
||||
Reference in New Issue
Block a user