Files

4.1 KiB

arduino-sketch-builder-nix

A flake that allows building Arduino sketches with nix.

This project is based on arduino-nix.

Example

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    arduino-sketch-builder-nix.url = "git+https://git.clerie.de/clerie/arduino-sketch-builder-nix";
    arduino-index = {
      url = "github:bouk/arduino-indexes";
      flake = false;
    };
    arduino-esp32 = {
      url = "github:espressif/arduino-esp32/gh-pages";
      flake = false;
    };
  };

  outputs = {
    self,
    nixpkgs,
    arduino-sketch-builder-nix,
    arduino-index,
    arduino-esp32,
    ...
  }@attrs:
  let
    overlays = [
      (arduino-sketch-builder-nix.overlay)
      (arduino-sketch-builder-nix.mkArduinoPackageOverlay (arduino-index + "/index/package_index.json"))
      (arduino-sketch-builder-nix.mkArduinoPackageOverlay (arduino-index + "/index/package_rp2040_index.json"))
      (arduino-sketch-builder-nix.mkArduinoLibraryOverlay (arduino-index + "/index/library_index.json"))
      (arduino-sketch-builder-nix.mkArduinoPackageOverlay (arduino-esp32 + "/package_esp32_index.json"))
    ];
  in
  (flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = (import nixpkgs) {
          inherit system overlays;
        };

        # arduinoEnv provides bin/arduino-cli and some useful helpers functions
        arduinoEnv = pkgs.makeArduinoEnv {
          libraries = with pkgs.arduinoLibraries; [
            (arduino-sketch-builder-nix.latestVersion ADS1X15)
            (arduino-sketch-builder-nix.latestVersion Ethernet_Generic)
            (arduino-sketch-builder-nix.latestVersion SCL3300)
            (arduino-sketch-builder-nix.latestVersion TMCStepper)
            (arduino-sketch-builder-nix.latestVersion pkgs.arduinoLibraries."Adafruit PWM Servo Driver Library")
          ];

          packages = with pkgs.arduinoPackages; [
            platforms.arduino.avr."1.6.23"
            platforms.rp2040.rp2040."2.3.3"
          ];
        };

        arduinoEnvESP32 = pkgs.makeArduinoEnv {
          packages = with pkgs.arduinoPackages; [
            platforms.esp32.esp32."2.0.14"
          ];

          # ESP32 tools require python with some custom dependencies
          # we can provide them via runtimeInputs
          runtimeInputs = with pkgs; [
            (python3.withPackages(ps: with ps; [ pyserial ]))
          ];
        };
      in rec {
        packages = {
          inherit arduinoEnv;

          my-rp2020-project = arduinoEnv.buildArduinoSketch {
            name = "my-rp2040-project";
            src = ./. + "/my-rp2040-project";
            fqbn = "arduino:mbed_rp2040:pico";
          };

          my-esp32-poe-iso-project = arduinoEnvESP32.buildArduinoSketch {
            name = "my-esp32-poe-iso-project";
            src = ./. + "/my-esp32-poe-iso-project";
            fqbn = "esp32:esp32:esp32-poe-iso";
          };
        };

        hydraJobs = {
          # Build binary tarball in CI
          my-esp32-poe-iso-project = my-esp32-poe-iso-project.binaryTarball;
        };
      }
    ));
}

Package and library index

You need to provide a package index and library index. You can use arduino-indexes as a collection of common indexes. Accessing them via this git repo allows Nix Flakes to pin them as an input.

Alternatively, you can just download and check in the official Arduino indexes:

Interactive arduino-cli usage:

nix shell .#arduinoEnv

This adds a wrapped version of arduino-cli to your path that reflects you Arduino environment.

Build Arduino Sketch

nix build .#my-rp2040-project

Now you have result in your current working directory containing the compile outputs.

Upload to board

nix run .#my-rp2040-project.uploadArduinoSketch -- -p /dev/ttyUSB0

This with upload the sketch via serial on /dev/ttyUSB0. You can append any additional options from arduino-cli upload after --. It uses the same fqbn as specified in buildArduinoSketch.