#+TITLE: Squirrel! Cardputer ADV — Nix flake (illustration) #+DESCRIPTION: Reference snapshot of a Nix-packaged ESP32 firmware build: what problem it solves, how the pieces fit, and what to read file-by-file — not only how to rerun it. #+OPTIONS: toc:2 num:nil **Illustration and reference** for wrapping a third-party vendor tree (M5 UIFlow / Cardputer ADV MicroPython) plus an application (Squirrel!) in Nix — without claiming a supported fork of either upstream or a turnkey product. #+begin_center /BTW this build didn't work. Black screen./ And, duh, we ditched the USB CDC so no MicroPython REPL. /Those instructions aren't successful./ #+end_center * What this build is for Squirrel! is open firmware for a pocket notes / todos stack on the **M5Stack Cardputer ADV** (ESP32-S3 + keyboard). Data lives on SD; optional sync to a T-Watch is **Bluetooth**, not USB mass-storage or TinyUSB gadget modes. Upstream ships =build_firmware.py=: clone vendor repos, stage the app from =ports/cardputer_adv/port.toml=, synthesize a =*_Squirrel= board, run =make pack=, flash or copy =.bin= to SD. The flake here is a **narrow slice** of that story: - **One port** (Cardputer ADV), **one output** (packed =uiflow= image suitable for M5Launcher-style SD install). - **No** interactive flash/REPL/T-Watch tooling in Nix — only the compile-and-pack path. - **Declared inputs** (Squirrel git rev, Cardputer firmware git rev, IDF 5.4.2, registry component hashes) so a machine with Nix can rebuild without “whatever happened to be in =managed_components/= last Tuesday.” It is useful as a **pattern reference** for: offline ESP-IDF component pins, =IDF_COMPONENT_MANAGER=0=, duplicating a vendor board directory with sed, and checking firmware blobs in =checkPhase=. * Pipeline (conceptual) #+begin_example flake inputs (Squirrel app, Cardputer vendor tree, esp-dev toolchains) │ ├─► cardputer-adv.nix ──► port_config.py policy, board names, patch list ├─► stage-app.nix ──► filtered Squirrel! tree for freezing ├─► managed-components.nix ──► Espressif zip FODs (no network at compile time) │ └─► package.nix fetchgit IDF vendor + copy m5stack tree make patch / littlefs / mpy-cross synthesize M5STACK_CardputerADV_Custom_Squirrel make BOARD=… pack → squirrel.bin (~6.4 MiB packed flash image) #+end_example Upstream =build_firmware.py= still owns **product** ergonomics (flash, device setup, multi-port). The Nix layer owns **reproducibility**: same inputs, same graph, repeatable full rebuilds on a Nix-capable host. * Design choices worth reading for **Dual ESP-IDF tree.** Nixpkgs =esp-idf-full= supplies toolchains and =idf.py= env; a separate =fetchgit= of Espressif’s repo (with submodules) is copied into the build because the trimmed nixpkgs tree is missing pieces the M5 Makefile expects. **Component manager off.** Vendor =idf_component.yml= files are not resolved at build time. Registry artifacts are prefetched in =managed-components.nix= and copied into =components/espressif__*=; CMake is patched where the manager would have wired =REQUIRES= (e.g. =mdns=, =quirc=). **Squirrel board ≠ vendor board.** The Custom Cardputer ADV board enables TinyUSB CDC and frozen M5 USB helpers. Squirrel does not use USB for sync; this overlay **strips TinyUSB**, uses **USB Serial/JTAG** for console on the S3, and drops frozen M5 =libs/usb=. That avoids registry =tinyusb= / =esp_tinyusb= and IDF 5.4 header mismatches in =dcd_esp32sx.c= when =soc/usb_periph.h= no longer carries register macros — at the cost of custom =sdkconfig= and manifest lines. **Camera stack still links.** M5’s shared =CMakeListsDefault.cmake= always pulls =esp32-camera= (and thus =esp_jpeg= in the offline set) even though Cardputer ADV has no camera. Vendor cmake baggage, not Squirrel features. **Store and I/O.** Full firmware rebuilds are heavy on =/nix/store=. The driver uses =cp -a --reflink=auto= on CoW filesystems to avoid duplicating large IDF trees every attempt. **What you do not get in this folder.** No =squirrel.bin= binary (artifact comes from =nix build=). No flash scripts bundled here; pins are fixed in =flake.lock= and may lag upstream =main=. * Layout All sources: [[file:SquirrelNixFlake/][ass/SquirrelNixFlake/]]. * [[file:SquirrelNixFlake/flake.nix][flake.nix]] - Flake entry: wires inputs to derivations. - =esp-dev=: Xtensa toolchain + =esp-idf-full= overlay (IDF 5.4.2 pin). - =squirrel= / =cardputer-firmware=: non-flake GitHub sources (app + vendor =m5stack/=). - Outputs =firmware= (default), =stagedApp=, =managedComponents=; =checks.default= equals firmware. * [[file:SquirrelNixFlake/flake.lock][flake.lock]] - Frozen input graph. - Exact Squirrel and Cardputer-adv-micropython commits for this snapshot. - Substitutes for ad-hoc “note the SHAs in README” discipline. * [[file:SquirrelNixFlake/cardputer-adv.nix][cardputer-adv.nix]] - Pure Nix policy module (no network). - Parses =port.toml= / =features.toml= like =build_firmware.py= when generating =port_config.py=. - Computes which Squirrel subtrees are frozen (=rsyncExcludes=) from hardware + feature gates. - Names =boardBase= / =boardSquirrel= and collects =firmware_patches/*.patch= paths for =git apply=. * [[file:SquirrelNixFlake/stage-app.nix][stage-app.nix]] - Small derivation: materialize “what gets frozen into MicroPython.” - Copies =Squirrel!= minus dev/tooling paths. - Injects generated =port_config.py= and a minimal =sq_info.py= (=BUILD = "nix"=). * [[file:SquirrelNixFlake/managed-components.nix][managed-components.nix]] - Offline mirror of Espressif Component Registry zips. - Pattern: each component =fetchurl= + unzip into =managed_components/espressif__*=. - Shows which blobs Cardputer ADV needs with manager off (ADF/audio path, mdns, JPEG for shared camera cmake, etc.). * [[file:SquirrelNixFlake/package.nix][package.nix]] - The long build script: vendor Makefile inside Nix. - Board synthesis (=cp= base board → =*_Squirrel=, manifest freeze line, USB/console overlays). - =make pack= orchestration, artifact pick (=uiflow*.bin=), sanity checks (ROM magic, partition table signature). - The place to compare with a one-off =build_firmware.py= run on a dirty tree. * Running the flake From a machine with Nix and enough disk: #+begin_src shell cd …/ass/SquirrelNixFlake nix build -L .#firmware ls -l result/bin/squirrel.bin #+end_src Flashing and SD layout follow Squirrel and M5Launcher documentation; rebuilding is optional once you have read the sources. * Upstream - Squirrel!: https://github.com/aparatowo/Squirrel - Cardputer ADV MicroPython vendor tree: https://github.com/TheRealHaoLiu/cardputer-adv-micropython - ESP-IDF Nix tooling: https://github.com/mirrexagon/nixpkgs-esp-dev #+LF #+NF