Jump to content

ESP-IDF: Difference between revisions

pkgconfig has been replaced by pkg-config
imported>Mirrexagon
(Updated toolchain derivation to latest as of 2021-11-03, overhauled shell.nix to create a Python venv)
imported>Mse63
(pkgconfig has been replaced by pkg-config)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[https://github.com/espressif/esp-idf ESP-IDF] is the official framework to develop programs for the Espressif Systems [https://en.wikipedia.org/wiki/ESP32 ESP32] series microcontrollers.
[https://github.com/espressif/esp-idf ESP-IDF] is the official framework to develop programs for the Espressif Systems [https://en.wikipedia.org/wiki/ESP32 ESP32] series microcontrollers.
This guide explains how to install and use ESP-IDF on NixOS (I have not tested this on non-NixOS systems, but it might also work).
This guide explains how to install and use ESP-IDF on NixOS.


== Setting up the toolchain ==
== The easy way ==
ESP-IDF uses the Xtensa ESP32 GCC toolchain. Espressif hosts [https://dl.espressif.com/dl/xtensa-esp32-elf-linux64-1.22.0-80-g6c4433a-5.2.0.tar.gz prebuilt binaries] on their website. Sadly, these are not statically compiled, and do not work on NixOS without the use of a FHS environment. I will use <code>buildFHSUserEnv</code> to make the binaries work. Let's make a derivation out of this:
The ESP32 toolchain and ESP-IDF have been packaged in https://github.com/mirrexagon/nixpkgs-esp-dev. 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):
 
<syntaxhighlight lang="sh">
nix --experimental-features 'nix-command flakes' develop github:mirrexagon/nixpkgs-esp-dev#esp32-idf
</syntaxhighlight>
 
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:
 
<syntaxhighlight lang="sh">
mkdir ~/esp
cd ~/esp
git clone https://github.com/mirrexagon/nixpkgs-esp-dev.git
cd nixpkgs-esp-dev
nix-shell shells/esp32-idf.nix
</syntaxhighlight>
 
See the [https://github.com/mirrexagon/nixpkgs-esp-dev#readme 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 [https://github.com/espressif/crosstool-NG/releases 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 <code>buildFHSUserEnv</code> to make the binaries work. Let's make a derivation out of this:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 18: Line 40:
stdenv.mkDerivation rec {
stdenv.mkDerivation rec {
   pname = "esp32-toolchain";
   pname = "esp32-toolchain";
   version = "2021r2";
   version = "2021r2-patch3";


   src = fetchurl {
   src = fetchurl {
#    url = "https://github.com/espressif/crosstool-NG/releases/download/esp-${version}/riscv32-esp-elf-gcc8_4_0-esp-${version}-linux-amd64.tar.gz";
#    hash = "sha256-F5y61Xl5CtNeD0FKGNkAF8DxWMOXAiQRqOmGfbIXTxU=";
     url = "https://github.com/espressif/crosstool-NG/releases/download/esp-${version}/xtensa-esp32-elf-gcc8_4_0-esp-${version}-linux-amd64.tar.gz";
     url = "https://github.com/espressif/crosstool-NG/releases/download/esp-${version}/xtensa-esp32-elf-gcc8_4_0-esp-${version}-linux-amd64.tar.gz";
     hash = "sha256-PrPWiyf6a6Wvb4jaIcuPrOm+AJTaqolgeTz+Vwq3hf8=";
     hash = "sha256-nt0ed2J2iPQ1Vhki0UKZ9qACG6H2/2fkcuEQhpWmnlM=";
   };
   };


Line 51: Line 75:
{{note| You can choose any other location instead of <code>~/esp</code>. This guide assumes that the location is <code>~/esp</code>.}}
{{note| You can choose any other location instead of <code>~/esp</code>. This guide assumes that the location is <code>~/esp</code>.}}


== Setting up ESP-IDF and the development shell ==
=== Setting up ESP-IDF and the development shell ===
Clone the [https://github.com/espressif/esp-idf espressif/esp-idf] repository:
Clone the [https://github.com/espressif/esp-idf espressif/esp-idf] repository:
<syntaxhighlight lang="sh">
<syntaxhighlight lang="sh">
Line 59: Line 83:


Now that we have ESP-IDF in place, it's time to set up the <code>nix-shell</code> environment with all the dependencies we need.
Now that we have ESP-IDF in place, it's time to set up the <code>nix-shell</code> environment with all the dependencies we need.
Note that 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 <code>.python_env</code> directory and re-run <code>nix-shell</code>.


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 67: Line 89:
pkgs.mkShell {
pkgs.mkShell {
   name = "esp-idf-env";
   name = "esp-idf-env";
   buildInputs = with pkgs; [
   buildInputs = with pkgs; [
     (pkgs.callPackage ./esp32-toolchain.nix {})
     (pkgs.callPackage ./esp32-toolchain.nix {})
Line 78: Line 99:
     bison
     bison
     gperf
     gperf
     pkgconfig
     pkg-config


     cmake
     cmake
Line 115: Line 136:
nix-shell
nix-shell
</syntaxhighlight>
</syntaxhighlight>
{{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 <code>.python_env</code> directory and re-run <code>nix-shell</code>.}}


That's all you need to start developing with ESP-IDF on NixOS!
That's all you need to start developing with ESP-IDF on NixOS!
Anonymous user