From 4b695b6ba8d58315417412664856cf6f2df774e0 Mon Sep 17 00:00:00 2001 From: Daniel Schaefer Date: Fri, 20 Nov 2026 10:30:47 +0000 Subject: [PATCH] Add flake.nix - `nix run` - runs inputmodule-control (--help output confirmed) - `nix build .#inputmodule-control` - host binary - `nix build .#ledmatrix / .#b1display / .#c1minimal / .#qtpy` - each produces bin/.elf and a ready-to-flash bin/.uf2 - `nix develop` - stable Rust with thumbv6m-none-eabi target, plus flip-link, cargo-make, pkg-config, elf2uf2-rs, and udev on Linux - README: new "Using the Nix flake" section documenting the above Signed-off-by: Daniel Schaefer --- README.md | 27 +++++++++++ flake.lock | 82 ++++++++++++++++++++++++++++++++++ flake.nix | 128 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 237 insertions(+) create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/README.md b/README.md index 64c168ca..f9ab2c13 100644 --- a/README.md +++ b/README.md @@ -174,6 +174,33 @@ cargo install cargo-make --locked > cargo make --cwd inputmodule-control run -- --version ``` +## Using the Nix flake + +If you have Nix with flakes enabled, you can build and run everything without +installing a toolchain yourself. + +```sh +# Drop into a dev shell with rustup, flip-link, cargo-make, elf2uf2-rs, etc. +nix develop + +# Build the host control tool (default package) +nix build + +# Run it directly +nix run . -- --list + +# Build firmware ELFs for a specific module +nix build .#ledmatrix +nix build .#b1display +nix build .#c1minimal +nix build .#qtpy +``` + +Each firmware build drops both the ELF and a ready-to-flash UF2 in +`result/bin/`, e.g. `result/bin/ledmatrix.elf` and `result/bin/ledmatrix.uf2`. +Put the module in bootloader mode and copy the `.uf2` onto the drive it +exposes. + ### Check the firmware version of the device ###### In-band using commandline diff --git a/flake.lock b/flake.lock new file mode 100644 index 00000000..3975c1e6 --- /dev/null +++ b/flake.lock @@ -0,0 +1,82 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1794427683, + "narHash": "sha256-o7ey9gEilsxaifqJWnBDlYSKbNWYQYtKwDyXeZU2kaU=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "cd489559845d81f673a12885576016c65cbe9363", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1795085501, + "narHash": "sha256-S5keGWzaKpuXo9MNoIC2FfTIuTyTnx8P4NhYJMEsTLI=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "0bb8272318b4ca41bf0edc6224df3e3e3046b70d", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1795168384, + "narHash": "sha256-aQ4Ns5O78Pg6B1i2hNRbI1HpRtnZst/jW34MSOOeaX4=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "a5c999945da3991d7aa3cc2a6075cfd22bd387e9", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1791244623, + "narHash": "sha256-uuanbkFk8a313Zfrxdcz8Us/yQtaxNnKJtX8v0ZPpd8=", + "owner": "nix-systems", + "repo": "default", + "rev": "d36eeed142f88b5a636f2e3856a53c61654cf29b", + "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 00000000..69c3417e --- /dev/null +++ b/flake.nix @@ -0,0 +1,128 @@ +{ + description = "Framework Laptop 16 Input Module firmware and control tool"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; + + outputs = { self, nixpkgs, flake-utils, rust-overlay }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { + inherit system; + overlays = [ (import rust-overlay) ]; + }; + + rustToolchain = pkgs.rust-bin.stable.latest.default.override { + targets = [ "thumbv6m-none-eabi" ]; + extensions = [ "rust-src" "rustfmt" "clippy" ]; + }; + + rustPlatform = pkgs.makeRustPlatform { + cargo = rustToolchain; + rustc = rustToolchain; + }; + + src = ./.; + + cargoLock = { + lockFile = ./Cargo.lock; + # st7306 is a git dependency; fetch it via builtins.fetchGit so we + # don't have to hand-maintain an outputHash entry. + allowBuiltinFetchGit = true; + }; + + firmwareTarget = "thumbv6m-none-eabi"; + + mkFirmware = pname: + rustPlatform.buildRustPackage { + inherit pname src cargoLock; + version = "0.2.0"; + + nativeBuildInputs = [ pkgs.flip-link pkgs.elf2uf2-rs ]; + + # buildRustPackage's cargoBuildHook hard-codes --target to the host + # triple, which breaks no_std firmware builds. Replace the build + # and install phases instead of fighting the hook. + buildPhase = '' + runHook preBuild + cargo build \ + --release \ + --frozen \ + --target ${firmwareTarget} \ + -p ${pname} + runHook postBuild + ''; + + checkPhase = '' + runHook preCheck + runHook postCheck + ''; + + installPhase = '' + runHook preInstall + mkdir -p $out/bin + cp target/${firmwareTarget}/release/${pname} $out/bin/${pname}.elf + elf2uf2-rs \ + target/${firmwareTarget}/release/${pname} \ + $out/bin/${pname}.uf2 + runHook postInstall + ''; + + meta.description = "Framework Input Module firmware (${pname})"; + }; + + inputmodule-control = rustPlatform.buildRustPackage { + pname = "inputmodule-control"; + version = "0.2.0"; + inherit src cargoLock; + + nativeBuildInputs = [ pkgs.pkg-config ]; + buildInputs = pkgs.lib.optionals pkgs.stdenv.hostPlatform.isLinux [ pkgs.udev ]; + + cargoBuildFlags = [ "-p" "inputmodule-control" ]; + # Override the workspace-default embedded target so this builds + # natively for the host. + CARGO_BUILD_TARGET = pkgs.stdenv.hostPlatform.rust.rustcTarget; + + doCheck = false; + + meta = { + description = "Host-side control tool for Framework Input Modules"; + mainProgram = "inputmodule-control"; + }; + }; + in { + packages = { + default = inputmodule-control; + inherit inputmodule-control; + ledmatrix = mkFirmware "ledmatrix"; + b1display = mkFirmware "b1display"; + c1minimal = mkFirmware "c1minimal"; + qtpy = mkFirmware "qtpy"; + }; + + apps.default = { + type = "app"; + program = "${inputmodule-control}/bin/inputmodule-control"; + }; + + devShells.default = pkgs.mkShell { + nativeBuildInputs = [ + rustToolchain + pkgs.flip-link + pkgs.cargo-make + pkgs.pkg-config + pkgs.elf2uf2-rs + ]; + buildInputs = pkgs.lib.optionals pkgs.stdenv.hostPlatform.isLinux [ pkgs.udev ]; + }; + + formatter = pkgs.nixpkgs-fmt; + }); +}