nix flake worky

This commit is contained in:
2026-03-23 11:45:41 +00:00
parent 7e300a5193
commit d3d64ef8ed
6 changed files with 187 additions and 20 deletions
+7
View File
@@ -4,8 +4,15 @@ members = [
"src/daemon", "src/daemon",
"src/data", "src/data",
] ]
resolver = "2" resolver = "2"
[workspace.package]
version = "0.1.0"
[workspace.metadata.crane]
name = "battery-logger"
[workspace.dependencies] [workspace.dependencies]
# Define versions here once; crates can inherit them # Define versions here once; crates can inherit them
rkyv = "0.8.15" rkyv = "0.8.15"
Generated
+17
View File
@@ -1,5 +1,21 @@
{ {
"nodes": { "nodes": {
"advisory-db": {
"flake": false,
"locked": {
"lastModified": 1774258319,
"narHash": "sha256-OyAOz14B8f6bdstIO9C/+khNZiebgt1+PNrNKqIbw/8=",
"owner": "rustsec",
"repo": "advisory-db",
"rev": "53e3e72004e53879ceb61566004769b013cf0393",
"type": "github"
},
"original": {
"owner": "rustsec",
"repo": "advisory-db",
"type": "github"
}
},
"crane": { "crane": {
"locked": { "locked": {
"lastModified": 1772080396, "lastModified": 1772080396,
@@ -51,6 +67,7 @@
}, },
"root": { "root": {
"inputs": { "inputs": {
"advisory-db": "advisory-db",
"crane": "crane", "crane": "crane",
"flake-utils": "flake-utils", "flake-utils": "flake-utils",
"nixpkgs": "nixpkgs" "nixpkgs": "nixpkgs"
+156 -17
View File
@@ -1,5 +1,5 @@
{ {
description = "hybrid laptop core and process management"; description = "Build a cargo workspace";
inputs = { inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
@@ -7,14 +7,21 @@
crane.url = "github:ipetkov/crane"; crane.url = "github:ipetkov/crane";
flake-utils.url = "github:numtide/flake-utils"; flake-utils.url = "github:numtide/flake-utils";
advisory-db = {
url = "github:rustsec/advisory-db";
flake = false;
};
}; };
#
outputs = outputs =
{ {
self, self,
nixpkgs, nixpkgs,
crane, crane,
flake-utils, flake-utils,
advisory-db,
... ...
}: }:
flake-utils.lib.eachDefaultSystem ( flake-utils.lib.eachDefaultSystem (
@@ -22,53 +29,185 @@
let let
pkgs = nixpkgs.legacyPackages.${system}; pkgs = nixpkgs.legacyPackages.${system};
inherit (pkgs) lib;
craneLib = crane.mkLib pkgs; craneLib = crane.mkLib pkgs;
src = craneLib.cleanCargoSource ./.;
# Common arguments can be set here to avoid repeating them later # Common arguments can be set here to avoid repeating them later
# Note: changes here will rebuild all dependency crates
commonArgs = { commonArgs = {
src = craneLib.cleanCargoSource ./.; inherit src;
strictDeps = true; strictDeps = true;
buildInputs = [ buildInputs = [
# Add additional build inputs here # Add additional build inputs here
] ]
++ pkgs.lib.optionals pkgs.stdenv.isDarwin [ ++ lib.optionals pkgs.stdenv.isDarwin [
# Additional darwin specific inputs can be set here # Additional darwin specific inputs can be set here
pkgs.libiconv pkgs.libiconv
]; ];
# Additional environment variables can be set directly
# MY_CUSTOM_VAR = "some value";
}; };
my-crate = craneLib.buildPackage ( # Build *just* the cargo dependencies (of the entire workspace),
commonArgs # so we can reuse all of that work (e.g. via cachix) when running in CI
// { # It is *highly* recommended to use something like cargo-hakari to avoid
# cache misses when building individual top-level-crates
cargoArtifacts = craneLib.buildDepsOnly commonArgs; cargoArtifacts = craneLib.buildDepsOnly commonArgs;
# Additional environment variables or build phases/hooks can be set individualCrateArgs = commonArgs // {
# here *without* rebuilding all dependency crates inherit cargoArtifacts;
# MY_CUSTOM_VAR = "some value"; inherit (craneLib.crateNameFromCargoToml { inherit src; }) version;
# NB: we disable tests since we'll run them all via cargo-nextest
doCheck = false;
};
fileSetForCrate =
crate:
lib.fileset.toSource {
root = ./.;
fileset = lib.fileset.unions [
./Cargo.toml
./Cargo.lock
(craneLib.fileset.commonCargoSources crate)
(craneLib.fileset.commonCargoSources ./src/data)
(craneLib.fileset.commonCargoSources ./src/daemon)
(craneLib.fileset.commonCargoSources ./src/tui)
];
};
# Build the top-level crates of the workspace as individual derivations.
# This allows consumers to only depend on (and build) only what they need.
# Though it is possible to build the entire workspace as a single derivation,
# so this is left up to you on how to organize things
#
# Note that the cargo workspace must define `workspace.members` using wildcards,
# otherwise, omitting a crate (like we do below) will result in errors since
# cargo won't be able to find the sources for all members.
tui = craneLib.buildPackage (
individualCrateArgs
// {
pname = "tui";
cargoExtraArgs = "--bin tui";
src = fileSetForCrate ./src/tui;
}
);
daemon = craneLib.buildPackage (
individualCrateArgs
// {
pname = "daemon";
cargoExtraArgs = "--bin daemon";
src = fileSetForCrate ./src/daemon;
} }
); );
in in
{ {
checks = { checks = {
inherit my-crate; # Build the crates as part of `nix flake check` for convenience
inherit tui daemon;
# Run clippy (and deny all warnings) on the workspace source,
# again, reusing the dependency artifacts from above.
#
# Note that this is done as a separate derivation so that
# we can block the CI if there are issues here, but not
# prevent downstream consumers from building our crate by itself.
my-workspace-clippy = craneLib.cargoClippy (
commonArgs
// {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
}
);
my-workspace-doc = craneLib.cargoDoc (
commonArgs
// {
inherit cargoArtifacts;
# This can be commented out or tweaked as necessary, e.g. set to
# `--deny rustdoc::broken-intra-doc-links` to only enforce that lint
env.RUSTDOCFLAGS = "--deny warnings";
}
);
# Check formatting
my-workspace-fmt = craneLib.cargoFmt {
inherit src;
}; };
packages.default = my-crate; my-workspace-toml-fmt = craneLib.taploFmt {
src = pkgs.lib.sources.sourceFilesBySuffices src [ ".toml" ];
# taplo arguments can be further customized below as needed
# taploExtraArgs = "--config ./taplo.toml";
};
apps.default = flake-utils.lib.mkApp { # Audit dependencies
drv = my-crate; my-workspace-audit = craneLib.cargoAudit {
inherit src advisory-db;
};
# Audit licenses
my-workspace-deny = craneLib.cargoDeny {
inherit src;
};
# Run tests with cargo-nextest
# Consider setting `doCheck = false` on other crate derivations
# if you do not want the tests to run twice
my-workspace-nextest = craneLib.cargoNextest (
commonArgs
// {
inherit cargoArtifacts;
partitions = 1;
partitionType = "count";
cargoNextestPartitionsExtraArgs = "--no-tests=pass";
}
);
# Ensure that cargo-hakari is up to date
my-workspace-hakari = craneLib.mkCargoDerivation {
inherit src;
pname = "my-workspace-hakari";
cargoArtifacts = null;
doInstallCargoArtifacts = false;
buildPhaseCargoCommand = ''
cargo hakari generate --diff # workspace-hack Cargo.toml is up-to-date
cargo hakari manage-deps --dry-run # all workspace crates depend on workspace-hack
cargo hakari verify
'';
nativeBuildInputs = [
pkgs.cargo-hakari
];
};
};
packages = {
inherit tui daemon;
};
apps = {
tui = flake-utils.lib.mkApp {
drv = tui;
};
daemon = flake-utils.lib.mkApp {
drv = daemon;
};
}; };
devShells.default = craneLib.devShell { devShells.default = craneLib.devShell {
# Inherit inputs from checks. # Inherit inputs from checks.
checks = self.checks.${system}; checks = self.checks.${system};
RUST_LOG = "debug"; # Additional dev-shell environment variables can be set directly
# MY_CUSTOM_DEVELOPMENT_VAR = "something else";
# Extra inputs can be added here; cargo and rustc are provided by default.
packages = [ packages = [
pkgs.clang pkgs.cargo-hakari
pkgs.mold
]; ];
}; };
} }
+1
View File
@@ -2,6 +2,7 @@
name = "battery-daemon" name = "battery-daemon"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
workspace = "../../"
[[bin]] [[bin]]
name = "daemon" name = "daemon"
+1
View File
@@ -2,6 +2,7 @@
name = "battery-tui" name = "battery-tui"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
workspace = "../../"
[[bin]] [[bin]]
name = "tui" name = "tui"
+4 -2
View File
@@ -37,8 +37,10 @@ impl Component for Menu {
} }
} }
fn view(&self, f: &mut Frame) { fn view(&self, f: &mut Frame) {
let [text, graph] = let [text, graph] = Layout::vertical([Constraint::Length(2), Constraint::Fill(1)])
Layout::vertical([Constraint::Length(5), Constraint::Fill(1)]).areas(f.area()); .flex(ratatui::layout::Flex::Center)
.areas(f.area());
let text_raw = format!("meow - {}", self.counter); let text_raw = format!("meow - {}", self.counter);
let text_widget = Text::raw(text_raw); let text_widget = Text::raw(text_raw);
f.render_widget(text_widget, text); f.render_widget(text_widget, text);