Files
kinect/flake.nix
T
kinect-botandsisyphus-dev-ai b0c418bd16 fix(build): link the missing mcfgthread static lib for mingw cross builds
The shim build failed under the Nix flake with a wall of "undefined
reference to __MCF_cxa_guard_acquire / _MCF_mutex_lock_slow / ..." from
libstdc++.a. Root cause: nixpkgs' pkgsCross.mingw32/mingwW64 GCC links
against the MCF threading runtime but doesn't pull in libmcfgthread
automatically - a known upstream nixpkgs gap (nixpkgs#156343,
"pkgsCross.mingw32.stdenv.cc provides slightly incomplete linking
environment: cannot find -lmcfgthread"). The project's build.sh /
build_win.sh hardcode -static -static-libgcc -static-libstdc++ (so the
shipped DLL has no runtime dependency), which pulls in libstdc++'s
MCF-based locale/guard code and hits exactly this gap.

Added mingwCxxWrapper: a thin wrapper around each cross g++ that appends
-L<mcfgthreads>/lib -lmcfgthread, using pkgsCross.<target>.windows.mcfgthreads
overridden with dontDisableStatic (upstream only ships the shared lib by
default). CXX32/CXX64/CXX_WIN now point at the wrapper's absolute store
path rather than a bare compiler name, so there's no PATH-ordering
ambiguity against the unwrapped g++ that pkgsCross.*.buildPackages.gcc
still puts on PATH for as/ld/objdump/windres.

Not yet verified against a real nix build (no nix in this sandbox) - based
on the confirmed upstream nixpkgs issue plus a real working example from
that issue thread (pkgsCross.mingwW64.windows.mcfgthreads.overrideAttrs
{ dontDisableStatic = true; }). Awaiting the user's real-environment
rebuild.

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

254 lines
9.8 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 ];
# 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 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="${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"
''
);
};
}
);
};
}