How simple writing ESP32 ESPHome firmware on NixOS

Sometime, OTA is not enought

My IOT are managed with Home Assistant with most IOT as ESP32 or ESP8266.

So ESPhome is the solution to program them all !

But sometime, pushing the limit of an ESP32 device, result in very heavy firmware, and this hudge size prevent to do OTA.

The solution is to download the binary firmware and write it thougth USB cable. A python tool called “esptool.py” do the flashing stuff preaty well.

On NixOS, you can fash an ESP32 firmware in one line without dealing with python versions.

nix-shell -p esptool --run "esptool.py --chip esp32 -p /dev/ttyUSB0 write_flash 0x0 epaper42esp32-factory.bin"

result :

these 3 paths will be fetched (0.52 MiB download, 2.70 MiB unpacked):
  /nix/store/khjrcjmazkzfhdd3f01nvyi8amn7vaqh-esptool-4.5.1
  /nix/store/mxrwjd28hfgj2fihqhw5lika2hkl145z-python3.10-bitstring-4.0.2
  /nix/store/2splqgw25s2ln7s289jbg75g1164ywmm-python3.10-reedsolo-1.5.4
copying path '/nix/store/mxrwjd28hfgj2fihqhw5lika2hkl145z-python3.10-bitstring-4.0.2' from 'https://cache.nixos.org'...
copying path '/nix/store/2splqgw25s2ln7s289jbg75g1164ywmm-python3.10-reedsolo-1.5.4' from 'https://cache.nixos.org'...
copying path '/nix/store/khjrcjmazkzfhdd3f01nvyi8amn7vaqh-esptool-4.5.1' from 'https://cache.nixos.org'...
esptool.py v4.5.1
Serial port /dev/ttyUSB0
Connecting.....
Chip is ESP32-D0WDQ6 (revision v1.0)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: 24:6f:28:97:6a:34
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Flash will be erased from 0x00000000 to 0x001b5fff...
Compressed 1793248 bytes to 1138905...
Wrote 1793248 bytes (1138905 compressed) at 0x00000000 in 100.1 seconds (effective 143.4 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

At the end, nothing leave in my NixOS system user ! Cool isn’t it ?