From cd7c759361f6ff9b209f82a5a651e51b14e6ed77 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Fri, 05 Sep 2025 01:39:09 +0000 Subject: [PATCH] fix: carry more settings from environment --- --- a/Cargo.lock +++ b/Cargo.lock @@ -738,6 +738,12 @@ ] [[package]] +name = "cursor-icon" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96a6ac251f4a2aca6b3f91340350eab87ae57c3f127ffeb585e92bd336717991" + +[[package]] name = "curve25519-dalek" version = "4.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1956,6 +1962,7 @@ "tokio-util", "tracing", "tracing-indicatif", + "vte 0.15.0", ] [[package]] @@ -3666,7 +3673,7 @@ "itoa", "log", "unicode-width 0.1.14", - "vte", + "vte 0.11.1", ] [[package]] @@ -3681,6 +3688,19 @@ ] [[package]] +name = "vte" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5924018406ce0063cd67f8e008104968b74b563ee1b85dde3ed1f7cb87d3dbd" +dependencies = [ + "arrayvec", + "bitflags", + "cursor-icon", + "log", + "memchr", +] + +[[package]] name = "vte_generate_state_changes" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" --- a/crates/fleet-base/src/opts.rs +++ b/crates/fleet-base/src/opts.rs @@ -8,8 +8,8 @@ use anyhow::{Context, Result, bail}; use nix_eval::{ - FetchSettings, FlakeReference, FlakeReferenceParseFlags, FlakeSettings, Value, nix_go, - util::assert_warn, + FetchSettings, FlakeLockFlags, FlakeReference, FlakeReferenceParseFlags, FlakeSettings, Value, + nix_go, util::assert_warn, }; use nom::{ Parser, @@ -229,8 +229,11 @@ &parse, &fetch_settings, )?; - let flake = flake.lock(&fetch_settings)?; + let lock = FlakeLockFlags::new(&flake_settings)?; + + let flake = flake.lock(&fetch_settings, &flake_settings, &lock)?; + let flake = flake.get_attrs(&mut flake_settings)?; let builtins_field = Value::eval("builtins")?; --- a/crates/nix-eval/Cargo.toml +++ b/crates/nix-eval/Cargo.toml @@ -19,6 +19,7 @@ itertools = "0.14.0" test-log = { version = "0.2.18", features = ["trace"] } tracing-indicatif = { version = "0.3.13", optional = true } +vte = { version = "0.15.0", features = ["ansi"] } [build-dependencies] bindgen = "0.72.0" --- a/crates/nix-eval/src/lib.rs +++ b/crates/nix-eval/src/lib.rs @@ -19,12 +19,11 @@ alloc_value, c_context, c_context_create, err_code, err_info_msg, eval_state_build, eval_state_builder_new, expr_eval_from_string, fetchers_settings, fetchers_settings_free, fetchers_settings_new, flake_lock, flake_lock_flags, flake_lock_flags_free, - flake_lock_flags_new, flake_lock_flags_set_mode_virtual, flake_reference_parse_flags, - flake_reference_parse_flags_free, flake_reference_parse_flags_new, - flake_reference_parse_flags_set_base_directory, flake_settings, flake_settings_free, - flake_settings_new, init_bool, init_int, init_string, locked_flake_free, - locked_flake_get_output_attrs, set_err_msg, setting_set, state_free, value_decref, - value_incref, + flake_lock_flags_new, flake_reference_parse_flags, flake_reference_parse_flags_free, + flake_reference_parse_flags_new, flake_reference_parse_flags_set_base_directory, + flake_settings, flake_settings_free, flake_settings_new, get_string, init_bool, init_int, + init_string, locked_flake_free, locked_flake_get_output_attrs, set_err_msg, setting_set, + state_free, value_decref, value_incref, }; // Contains macros helpers @@ -226,11 +225,15 @@ fn new() -> Result { let mut ctx = NixContext::new(); let store = ctx - .run_in_context(|c| unsafe { nix_raw::store_open(c, c"daemon".as_ptr(), null_mut()) }) + .run_in_context(|c| unsafe { nix_raw::store_open(c, c"auto".as_ptr(), null_mut()) }) .map(Store)?; let builder = ctx.run_in_context(|c| unsafe { eval_state_builder_new(c, store.0) })?; ctx.run_in_context(|c| { + unsafe { nix_raw::eval_state_builder_load(c, builder) } + // eval_s + })?; + ctx.run_in_context(|c| { unsafe { nix_raw::eval_state_builder_set_eval_setting( c, @@ -363,12 +366,12 @@ } } } -struct FlakeLockFlags(*mut flake_lock_flags); +pub struct FlakeLockFlags(*mut flake_lock_flags); impl FlakeLockFlags { - fn new(settings: &mut FlakeSettings) -> Result { + pub fn new(settings: &FlakeSettings) -> Result { let o = with_default_context(|c, _| unsafe { flake_lock_flags_new(c, settings.0) }) .map(Self)?; - with_default_context(|c, _| unsafe { flake_lock_flags_set_mode_virtual(c, o.0) })?; + // with_default_context(|c, _| unsafe { flake_lock_flags_set_mode_virtual(c, o.0) })?; Ok(o) } @@ -432,14 +435,15 @@ Ok((Self(out), fragment)) } - #[instrument(name = "lock-flake", skip(self, fetch))] - pub fn lock(&mut self, fetch: &FetchSettings) -> Result { - let mut settings = FlakeSettings::new()?; - let lock_flags = FlakeLockFlags::new(&mut settings)?; - with_default_context(|c, es| unsafe { - flake_lock(c, fetch.0, settings.0, es, lock_flags.0, self.0) - }) - .map(LockedFlake) + #[instrument(name = "lock-flake", skip(self, fetch, flake, lock))] + pub fn lock( + &mut self, + fetch: &FetchSettings, + flake: &FlakeSettings, + lock: &FlakeLockFlags, + ) -> Result { + with_default_context(|c, es| unsafe { flake_lock(c, fetch.0, flake.0, es, lock.0, self.0) }) + .map(LockedFlake) } } unsafe impl Send for FlakeReference {} @@ -611,8 +615,17 @@ .expect("get_type should not fail"); NixType::from_int(ty) } + fn builtin_to_string(&self) -> Result { + let builtin = Self::eval("builtins.toString")?; + builtin.call(self.clone()) + } pub fn to_string(&self) -> Result { - Ok(self.to_realised_string()?.as_str().to_owned()) + let mut str_out = String::new(); + with_default_context(|c, _| unsafe { + get_string(c, self.0, Some(copy_nix_str), (&raw mut str_out).cast()) + })?; + + Ok(str_out) } pub fn to_realised_string(&self) -> Result { with_default_context(|c, es| unsafe { nix_raw::string_realise(c, es, self.0, false) }) @@ -751,10 +764,11 @@ self.clone() }; // to_string here blocks until the path is built - let drv_path = tokio::task::spawn_blocking(move || v.get_field("outPath")?.to_string()) - .await - .expect("should not fail")?; - Ok(PathBuf::from(drv_path)) + let drv_path = + tokio::task::spawn_blocking(move || v.builtin_to_string()?.to_realised_string()) + .await + .expect("spawn should not fail")?; + Ok(PathBuf::from(drv_path.as_str())) } pub fn as_json(&self) -> Result { let to_json = Self::eval("builtins.toJSON")?; @@ -835,6 +849,9 @@ pub fn init_libraries() { unsafe { nix_raw::GC_allow_register_threads() }; + unsafe { + nix_raw::GC_init(); + }; let mut ctx = NixContext::new(); ctx.run_in_context(|c| unsafe { nix_raw::libutil_init(c) }) --- a/crates/nix-eval/src/logging.cc +++ b/crates/nix-eval/src/logging.cc @@ -60,5 +60,6 @@ extern "C" { void apply_tracing_logger() { logger = std::make_unique(); + // verbosity = lvlVomit; } } --- a/crates/nix-eval/src/logging.rs +++ b/crates/nix-eval/src/logging.rs @@ -8,6 +8,7 @@ }; #[cfg(feature = "indicatif")] use tracing_indicatif::span_ext::IndicatifSpanExt as _; +use vte::Parser; #[derive(Debug)] enum ActivityType { @@ -374,6 +375,7 @@ } }; if !s.trim().is_empty() { + let s = ansi_filter(s); #[cfg(feature = "indicatif")] { span.pb_set_message(s); @@ -413,7 +415,8 @@ match (&res, self.fields.as_slice()) { // ResultType::FileLinked => todo!(), (ResultType::BuildLogLine, [Str(s)]) => { - info!("{s:?}"); + let s = ansi_filter(s); + info!("{s}"); } // ResultType::UntrustedPath => todo!(), // ResultType::CorruptedPath => todo!(), @@ -468,6 +471,68 @@ } } +struct AnsiFiltered { + output: String, +} +impl vte::Perform for AnsiFiltered { + fn print(&mut self, c: char) { + self.output.push(c); + } + + fn execute(&mut self, byte: u8) { + // We don't want \r, bells, etc + if byte == b'\n' { + self.output.push('\n'); + } else if byte == b'\t' { + // TODO: align output to the correct multiplier? + self.output.push('\t'); + } + } + + fn osc_dispatch(&mut self, _params: &[&[u8]], _bell_terminated: bool) {} + fn esc_dispatch(&mut self, _intermediates: &[u8], _ignore: bool, _byte: u8) {} + + fn csi_dispatch( + &mut self, + params: &vte::Params, + _intermediates: &[u8], + _ignore: bool, + action: char, + ) { + use std::fmt::Write; + if action != 'm' { + // Only plain colors are enabled, everything other might corrupt the output + return; + } + self.output.push_str("IDK\x1b["); + for (i, par) in params.iter().enumerate() { + if i != 0 { + let _ = write!(self.output, ";"); + } + for (i, sub) in par.iter().enumerate() { + if i != 0 { + let _ = write!(self.output, ":"); + } + let _ = write!(self.output, "{sub}"); + } + } + self.output.push(action); + } +} +fn ansi_filter(i: &str) -> String { + let mut out = AnsiFiltered { + output: String::new(), + }; + let mut parser = Parser::new(); + + // For some reason it gets stuck with longer inputs + for chunk in i.as_bytes().chunks(50) { + parser.advance(&mut out, chunk); + } + + out.output +} + #[cxx::bridge] pub mod nix_logging_cxx { extern "Rust" { --- a/flake.lock +++ b/flake.lock @@ -104,10 +104,10 @@ "nixpkgs-regression": "nixpkgs-regression" }, "locked": { - "lastModified": 1757000273, + "lastModified": 1757031817, "owner": "deltarocks", "repo": "nix", - "rev": "eba1f549ec21208cf98343f1351a95e2e6eb3fbb", + "rev": "2261305161dc02496c8aedcdda14ace04b9b1dc1", "type": "github" }, "original": { --- a/lib/flakePart.nix +++ b/lib/flakePart.nix @@ -65,25 +65,28 @@ normalEval = bootstrapNixpkgs.lib.evalModules { modules = (import ../modules/module-list.nix) ++ [ module - ({inputs', ...}: { - config = { - data = if isPath data then import data else data; - nixpkgs.buildUsing = mkOptionDefault bootstrapNixpkgs; - nixpkgs.overlays = [ - (final: prev: { - inherit - (import ../pkgs { - inherit (prev) callPackage; - inherit inputs'; - craneLib = crane.mkLib prev; - }) - fleet-install-secrets - fleet-generator-helper - ; - }) - ]; - }; - }) + ( + { inputs', ... }: + { + config = { + data = if isPath data then import data else data; + nixpkgs.buildUsing = mkOptionDefault bootstrapNixpkgs; + nixpkgs.overlays = [ + (final: prev: { + inherit + (import ../pkgs { + inherit (prev) callPackage; + inherit inputs'; + craneLib = crane.mkLib prev; + }) + fleet-install-secrets + fleet-generator-helper + ; + }) + ]; + }; + } + ) ]; specialArgs = { inherit inputs self; -- gitstuff