Files
kinect/flake.nix
T
kinect-botandsisyphus-dev-ai 7d055fc847 fix(build): move kDefaultShmName into the outer let block (scope bug)
Placed it as a sibling of devShells/packages/apps in the output attrset
instead of inside the let ... in binding those three outputs share, so it
was never in lexical scope where apps = forAllSystems (...) referenced it -
"error: undefined variable 'kDefaultShmName'" on any nix run. Moved it next
to the other shared helpers (mingwCxxWrapper, poseModels, etc.) in the one
outer let block.

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

282 lines
11 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
]
)
);
# nixpkgs' mingw32/mingwW64 GCC links against the MCF threading runtime but
# does not pull in libmcfgthread automatically - a known upstream nixpkgs gap
# ("pkgsCross.mingw32.stdenv.cc provides slightly incomplete linking
# environment: cannot find -lmcfgthread", nixpkgs#156343). The project's
# build.sh / build_win.sh hardcode -static -static-libgcc -static-libstdc++
# (for a dependency-free shipped DLL), which pulls in libstdc++'s MCF-based
# locale/guard code - so link a wrapped g++ that appends the (overridden to
# build statically; upstream only ships the shared lib) mcfgthread archive.
mingwCxxWrapper =
pkgs: crossPkgs: triple:
let
mcf = crossPkgs.windows.mcfgthreads.overrideAttrs (_: { dontDisableStatic = true; });
realGxx = "${crossPkgs.buildPackages.gcc}/bin/${triple}-g++";
in
pkgs.writeShellScriptBin "${triple}-g++" ''
exec "${realGxx}" "$@" -L${mcf}/lib -lmcfgthread
'';
mingw64Cxx = pkgs: mingwCxxWrapper pkgs pkgs.pkgsCross.mingwW64 "x86_64-w64-mingw32";
mingw32Cxx = pkgs: mingwCxxWrapper pkgs pkgs.pkgsCross.mingw32 "i686-w64-mingw32";
# Native toolchain (core/ + fallback/windows-nui/ + tracker-native/) shared
# by the devShell and the build/test/run apps below. The raw gcc packages
# stay on PATH too (as/ld/objdump/windres live there); CXX32/CXX64/CXX_WIN
# below point at the wrapper's absolute store path, not a bare name, so
# there is no PATH-ordering ambiguity between the wrapper and the raw g++.
nativeTools =
pkgs: with pkgs; [
cmake
gnumake
gcc
git
curl
pkgsCross.mingwW64.buildPackages.gcc
pkgsCross.mingw32.buildPackages.gcc
(mingw64Cxx pkgs)
(mingw32Cxx pkgs)
];
# 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 ];
# The shared-memory name the Linux tracker publishes to and the Wine/Proton
# shim reads (docs/linux-wine.md's KINECT_SHIM_SHM_FILE bridge). Defined once
# here so the tracker app and the launch-options helper below can't drift.
kDefaultShmName = "virtual_kinect_frames";
# 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 the mcfgthread-wrapped compilers above
# (absolute paths - no bare-name/PATH ambiguity with the unwrapped g++
# that pkgsCross.*.buildPackages.gcc also puts on PATH for as/ld/objdump).
CXX32 = "${mingw32Cxx pkgs}/bin/i686-w64-mingw32-g++";
CXX64 = "${mingw64Cxx pkgs}/bin/x86_64-w64-mingw32-g++";
CXX_WIN = "${mingw64Cxx pkgs}/bin/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 and --shm-name are the flake's defaults; a
# user-supplied --models-dir/--shm-name/--model in "$@" overrides
# them (argparse keeps the last occurrence of a repeated option).
exec "${trackerEnv}/bin/python" -m tracker.tracker \
--models-dir "${poseModels pkgs}" \
--shm-name "${kDefaultShmName}" "$@"
''
);
};
launchOptionsApp = {
type = "app";
program = toString (
pkgs.writeShellScript "kinect-launch-options" ''
set -euo pipefail
# Wine's default Z: drive maps the whole host filesystem at /, so a
# POSIX path becomes Z:\the\same\path with slashes flipped - this is
# the one piece docs/linux-wine.md otherwise has you compute by hand.
to_wine_path() {
printf 'Z:%s' "$1" | sed 's#/#\\#g'
}
shm_file="$(to_wine_path "/dev/shm/${kDefaultShmName}")"
config_file="$(to_wine_path "''${XDG_CONFIG_HOME:-$HOME/.config}/VirtualKinect/calibration.cfg")"
echo "Paste into Steam -> (game) -> Properties -> General -> Launch Options:"
echo "KINECT_SHIM_SHM_FILE='$shm_file' KINECT_SHIM_CONFIG='$config_file' %command%"
echo
echo "Run the tracker with: nix run .#tracker -- --source 0"
echo "Calibrate the cursor with: nix run .#tracker -- --calibrate --source 0"
''
);
};
in
{
# nix run .#tracker -- --source 0 --depth-size 640x480
# nix run (no args) defaults to the tracker too.
default = trackerApp;
tracker = trackerApp;
launch-options = launchOptionsApp;
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="${mingw64Cxx pkgs}/bin/x86_64-w64-mingw32-g++"
export CXX32="${mingw32Cxx pkgs}/bin/i686-w64-mingw32-g++"
export OBJDUMP64=x86_64-w64-mingw32-objdump
export OBJDUMP32=i686-w64-mingw32-objdump
exec "${self}/fallback/windows-nui/build.sh"
''
);
};
}
);
};
}