Files
kinect/flake.nix
T
kinect-botandsisyphus-dev-ai c4796d7dcf feat(build): fetch the pose model bundles through Nix, hash-pinned
tracker.py failed with FileNotFoundError on pose_landmarker_full.task - the
flake built the Python env fine but never fetched the .task model bundles
that docs/setup-linux.md step 2a used to curl by hand into the git-ignored
tracker/models/.

Added poseModels: a runCommand derivation that fetches all three tiers
(lite/full/heavy) via pkgs.fetchurl with sha256 hashes pinned from the real
bundles (verified by downloading and hashing each one directly, not guessed).
The google storage "latest" path can move the file it points to; fetchurl's
hash check fails loudly if it ever does, rather than silently serving a
different model.

nix run .#tracker now passes --models-dir pointing at this derivation by
default (a user-supplied --models-dir/--model after it still wins, argparse
keeps the last occurrence). nix develop also symlinks the fetched bundles
into tracker/models/ so a manual python -m tracker.tracker inside the shell
needs no separate fetch either. docs/setup-linux.md updated to note the Nix
path skips the manual model-download step too.

Co-authored-by: sisyphus-dev-ai <sisyphus-dev-ai@users.noreply.github.com>
2026-11-30 10:09:14 -05:00

227 lines
8.2 KiB
Nix

