Setting Up PlatformIO and PlatformIO IDE on NixOS with VSCodium

This guide provides step-by-step instructions for setting up PlatformIO along with its IDE extension in a NixOS environment, using VSCodium as the code editor. This setup leverages nix-shell to create an isolated development environment tailored for embedded systems development.

Prerequisites / Sources

  • A functioning NixOS NixOS installation.
  • VSCodium information.
  • Basic familiarity with terminal commands and editing files on Linux.

This guide is partially based on information from Nixos wiki and Nixpgs PR-237313.

Step 1: Create Your shell.nix

Create a file named shell.nix in your desired directory. This file will define the development environment:

{ pkgs ? import (builtins.fetchTarball {
    # NixOS/nixpkgs#237313 = ppenguin:refactor-platformio-fix-ide
    url = "https://github.com/NixOS/nixpkgs/archive/pull/237313/head.tar.gz";
  }) {},
}:
let
  envname = "platformio-fhs";
  mypython = pkgs.python3.withPackages (ps: with ps; [ platformio ]);
  
  # Define an overlay to modify platformio-core
  overlay = self: super: {
    platformio-core = super.platformio-core.overrideAttrs (old: {
      propagatedBuildInputs = old.propagatedBuildInputs ++ [ self.python3Packages.pip ];
    });
  };

in
(pkgs.buildFHSEnv {
  name = envname;
  
  # Use an overlay to modify packages
  overlays = [ overlay ];

  # Define target packages within the FHS environment
  targetPkgs = pkgs: (with pkgs; [
    platformio-core
    mypython
    vscodium
    aria2
    unzip
    zip
    aria2
  ]);

  extraBashrc = ''
    echo "Entering PlatformIO FHS environment"
    export PLATFORMIO_CORE_DIR=$PWD/.platformio
  '';

}).env

Step 2: Enter the Nix Shell Environment

Open a terminal, navigate to your directory containing shell.nix, and run:

nix-shell

This command loads the specified environment with all necessary packages.

Step 3: Download and Patch PlatformIO IDE Extension

Create a bash script named setup-platformio-ide.sh in the same directory as your shell.nix. This script will download, patch, and install the PlatformIO extension for VSCodium:


# Download the latest version of PlatformIO IDE extension
aria2c https://marketplace.visualstudio.com/_apis/public/gallery/publishers/platformio/vsextensions/platformio-ide/latest/vspackage

# Create directory for unpacking
mkdir platformio-ide

# Unzip the downloaded package
zcat platformio.platformio-ide-latest@linux-armhf.vsix > x.zip
cd platformio-ide; unzip -x ../x.zip

# Patch settings and remove ms-vscode.cpptools dependency
sed -i 's/extensionDependencies/x-extensionDependencies/' extension/package.json
sed -i '/"platformio-ide.useBuiltinPython": {/,/}/{/"default": true/s/true/false/}' extension/package.json
sed -i '/"platformio-ide.useBuiltinPIOCore": {/,/}/{/"default": true/s/true/false/}' extension/package.json

# Repack to vsix (do not gzip)
zip -r ../platformio.platformio-ide-latest@nixos.vsix .

# Return to the parent directory
cd ..

# Install the patched PlatformIO IDE extension in VSCodium
codium --install-extension platformio.platformio-ide-latest@nixos.vsix

Step 4: Run the Setup Script

Execute the script to download, patch, and install the PlatformIO IDE extension:

bash ./setup-platformio-ide.sh

This process may take a few minutes. Once completed, you can launch VSCodium using the codium command, and the PlatformIO IDE should be available as an installed extension.

Step 5: Start Using PlatformIO

With everything set up, you’re now ready to start developing with PlatformIO on NixOS using VSCodium. Open or create your projects in VSCodium, and utilize PlatformIO’s capabilities for embedded development.

This guide provides a reproducible environment setup that can be easily adjusted or expanded by modifying the shell.nix file according to your project requirements. Happy coding!