Platformio

From NixOS Wiki
Jump to: navigation, search

PlatformIO is a SDK/toolchain manager for various microcontrollers and and embedded platforms i.e. esp32.

Basic development environment

{ pkgs ? import <nixpkgs> {} }:
let
in
  pkgs.mkShell {
    buildInputs = [
      pkgs.platformio
      # optional: needed as a programmer i.e. for esp32
      # pkgs.avrdude
    ];
}

Environment considerations specific to PlatformIO

PlatformIO, unless otherwise specified, interprets the configuration-variable core_dir as $HOME/.platformio. All platform development packages, relevant global libraries and other data is installed under this directory as-needed by platformIO.[1]

If this is never, or seldom desired, one (or both) of $HOME and $PLATFORMIO_CORE_DIR environment variables can be set to a location inside the current project, in the used development environment template. Alternatively these can be set in the project's configuration file.[2]

Simple example for moving the core-dir[1] with a shell-hookshellhook. The hook is evaluated once after the build-process, meaning the environment variable is no longer dependent on $PWD. However, if nix-shell is created from elsewhere than the directory shell.nix is located (with command nix-shell /any/path/to/any/shell.nix), this template will not set the core directory inside the project root. In such cases, more robust method of identifying the projects root-directory is needed.

{ pkgs ? import <nixpkgs> {} }:
let
in
  pkgs.mkShell {
    buildInputs = [
      pkgs.platformio
      # optional: needed as a programmer i.e. for esp32
      # pkgs.avrdude
    ];
    shellHook = ''
export PLATFORMIO_CORE_DIR=$PWD/.platformio
'';
}

Full list of environment variables used by platformiocan be found in platformio online-docs, among other configuration documentation.


NixOS

Add the required udev rules.

{
  services.udev.packages = [ 
    pkgs.platformio-core
    pkgs.openocd
  ];
}

Use in vscode

To use the nix-shell provided PlatformIO rather the builtin one first open vscode within the nix-shell and also modify it's settings.json to also contain the following line:

{
      "platformio-ide.useBuiltinPIOCore": false,
}

As of PlatformIO IDE 2.0.0, you will need a shell that allows the extension to run “python -m platformio” (#237313):

{ pkgs ? import (builtins.fetchTarball {
    # NixOS/nixpkgs#237313 = ppenguin:refactor-platformio-fix-ide
    url = "https://github.com/NixOS/nixpkgs/archive/3592b10a67b518700002f1577e301d73905704fe.tar.gz";
  }) {},
}:
let
  envname = "platformio-fhs";
  mypython = pkgs.python3.withPackages (ps: with ps; [ platformio ]);
in
(pkgs.buildFHSUserEnv {
  name = envname;
  targetPkgs = pkgs: (with pkgs; [
    platformio-core
    mypython
    openocd
  ]);
  # NixOS/nixpkgs#263201, NixOS/nixpkgs#262775, NixOS/nixpkgs#262080
  runScript = "env LD_LIBRARY_PATH= bash";
}).env

NOTES

See also