From 95397572cd6fb822921bafa1b27a3d56d1b85e08 Mon Sep 17 00:00:00 2001 From: Miles Breslin Date: Sat, 22 Aug 2026 23:02:15 -0700 Subject: [PATCH 1/3] flake.nix: Init Adds a nix flake for building bldc firmware. This has been ported from the vesc_tool nix package for bldc. --- .gitignore | 2 ++ flake.lock | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 39 ++++++++++++++++++++++++++++++++ pkgs/bldc.nix | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 164 insertions(+) create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 pkgs/bldc.nix diff --git a/.gitignore b/.gitignore index 8937f9551f..15c427a2fd 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,5 @@ workspace.code-workspace .cache compile_commands.json + +/result diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000000..2e95b1df7e --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1787204541, + "narHash": "sha256-OURZPknrTjQrlNyxPdqzyqmU/81Wes1CUP/Ft1Rv/YI=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "5880666fd9eb563038431edb35c2d0aa595884e6", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-26.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000000..af262812fb --- /dev/null +++ b/flake.nix @@ -0,0 +1,39 @@ +{ + description = "Packages VESC firmware into a flake."; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-26.05"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = + { + self, + nixpkgs, + flake-utils, + }: + flake-utils.lib.eachDefaultSystem ( + system: + let + pkgs = import nixpkgs { + inherit system; + }; + bldc-fw = pkgs.callPackage ./pkgs/bldc.nix { + src = self; + }; + in + { + packages = { + inherit bldc-fw; + default = bldc-fw; + }; + } + ) + // { + overlays.default = final: _prev: { + bldc-fw = final.callPackage ./pkgs/bldc.nix { + src = self; + }; + }; + }; +} diff --git a/pkgs/bldc.nix b/pkgs/bldc.nix new file mode 100644 index 0000000000..2a1912be84 --- /dev/null +++ b/pkgs/bldc.nix @@ -0,0 +1,62 @@ +# Builds res_fw.qrc for the specified firmware versions. +{ + src, + # List of bldc board names to be built, i.e. a list of bldc "fw_*" Makefile + # targets without the "fw_" prefix. + # Can also be the string "all", which builds all standard board firmwares. + fwBoards ? [ ], + + gcc-arm-embedded-14, + git, + python3, + stdenv, +}: + +let + fwTargets = + if fwBoards == "all" then + [ "all_fw" ] + # VESC Tool doesn't build if the provided res_fw.qrc file is empty for some + # reason. Therefore always include "general purpose" firmware. + else + builtins.map (board: "fw_${board}") (fwBoards ++ [ "gp" ]); +in +stdenv.mkDerivation rec { + pname = "bldc-fw"; + version = src.shortRev or src.dirtyShortRev or "unknown"; + + inherit src; + + dontPatch = true; + dontFixup = true; + + buildPhase = '' + # Initialize dummy git repo to make package_firmware.py happy. + # It doesn't actually use the hash for anything but just queries it for + # whatever reason so it doesn't matter if it's not correct. (: + git init . + git config user.email "nixbld@localhost" + git config user.name "nixbld" + git commit --allow-empty -m "dummy" + + ${ + if builtins.length fwTargets != 0 then + "make -j $NIX_BUILD_CORES ${builtins.concatStringsSep " " fwTargets}" + else + "" + } + + python package_firmware.py + ''; + installPhase = '' + mkdir -p $out + + cp -r ./package/* $out/ + ''; + + nativeBuildInputs = [ + gcc-arm-embedded-14 + python3 + git + ]; +} From 3100fe48c632637c5372fa7e997e22bc266deace Mon Sep 17 00:00:00 2001 From: Miles Breslin Date: Sat, 22 Aug 2026 23:16:16 -0700 Subject: [PATCH 2/3] package_firmware.py: Remove git SHA logic This git_hash variable is unused. Remove it. This removes a dependency on git, allowing removal of a hack in bldc.nix. --- package_firmware.py | 7 ------- pkgs/bldc.nix | 10 ++-------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/package_firmware.py b/package_firmware.py index 8480b44b20..4346068613 100755 --- a/package_firmware.py +++ b/package_firmware.py @@ -3,17 +3,10 @@ import subprocess import sys -# https://stackoverflow.com/questions/14989858/get-the-current-git-hash-in-a-python-script -def get_git_revision_short_hash() -> str: - return subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('ascii').strip() - # Get the origin and destination directories build_dir = os.path.dirname(os.path.abspath(__file__)) + '/build' package_dir = os.path.dirname(os.path.abspath(__file__)) + '/package' -# Get the short git hash -git_hash = get_git_revision_short_hash() - # Define default destination filenames no_limits_name = "VESC_default_no_hw_limits.bin" default_name = "VESC_default.bin" diff --git a/pkgs/bldc.nix b/pkgs/bldc.nix index 2a1912be84..aaff1a9fcb 100644 --- a/pkgs/bldc.nix +++ b/pkgs/bldc.nix @@ -25,20 +25,14 @@ stdenv.mkDerivation rec { pname = "bldc-fw"; version = src.shortRev or src.dirtyShortRev or "unknown"; + BLDC_FW_SHORT_SHA = src.shortRev or "unknown"; + inherit src; dontPatch = true; dontFixup = true; buildPhase = '' - # Initialize dummy git repo to make package_firmware.py happy. - # It doesn't actually use the hash for anything but just queries it for - # whatever reason so it doesn't matter if it's not correct. (: - git init . - git config user.email "nixbld@localhost" - git config user.name "nixbld" - git commit --allow-empty -m "dummy" - ${ if builtins.length fwTargets != 0 then "make -j $NIX_BUILD_CORES ${builtins.concatStringsSep " " fwTargets}" From b2ddf7b02d7028da43e54e92369b825f9b1c52d3 Mon Sep 17 00:00:00 2001 From: Miles Breslin Date: Sat, 22 Aug 2026 23:29:28 -0700 Subject: [PATCH 3/3] README: add Nix quick start Document the default flake build and point new users to the packaged firmware output. --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 2b1b410360..47d0d01cc4 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,18 @@ Open up a terminal 3. `make` <-- Pick out the name of your target device from the supported boards list. For instance, I have a Trampa **VESC 100/250**, so my target is `100_250` 4. `make 100_250` <-- This will build the **VESC 100/250** firmware and place it into the `bldc/builds/100_250/` directory +## Building with Nix + +Nix is a build tool which manages all dependencies. With [Nix flakes](https://nixos.wiki/wiki/Flakes) +enabled, build the general-purpose firmware package from the repository root. + +```bash +nix build .#bldc-fw +``` + +The packaged firmware and `res_fw.qrc` are available under `result/`. +All boards are built by default. `make *_flash` helpers are not supported with nix. + ## Other tools **Linux Optional - Add udev rules to use the stlink v2 programmer without being root**