From NixOS Wiki

ESP-IDF is the official framework to develop programs for the Espressif Systems ESP32 series microcontrollers. This guide explains how to install and use ESP-IDF on NixOS.

The easy way

The ESP32 toolchain and ESP-IDF have been packaged in If you have Nix 2.4 or later, you can get a shell with everything you need to build ESP-IDF projects for ESP32 with this command (no need to download anything yourself):

nix --experimental-features 'nix-command flakes' develop github:mirrexagon/nixpkgs-esp-dev#esp32-idf

If you have an older version of Nix that doesn't support flakes, you can clone the repo and use one of the included shell files:

mkdir ~/esp
cd ~/esp
git clone
cd nixpkgs-esp-dev
nix-shell shells/esp32-idf.nix

See the README for more information.

The manual way

If you want to set up the environment yourself, here is one way to do it.

Setting up the toolchain

ESP-IDF uses the Xtensa or risc-v32 ESP32 GCC toolchain. Espressif hosts the official prebuilt binaries on GitHub. Sadly, these are not statically compiled, and do not work on NixOS without the use of a FHS environment. I will use buildFHSUserEnv to make the binaries work. Let's make a derivation out of this:

{ stdenv, lib, fetchurl, makeWrapper, buildFHSUserEnv }:

  fhsEnv = buildFHSUserEnv {
    name = "esp32-toolchain-env";
    targetPkgs = pkgs: with pkgs; [ zlib ];
    runScript = "";

stdenv.mkDerivation rec {
  pname = "esp32-toolchain";
  version = "2021r2-patch3";

  src = fetchurl {
#    url = "${version}/riscv32-esp-elf-gcc8_4_0-esp-${version}-linux-amd64.tar.gz";
#    hash = "sha256-F5y61Xl5CtNeD0FKGNkAF8DxWMOXAiQRqOmGfbIXTxU=";
    url = "${version}/xtensa-esp32-elf-gcc8_4_0-esp-${version}-linux-amd64.tar.gz";
    hash = "sha256-nt0ed2J2iPQ1Vhki0UKZ9qACG6H2/2fkcuEQhpWmnlM=";

  buildInputs = [ makeWrapper ];

  phases = [ "unpackPhase" "installPhase" ];

  installPhase = ''
    cp -r . $out
    for FILE in $(ls $out/bin); do
      if [[ -x $FILE_PATH ]]; then
        mv $FILE_PATH $FILE_PATH-unwrapped
        makeWrapper ${fhsEnv}/bin/esp32-toolchain-env $FILE_PATH --add-flags "$FILE_PATH-unwrapped"

  meta = with lib; {
    description = "ESP32 toolchain";
    homepage =;
    license = licenses.gpl3;

Create a new directory ~/esp and save this derivation as ~/esp/esp-toolchain.nix.

Note: You can choose any other location instead of ~/esp. This guide assumes that the location is ~/esp.

Setting up ESP-IDF and the development shell

Clone the espressif/esp-idf repository:

cd ~/esp
git clone --recursive

Now that we have ESP-IDF in place, it's time to set up the nix-shell environment with all the dependencies we need.

{ pkgs ? import <nixpkgs> {} }:

pkgs.mkShell {
  name = "esp-idf-env";
  buildInputs = with pkgs; [
    (pkgs.callPackage ./esp32-toolchain.nix {})






    (python3.withPackages (p: with p; [

  shellHook = ''
    export IDF_PATH=$(pwd)/esp-idf
    export PATH=$IDF_PATH/tools:$PATH
    export IDF_PYTHON_ENV_PATH=$(pwd)/.python_env

    if [ ! -e $IDF_PYTHON_ENV_PATH ]; then
      python -m venv $IDF_PYTHON_ENV_PATH
      . $IDF_PYTHON_ENV_PATH/bin/activate
      pip install -r $IDF_PATH/requirements.txt
      . $IDF_PYTHON_ENV_PATH/bin/activate

Save this as ~/esp/shell.nix.

You can now enter the development shell with the ESP32 toolchain and dependencies of ESP-IDF:

cd ~/esp
Note: This environment uses a Python virtual environment and pip to get all the necessary Python dependencies, which is easier to keep up to date than using Python packages from Nix (at the cost of some reproducibility). The virtual environment is created if it doesn't already exist. When updating ESP-IDF, delete the .python_env directory and re-run nix-shell.

That's all you need to start developing with ESP-IDF on NixOS! The next step is to follow the ESP-IDF Get Started guide from section "Start a project" onward.

See also

  • esp32.nix provides nix expression for building the esp32 sdk as well as micropython.
  • esp32-baremetal has an example how to build esp32 firmware without relying on an sdk.
  • tutorial for setting up the prebuilt toolchain with vscode