From 4d43709f76140408d3c65c0eeb9ad6fcade2f866 Mon Sep 17 00:00:00 2001 From: mwojcik Date: Wed, 19 Jan 2022 11:13:47 +0800 Subject: [PATCH] nix flakes support (#64) Reviewed-on: https://git.m-labs.hk/M-Labs/thermostat/pulls/64 Co-authored-by: mwojcik Co-committed-by: mwojcik --- README.md | 12 ++++---- cargosha256.nix | 1 - flake.lock | 44 +++++++++++++++++++++++++++ flake.nix | 80 +++++++++++++++++++++++++++++++++++++++++++++++++ shell.nix | 23 -------------- 5 files changed, 131 insertions(+), 29 deletions(-) delete mode 100644 cargosha256.nix create mode 100644 flake.lock create mode 100644 flake.nix delete mode 100644 shell.nix diff --git a/README.md b/README.md index 3d8acc5..91a0cf4 100644 --- a/README.md +++ b/README.md @@ -8,20 +8,22 @@ ### Reproducible build with Nix -See the `mcu` folder of the [nix-scripts repository](https://git.m-labs.hk/M-Labs/nix-scripts). +Thermostat firmware is packaged using the [Nix](https://nixos.org) Flakes system. Install Nix 2.4+ and enable flakes by adding ``experimental-features = nix-command flakes`` to ``nix.conf`` (e.g. ``~/.config/nix/nix.conf``). + +Once you have Flakes enabled, you can use ``nix build`` to build the firmware. ### Development environment -Clone this repository and [nix-scripts](https://git.m-labs.hk/M-Labs/nix-scripts). +Clone this repository and with Nix Flakes enabled, use the following commands: ```shell -nix-shell -I nix-scripts=[path to nix-scripts checkout] +nix develop cargo build --release ``` -The resulting ELF file will be located under `target/thumbv7em-none-eabihf/release/thermostat` +The resulting ELF file will be located under `target/thumbv7em-none-eabihf/release/thermostat`. -Alternatively, you can install the Rust toolchain without Nix using rustup; see the channel manifest file in nix-scripts (`channel-rust-nightly.toml`) to determine which Rust version to use. +Alternatively, you can install the Rust toolchain without Nix using rustup; see the Rust manifest file pulled in `flake.nix` to determine which Rust version to use. ## Debugging diff --git a/cargosha256.nix b/cargosha256.nix deleted file mode 100644 index b3d9617..0000000 --- a/cargosha256.nix +++ /dev/null @@ -1 +0,0 @@ -"0qb4s06jwgj3i9df6qq9gwcnyr3jq6dh4l5ygjghq5x1bmcqliix" diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..7fee722 --- /dev/null +++ b/flake.lock @@ -0,0 +1,44 @@ +{ + "nodes": { + "mozilla-overlay": { + "flake": false, + "locked": { + "lastModified": 1638887313, + "narHash": "sha256-FMYV6rVtvSIfthgC1sK1xugh3y7muoQcvduMdriz4ag=", + "owner": "mozilla", + "repo": "nixpkgs-mozilla", + "rev": "7c1e8b1dd6ed0043fb4ee0b12b815256b0b9de6f", + "type": "github" + }, + "original": { + "owner": "mozilla", + "repo": "nixpkgs-mozilla", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1641870998, + "narHash": "sha256-6HkxR2WZsm37VoQS7jgp6Omd71iw6t1kP8bDbaqCDuI=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "386234e2a61e1e8acf94dfa3a3d3ca19a6776efb", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-21.11", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "mozilla-overlay": "mozilla-overlay", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..2c41e1e --- /dev/null +++ b/flake.nix @@ -0,0 +1,80 @@ +{ + description = "Firmware for the Sinara 8451 Thermostat"; + + inputs.nixpkgs.url = github:NixOS/nixpkgs/nixos-21.11; + inputs.mozilla-overlay = { url = github:mozilla/nixpkgs-mozilla; flake = false; }; + + outputs = { self, nixpkgs, mozilla-overlay }: + let + pkgs = import nixpkgs { system = "x86_64-linux"; overlays = [ (import mozilla-overlay) ]; }; + rustManifest = pkgs.fetchurl { + url = "https://static.rust-lang.org/dist/2020-10-30/channel-rust-nightly.toml"; + sha256 = "0iygcwzh8s0lfdghj5809krvzifc1ii1wm4sd3qqn7s0rz1s14hi"; + }; + + targets = [ + "thumbv7em-none-eabihf" + ]; + rustChannelOfTargets = _channel: _date: targets: + (pkgs.lib.rustLib.fromManifestFile rustManifest { + inherit (pkgs) stdenv lib fetchurl patchelf; + }).rust.override { + inherit targets; + extensions = ["rust-src"]; + }; + rust = rustChannelOfTargets "nightly" null targets; + rustPlatform = pkgs.recurseIntoAttrs (pkgs.makeRustPlatform { + rustc = rust; + cargo = rust; + }); + thermostat = rustPlatform.buildRustPackage rec { + name = "thermostat"; + version = "0.0.0"; + + src = self; + cargoLock = { + lockFile = ./Cargo.lock; + outputHashes = { + "stm32-eth-0.2.0" = "sha256-HXRr/NDhdIKqyjdA4D8ZmcO1dDpDawdlYPUOwcEbPQk="; + "stm32f4xx-hal-0.8.3" = "sha256-MOv7tVtVMxr3IYMaN0Q8EQWxv3rubmCxjXMXuw/ZKAw="; + }; + }; + + nativeBuildInputs = [ pkgs.llvm ]; + + buildPhase = '' + cargo build --release --bin thermostat + ''; + + installPhase = '' + mkdir -p $out $out/nix-support + cp target/thumbv7em-none-eabihf/release/thermostat $out/thermostat.elf + echo file binary-dist $out/thermostat.elf >> $out/nix-support/hydra-build-products + llvm-objcopy -O binary target/thumbv7em-none-eabihf/release/thermostat $out/thermostat.bin + echo file binary-dist $out/thermostat.bin >> $out/nix-support/hydra-build-products + ''; + + dontFixup = true; + }; + in { + packages.x86_64-linux = { + inherit thermostat; + }; + + hydraJobs = { + inherit thermostat; + }; + + devShell.x86_64-linux = pkgs.mkShell { + name = "thermostat-dev-shell"; + buildInputs = with pkgs; [ + rustPlatform.rust.rustc + rustPlatform.rust.cargo + openocd dfu-util + ] ++ (with python3Packages; [ + numpy matplotlib + ]); + }; + defaultPackage.x86_64-linux = thermostat; + }; +} \ No newline at end of file diff --git a/shell.nix b/shell.nix deleted file mode 100644 index d325fdc..0000000 --- a/shell.nix +++ /dev/null @@ -1,23 +0,0 @@ -{ mozillaOverlay ? builtins.fetchTarball "https://github.com/mozilla/nixpkgs-mozilla/archive/master.tar.gz", - latestRustNightly ? false, -}: -let - pkgs = import { - overlays = [ (import mozillaOverlay) ]; - }; - rust = - if latestRustNightly - then pkgs.rustChannelOfTargets "nightly" null [ "thumbv7em-none-eabihf" ] - else (pkgs.recurseIntoAttrs ( - pkgs.callPackage (import ) {} - )).rust.cargo; -in -pkgs.mkShell { - name = "thermostat-env"; - buildInputs = with pkgs; [ - rust gcc - openocd dfu-util - ] ++ (with python3Packages; [ - numpy matplotlib - ]); -}