How to use platformio with vscode

Here is how to run platformio and build an esp32 firmware

  1. create a nix shell

shell.nix

{ pkgs ? import <nixpkgs> {} }:
let
in
  pkgs.mkShell {
    buildInputs = [
      pkgs.platformio
      pkgs.avrdude
    ];
}
  1. open the shell
nix-shell
  1. Open vscode inside the shell
code .
  1. tell vscode to not use the built-in platformio in settings.json
    "platformio-ide.useBuiltinPIOCore": false,

and thats it !
at least thats what worked for me after scratching my head for 3hours on this …
i hope it will help someone else <3

4 Likes

transformed into a wiki article: Platformio - NixOS Wiki

3 Likes

Unfortunately, this is no longer possible (at least it seems not to work in my case) in platformio-vscode-ide version >=2.0.0. The previous versions were executing the platformio scripts directly, while the newer versions are executing the platformio Python module which is not exported from the platformio derivation outputs hidden behind buildFHSUserEnv; see https://github.com/platformio/platformio-node-helpers/blob/8fac63be73be40589cb006ab0f71cfe7e0b53835/src/core.js#L85 and https://github.com/platformio/platformio-node-helpers/blob/c59068bb37d24ff3af0ff8adf138eeed89803a37/src/core.js#L117

Help would be appreciated.

1 Like

I have the same problem @rychly :frowning:

Any update? I still cant use VSCode on NixOS with PlatformIO :frowning:

The solution from @loucass003 no longer works

3 Likes

Any update? (again necrobump :stuck_out_tongue_winking_eye: )
I’m surprised there’s not more info on this. But I guess the whole paradigm of the platform (managing and downloading frameworks all the time) is “incompatible” with NixOS?

So what’s the best way to do e.g. arduino development under NixOS? Should I be running platformio in a container and pass-through my serial device with the programmer?

(Just as a remote comparison: I already had to resort to running graphical apps (openmodelica in this case) in a container because even though it’s in nixpkgs it suffers from subtle bugs (only under NixOS) making it unusable, at least for many OMEdit use cases.)

1 Like

Ok, I took a deeper look and can confirm @rychly 's analysis.
I tried a very dirty workaround by making a wrapper bash script that wraps python and pio and forwards all invocations to a podman container with platformio. This gets pretty close (in that it intercepts most (?) script calls from the vscode-extension), but it still fails before having started the server. Starting the server manually in parallel doesn’t work, possibly of mismatched port number negotiation due to unsynced startup.

Going back to the “nix-way”: basically all we would need to do is make an FHS-nix-shell that combines platformio and a python3 interpreter (this by itself is trivial), but also adds the realised/built platformio python module to the python environment.

Could this mean we have to use buildPythonPackage on the platformio-core sources (maybe elegantly adding an extra output to the nix-package expression of platform-core?) and add it to the python env instantiation in the FHS? And while at it, make the current platformio-core build which uses buildPythonApplication to build the whole thing simply into the original thin python wrapper that then imports our new module?

Looking further: will the toolchains that platformio downloads into ~/.platformio/... work in the FHS, or can we expect difficult issues with that further down the line as well?

1 Like

Working PoC for refactoring platformio in nixpkgs: Refactor platformio fix ide by ppenguin · Pull Request #237313 · NixOS/nixpkgs · GitHub

1 Like