flake reorg + clippy

This commit is contained in:
Robin Appelman 2024-10-27 13:19:58 +01:00
commit 8687197051
12 changed files with 160 additions and 163 deletions

View file

@ -1,34 +1,34 @@
on: [push, pull_request]
name: Continuous integration
name: CI
jobs:
build:
matrix:
runs-on: ubuntu-latest
outputs:
check-matrix: ${{ steps.set-matrix.outputs.check-matrix }}
cross-matrix: ${{ steps.set-matrix.outputs.cross-matrix }}
steps:
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v20
- uses: icewind1991/attic-action@v1
with:
name: ci
instance: https://cache.icewind.me
authToken: '${{ secrets.ATTIC_TOKEN }}'
- run: nix build
docker:
runs-on: ubuntu-latest
needs: build
steps:
- name: Checkout code
uses: actions/checkout@v3
- uses: cachix/install-nix-action@v20
- uses: icewind1991/attic-action@v1
with:
name: ci
instance: https://cache.icewind.me
authToken: '${{ secrets.ATTIC_TOKEN }}'
- run: nix build .#docker
- name: Push image
if: github.ref == 'refs/heads/master'
- uses: actions/checkout@v4
- uses: cachix/install-nix-action@v27
- id: set-matrix
run: |
skopeo copy --dest-creds="${{ secrets.DOCKERHUB_USERNAME }}:${{ secrets.DOCKERHUB_TOKEN }}" "docker-archive:$(nix build .#docker --print-out-paths)" "docker://icewind1991/taspromto"
echo "check-matrix={\"check\":$(nix eval --json '.#checks.x86_64-linux' --apply 'builtins.attrNames')}" | tee -a $GITHUB_OUTPUT
echo "cross-matrix={\"include\":$(nix eval --json '.#lib.crossMatrix')}" | tee -a $GITHUB_OUTPUT
checks:
runs-on: ubuntu-latest
needs: [matrix]
strategy:
fail-fast: false
matrix: ${{fromJson(needs.matrix.outputs.check-matrix)}}
name: ${{ matrix.check }}
steps:
- uses: actions/checkout@v4
- uses: cachix/install-nix-action@v26
- uses: icewind1991/attic-action@v1
with:
name: ci
instance: https://cache.icewind.me
authToken: "${{ secrets.ATTIC_TOKEN }}"
- run: nix build .#checks.x86_64-linux.${{ matrix.check }}

1
Cargo.lock generated
View file

