nix flakes support #64

Merged
sb10q merged 7 commits from mwojcik/thermostat:nixflakes into master 2022-01-19 11:13:47 +08:00
4 changed files with 125 additions and 24 deletions
Showing only changes of commit 22cc53d854 - Show all commits

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
}

81
flake.nix Normal file
View File

@ -0,0 +1,81 @@
{
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;
});
cargoSha256 = "0qb4s06jwgj3i9df6qq9gwcnyr3jq6dh4l5ygjghq5x1bmcqliix";
buildStm32Firmware = { name, src, cargoDepsName ? name, patchPhase ? "", extraNativeBuildInputs ? [], checkPhase ? "", doCheck ? true, binaryName ? name, extraCargoBuildArgs ? "" }:
rustPlatform.buildRustPackage rec {
inherit name cargoDepsName;
version = "0.0.0";
inherit src;
inherit cargoSha256;
inherit patchPhase;
nativeBuildInputs = [ pkgs.llvm ] ++ extraNativeBuildInputs;
buildPhase = ''
export CARGO_HOME=$(mktemp -d cargo-home.XXX)
cargo build --release --bin ${binaryName} ${extraCargoBuildArgs}
Outdated
Review

Remove?

Remove?
'';
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.
inherit checkPhase doCheck;
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.
# binaryName defaults to the `name` arg (i.e. the Rust package name);
# it is used as the Cargo binary filename
installPhase = ''
mkdir -p $out $out/nix-support
cp target/thumbv7em-none-eabihf/release/${binaryName} $out/${name}.elf
Outdated
Review

cargoTestFlags

cargoTestFlags
echo file binary-dist $out/${name}.elf >> $out/nix-support/hydra-build-products
llvm-objcopy -O binary target/thumbv7em-none-eabihf/release/${binaryName} $out/${name}.bin
echo file binary-dist $out/${name}.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 = rec {
Outdated
Review

also here

also here
thermostat = buildStm32Firmware {
name = "thermostat";
src = self;
checkPhase = ''
cargo test --target=${pkgs.rust.toRustTarget pkgs.stdenv.targetPlatform};
'';
};
};
devShell.x86_64-linux = pkgs.mkShell {
name = "thermostat-dev-shell";
buildInputs = with pkgs; [
rustPlatform.rust.rustc
Review

gcc?

gcc?
rustPlatform.rust.cargo
gcc openocd dfu-util
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 >>> ```
] ++ (with python3Packages; [
numpy matplotlib
]);
};
defaultPackage.x86_64-linux = pkgs.python3.withPackages(ps: [ ]);
};
}

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
]);
}