Platformio: Difference between revisions

From NixOS Wiki
imported>Delan
update udev and vscode advice
formatting
 
(2 intermediate revisions by the same user not shown)
Line 40: Line 40:
</syntaxHighlight>
</syntaxHighlight>


[https://discourse.nixos.org/t/how-to-use-platformio-with-vscode/15805/3 As of PlatformIO IDE 2.0.0], you will need a shell that allows the extension to run “python -m platformio” ([https://github.com/NixOS/nixpkgs/pull/237313 #237313]):
[https://discourse.nixos.org/t/how-to-use-platformio-with-vscode/15805/3 As of PlatformIO IDE 2.0.0], <s>you will need a shell that allows the extension to run “python -m platformio” ([https://github.com/NixOS/nixpkgs/pull/237313 #237313])</s>:


<syntaxHighlight lang=nix>
Recently (2024?) simply building an FHS shell with the referred <code>nixpkgs</code> commit doesn't work anymore. The modification is probably incompatible with newer <code>nixpkgs</code>.
{ pkgs ? import (builtins.fetchTarball {
 
    # NixOS/nixpkgs#237313 = ppenguin:refactor-platformio-fix-ide
There's a quick&dirty fix available as a <code>flake</code> <code>devShell</code>. For usage with <code>direnv</code> (recommended), make an <code>.envrc</code> in your project dir like this:<syntaxhighlight lang="bash">
    url = "https://github.com/NixOS/nixpkgs/archive/3592b10a67b518700002f1577e301d73905704fe.tar.gz";
#!/usr/bin/env bash
  }) {},
 
}:
PRJROOT="$(git rev-parse --show-toplevel)"
let
FLAKE=github:ppenguin/nixenvs
  envname = "platformio-fhs";
 
  mypython = pkgs.python3.withPackages (ps: with ps; [ platformio ]);
# avoid a "load loop" of direnv when the new fhs env is entered
in
# https://github.com/direnv/direnv/issues/992
(pkgs.buildFHSUserEnv {
if [ -z "$IN_NIX_SHELL" ]; then
  name = envname;
     use flake $FLAKE\#pio-arduino-fhs
  targetPkgs = pkgs: (with pkgs; [
fi
     platformio-core
 
    mypython
export PRJROOT
    openocd
</syntaxhighlight>This will start a the <code>devShell</code> defined here: https://github.com/ppenguin/nixenvs/blob/main/dev/devshell-pio-arduino-fhs.nix, which includes a working update of the <code>platformio</code> <code>python</code> module for <code>nixpkgs</code>.
  ]);
  # NixOS/nixpkgs#263201, NixOS/nixpkgs#262775, NixOS/nixpkgs#262080
  runScript = "env LD_LIBRARY_PATH= bash";
}).env
</syntaxHighlight>


== See also ==
== See also ==

Latest revision as of 17:45, 2 November 2024

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
    ];
}

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):

Recently (2024?) simply building an FHS shell with the referred nixpkgs commit doesn't work anymore. The modification is probably incompatible with newer nixpkgs.

There's a quick&dirty fix available as a flake devShell. For usage with direnv (recommended), make an .envrc in your project dir like this:

#!/usr/bin/env bash

PRJROOT="$(git rev-parse --show-toplevel)"
FLAKE=github:ppenguin/nixenvs

# avoid a "load loop" of direnv when the new fhs env is entered
# https://github.com/direnv/direnv/issues/992
if [ -z "$IN_NIX_SHELL" ]; then
    use flake $FLAKE\#pio-arduino-fhs
fi

export PRJROOT

This will start a the devShell defined here: https://github.com/ppenguin/nixenvs/blob/main/dev/devshell-pio-arduino-fhs.nix, which includes a working update of the platformio python module for nixpkgs.

See also