@ -1038,6 +1038,7 @@ dependencies = [
"jzon",
"pin-utils",
"rumqttc",
"serde",
"tokio",
"tokio-stream",
"warp",

View file

@ -17,6 +17,7 @@ async-stream = "0.3.5"
pin-utils = "0.1.0"
hostname = "0.4.0"
tokio-stream = "0.1.15"
serde = { version = "1.0.200", features = ["derive"] }
[profile.release]
lto = true

View file

@ -1,6 +1,6 @@
{
dockerTools,
taspromto,
{ dockerTools
, taspromto
,
}:
dockerTools.buildLayeredImage {
name = "icewind1991/taspromto";
@ -11,9 +11,9 @@ dockerTools.buildLayeredImage {
dockerTools.caCertificates
];
config = {
Cmd = ["taspromto"];
Cmd = [ "taspromto" ];
ExposedPorts = {
"80/tcp" = {};
"80/tcp" = { };
};
};
}

93
flake.lock generated
View file

@ -1,30 +1,69 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"crane": {
"locked": {
"lastModified": 1710146030,
"narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a",
"lastModified": 1727060013,
"narHash": "sha256-/fC5YlJy4IoAW9GhkJiwyzk0K/gQd9Qi4rRcoweyG9E=",
"owner": "ipetkov",
"repo": "crane",
"rev": "6b40cc876c929bfe1e3a24bf538ce3b5622646ba",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"owner": "ipetkov",
"repo": "crane",
"type": "github"
}
},
"flakelight": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1729514458,
"narHash": "sha256-e4Yf5Jo9ouojCVxTVugGfOtYk75xuA22/qrRXrxmFwI=",
"owner": "nix-community",
"repo": "flakelight",
"rev": "adffa2b3413adedae6ce5b685ca85c4a0c56f32f",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "flakelight",
"type": "github"
}
},
"mill-scale": {
"inputs": {
"crane": "crane",
"flakelight": [
"flakelight"
],
"rust-overlay": "rust-overlay"
},
"locked": {
"lastModified": 1729438560,
"narHash": "sha256-tVsGuad1QIoDJlM8aI3jJD5coG9Xgn2RYr+qgbeOzEU=",
"owner": "icewind1991",
"repo": "mill-scale",
"rev": "25737430ec43c29beb2158f1179983824161161a",
"type": "github"
},
"original": {
"owner": "icewind1991",
"repo": "mill-scale",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1720954236,
"narHash": "sha256-1mEKHp4m9brvfQ0rjCca8P1WHpymK3TOr3v34ydv9bs=",
"lastModified": 1729973466,
"narHash": "sha256-knnVBGfTCZlQgxY1SgH0vn2OyehH9ykfF8geZgS95bk=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "53e81e790209e41f0c1efa9ff26ff2fd7ab35e27",
"rev": "cd3e8833d70618c4eea8df06f95b364b016d4950",
"type": "github"
},
"original": {
@ -35,22 +74,30 @@
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"flakelight": "flakelight",
"mill-scale": "mill-scale",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"rust-overlay": {
"inputs": {
"nixpkgs": [
"mill-scale",
"flakelight",
"nixpkgs"
]
},
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"lastModified": 1727058553,
"narHash": "sha256-tY/UU3Qk5gP/J0uUM4DZ6wo4arNLGAVqLKBotILykfQ=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "edc5b0f896170f07bd39ad59d6186fcc7859bbb2",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
}

View file

@ -1,47 +1,31 @@
{
inputs = {
nixpkgs.url = "nixpkgs/nixos-24.05";
flake-utils.url = "github:numtide/flake-utils";
flakelight = {
url = "github:nix-community/flakelight";
inputs.nixpkgs.follows = "nixpkgs";
};
mill-scale = {
url = "github:icewind1991/mill-scale";
inputs.flakelight.follows = "flakelight";
};
};
outputs = { mill-scale, ... }: mill-scale ./. {
packages.tasprompto = import ./package.nix;
outputs = {
self,
nixpkgs,
flake-utils,
}:
flake-utils.lib.eachDefaultSystem (
system: let
overlays = [
(import ./overlay.nix)
];
pkgs = (import nixpkgs) {
inherit system overlays;
};
in rec {
packages = rec {
taspromto = pkgs.taspromto;
docker = pkgs.callPackage ./docker.nix {};
default = taspromto;
};
devShell = pkgs.mkShell {
nativeBuildInputs = with pkgs; [rustc cargo bacon cargo-edit cargo-outdated clippy cargo-audit];
};
}
)
// {
overlays.default = import ./overlay.nix;
nixosModules.default = {
pkgs,
config,
lib,
...
nixosModules = { outputs, ... }: {
default =
{ pkgs
, config
, lib
, ...
}: {
imports = [./module.nix];
imports = [ ./module.nix ];
config = lib.mkIf config.services.taspromto.enable {
nixpkgs.overlays = [self.overlays.default];
nixpkgs.overlays = [ outputs.overlays.default ];
services.taspromto.package = lib.mkDefault pkgs.taspromto;
};
};
};
};
}

View file

@ -1,24 +1,24 @@
{
config,
lib,
pkgs,
...
{ config
, lib
, pkgs
, ...
}:
with lib; let
cfg = config.services.taspromto;
in {
in
{
options.services.taspromto = {
enable = mkEnableOption "taspromto";
mitempNames = mkOption {
type = types.attrs;
default = {};
default = { };
description = "Names for mitemp sensors";
};
rfChannelNames = mkOption {
type = types.attrs;
default = {};
default = { };
description = "Names for 433mhz temperature sensors";
};
@ -42,7 +42,7 @@ in {
config = mkIf cfg.enable {
systemd.services."taspromto" = {
wantedBy = ["multi-user.target"];
wantedBy = [ "multi-user.target" ];
environment = {
PORT = toString cfg.port;
MITEMP_NAMES = concatStringsSep "," (map (k: k + "=" + cfg.mitempNames."${k}") (attrNames cfg.mitempNames));
@ -73,9 +73,9 @@ in {
RestrictAddressFamilies = "AF_INET AF_INET6";
RestrictRealtime = true;
ProtectProc = "noaccess";
SystemCallFilter = ["@system-service" "~@resources" "~@privileged"];
SystemCallFilter = [ "@system-service" "~@resources" "~@privileged" ];
IPAddressDeny = "any";
IPAddressAllow = ["localhost"];
IPAddressAllow = [ "localhost" ];
PrivateUsers = true;
ProcSubset = "pid";
};

View file

@ -1,3 +1,3 @@
final: prev: {
taspromto = final.callPackage ./package.nix {};
taspromto = final.callPackage ./package.nix { };
}

View file

@ -1,12 +1,13 @@
{
stdenv,
rustPlatform,
lib,
}: let
{ stdenv
, rustPlatform
, lib
,
}:
let
inherit (lib.sources) sourceByRegex;
src = sourceByRegex ./. ["Cargo.*" "(src)(/.*)?"];
src = sourceByRegex ./. [ "Cargo.*" "(src)(/.*)?" ];
in
rustPlatform.buildRustPackage rec {
rustPlatform.buildRustPackage rec {
pname = "taspromto";
version = "0.1.0";
@ -15,4 +16,4 @@ in
cargoLock = {
lockFile = ./Cargo.lock;
};
}
}

View file

@ -237,60 +237,33 @@ impl DeviceState {
if json["POWER"].is_string() && !json["POWER"].is_empty() {
self.state = Some(json["POWER"] == "ON");
}
if let Some(power) = json["ENERGY"]["Power"]
.as_number()
.and_then(|num| f32::try_from(num).ok())
{
if let Some(power) = json["ENERGY"]["Power"].as_number().map(f32::from) {
self.power_watts = Some(power);
}
if let Some(yesterday) = json["ENERGY"]["Yesterday"]
.as_number()
.and_then(|num| f32::try_from(num).ok())
{
if let Some(yesterday) = json["ENERGY"]["Yesterday"].as_number().map(f32::from) {
self.power_yesterday = Some(yesterday);
}
if let Some(today) = json["ENERGY"]["Today"]
.as_number()
.and_then(|num| f32::try_from(num).ok())
{
if let Some(today) = json["ENERGY"]["Today"].as_number().map(f32::from) {
self.power_today = Some(today);
}
if let Some(co2) = json["MHZ19B"]["CarbonDioxide"]
.as_number()
.and_then(|num| f32::try_from(num).ok())
{
if let Some(co2) = json["MHZ19B"]["CarbonDioxide"].as_number().map(f32::from) {
if co2 > 1.0 {
self.co2 = Some(co2);
}
}
if let Some(power) = json["OBIS"]["Power"]
.as_number()
.and_then(|num| f32::try_from(num).ok())
{
if let Some(power) = json["OBIS"]["Power"].as_number().map(f32::from) {
self.power_watts = Some(power);
}
if let Some(total) = json["OBIS"]["Total"]
.as_number()
.and_then(|num| f32::try_from(num).ok())
{
if let Some(total) = json["OBIS"]["Total"].as_number().map(f32::from) {
self.power_total = Some(total);
}
if let Some(total) = json["OBIS"]["Total_high"]
.as_number()
.and_then(|num| f32::try_from(num).ok())
{
if let Some(total) = json["OBIS"]["Total_high"].as_number().map(f32::from) {
self.power_total_high = Some(total);
}
if let Some(total) = json["OBIS"]["Total_low"]
.as_number()
.and_then(|num| f32::try_from(num).ok())
{
if let Some(total) = json["OBIS"]["Total_low"].as_number().map(f32::from) {
self.power_total_low = Some(total);
}
if let Some(gas) = json["OBIS"]["Gas_total"]
.as_number()
.and_then(|num| f32::try_from(num).ok())
{
if let Some(gas) = json["OBIS"]["Gas_total"].as_number().map(f32::from) {
self.gas_total = Some(gas);
}
@ -336,16 +309,10 @@ impl Default for MiTempState {
impl MiTempState {
pub fn update(&mut self, json: &JsonValue) {
self.last_seen = Instant::now();
if let Some(temperature) = json["Temperature"]
.as_number()
.and_then(|num| f32::try_from(num).ok())
{
if let Some(temperature) = json["Temperature"].as_number().map(f32::from) {
self.temperature = temperature;
}
if let Some(humidity) = json["Humidity"]
.as_number()
.and_then(|num| f32::try_from(num).ok())
{
if let Some(humidity) = json["Humidity"].as_number().map(f32::from) {
self.humidity = humidity;
}
if let Some(battery) = json["Battery"]
@ -354,10 +321,7 @@ impl MiTempState {
{
self.battery = battery;
}
if let Some(dew_point) = json["DewPoint"]
.as_number()
.and_then(|num| f32::try_from(num).ok())
{
if let Some(dew_point) = json["DewPoint"].as_number().map(f32::from) {
self.dew_point = dew_point;
}
}
@ -595,7 +559,6 @@ pub fn format_dsmr_state<W: Write>(
}
/// Stores the 6 byte address used to identify Bluetooth devices.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Hash, Eq, PartialEq, Default, Ord, PartialOrd)]
#[repr(C)]
pub struct BDAddr {

View file

@ -78,7 +78,7 @@ async fn serve(device_states: Arc<Mutex<DeviceStates>>, config: Config) {
format_mi_temp_state(&mut response, *addr, &mi_temp_names, state).unwrap()
}
for (channel, state) in state.rf_temp() {
format_rf_temp_state(&mut response, &channel, &rf_temp_names, state).unwrap()
format_rf_temp_state(&mut response, channel, &rf_temp_names, state).unwrap()
}
response
});