From cfda626614853f7227a362ced715956ac12e1982 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Fri, 5 Dec 2025 16:51:15 -0500 Subject: [PATCH 1/2] Format, and keep formatted --- dev/flake-module.nix | 2 +- flake.lock | 23 +++++++++- flake.nix | 26 ++++++++---- nci.nix | 2 - nix-bindings-expr/src/eval_state.rs | 11 ++--- nix-bindings-flake/src/lib.rs | 5 ++- nix-bindings-store/src/store.rs | 66 +++++++++++++++++++---------- 7 files changed, 94 insertions(+), 41 deletions(-) diff --git a/dev/flake-module.nix b/dev/flake-module.nix index d19b37b..374fba3 100644 --- a/dev/flake-module.nix +++ b/dev/flake-module.nix @@ -90,7 +90,7 @@ }; }; herculesCI = - { config, ... }: + { ... }: { ciSystems = [ "x86_64-linux" ]; }; diff --git a/flake.lock b/flake.lock index 602976c..0d023db 100644 --- a/flake.lock +++ b/flake.lock @@ -344,7 +344,8 @@ "flake-parts": "flake-parts", "nix": "nix", "nix-cargo-integration": "nix-cargo-integration", - "nixpkgs": "nixpkgs" + "nixpkgs": "nixpkgs", + "treefmt-nix": "treefmt-nix" } }, "rust-overlay": { @@ -411,6 +412,26 @@ "repo": "treefmt-nix", "type": "github" } + }, + "treefmt-nix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1762938485, + "narHash": "sha256-AlEObg0syDl+Spi4LsZIBrjw+snSVU4T8MOeuZJUJjM=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "5b4ee75aeefd1e2d5a1cc43cf6ba65eba75e83e4", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index 7414299..b33e9ce 100644 --- a/flake.nix +++ b/flake.nix @@ -8,14 +8,15 @@ nix-cargo-integration.url = "github:yusdacra/nix-cargo-integration"; nix-cargo-integration.inputs.nixpkgs.follows = "nixpkgs"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + treefmt-nix.url = "github:numtide/treefmt-nix"; + treefmt-nix.inputs.nixpkgs.follows = "nixpkgs"; }; outputs = - inputs@{ self, flake-parts, ... }: + inputs@{ flake-parts, ... }: flake-parts.lib.mkFlake { inherit inputs; } ( toplevel@{ lib, - withSystem, ... }: let @@ -24,9 +25,7 @@ */ flake-parts-modules.basic = { - config, flake-parts-lib, - withSystem, ... }: { @@ -114,7 +113,7 @@ */ flake-parts-modules.tested = # Consumer toplevel - { options, config, ... }: + { config, ... }: { _file = ./flake.nix; imports = [ flake-parts-modules.basic ]; @@ -165,6 +164,7 @@ inputs.nix-cargo-integration.flakeModule inputs.flake-parts.flakeModules.partitions inputs.flake-parts.flakeModules.modules + inputs.treefmt-nix.flakeModule # dogfood flake-parts-modules.tested ./nci.nix @@ -177,14 +177,24 @@ ]; perSystem = { - config, - self', inputs', - pkgs, ... }: { packages.nix = inputs'.nix.packages.nix; + + treefmt = { + # Used to find the project root + projectRootFile = "flake.lock"; + + programs.rustfmt = { + enable = true; + edition = "2021"; + }; + programs.nixfmt.enable = true; + programs.deadnix.enable = true; + #programs.clang-format.enable = true; + }; }; partitionedAttrs.devShells = "dev"; diff --git a/nci.nix b/nci.nix index f0b4b50..053a832 100644 --- a/nci.nix +++ b/nci.nix @@ -1,9 +1,7 @@ { perSystem = { - lib, config, - pkgs, ... }: { diff --git a/nix-bindings-expr/src/eval_state.rs b/nix-bindings-expr/src/eval_state.rs index 0cdde8c..5f92b92 100644 --- a/nix-bindings-expr/src/eval_state.rs +++ b/nix-bindings-expr/src/eval_state.rs @@ -138,7 +138,9 @@ use nix_bindings_bindgen_raw as raw; use nix_bindings_store::path::StorePath; use nix_bindings_store::store::{Store, StoreWeak}; use nix_bindings_util::context::Context; -use nix_bindings_util::string_return::{callback_get_result_string, callback_get_result_string_data}; +use nix_bindings_util::string_return::{ + callback_get_result_string, callback_get_result_string_data, +}; use nix_bindings_util::{check_call, check_call_opt_key, result_string_init}; use std::ffi::{c_char, CString}; use std::iter::FromIterator; @@ -1217,7 +1219,8 @@ pub fn test_init() { // which causes an error. So we set a custom build dir here. // Only available on linux if cfg!(target_os = "linux") { - nix_bindings_util::settings::set("sandbox-build-dir", "/custom-build-dir-for-test").unwrap(); + nix_bindings_util::settings::set("sandbox-build-dir", "/custom-build-dir-for-test") + .unwrap(); } std::env::set_var("_NIX_TEST_NO_SANDBOX", "1"); @@ -1784,9 +1787,7 @@ mod tests { gc_registering_current_thread(|| { let store = Store::open(None, HashMap::new()).unwrap(); let mut es = EvalState::new(store, []).unwrap(); - let v = es - .eval_from_string("[ ]", "") - .unwrap(); + let v = es.eval_from_string("[ ]", "").unwrap(); es.force(&v).unwrap(); let t = es.value_type_unforced(&v); assert!(t == Some(ValueType::List)); diff --git a/nix-bindings-flake/src/lib.rs b/nix-bindings-flake/src/lib.rs index 96d5852..91b4bb2 100644 --- a/nix-bindings-flake/src/lib.rs +++ b/nix-bindings-flake/src/lib.rs @@ -45,7 +45,10 @@ impl FlakeSettings { pub trait EvalStateBuilderExt { /// Configures the eval state to provide flakes features such as `builtins.getFlake`. - fn flakes(self, settings: &FlakeSettings) -> Result; + fn flakes( + self, + settings: &FlakeSettings, + ) -> Result; } impl EvalStateBuilderExt for nix_bindings_expr::eval_state::EvalStateBuilder { /// Configures the eval state to provide flakes features such as `builtins.getFlake`. diff --git a/nix-bindings-store/src/store.rs b/nix-bindings-store/src/store.rs index 5e0885f..bf6e54a 100644 --- a/nix-bindings-store/src/store.rs +++ b/nix-bindings-store/src/store.rs @@ -2,11 +2,13 @@ use anyhow::{bail, Error, Result}; use lazy_static::lazy_static; use nix_bindings_bindgen_raw as raw; use nix_bindings_util::context::Context; -use nix_bindings_util::string_return::{callback_get_result_string, callback_get_result_string_data}; +use nix_bindings_util::string_return::{ + callback_get_result_string, callback_get_result_string_data, +}; use nix_bindings_util::{check_call, result_string_init}; -use std::collections::HashMap; #[cfg(nix_at_least = "2.33")] use std::collections::BTreeMap; +use std::collections::HashMap; use std::ffi::{c_char, CString}; use std::ptr::null_mut; use std::ptr::NonNull; @@ -309,8 +311,8 @@ impl Store { self.inner.ptr(), drv.inner.as_ptr() ))?; - let path = NonNull::new(path) - .ok_or_else(|| Error::msg("add_derivation returned null"))?; + let path = + NonNull::new(path).ok_or_else(|| Error::msg("add_derivation returned null"))?; Ok(StorePath::new_raw(path)) } } @@ -333,7 +335,8 @@ impl Store { #[doc(alias = "nix_store_realise")] pub fn realise(&mut self, path: &StorePath) -> Result> { let mut outputs = BTreeMap::new(); - let userdata = &mut outputs as *mut BTreeMap as *mut std::os::raw::c_void; + let userdata = + &mut outputs as *mut BTreeMap as *mut std::os::raw::c_void; unsafe extern "C" fn callback( userdata: *mut std::os::raw::c_void, @@ -428,8 +431,8 @@ impl Clone for Store { #[cfg(test)] mod tests { - use std::collections::HashMap; use ctor::ctor; + use std::collections::HashMap; use super::*; @@ -446,7 +449,8 @@ mod tests { // Set custom build dir for sandbox if cfg!(target_os = "linux") { - nix_bindings_util::settings::set("sandbox-build-dir", "/custom-build-dir-for-test").ok(); + nix_bindings_util::settings::set("sandbox-build-dir", "/custom-build-dir-for-test") + .ok(); } std::env::set_var("_NIX_TEST_NO_SANDBOX", "1"); @@ -658,9 +662,8 @@ mod tests { #[cfg(nix_at_least = "2.33")] fn create_multi_output_derivation_json() -> String { - let system = current_system().unwrap_or_else(|_| { - format!("{}-{}", std::env::consts::ARCH, std::env::consts::OS) - }); + let system = current_system() + .unwrap_or_else(|_| format!("{}-{}", std::env::consts::ARCH, std::env::consts::OS)); format!( r#"{{ @@ -716,7 +719,9 @@ mod tests { // Verify outputs are complete (BTreeMap guarantees ordering) let output_names: Vec<&String> = outputs.keys().collect(); - let expected_order = vec!["outa", "outb", "outc", "outd", "oute", "outf", "outg", "outh", "outi", "outj"]; + let expected_order = vec![ + "outa", "outb", "outc", "outd", "oute", "outf", "outg", "outh", "outi", "outj", + ]; assert_eq!(output_names, expected_order); drop(store); @@ -779,9 +784,8 @@ mod tests { fn realise_builder_fails() { let (mut store, temp_dir) = create_temp_store(); - let system = current_system().unwrap_or_else(|_| { - format!("{}-{}", std::env::consts::ARCH, std::env::consts::OS) - }); + let system = current_system() + .unwrap_or_else(|_| format!("{}-{}", std::env::consts::ARCH, std::env::consts::OS)); // Create a derivation where the builder exits with error let drv_json = format!( @@ -833,9 +837,8 @@ mod tests { fn realise_builder_no_output() { let (mut store, temp_dir) = create_temp_store(); - let system = current_system().unwrap_or_else(|_| { - format!("{}-{}", std::env::consts::ARCH, std::env::consts::OS) - }); + let system = current_system() + .unwrap_or_else(|_| format!("{}-{}", std::env::consts::ARCH, std::env::consts::OS)); // Create a derivation where the builder succeeds but produces no output let drv_json = format!( @@ -899,11 +902,17 @@ mod tests { let closure = store.get_fs_closure(&drv_path, false, true, false).unwrap(); // The closure should contain at least the derivation and its output - assert!(closure.len() >= 2, "Closure should contain at least drv and output"); + assert!( + closure.len() >= 2, + "Closure should contain at least drv and output" + ); // Verify the output path is in the closure let out_in_closure = closure.iter().any(|p| p.name().unwrap() == out_path_name); - assert!(out_in_closure, "Output path should be in closure when include_outputs=true"); + assert!( + out_in_closure, + "Output path should be in closure when include_outputs=true" + ); drop(store); drop(temp_dir); @@ -923,11 +932,16 @@ mod tests { let out_path_name = out_path.name().unwrap(); // Get closure with include_outputs=false - let closure = store.get_fs_closure(&drv_path, false, false, false).unwrap(); + let closure = store + .get_fs_closure(&drv_path, false, false, false) + .unwrap(); // Verify the output path is NOT in the closure let out_in_closure = closure.iter().any(|p| p.name().unwrap() == out_path_name); - assert!(!out_in_closure, "Output path should not be in closure when include_outputs=false"); + assert!( + !out_in_closure, + "Output path should not be in closure when include_outputs=false" + ); drop(store); drop(temp_dir); @@ -951,7 +965,10 @@ mod tests { // Verify the output path is NOT in the closure when direction is flipped let out_in_closure = closure.iter().any(|p| p.name().unwrap() == out_path_name); - assert!(!out_in_closure, "Output path should not be in closure when flip_direction=true"); + assert!( + !out_in_closure, + "Output path should not be in closure when flip_direction=true" + ); drop(store); drop(temp_dir); @@ -975,7 +992,10 @@ mod tests { // Verify the derivation path is in the closure let drv_in_closure = closure.iter().any(|p| p.name().unwrap() == drv_path_name); - assert!(drv_in_closure, "Derivation should be in closure when include_derivers=true"); + assert!( + drv_in_closure, + "Derivation should be in closure when include_derivers=true" + ); drop(store); drop(temp_dir); From 4a1889fb0cf9d559793ec3339bcd2b8add888413 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sat, 6 Dec 2025 12:04:26 +0100 Subject: [PATCH 2/2] maint: Move treefmt to dev partition Moves treefmt-nix input and configuration from the main flake to the dev partition, since it's a development-only dependency. Also switches pre-commit hooks from individual formatter hooks to the unified treefmt hook for better consistency. --- dev/flake-module.nix | 16 +++++++++- dev/flake.lock | 72 ++++---------------------------------------- dev/flake.nix | 2 ++ flake.lock | 23 +------------- flake.nix | 16 ---------- 5 files changed, 24 insertions(+), 105 deletions(-) diff --git a/dev/flake-module.nix b/dev/flake-module.nix index 374fba3..504a523 100644 --- a/dev/flake-module.nix +++ b/dev/flake-module.nix @@ -6,6 +6,7 @@ imports = [ inputs.pre-commit-hooks-nix.flakeModule inputs.hercules-ci-effects.flakeModule + inputs.treefmt-nix.flakeModule ]; perSystem = { @@ -17,7 +18,20 @@ { nix-bindings-rust.nixPackage = inputs'.nix.packages.default; - pre-commit.settings.hooks.nixfmt-rfc-style.enable = true; + treefmt = { + # Used to find the project root + projectRootFile = "flake.lock"; + + programs.rustfmt = { + enable = true; + edition = "2021"; + }; + programs.nixfmt.enable = true; + programs.deadnix.enable = true; + #programs.clang-format.enable = true; + }; + + pre-commit.settings.hooks.treefmt.enable = true; # Temporarily disable rustfmt due to configuration issues # pre-commit.settings.hooks.rustfmt.enable = true; pre-commit.settings.settings.rust.cargoManifestPath = "./Cargo.toml"; diff --git a/dev/flake.lock b/dev/flake.lock index d9508ff..c988f3b 100644 --- a/dev/flake.lock +++ b/dev/flake.lock @@ -36,27 +36,6 @@ "type": "indirect" } }, - "flake-parts_2": { - "inputs": { - "nixpkgs-lib": [ - "nix-unit", - "nixpkgs" - ] - }, - "locked": { - "lastModified": 1754487366, - "narHash": "sha256-pHYj8gUBapuUzKV/kN/tR3Zvqc7o6gdFB9XKXIp1SQ8=", - "owner": "hercules-ci", - "repo": "flake-parts", - "rev": "af66ad14b28a127c5c0f3bbb298218fc63528a18", - "type": "github" - }, - "original": { - "owner": "hercules-ci", - "repo": "flake-parts", - "type": "github" - } - }, "gitignore": { "inputs": { "nixpkgs": [ @@ -97,26 +76,6 @@ "type": "github" } }, - "nix-unit": { - "inputs": { - "flake-parts": "flake-parts_2", - "nixpkgs": "nixpkgs_2", - "treefmt-nix": "treefmt-nix" - }, - "locked": { - "lastModified": 1754985033, - "narHash": "sha256-OLaoMtlNL5AvYuaQ5P4p7riy7M9J8XpFdWKpD5RzQfU=", - "owner": "nix-community", - "repo": "nix-unit", - "rev": "388045c7bee5a2617cca8e94444bea1862df19ac", - "type": "github" - }, - "original": { - "owner": "nix-community", - "repo": "nix-unit", - "type": "github" - } - }, "nixpkgs": { "locked": { "lastModified": 1755027561, @@ -133,22 +92,6 @@ "type": "github" } }, - "nixpkgs_2": { - "locked": { - "lastModified": 1754800730, - "narHash": "sha256-HfVZCXic9XLBgybP0318ym3cDnGwBs/+H5MgxFVYF4I=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "641d909c4a7538f1539da9240dedb1755c907e40", - "type": "github" - }, - "original": { - "owner": "NixOS", - "ref": "nixpkgs-unstable", - "repo": "nixpkgs", - "type": "github" - } - }, "pre-commit-hooks-nix": { "inputs": { "flake-compat": "flake-compat", @@ -172,23 +115,20 @@ "root": { "inputs": { "hercules-ci-effects": "hercules-ci-effects", - "nix-unit": "nix-unit", - "pre-commit-hooks-nix": "pre-commit-hooks-nix" + "pre-commit-hooks-nix": "pre-commit-hooks-nix", + "treefmt-nix": "treefmt-nix" } }, "treefmt-nix": { "inputs": { - "nixpkgs": [ - "nix-unit", - "nixpkgs" - ] + "nixpkgs": [] }, "locked": { - "lastModified": 1754847726, - "narHash": "sha256-2vX8QjO5lRsDbNYvN9hVHXLU6oMl+V/PsmIiJREG4rE=", + "lastModified": 1762938485, + "narHash": "sha256-AlEObg0syDl+Spi4LsZIBrjw+snSVU4T8MOeuZJUJjM=", "owner": "numtide", "repo": "treefmt-nix", - "rev": "7d81f6fb2e19bf84f1c65135d1060d829fae2408", + "rev": "5b4ee75aeefd1e2d5a1cc43cf6ba65eba75e83e4", "type": "github" }, "original": { diff --git a/dev/flake.nix b/dev/flake.nix index 062e27a..09a06a1 100644 --- a/dev/flake.nix +++ b/dev/flake.nix @@ -4,6 +4,8 @@ pre-commit-hooks-nix.url = "github:cachix/pre-commit-hooks.nix"; pre-commit-hooks-nix.inputs.nixpkgs.follows = ""; hercules-ci-effects.url = "github:hercules-ci/hercules-ci-effects"; + treefmt-nix.url = "github:numtide/treefmt-nix"; + treefmt-nix.inputs.nixpkgs.follows = ""; }; outputs = { ... }: { }; } diff --git a/flake.lock b/flake.lock index 0d023db..602976c 100644 --- a/flake.lock +++ b/flake.lock @@ -344,8 +344,7 @@ "flake-parts": "flake-parts", "nix": "nix", "nix-cargo-integration": "nix-cargo-integration", - "nixpkgs": "nixpkgs", - "treefmt-nix": "treefmt-nix" + "nixpkgs": "nixpkgs" } }, "rust-overlay": { @@ -412,26 +411,6 @@ "repo": "treefmt-nix", "type": "github" } - }, - "treefmt-nix": { - "inputs": { - "nixpkgs": [ - "nixpkgs" - ] - }, - "locked": { - "lastModified": 1762938485, - "narHash": "sha256-AlEObg0syDl+Spi4LsZIBrjw+snSVU4T8MOeuZJUJjM=", - "owner": "numtide", - "repo": "treefmt-nix", - "rev": "5b4ee75aeefd1e2d5a1cc43cf6ba65eba75e83e4", - "type": "github" - }, - "original": { - "owner": "numtide", - "repo": "treefmt-nix", - "type": "github" - } } }, "root": "root", diff --git a/flake.nix b/flake.nix index b33e9ce..c8f24c0 100644 --- a/flake.nix +++ b/flake.nix @@ -8,8 +8,6 @@ nix-cargo-integration.url = "github:yusdacra/nix-cargo-integration"; nix-cargo-integration.inputs.nixpkgs.follows = "nixpkgs"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; - treefmt-nix.url = "github:numtide/treefmt-nix"; - treefmt-nix.inputs.nixpkgs.follows = "nixpkgs"; }; outputs = @@ -164,7 +162,6 @@ inputs.nix-cargo-integration.flakeModule inputs.flake-parts.flakeModules.partitions inputs.flake-parts.flakeModules.modules - inputs.treefmt-nix.flakeModule # dogfood flake-parts-modules.tested ./nci.nix @@ -182,19 +179,6 @@ }: { packages.nix = inputs'.nix.packages.nix; - - treefmt = { - # Used to find the project root - projectRootFile = "flake.lock"; - - programs.rustfmt = { - enable = true; - edition = "2021"; - }; - programs.nixfmt.enable = true; - programs.deadnix.enable = true; - #programs.clang-format.enable = true; - }; }; partitionedAttrs.devShells = "dev";