{
description = ''
Webcam-driven virtual Kinect v1. Builds the whole project with Nix, no
manual venv:
- the Python tracker (tracker/) as a pure Nix-built environment from
tracker/uv.lock (via uv2nix - no `uv venv` / `uv pip install` step,
the environment itself is a Nix store path built from locked,
hash-pinned wheels),
- the OS-agnostic C++ core + its CTest suite (cmake),
- the Windows Kinect10.dll shim and the native Windows tracker, cross
compiled with mingw-w64.
'';
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
pyproject-nix = {
url = "github:pyproject-nix/pyproject.nix";
inputs.nixpkgs.follows = "nixpkgs";
};
uv2nix = {
url = "github:pyproject-nix/uv2nix";
inputs.pyproject-nix.follows = "pyproject-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
pyproject-build-systems = {
url = "github:pyproject-nix/build-system-pkgs";
inputs.pyproject-nix.follows = "pyproject-nix";
inputs.uv2nix.follows = "uv2nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
{
self,
nixpkgs,
pyproject-nix,
uv2nix,
pyproject-build-systems,
...
}:
let
inherit (nixpkgs) lib;
forAllSystems = lib.genAttrs [
"x86_64-linux"
"aarch64-linux"
];
# The Python project lives in tracker/ (its own pyproject.toml + uv.lock,
# pinned to the known-good combination: Python 3.12, numpy 2.5.5,
# opencv-python-headless 5.1.0.95, mediapipe 1.2.3).
workspace = uv2nix.lib.workspace.loadWorkspace { workspaceRoot = ./tracker; };
overlay = workspace.mkPyprojectOverlay { sourcePreference = "wheel"; };
pythonSets = forAllSystems (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
python = pkgs.python312;
in
(pkgs.callPackage pyproject-nix.build.packages { inherit python; }).overrideScope
(
lib.composeManyExtensions [
pyproject-build-systems.overlays.wheel
overlay
]
)
);
# Native toolchain (core/ + fallback/windows-nui/ + tracker-native/) shared
# by the devShell and the build/test/run apps below.
nativeTools =
pkgs: with pkgs; [
cmake
gnumake
gcc
git
curl
pkgsCross.mingwW64.buildPackages.gcc # x86_64-w64-mingw32-g++ (64-bit shim + native tracker)
pkgsCross.mingw32.buildPackages.gcc # i686-w64-mingw32-g++ (32-bit shim, most DEA/spice2x builds)
];
# libGLESv2.so.2 is what mediapipe's native bindings dlopen at runtime
# (docs/setup-linux.md step 2b used to stage this by hand from a .deb;
# Nix just needs it on the library path).
runtimeLibs = pkgs: with pkgs; [ libglvnd ];
# MediaPipe Pose Landmarker .task bundles (docs/setup-linux.md step 2a used
# to curl these by hand into tracker/models/, git-ignored). Fetched once,
# content-addressed and hash-pinned by Nix instead - the google storage
# "latest" path can move, fetchurl's hash check just fails loudly if it ever
# does, rather than silently serving a different model.
poseModels =
pkgs:
pkgs.runCommand "kinect-pose-models" { } (
let
tiers = {
lite = "59929e1d1ee95287735ddd833b19cf4ac46d29bc7afddbbf6753c459690d574a";
full = "4eaa5eb7a98365221087693fcc286334cf0858e2eb6e15b506aa4a7ecdcec4ad";
heavy = "64437af838a65d18e5ba7a0d39b465540069bc8aae8308de3e318aad31fcbc7b";
};
in
''
mkdir -p $out
''
+ lib.concatStrings (
lib.mapAttrsToList (tier: hash: ''
cp ${
pkgs.fetchurl {
url = "https://storage.googleapis.com/mediapipe-models/pose_landmarker/pose_landmarker_${tier}/float16/latest/pose_landmarker_${tier}.task";
sha256 = hash;
}
} $out/pose_landmarker_${tier}.task
'') tiers
)
);
in
{
devShells = forAllSystems (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
pythonSet = pythonSets.${system};
# A pure Nix-built environment containing every locked dependency
# (tracker/uv.lock) - not a venv, not `pip install`d, a Nix store
# path assembled from hash-pinned wheels.
trackerEnv = pythonSet.mkVirtualEnv "kinect-tracker-env" workspace.deps.all;
in
{
default = pkgs.mkShell {
packages = [ trackerEnv ] ++ nativeTools pkgs ++ runtimeLibs pkgs;
LD_LIBRARY_PATH = lib.makeLibraryPath (runtimeLibs pkgs);
# Match the shim build scripts' override knobs (fallback/windows-nui/build.sh,
# tracker-native/build_win.sh) to nixpkgs' mingw-w64 binary names, since the
# scripts default to the Debian/Ubuntu "-win32"/"-posix" thread-variant names.
CXX32 = "i686-w64-mingw32-g++";
CXX64 = "x86_64-w64-mingw32-g++";
CXX_WIN = "x86_64-w64-mingw32-g++";
OBJDUMP32 = "i686-w64-mingw32-objdump";
OBJDUMP64 = "x86_64-w64-mingw32-objdump";
# tracker.py's --models-dir defaults to tracker/models/ (git-ignored) when
# unset; point it at the Nix-fetched bundles so a manual
# `python -m tracker.tracker` inside nix develop needs no separate fetch.
shellHook = ''
mkdir -p tracker/models
for f in ${poseModels pkgs}/*.task; do
ln -sf "$f" "tracker/models/$(basename "$f")"
done
'';
};
}
);
packages = forAllSystems (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
pythonSet = pythonSets.${system};
in
{
default = pythonSet.mkVirtualEnv "kinect-tracker-env" workspace.deps.default;
}
);
apps = forAllSystems (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
pythonSet = pythonSets.${system};
trackerEnv = pythonSet.mkVirtualEnv "kinect-tracker-env" workspace.deps.all;
trackerApp = {
type = "app";
program = toString (
pkgs.writeShellScript "kinect-tracker-run" ''
# --models-dir defaults to the Nix-fetched bundles; a user-supplied
# --models-dir/--model in "$@" overrides it (argparse keeps the last
# occurrence of a repeated option).
exec "${trackerEnv}/bin/python" -m tracker.tracker \
--models-dir "${poseModels pkgs}" "$@"
''
);
};
in
{
# nix run .#tracker -- --source 0 --depth-size 640x480 --shm-name virtual_kinect_frames
# nix run (no args) defaults to the tracker too.
default = trackerApp;
tracker = trackerApp;
core-test = {
type = "app";
program = toString (
pkgs.writeShellScript "kinect-core-test" ''
set -euo pipefail
cd "${self}"
build_dir="$(mktemp -d)"
trap 'rm -rf "$build_dir"' EXIT
${pkgs.cmake}/bin/cmake -S . -B "$build_dir"
${pkgs.cmake}/bin/cmake --build "$build_dir"
${pkgs.cmake}/bin/ctest --test-dir "$build_dir" --output-on-failure
''
);
};
shim-build = {
type = "app";
program = toString (
pkgs.writeShellScript "kinect-shim-build" ''
set -euo pipefail
export PATH="${pkgs.pkgsCross.mingwW64.buildPackages.gcc}/bin:${pkgs.pkgsCross.mingw32.buildPackages.gcc}/bin:$PATH"
export CXX64=x86_64-w64-mingw32-g++
export CXX32=i686-w64-mingw32-g++
export OBJDUMP64=x86_64-w64-mingw32-objdump
export OBJDUMP32=i686-w64-mingw32-objdump
exec "${self}/fallback/windows-nui/build.sh"
''
);
};
}
);
};
}