nix flakes support #64

Merged
sb10q merged 7 commits from mwojcik/thermostat:nixflakes into master 2022-01-19 11:13:47 +08:00
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 generated 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 ];
Outdated
Review

Remove?

Remove?
buildPhase = ''
Review

cargoBuildFlags?

cargoBuildFlags?
Review

Is there API of that function described somewhere for quick reference? Besides the little tutorial section...

Is there API of that function described somewhere for quick reference? Besides the little tutorial section...
Review

Actually, using just cargoBuildFlags (--bin thermostat as release is already included) causes the build to fail, as the default builder forces --target=x86_64-linux.

Actually, using just cargoBuildFlags (``--bin thermostat`` as release is already included) causes the build to fail, as the default builder forces ``--target=x86_64-linux``.
Review

One usually has to RTFS of nixpkgs for this kind of extended documentation :)
If there's no easy solution then overriding buildPhase is fine.

One usually has to RTFS of nixpkgs for this kind of extended documentation :) If there's no easy solution then overriding buildPhase is fine.
cargo build --release --bin thermostat
Outdated
Review

Is CARGO_HOME necessary? NAC3 doesn't use it.

Is CARGO_HOME necessary? NAC3 doesn't use it.
Outdated
Review

You can use cargoBuildFlags instead.

You can use cargoBuildFlags instead.
'';
installPhase = ''
mkdir -p $out $out/nix-support
cp target/thumbv7em-none-eabihf/release/thermostat $out/thermostat.elf
Outdated
Review

cargoTestFlags

cargoTestFlags
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
'';
Outdated
Review

I don't understand this comment?

I don't understand this comment?
dontFixup = true;
};
in {
Outdated
Review

rec not needed

rec not needed
packages.x86_64-linux = {
Outdated
Review

also here

also here
inherit thermostat;
};
hydraJobs = {
inherit thermostat;
};
devShell.x86_64-linux = pkgs.mkShell {
name = "thermostat-dev-shell";
buildInputs = with pkgs; [
rustPlatform.rust.rustc
rustPlatform.rust.cargo
Review

gcc?

gcc?
openocd dfu-util
] ++ (with python3Packages; [
Review

Did you test this? I think this doesn't work and you need python3.withPackages instead.

Did you test this? I think this doesn't work and you need ``python3.withPackages`` instead.
Review

Weirdly enough it seems to work

❯ nix develop

[spaqin@hera:~/m-labs/thermostat]$ python
Python 3.9.6 (default, Jun 28 2021, 08:57:49) 
[GCC 10.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> import matplotlib
>>> 
Weirdly enough it seems to work ```shell ❯ nix develop [spaqin@hera:~/m-labs/thermostat]$ python Python 3.9.6 (default, Jun 28 2021, 08:57:49) [GCC 10.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import numpy >>> import matplotlib >>> ```
Review

This is the lab computer and those packages are already included in the system Python. You can use nix develop --ignore-environment to isolate the system profile (see https://github.com/NixOS/nix/issues/4359).

This is the lab computer and those packages are already included in the system Python. You can use ``nix develop --ignore-environment`` to isolate the system profile (see https://github.com/NixOS/nix/issues/4359).
Review

Still available:

❯ nix develop --ignore-environment
bash-5.1$ python
Python 3.9.6 (default, Jun 28 2021, 08:57:49) 
[GCC 10.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> import numpy 
>>> 
Still available: ```shell ❯ nix develop --ignore-environment bash-5.1$ python Python 3.9.6 (default, Jun 28 2021, 08:57:49) [GCC 10.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import matplotlib >>> import numpy >>> ```
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
]);
}