# Cardputer ADV only → $out/bin/squirrel.bin (no build_firmware.py). { lib, stdenv, firmwareSrc, espIdf, espIdfVendor, squirrelRev ? "unknown", cardputerAdv, stagedApp, managedComponents, gnumake, quilt, git, python3, idfPython, flex, bison, gperf, pkg-config, cmake, ninja, writeShellScript, writeText, }: let idfPythonDrv = idfPython; squirrelNoUsbSdkconfig = writeText "sdkconfig.squirrel_no_usb" '' # Squirrel! board: no TinyUSB OTG CDC (sync is BLE). Console on USB Serial/JTAG. CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG=y CONFIG_ESP_CONSOLE_UART_DEFAULT=n CONFIG_ESP_CONSOLE_UART=n ''; pickNewestBin = '' shopt -s nullglob bins=( "$1"/*.bin ) if [ "''${#bins[@]}" -eq 0 ]; then return 1; fi local newest="''${bins[0]}" local f for f in "''${bins[@]}"; do if [ "$f" -nt "$newest" ]; then newest="$f"; fi done printf '%s' "$newest" ''; # --reflink=auto: native copy when $TMPDIR is XFS/btrfs (COW); avoids full data duplicate. storeCp = "cp -a --reflink=auto"; build = writeShellScript "squirrel-firmware-build" '' set -euo pipefail export HOME="$TMPDIR/home" mkdir -p "$HOME" IDF_WORK="$TMPDIR/esp-idf" rm -rf "$IDF_WORK" mkdir -p "$IDF_WORK" ${storeCp} ${espIdfVendor}/. "$IDF_WORK/" # Quilt / make patch may write under IDF; reflink keeps blobs shared until mutated. chmod -R u+w "$IDF_WORK" export IDF_PATH="$IDF_WORK" . ${espIdf}/.tool-env export PYTHONPATH="${idfPythonDrv}/${idfPythonDrv.sitePackages}''${PYTHONPATH:+:}''${PYTHONPATH:-}" export IDF_COMPONENT_MANAGER=0 FW="$(pwd)" M5="$FW/m5stack" STAGED=${stagedApp} BASE=${cardputerAdv.boardBase} NEW=${cardputerAdv.boardSquirrel} BOARDS="$M5/boards" DIST="$NIX_BUILD_TOP/dist" mkdir -p "$DIST" # MicroPython patches from Squirrel! (BLE central, etc.) if [ -d "$FW/micropython" ]; then for p in ${lib.concatStringsSep " " (map lib.escapeShellArg cardputerAdv.firmwarePatches)}; do if [ -f "$p" ]; then git -C "$FW/micropython" apply --check "$p" 2>/dev/null \ && git -C "$FW/micropython" apply "$p" || true fi done fi cd "$M5" # No registry TinyUSB for Squirrel: main manifest still lists esp_tinyusb for stock boards. yml="$M5/main/idf_component.yml" if [ -f "$yml" ] && grep -q 'espressif/esp_tinyusb' "$yml"; then chmod u+w "$yml" sed -i '/espressif\/esp_tinyusb/,+3d' "$yml" fi cmake_default="$M5/CMakeListsDefault.cmake" if [ -f "$cmake_default" ] && ! grep -q 'espressif__mdns' "$cmake_default"; then chmod u+w "$cmake_default" sed -i '/esp32-camera/a\ espressif__mdns\n espressif__quirc' "$cmake_default" fi # fetchgit + fetchSubmodules already vendor submodules; make submodules needs .git make patch littlefs mpy-cross rm -rf managed_components ${storeCp} ${managedComponents}/managed_components managed_components # Registry zips live as espressif__*; with manager off, materialize under components/. mkdir -p components shopt -s nullglob shortLinkComponents="mdns nghttp quirc esp-dsp esp_codec_dev" for d in managed_components/espressif__*; do base="''${d##*/}" short="''${base#espressif__}" rm -rf "components/$base" "components/$short" ${storeCp} "$d" "components/$base" case " $shortLinkComponents " in *" $short "*) ln -sfn "$base" "components/$short" ;; esac done chmod -R u+w components/espressif__* cam_cmake="$M5/components/esp32-camera/CMakeLists.txt" if [ -f "$cam_cmake" ] && ! grep -q 'espressif__esp_jpeg' "$cam_cmake"; then chmod u+w "$cam_cmake" sed -i 's/^set(req driver)$/set(req driver espressif__esp_jpeg)/' "$cam_cmake" fi # Board synthesis (was build_firmware.make_board) rm -rf "$BOARDS/$NEW" ${storeCp} "$BOARDS/$BASE" "$BOARDS/$NEW" while IFS= read -r -d $'\0' f; do if ! grep -q "$BASE" "$f" 2>/dev/null; then continue fi sed -i "s/$BASE/$NEW/g" "$f" done < <(find "$BOARDS/$NEW" -type f ! -name '*.py' -print0) if [ -f "$BOARDS/$NEW/manifest.py" ]; then sed -i "s/$BASE/$NEW/g" "$BOARDS/$NEW/manifest.py" fi if [ -f "$M5/scripts/gen-firmware-info.sh" ]; then bash "$M5/scripts/gen-firmware-info.sh" > "$BOARDS/$NEW/firmware_info.py" elif [ -f "$BOARDS/$BASE/firmware_info.py" ]; then cp "$BOARDS/$BASE/firmware_info.py" "$BOARDS/$NEW/firmware_info.py" fi rm -rf "$BOARDS/$NEW/squirrel" ${storeCp} "$STAGED" "$BOARDS/$NEW/squirrel" find "$BOARDS/$NEW/squirrel" -type d -name __pycache__ -prune -exec rm -rf {} + 2>/dev/null || true printf '\n%s\nfreeze("$(BOARD_DIR)/squirrel")\n' ${lib.escapeShellArg cardputerAdv.manifestMarker} \ >> "$BOARDS/$NEW/manifest.py" sed -i '\|libs/usb/manifest.py|d' "$BOARDS/$NEW/manifest.py" cp ${squirrelNoUsbSdkconfig} "$BOARDS/$NEW/sdkconfig.squirrel_no_usb" cmake_board="$BOARDS/$NEW/mpconfigboard.cmake" h_board="$BOARDS/$NEW/mpconfigboard.h" chmod u+w "$cmake_board" "$h_board" sed -i '/sdkconfig\.usb/d' "$cmake_board" sed -i "/sdkconfig.board/i\\ ./boards/$NEW/sdkconfig.squirrel_no_usb" "$cmake_board" if ! grep -q 'MICROPY_PY_TINYUSB' "$cmake_board"; then sed -i '/set(MICROPY_PY_LVGL/a set(MICROPY_PY_TINYUSB 0)' "$cmake_board" fi if ! grep -q 'MICROPY_HW_ENABLE_USBDEV' "$h_board"; then sed -i '/MICROPY_HW_MCU_NAME/a\ \ // Squirrel! build (tools/squirrel): no TinyUSB CDC — use USB Serial/JTAG for REPL.\ #define MICROPY_HW_ENABLE_USBDEV (0) ' "$h_board" fi printf 'generated by tools/squirrel (nix)\n' > "$BOARDS/$NEW/.squirrel_generated" # Optional VLW font into the packed system FS (pack removes temp copy) font="" for candidate in ${lib.concatStringsSep " " (map lib.escapeShellArg cardputerAdv.fontCandidates)}; do if [ -f "$candidate" ]; then font="$candidate"; break; fi done font_dest="$M5/fs/system/common/font/squirrel.vlw" if [ -n "$font" ] && [ ! -f "$font_dest" ]; then mkdir -p "$(dirname "$font_dest")" cp "$font" "$font_dest" fi make_started=$(date +%s) make BOARD="$NEW" BOARD_TYPE=${cardputerAdv.boardType} pack if [ -n "$font" ] && [ -f "$font_dest" ]; then rm -f "$font_dest" fi build_out="$M5/build-$NEW" found="" shopt -s nullglob if [ -d "$build_out" ]; then found=$(ls -t "$build_out"/uiflow*.bin 2>/dev/null | head -1 || true) fi # Newest complete .bin under m5stack/ (skip esp-idf component trees) while IFS= read -r -d $'\0' bin; do mtime=$(stat -c%Y "$bin") if [ "$mtime" -lt "$make_started" ]; then continue fi if [[ "$bin" == *"/esp-idf/"* ]] || [[ "$bin" == *"/managed_components/"* ]]; then continue fi boot=$(od -An -tu1 -N1 "$bin" 2>/dev/null | tr -d ' \n') sig=$(dd if="$bin" bs=1 skip=32768 count=2 2>/dev/null | od -An -tx1 | tr -d ' \n') if [ "$boot" = "233" ] && [ "$sig" = "aa50" ]; then if [ -z "$found" ] || [ "$bin" -nt "$found" ]; then found="$bin" fi fi done < <(find "$M5" -name '*.bin' -type f -print0) if [ -z "$found" ]; then echo "no complete firmware .bin under $M5" exit 1 fi cp "$found" "$DIST/squirrel.bin" cp "$found" "$FW/squirrel.bin" ''; in stdenv.mkDerivation { pname = "squirrel-firmware"; version = "unstable-${squirrelRev}"; src = firmwareSrc; dontConfigure = true; nativeBuildInputs = [ espIdf python3 git quilt gnumake flex bison gperf pkg-config cmake ninja stdenv.cc idfPythonDrv ]; doCheck = true; __darwinAllowLocalNetworking = true; buildPhase = "${build}"; checkPhase = '' newest="" if [ -f squirrel.bin ]; then newest="$(pwd)/squirrel.bin" else newest="$(${pickNewestBin} "$NIX_BUILD_TOP/dist")" || true fi if [ -z "$newest" ] || [ ! -f "$newest" ]; then echo "check: no .bin in build output" exit 1 fi size=$(stat -c%s "$newest") if [ "$size" -lt 65536 ]; then echo "check: firmware too small ($size bytes)" exit 1 fi boot=$(od -An -tu1 -N1 "$newest" | tr -d ' \n') if [ "$boot" != "233" ]; then echo "check: missing ESP-ROM magic 0xE9 at 0x0 (got ''${boot})" exit 1 fi sig=$(dd if="$newest" bs=1 skip=32768 count=2 2>/dev/null | od -An -tx1 | tr -d ' \n') if [ "$sig" != "aa50" ]; then echo "check: missing partition table 0xAA50 at 0x8000 (got ''${sig})" exit 1 fi echo "check: ok $newest ($size bytes, complete@0x0)" ''; installPhase = '' install -Dm644 squirrel.bin $out/bin/squirrel.bin ''; meta = with lib; { description = "Squirrel! complete flash image for M5Stack Cardputer ADV"; homepage = "https://github.com/aparatowo/Squirrel"; license = licenses.mit; platforms = platforms.linux; }; }