├── .gitignore ├── flake.lock ├── README.md └── flake.nix /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | .vscode*/ 3 | logs/ 4 | result 5 | 6 | -------------------------------------------------------------------------------- /flake.lock: -------------------------------------------------------------------------------- 1 | { 2 | "nodes": { 3 | "nixpkgs": { 4 | "locked": { 5 | "lastModified": 0, 6 | "narHash": "sha256-HYcP2jaRQICu3P2CXmTeBJvuWP0JpC6svPaAZk9JfJ0=", 7 | "path": "/nix/store/8y9vk56lybhgvpz8jnnhm2jcq69yixl1-source", 8 | "type": "path" 9 | }, 10 | "original": { 11 | "id": "nixpkgs", 12 | "type": "indirect" 13 | } 14 | }, 15 | "root": { 16 | "inputs": { 17 | "nixpkgs": "nixpkgs" 18 | } 19 | } 20 | }, 21 | "root": "root", 22 | "version": 7 23 | } 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Portable PlatformIO Development Environment with VSCodium 2 | 3 | This flake provides a working PlatformIO development environment using VSCodium on NixOS or other Nix-based systems. It leverages `nixpkgs` for Python and PlatformIO, ensuring compatibility while bypassing the versions provided by the PlatformIO extension. 4 | 5 | **Note:** This method involves patching the PlatformIO VSCode extension and using overrides in the Python environment. It may break in the future due to updates or changes in dependencies. 6 | 7 | --- 8 | 9 | ## Quick Start 10 | 11 | ### Option 1: Start VSCodium Directly 12 | Run the following command to launch VSCodium with the PlatformIO environment pre-configured. You can also pass options such as `--help` to VSCodium: 13 | ```bash 14 | nix run --impure github:xdadrm/nixos_use_platformio_patformio-ide_and_vscode#codium -- . 15 | ``` 16 | 17 | ### Option 2: Open a Development Shell 18 | To open a shell with VSCodium, PlatformIO (`pio`), and `tio` available, use: 19 | ```bash 20 | nix develop --impure github:xdadrm/nixos_use_platformio_patformio-ide_and_vscode 21 | ``` 22 | 23 | --- 24 | 25 | ## Customizing the Development Environment 26 | 27 | If you want to modify the development environment, follow these steps: 28 | 29 | 1. Clone the repository: 30 | ```bash 31 | git clone https://github.com/xdadrm/nixos_use_platformio_patformio-ide_and_vscode.git 32 | cd nixos_use_platformio_patformio-ide_and_vscode 33 | ``` 34 | 35 | 2. Enter the development shell and launch VSCodium: 36 | ```bash 37 | nix develop --impure .# 38 | codium . 39 | ``` 40 | 41 | --- 42 | 43 | ## Detailed Setup Guide 44 | 45 | ### Prerequisites 46 | - **NixOS**: Installed on an x86-64 system. Follow the [official installation guide](https://nixos.org/download.html). 47 | - **Git**: Required to clone the repository. 48 | - **Permissions** If you want to be able to communicate with your microcontrollers via serial ports - you'll have to add your user to the dialout group 49 | 50 | --- 51 | 52 | ### How It Works 53 | 54 | This setup allows you to use VSCodium with the PlatformIO IDE extension on NixOS. It modifies your existing VSCodium configuration by: 55 | - Adding the PlatformIO extension. 56 | - Adjusting settings of the PlatformIO extension to ensure compatibility. 57 | 58 | Since the extension automatically downloads new PlatformIO Core versions, the setup is not "pure." Using the `--impure` flag allows the system to reuse the system's `nixpkgs` instead of downloading a specific version. 59 | 60 | **Important:** Two versions of PlatformIO will be installed: 61 | 1. A native version provided by `nixpkgs`, available regardless of whether the PlatformIO IDE extension has downloaded PlatformIO Core. 62 | 2. A version installed by the PlatformIO IDE extension (currently 1.6.17). The extension always installs the latest version, which may diverge from the native version. 63 | 64 | For consistency, this flake will prefer the PlatformIO IDE's version if available via the `PATH` variable. 65 | 66 | --- 67 | 68 | ## Using `tio` for Serial Communication 69 | 70 | `tio` is a simple serial communication tool included in this environment. It allows you to interact with microcontrollers connected via USB serial devices (e.g., `/dev/ttyUSB0` or `/dev/ttyACM0`). 71 | 72 | ### Example Usage 73 | To connect to a serial device, use: 74 | ```bash 75 | tio /dev/ttyUSB0 76 | ``` 77 | or 78 | ```bash 79 | tio /dev/ttyACM0 80 | ``` 81 | 82 | ### Configuring `tio` 83 | You can customize `tio` behavior by creating a configuration file at `~/.config/tio/config`. This will then allow you to use `tio usb0` to connect to /dev/ttyUSB0. 84 | 85 | Here’s an example configuration: 86 | 87 | ```ini 88 | [usb-devices] 89 | pattern = ^usb([0-9]*) 90 | device = /dev/ttyUSB%m1 91 | baudrate = 115200 92 | 93 | [acm-devices] 94 | pattern = ^acm([0-9]*) 95 | device = /dev/ttyACM%m1 96 | baudrate = 115200 97 | 98 | [forth] 99 | pattern = ^forth([0-9]*) 100 | device = /dev/ttyUSB%m1 101 | baudrate = 115200 102 | map = OCRNL,INLCRNL 103 | ``` 104 | 105 | This configuration: 106 | - Automatically detects USB (`/dev/ttyUSB*`) and ACM (`/dev/ttyACM*`) devices. 107 | - Sets a default baud rate of `115200`. 108 | - Maps specific serial port behaviors for devices matching the `forth` pattern. 109 | 110 | ### Permissions for Serial Devices 111 | To communicate with serial devices, ensure your user is part of the `dialout` group: 112 | ```bash 113 | sudo usermod -a -G dialout $USER 114 | ``` 115 | Log out and back in for the changes to take effect. 116 | 117 | --- 118 | ### Credits 119 | 120 | This approach builds upon but does not depend on: 121 | - [ppenguin's PR-237313](https://github.com/NixOS/nixpkgs/pull/237313). 122 | - [delan's approach](https://github.com/NixOS/nixpkgs/pull/237313#issuecomment-1848198106) documented in the [PlatformIO Nix Wiki](https://nixos.wiki/index.php?title=Platformio&oldid=10699). 123 | 124 | --- 125 | 126 | ## Troubleshooting 127 | 128 | - **Conda Conflicts**: If you have Conda in your default shell (e.g., `.bashrc`), it may interfere with this flake, resulting in missing modules or other Python errors. Temporarily disable Conda initialization or use a clean shell environment. 129 | 130 | --- 131 | 132 | ## Contributing 133 | 134 | If you encounter issues or have suggestions for improvement, feel free to open an issue or submit a pull request on the [GitHub repository](https://github.com/xdadrm/nixos_use_platformio_patformio-ide_and_vscode). 135 | 136 | --- 137 | 138 | -------------------------------------------------------------------------------- /flake.nix: -------------------------------------------------------------------------------- 1 | { 2 | description = "PlatformIO Development Environment with VSCodium"; 3 | 4 | inputs = { 5 | }; 6 | 7 | outputs = { self, nixpkgs }: 8 | let 9 | system = "x86_64-linux"; 10 | pkgs = import { inherit system; }; 11 | 12 | platformioVsix = pkgs.fetchurl { 13 | url = "https://marketplace.visualstudio.com/_apis/public/gallery/publishers/platformio/vsextensions/platformio-ide/3.3.4/vspackage?targetPlatform=linux-x64"; 14 | sha256 = "sha256-Ri5TZDxSsW1cW33Rh+l/5Fxl23MNzFEjcFGLDx/xzT8="; 15 | }; 16 | 17 | patchedExtension = pkgs.stdenv.mkDerivation { 18 | name = "platformio-ide-patched"; 19 | src = platformioVsix; 20 | buildInputs = [ pkgs.jq pkgs.unzip pkgs.gzip ]; 21 | unpackCmd = '' 22 | gzip -d < $src > temp.zip 23 | unzip temp.zip 24 | rm temp.zip 25 | ''; 26 | buildPhase = '' 27 | jq '.extensionDependencies = [] | 28 | .["platformio-ide.useBuiltinPIOCore"].default = false | 29 | .["platformio-ide.useBuiltinPython"].default = false | 30 | .["platformio-ide.forceSystemPIOCore"].default = true | 31 | .["platformio-ide.forceSystemPython"].default = true' \ 32 | package.json > package.json.new 33 | mv package.json.new package.json 34 | ''; 35 | installPhase = '' 36 | cd .. 37 | mkdir -p $out 38 | ${pkgs.zip}/bin/zip -r $out/platformio-ide.vsix . 39 | ''; 40 | }; 41 | 42 | platformioWrapper = pkgs.writeScriptBin "platformio" '' 43 | #!/bin/sh 44 | VENV_DIR="''${PLATFORMIO_VENV_DIR:-$HOME/.platformio/penv}" 45 | . "$VENV_DIR/bin/activate" 46 | exec ${pkgs.platformio}/bin/platformio "$@" 47 | ''; 48 | 49 | configureVSCodeSettings = '' 50 | USER_CONFIG_DIR="''${XDG_CONFIG_HOME:-$HOME/.config}/VSCodium/User" 51 | mkdir -p "$USER_CONFIG_DIR" 52 | SETTINGS_FILE="$USER_CONFIG_DIR/settings.json" 53 | if [ ! -f "$SETTINGS_FILE" ]; then 54 | echo '{}' > "$SETTINGS_FILE" 55 | fi 56 | ${pkgs.jq}/bin/jq '. + { 57 | "platformio-ide.useBuiltinPIOCore": true, 58 | "platformio-ide.useBuiltinPython": false, 59 | "platformio-ide.forceSystemPIOCore": false, 60 | "platformio-ide.forceSystemPython": true, 61 | "platformio-ide.customPATH": "$PATH" 62 | }' "$SETTINGS_FILE" > "$SETTINGS_FILE.tmp" 63 | if [ $? -eq 0 ]; then 64 | mv "$SETTINGS_FILE.tmp" "$SETTINGS_FILE" 65 | else 66 | rm -f "$SETTINGS_FILE.tmp" 67 | fi 68 | ''; 69 | 70 | fhsEnv = pkgs.buildFHSEnv { 71 | name = "platformio-env"; 72 | targetPkgs = pkgs: with pkgs; [ 73 | platformio 74 | platformioWrapper 75 | python312 76 | git 77 | vscodium 78 | gcc 79 | gdb 80 | gnumake 81 | udev 82 | zlib 83 | ncurses 84 | stdenv.cc.cc.lib 85 | glibc 86 | libusb1 87 | openssl 88 | tio 89 | ]; 90 | profile = '' 91 | export PYTHONPATH=${pkgs.platformio}/lib/python3.12/site-packages:$PYTHONPATH 92 | export PLATFORMIO_CORE_DIR="''${PLATFORMIO_CORE_DIR:-$HOME/.platformio}" 93 | export PATH=${platformioWrapper}/bin:$PATH 94 | export PATH=${pkgs.python312}/bin:$PATH 95 | ''; 96 | runScript = pkgs.writeScript "platformio-shell" '' 97 | export XDG_DATA_HOME="''${XDG_DATA_HOME:-$HOME/.local/share}" 98 | export PATH=${pkgs.python312}/bin:${platformioWrapper}/bin:$PATH 99 | export PYTHONPATH=${pkgs.platformio}/lib/python3.12/site-packages:$PYTHONPATH 100 | ${configureVSCodeSettings} 101 | VSCODE_PORTABLE="''${VSCODE_PORTABLE:-$HOME/.vscode-portable}" 102 | EXTENSION_DIR="''${VSCODE_PORTABLE}/extensions" 103 | mkdir -p "$EXTENSION_DIR" 104 | ${pkgs.vscodium}/bin/codium --install-extension ${patchedExtension}/platformio-ide.vsix 105 | mkdir -p $HOME/.local/bin 106 | ln -sf ${platformioWrapper}/bin/platformio $HOME/.local/bin/pio 107 | echo "PlatformIO environment ready. PlatformIO Core: $(platformio --version)" 108 | echo "Run 'codium .' to open VSCodium in current directory" 109 | if [ -f $HOME/.platformio/penv/bin/activate ]; then 110 | source $HOME/.platformio/penv/bin/activate 111 | fi 112 | PS1="Codium-PIO> " 113 | exec bash --norc 114 | ''; 115 | }; 116 | 117 | # Create a dedicated VSCodium launcher package that accepts arguments 118 | codiumLauncher = pkgs.writeScriptBin "launch-codium" '' 119 | #!/usr/bin/env bash 120 | export XDG_DATA_HOME="''${XDG_DATA_HOME:-$HOME/.local/share}" 121 | export PATH=${pkgs.python312}/bin:${platformioWrapper}/bin:$PATH 122 | export PYTHONPATH=${pkgs.platformio}/lib/python3.12/site-packages:$PYTHONPATH 123 | ${configureVSCodeSettings} 124 | VSCODE_PORTABLE="''${VSCODE_PORTABLE:-$HOME/.vscode-portable}" 125 | EXTENSION_DIR="''${VSCODE_PORTABLE}/extensions" 126 | mkdir -p "$EXTENSION_DIR" 127 | ${pkgs.vscodium}/bin/codium --install-extension ${patchedExtension}/platformio-ide.vsix 128 | mkdir -p $HOME/.local/bin 129 | ln -sf ${platformioWrapper}/bin/platformio $HOME/.local/bin/pio 130 | 131 | # Initialize PlatformIO environment if needed 132 | if [ ! -f $HOME/.platformio/penv/bin/activate ] && [ -x "$(command -v python3)" ]; then 133 | echo "Initializing PlatformIO environment..." 134 | python3 -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py)" 135 | fi 136 | 137 | if [ -f $HOME/.platformio/penv/bin/activate ]; then 138 | source $HOME/.platformio/penv/bin/activate 139 | fi 140 | 141 | # If no arguments are provided, open the current directory 142 | if [ $# -eq 0 ]; then 143 | exec ${pkgs.vscodium}/bin/codium . 144 | else 145 | # Otherwise, pass all arguments to VSCodium 146 | exec ${pkgs.vscodium}/bin/codium "$@" 147 | fi 148 | ''; 149 | 150 | # Create an FHS environment specifically for launching VSCodium 151 | codiumFhsEnv = pkgs.buildFHSEnv { 152 | name = "codium-launcher"; 153 | targetPkgs = pkgs: with pkgs; [ 154 | platformio 155 | platformioWrapper 156 | python312 157 | git 158 | vscodium 159 | gcc 160 | gdb 161 | gnumake 162 | udev 163 | zlib 164 | ncurses 165 | stdenv.cc.cc.lib 166 | glibc 167 | libusb1 168 | openssl 169 | codiumLauncher 170 | tio 171 | ]; 172 | profile = '' 173 | export PYTHONPATH=${pkgs.platformio}/lib/python3.12/site-packages:$PYTHONPATH 174 | export PLATFORMIO_CORE_DIR="''${PLATFORMIO_CORE_DIR:-$HOME/.platformio}" 175 | export PATH=${platformioWrapper}/bin:$PATH 176 | export PATH=${pkgs.python312}/bin:$PATH 177 | ''; 178 | # Pass all arguments received to the launch-codium script 179 | runScript = pkgs.writeScript "codium-launch-wrapper" '' 180 | exec ${codiumLauncher}/bin/launch-codium "$@" 181 | ''; 182 | }; 183 | 184 | in 185 | { 186 | devShells.${system} = { 187 | default = fhsEnv.env; 188 | codium = codiumFhsEnv.env; 189 | }; 190 | 191 | packages.${system} = { 192 | default = fhsEnv; 193 | platformioExtension = patchedExtension; 194 | codium = codiumFhsEnv; 195 | }; 196 | }; 197 | } 198 | --------------------------------------------------------------------------------