nix flakes support (#64)

Reviewed-on: M-Labs/thermostat#64
Co-authored-by: mwojcik <mw@m-labs.hk>
Co-committed-by: mwojcik <mw@m-labs.hk>
This commit is contained in:
mwojcik 2022-01-19 11:13:47 +08:00 committed by sb10q
parent 1c00e732fa
commit 4d43709f76
5 changed files with 131 additions and 29 deletions

View File

@ -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

View File

@ -1 +0,0 @@
"0qb4s06jwgj3i9df6qq9gwcnyr3jq6dh4l5ygjghq5x1bmcqliix"

44
flake.lock Normal file
View File

@ -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
}

80
flake.nix Normal file
View File

@ -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;
};
}

View File

@ -1,23 +0,0 @@
{ mozillaOverlay ? builtins.fetchTarball "https://github.com/mozilla/nixpkgs-mozilla/archive/master.tar.gz",
latestRustNightly ? false,
}:
let
pkgs = import <nixpkgs> {
overlays = [ (import mozillaOverlay) ];
};
rust =
if latestRustNightly
then pkgs.rustChannelOfTargets "nightly" null [ "thumbv7em-none-eabihf" ]
else (pkgs.recurseIntoAttrs (
pkgs.callPackage (import <nix-scripts/mcu/rustPlatform.nix>) {}
)).rust.cargo;
in
pkgs.mkShell {
name = "thermostat-env";
buildInputs = with pkgs; [
rust gcc
openocd dfu-util
] ++ (with python3Packages; [
numpy matplotlib
]);
}