difftreelog
refactor cleanup bindings
in: trunk
12 files changed
Cargo.lockdiffbeforeafterboth728 "typenum",728 "typenum",729]729]730731[[package]]732name = "ctor"733version = "0.5.0"734source = "registry+https://github.com/rust-lang/crates.io-index"735checksum = "67773048316103656a637612c4a62477603b777d91d9c62ff2290f9cde178fdb"736dependencies = [737 "ctor-proc-macro",738 "dtor",739]740741[[package]]742name = "ctor-proc-macro"743version = "0.0.6"744source = "registry+https://github.com/rust-lang/crates.io-index"745checksum = "e2931af7e13dc045d8e9d26afccc6fa115d64e115c9c84b1166288b46f6782c2"746730747[[package]]731[[package]]748name = "ctr"732name = "ctr"915 "litrs",899 "litrs",916]900]917918[[package]]919name = "dtor"920version = "0.1.0"921source = "registry+https://github.com/rust-lang/crates.io-index"922checksum = "e58a0764cddb55ab28955347b45be00ade43d4d6f3ba4bf3dc354e4ec9432934"923dependencies = [924 "dtor-proc-macro",925]926927[[package]]928name = "dtor-proc-macro"929version = "0.0.6"930source = "registry+https://github.com/rust-lang/crates.io-index"931checksum = "f678cf4a922c215c63e0de95eb1ff08a958a81d47e485cf9da1e27bf6305cfa5"932901933[[package]]902[[package]]934name = "ed25519"903name = "ed25519"1973version = "0.1.0"1942version = "0.1.0"1974dependencies = [1943dependencies = [1975 "anyhow",1944 "anyhow",1976 "better-command",1977 "bindgen",1945 "bindgen",1978 "ctor",1979 "cxx",1946 "cxx",1980 "cxx-build",1947 "cxx-build",1981 "futures",1982 "itertools 0.14.0",1948 "itertools 0.14.0",1983 "nixlike",1949 "nixlike",1984 "pkg-config",1950 "pkg-config",1985 "r2d2",1986 "regex",1987 "serde",1951 "serde",1988 "serde_json",1952 "serde_json",1989 "test-log",1953 "test-log",1992 "tokio-util",1956 "tokio-util",1993 "tracing",1957 "tracing",1994 "tracing-indicatif",1958 "tracing-indicatif",1995 "unindent",1996]1959]199719601998[[package]]1961[[package]]2455source = "registry+https://github.com/rust-lang/crates.io-index"2418source = "registry+https://github.com/rust-lang/crates.io-index"2456checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"2419checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"24572458[[package]]2459name = "r2d2"2460version = "0.8.10"2461source = "registry+https://github.com/rust-lang/crates.io-index"2462checksum = "51de85fb3fb6524929c8a2eb85e6b6d363de4e8c48f9e2c2eac4944abc181c93"2463dependencies = [2464 "log",2465 "parking_lot",2466 "scheduled-thread-pool",2467]246824202469[[package]]2421[[package]]2470name = "rand"2422name = "rand"2828 "winapi-util",2780 "winapi-util",2829]2781]28302831[[package]]2832name = "scheduled-thread-pool"2833version = "0.2.7"2834source = "registry+https://github.com/rust-lang/crates.io-index"2835checksum = "3cbc66816425a074528352f5789333ecff06ca41b36b0b0efdfbb29edc391a19"2836dependencies = [2837 "parking_lot",2838]283927822840[[package]]2783[[package]]2841name = "scopeguard"2784name = "scopeguard"3674source = "registry+https://github.com/rust-lang/crates.io-index"3617source = "registry+https://github.com/rust-lang/crates.io-index"3675checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e"3618checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e"36763677[[package]]3678name = "unindent"3679version = "0.2.4"3680source = "registry+https://github.com/rust-lang/crates.io-index"3681checksum = "7264e107f553ccae879d21fbea1d6724ac785e8c3bfc762137959b5802826ef3"368236193683[[package]]3620[[package]]3684name = "unit-prefix"3621name = "unit-prefix"cmds/fleet/Cargo.tomldiffbeforeafterboth55 "dep:indicatif",55 "dep:indicatif",56 "dep:human-repr",56 "dep:human-repr",57 "better-command/indicatif",57 "better-command/indicatif",58 "nix-eval/indicatif",58]59]5960cmds/fleet/src/cmds/build_systems.rsdiffbeforeafterboth31async fn build_task(31async fn build_task(config: Config, hostname: String, build_attr: &str) -> Result<PathBuf> {32 config: Config,33 hostname: String,34 build_attr: &str,35 // batch: Option<NixBuildBatch>,36) -> Result<PathBuf> {37 info!("building");32 info!("building");38 let host = config.host(&hostname).await?;33 let host = config.host(&hostname).await?;39 // let action = Action::from(self.subcommand.clone());34 // let action = Action::from(self.subcommand.clone());40 let nixos = host.nixos_config().await?;35 let nixos = host.nixos_config().await?;41 let drv = nix_go!(nixos.system.build[{ build_attr }]);36 let drv = nix_go!(nixos.system.build[{ build_attr }]);42 // let outputs = drv.build_maybe_batch(batch).await?;43 let out_output = drv.build("out").await?;37 let out_output = drv.build("out").await?;443845 // We already have system profiles for backups.39 // We already have system profiles for backups.66 let hosts = opts.filter_skipped(config.list_hosts().await?).await?;60 let hosts = opts.filter_skipped(config.list_hosts().await?).await?;67 let set = LocalSet::new();61 let set = LocalSet::new();68 let build_attr = self.build_attr.clone();62 let build_attr = self.build_attr.clone();69 // let batch = (hosts.len() > 1).then(|| {70 // config71 // .nix_session72 // .new_build_batch("build-hosts".to_string())73 // });74 for host in hosts {63 for host in hosts {75 let config = config.clone();64 let config = config.clone();76 let span = info_span!("build", host = field::display(&host.name));65 let span = info_span!("build", host = field::display(&host.name));77 let hostname = host.name;66 let hostname = host.name;78 let build_attr = build_attr.clone();67 let build_attr = build_attr.clone();79 // let batch = batch.clone();80 set.spawn_local(68 set.spawn_local(81 (async move {69 (async move {82 let built = match build_task(config, hostname.clone(), &build_attr).await {70 let built = match build_task(config, hostname.clone(), &build_attr).await {107 pub async fn run(self, config: &Config, opts: &FleetOpts) -> Result<()> {95 pub async fn run(self, config: &Config, opts: &FleetOpts) -> Result<()> {108 let hosts = opts.filter_skipped(config.list_hosts().await?).await?;96 let hosts = opts.filter_skipped(config.list_hosts().await?).await?;109 let set = LocalSet::new();97 let set = LocalSet::new();110 // let batch = (hosts.len() > 1).then(|| {111 // config112 // .nix_session113 // .new_build_batch("deploy-hosts".to_string())114 // });115 for host in hosts.into_iter() {98 for host in hosts.into_iter() {116 let config = config.clone();99 let config = config.clone();117 let span = info_span!("deploy", host = field::display(&host.name));100 let span = info_span!("deploy", host = field::display(&host.name));cmds/fleet/src/cmds/secrets/mod.rsdiffbeforeafterboth169 expected_owners: &[String],169 expected_owners: &[String],170 expected_generation_data: serde_json::Value,170 expected_generation_data: serde_json::Value,171 prefer_identities: &[String],171 prefer_identities: &[String],172 // batch: Option<NixBuildBatch>,173) -> Result<FleetSharedSecret> {172) -> Result<FleetSharedSecret> {174 let original_set = secret.owners.clone();173 let original_set = secret.owners.clone();175174207 field,206 field,208 expected_owners.to_vec(),207 expected_owners.to_vec(),209 expected_generation_data,208 expected_generation_data,210 // batch,211 )209 )212 .await?;210 .await?;213 Ok(generated)211 Ok(generated)214 } else {212 } else {215 // drop(batch);216 let identity_holder = if !prefer_identities.is_empty() {213 let identity_holder = if !prefer_identities.is_empty() {217 prefer_identities214 prefer_identities218 .iter()215 .iter()264 default_generator: Value,261 default_generator: Value,265 expected_owners: &[String],262 expected_owners: &[String],266 expected_generation_data: serde_json::Value,263 expected_generation_data: serde_json::Value,267 // batch: Option<NixBuildBatch>,268) -> Result<FleetSecret> {264) -> Result<FleetSecret> {269 let generator = nix_go!(secret.generator);265 let generator = nix_go!(secret.generator);270 let on: Option<String> = nix_go_json!(default_generator.impureOn);266 let on: Option<String> = nix_go_json!(default_generator.impureOn);292288293 let generator = nix_go!(call_package(generator)(Obj {}));289 let generator = nix_go!(call_package(generator)(Obj {}));294290295 // let generator = generator.build_maybe_batch(batch).await?;296 let generator = generator.build("out").await?;291 let generator = generator.build("out").await?;297 let generator = host.remote_derivation(&generator).await?;292 let generator = host.remote_derivation(&generator).await?;298293347 secret: Value,342 secret: Value,348 expected_owners: &[String],343 expected_owners: &[String],349 expected_generation_data: serde_json::Value,344 expected_generation_data: serde_json::Value,350 // batch: Option<NixBuildBatch>,351) -> Result<FleetSecret> {345) -> Result<FleetSecret> {352 let generator = nix_go!(secret.generator);346 let generator = nix_go!(secret.generator);353 // Can't properly check on nix module system level347 // Can't properly check on nix module system level354 {348 {355 let gen_ty = generator.type_of()?;349 let gen_ty = generator.type_of();356 if matches!(gen_ty, NixType::Null) {350 if matches!(gen_ty, NixType::Null) {357 bail!("secret has no generator defined, can't automatically generate it.");351 bail!("secret has no generator defined, can't automatically generate it.");358 }352 }394 default_generator,388 default_generator,395 expected_owners,389 expected_owners,396 expected_generation_data,390 expected_generation_data,397 // batch,398 )391 )399 .await392 .await400 }393 }416 secret: Value,409 secret: Value,417 expected_owners: Vec<String>,410 expected_owners: Vec<String>,418 expected_generation_data: serde_json::Value,411 expected_generation_data: serde_json::Value,419 // batch: Option<NixBuildBatch>,420) -> Result<FleetSharedSecret> {412) -> Result<FleetSharedSecret> {421 // let owners: Vec<String> = nix_go_json!(secret.expectedOwners);413 // let owners: Vec<String> = nix_go_json!(secret.expectedOwners);422 Ok(FleetSharedSecret {414 Ok(FleetSharedSecret {426 secret,418 secret,427 &expected_owners,419 &expected_owners,428 expected_generation_data,420 expected_generation_data,429 // batch,430 )421 )431 .await?,422 .await?,432 owners: expected_owners,423 owners: expected_owners,747 let stored_shared_set = config.list_shared().into_iter().collect::<HashSet<_>>();738 let stored_shared_set = config.list_shared().into_iter().collect::<HashSet<_>>();748 {739 {749 // Generate missing shared740 // Generate missing shared750 // let shared_batch = None;751 let _span = info_span!("shared").entered();741 let _span = info_span!("shared").entered();752 let expected_shared_set = config742 let expected_shared_set = config753 .list_configured_shared()743 .list_configured_shared()772 secret,762 secret,773 expected_owners,763 expected_owners,774 expected_generation_data,764 expected_generation_data,775 // shared_batch.clone(),776 )765 )777 .in_current_span()766 .in_current_span()778 .await?;767 .await?;779 config.replace_shared(missing.to_string(), shared)768 config.replace_shared(missing.to_string(), shared)780 }769 }781 }770 }782 if !skip_hosts {771 if !skip_hosts {783 // let hosts_batch = None;784 for host in config.list_hosts().await? {772 for host in config.list_hosts().await? {785 if opts.should_skip(&host).await? {773 if opts.should_skip(&host).await? {786 continue;774 continue;808 secret,796 secret,809 slice::from_ref(&host.name),797 slice::from_ref(&host.name),810 expected_generation_data,798 expected_generation_data,811 // hosts_batch.clone(),812 )799 )813 .in_current_span()800 .in_current_span()814 .await801 .await834 secret,821 secret,835 slice::from_ref(&host.name),822 slice::from_ref(&host.name),836 expected_generation_data,823 expected_generation_data,837 // hosts_batch.clone(),838 )824 )839 .in_current_span()825 .in_current_span()840 .await826 .awaitcmds/fleet/src/cmds/tf.rsdiffbeforeafterboth1use std::{1use std::{collections::BTreeMap, ffi::OsString, path::PathBuf};2 collections::{BTreeMap, HashMap},3 ffi::OsString,4 path::PathBuf,5};627use anyhow::{Context, Result};3use anyhow::{Context, Result};crates/nix-eval/Cargo.tomldiffbeforeafterboth778[dependencies]8[dependencies]9anyhow.workspace = true9anyhow.workspace = true10better-command.workspace = true11nixlike.workspace = true10nixlike.workspace = true12serde = { workspace = true, features = ["derive"] }11serde = { workspace = true, features = ["derive"] }13serde_json.workspace = true12serde_json.workspace = true14thiserror.workspace = true13thiserror.workspace = true15tokio = { workspace = true, features = ["io-util", "process"] }14tokio = { workspace = true }16tokio-util.workspace = true15tokio-util.workspace = true17tracing.workspace = true16tracing.workspace = true181719cxx = "1.0.168"18cxx = "1.0.168"20futures = "0.3.31"21itertools = "0.14.0"19itertools = "0.14.0"22r2d2 = "0.8.10"23regex = "1.11.1"24test-log = { version = "0.2.18", features = ["trace"] }20test-log = { version = "0.2.18", features = ["trace"] }25unindent = "0.2.4"21tracing-indicatif = { version = "0.3.13", optional = true }26tracing-indicatif = "0.3.13"27ctor = "0.5.0"282229[build-dependencies]23[build-dependencies]30bindgen = "0.72.0"24bindgen = "0.72.0"31cxx-build = "1.0.168"25cxx-build = "1.0.168"32pkg-config = "0.3.30"26pkg-config = "0.3.30"2728[features]29indicatif = ["dep:tracing-indicatif"]3330crates/nix-eval/src/lib.rsdiffbeforeafterboth1//! This whole library should be replaced with either binding to nix libexpr,2//! or with tvix (once it is able to build NixOS).3//!4//! Current api is awful, little effort was put into this implementation.56use std::borrow::Cow;1use std::borrow::Cow;7use std::cell::RefCell;2use std::cell::RefCell;11use std::sync::LazyLock;6use std::sync::LazyLock;12use std::{collections::HashMap, path::PathBuf};7use std::{collections::HashMap, path::PathBuf};13814use anyhow::{Context, bail};9use anyhow::{Context, anyhow, bail};15use serde::Serialize;10use serde::Serialize;16use serde::de::DeserializeOwned;11use serde::de::DeserializeOwned;171227 flake_reference_parse_flags_new, flake_reference_parse_flags_set_base_directory,22 flake_reference_parse_flags_new, flake_reference_parse_flags_set_base_directory,28 flake_settings, flake_settings_free, flake_settings_new, init_bool, init_int, init_string,23 flake_settings, flake_settings_free, flake_settings_new, init_bool, init_int, init_string,29 locked_flake_free, locked_flake_get_output_attrs, set_err_msg, setting_set, state_free,24 locked_flake_free, locked_flake_get_output_attrs, set_err_msg, setting_set, state_free,30 value_decref, value_force, value_incref,25 value_decref, value_incref,31};26};322733mod value;34// Contains macros helpers28// Contains macros helpers35pub mod logging;29pub mod logging;36#[doc(hidden)]30#[doc(hidden)]37pub mod macros;31pub mod macros;38pub mod util;32pub mod util;393340#[allow(non_upper_case_globals, non_camel_case_types, non_snake_case)]34#[allow(35 non_upper_case_globals,36 non_camel_case_types,37 non_snake_case,38 dead_code39)]41mod nix_raw {40mod nix_raw {42 include!(concat!(env!("OUT_DIR"), "/bindings.rs"));41 include!(concat!(env!("OUT_DIR"), "/bindings.rs"));88 }87 }89}88}8990enum FunctorKind {91 Function,92 Functor,93}909491#[derive(Debug)]95#[derive(Debug)]92#[repr(i32)]96#[repr(i32)]130 unsafe { nix_raw::GC_unregister_my_thread() };134 unsafe { nix_raw::GC_unregister_my_thread() };131}135}132136133struct ThreadRegisterGuard {}137pub struct ThreadRegisterGuard {}134impl ThreadRegisterGuard {138impl ThreadRegisterGuard {135 fn new() -> Self {139 pub fn new() -> Self {136 gc_register_my_thread();140 gc_register_my_thread();137 Self {}141 Self {}138 }142 }143 }147 }144}148}145149146struct NixContext(*mut c_context);150pub struct NixContext(*mut c_context);147impl NixContext {151impl NixContext {148 fn set_err(&mut self, err: NixErrorKind, msg: &CStr) {152 pub fn set_err(&mut self, err: NixErrorKind, msg: &CStr) {149 unsafe { set_err_msg(self.0, err as c_int, msg.as_ptr()) };153 unsafe { set_err_msg(self.0, err as c_int, msg.as_ptr()) };150 }154 }151 fn new() -> Self {155 pub fn new() -> Self {152 let ctx = unsafe { c_context_create() };156 let ctx = unsafe { c_context_create() };153 Self(ctx)157 Self(ctx)154 }158 }175 let str = unsafe { nix_raw::err_msg(null_mut(), self.0, null_mut()) };179 let str = unsafe { nix_raw::err_msg(null_mut(), self.0, null_mut()) };176 Some(unsafe { CStr::from_ptr(str) }.to_string_lossy())180 Some(unsafe { CStr::from_ptr(str) }.to_string_lossy())177178 // TODO: There is also nix_err_info_msg, but I don't understand when it should be used179 // Some(match self.error_kind()? {180 // NixErrorKind::Generic => {181 // }182 // })183 }181 }184 fn clean_err(&mut self) {182 fn clean_err(&mut self) {185 unsafe {183 unsafe {210 }208 }211}209}212struct GlobalState {210struct GlobalState {211 // Store should be valid as long as EvalState is valid212 #[allow(dead_code)]213 store: Store,213 store: Store,214 state: EvalState,214 state: EvalState,215}215}269 v269 v270}270}271271272fn set_setting(s: &CStr, v: &CStr) -> Result<()> {272pub fn set_setting(s: &CStr, v: &CStr) -> Result<()> {273 with_default_context(|c, _| unsafe { setting_set(c, s.as_ptr(), v.as_ptr()) }).map(|_| ())273 with_default_context(|c, _| unsafe { setting_set(c, s.as_ptr(), v.as_ptr()) }).map(|_| ())274}274}275275290unsafe impl Send for FetchSettings {}290unsafe impl Send for FetchSettings {}291unsafe impl Sync for FetchSettings {}291unsafe impl Sync for FetchSettings {}292293impl Default for FetchSettings {294 fn default() -> Self {295 Self::new()296 }297}298292impl Drop for FetchSettings {299impl Drop for FetchSettings {293 fn drop(&mut self) {300 fn drop(&mut self) {310 }317 }311}318}312319313struct FlakeReferenceParseFlags(*mut flake_reference_parse_flags);320pub struct FlakeReferenceParseFlags(*mut flake_reference_parse_flags);314impl FlakeReferenceParseFlags {321impl FlakeReferenceParseFlags {315 fn new(settings: &mut FlakeSettings) -> Result<Self> {322 pub fn new(settings: &mut FlakeSettings) -> Result<Self> {316 with_default_context(|c, _| unsafe { flake_reference_parse_flags_new(c, settings.0) })323 with_default_context(|c, _| unsafe { flake_reference_parse_flags_new(c, settings.0) })317 .map(Self)324 .map(Self)318 }325 }319 fn set_base_dir(&mut self, dir: &str) -> Result<()> {326 pub fn set_base_dir(&mut self, dir: &str) -> Result<()> {320 with_default_context(|c, _| {327 with_default_context(|c, _| {321 unsafe {328 unsafe {322 flake_reference_parse_flags_set_base_directory(329 flake_reference_parse_flags_set_base_directory(361unsafe impl Sync for Store {}368unsafe impl Sync for Store {}362369363struct EvalState(*mut nix_raw::EvalState);370struct EvalState(*mut nix_raw::EvalState);364impl EvalState {365 // TODO: store ownership366 fn new_raw(store: *mut nix_raw::Store) -> Result<Self> {367 let builder =368 with_default_context(|c, _| unsafe { nix_raw::eval_state_builder_new(c, store) })?;369370 with_default_context(|c, _| unsafe { eval_state_build(c, builder) }).map(Self)371372 // with_default_context(|c| state_create(c))373 }374}375unsafe impl Send for EvalState {}371unsafe impl Send for EvalState {}376unsafe impl Sync for EvalState {}372unsafe impl Sync for EvalState {}373386impl FlakeReference {383impl FlakeReference {387 pub fn new(s: &str, fetch: &FetchSettings) -> Result<(Self, String)> {384 pub fn new(s: &str, fetch: &FetchSettings) -> Result<(Self, String)> {388 let mut flake_settings = FlakeSettings::new()?;385 let mut flake_settings = FlakeSettings::new()?;389 let mut parse_flags = FlakeReferenceParseFlags::new(&mut flake_settings)?;386 let parse_flags = FlakeReferenceParseFlags::new(&mut flake_settings)?;390391 // parse_flags.set_base_dir("/home/lach/build/fleet")?;392387393 let mut out = null_mut();388 let mut out = null_mut();394 let mut fragment = String::new();389 let mut fragment = String::new();461}456}462457463impl RealisedString {458impl RealisedString {464 fn as_str(&self) -> &str {459 pub fn as_str(&self) -> &str {465 let len = unsafe { nix_raw::realised_string_get_buffer_size(self.0) };460 let len = unsafe { nix_raw::realised_string_get_buffer_size(self.0) };466 let data: *const u8 = unsafe { nix_raw::realised_string_get_buffer_start(self.0) }.cast();461 let data: *const u8 = unsafe { nix_raw::realised_string_get_buffer_start(self.0) }.cast();467 let data = unsafe { std::slice::from_raw_parts(data, len) };462 let data = unsafe { std::slice::from_raw_parts(data, len) };468 std::str::from_utf8(data).expect("non-utf8 strings not supported")463 std::str::from_utf8(data).expect("non-utf8 strings not supported")469 }464 }470 fn path_count(&self) -> usize {465 pub fn path_count(&self) -> usize {471 unsafe { nix_raw::realised_string_get_store_path_count(self.0) }466 unsafe { nix_raw::realised_string_get_store_path_count(self.0) }472 }467 }473 fn path(&self, i: usize) -> String {468 pub fn path(&self, i: usize) -> String {474 assert!(i < self.path_count());469 assert!(i < self.path_count());475 let path = unsafe { nix_raw::realised_string_get_store_path(self.0, i) };470 let path = unsafe { nix_raw::realised_string_get_store_path(self.0, i) };476 let mut err_out = String::new();471 let mut err_out = String::new();482unsafe impl Send for RealisedString {}477unsafe impl Send for RealisedString {}483impl Drop for RealisedString {478impl Drop for RealisedString {484 fn drop(&mut self) {479 fn drop(&mut self) {485 with_default_context(|c, _| unsafe { nix_raw::realised_string_free(self.0) })480 unsafe { nix_raw::realised_string_free(self.0) }486 .expect("string free should not fail")487 }481 }488}482}489483541}535}542536543impl Value {537impl Value {544 pub fn new_attrs(v: HashMap<&str, Value>) -> Result<Self> {538 pub fn new_attrs(v: HashMap<&str, Value>) -> Self {545 let out = Self::new_uninit()?;539 let out = Self::new_uninit();546 let mut b = AttrsBuilder::new(v.len());540 let mut b = AttrsBuilder::new(v.len());547 for (k, v) in v {541 for (k, v) in v {548 b.insert(&k, v);542 b.insert(&k, v);549 }543 }550 with_default_context(|c, _| unsafe { nix_raw::make_attrs(c, out.0, b.0) })?;544 with_default_context(|c, _| unsafe { nix_raw::make_attrs(c, out.0, b.0) })545 .expect("attrs initialization should not fail");551 Ok(out)546 out552 }547 }553 fn new_list<T: Into<Self>>(v: Vec<T>) -> Result<Self> {548 fn new_list<T: Into<Self>>(v: Vec<T>) -> Self {554 todo!()549 todo!()555 }550 }556 fn new_uninit() -> Result<Self> {551 fn new_uninit() -> Self {557 let out = with_default_context(|c, es| unsafe { alloc_value(c, es) })?;552 let out = with_default_context(|c, es| unsafe { alloc_value(c, es) })553 .expect("value allocation should not fail");558 Ok(Self(out))554 Self(out)559 }555 }560 fn new_str(v: &str) -> Result<Self> {556 pub fn new_str(v: &str) -> Self {561 let s = CString::new(v).expect("string should not contain NULs");557 let s = CString::new(v).expect("string should not contain NULs");562 let uninit = Self::new_uninit()?;558 let out = Self::new_uninit();563 // String is copied, `s` is free to be dropped559 // String is copied, `s` is free to be dropped564 with_default_context(|c, _| unsafe { init_string(c, uninit.0, s.as_ptr()) })?;560 with_default_context(|c, _| unsafe { init_string(c, out.0, s.as_ptr()) })565 Ok(uninit)561 .expect("string initialization should not fail");562 out566 }563 }567 fn new_int(i: i64) -> Result<Self> {564 pub fn new_int(i: i64) -> Self {568 let uninit = Self::new_uninit()?;565 let out = Self::new_uninit();569 with_default_context(|c, _| unsafe { init_int(c, uninit.0, i) })?;566 with_default_context(|c, _| unsafe { init_int(c, out.0, i) })570 Ok(uninit)567 .expect("int initialization should not fail");568 out571 }569 }572 fn new_bool(v: bool) -> Result<Self> {570 pub fn new_bool(v: bool) -> Self {573 let uninit = Self::new_uninit()?;571 let out = Self::new_uninit();574 with_default_context(|c, _| unsafe { init_bool(c, uninit.0, v) })?;572 with_default_context(|c, _| unsafe { init_bool(c, out.0, v) })575 Ok(uninit)573 .expect("bool initialization should not fail");574 out576 }575 }577 fn force(&mut self, st: &mut EvalState) -> Result<()> {576 // TODO: As far as I can see, there is no way to get Thunks from nix public C api, so this function is useless578 with_default_context(|c, _| unsafe { value_force(c, st.0, self.0) })?;577 // fn force(&mut self, st: &mut EvalState) -> Result<()> {579 Ok(())578 // with_default_context(|c, _| unsafe { value_force(c, st.0, self.0) })?;580 }579 // Ok(())580 // }581 pub fn type_of(&self) -> Result<NixType> {581 pub fn type_of(&self) -> NixType {582 let ty = with_default_context(|c, _| unsafe { nix_raw::get_type(c, self.0) })?;582 let ty = with_default_context(|c, _| unsafe { nix_raw::get_type(c, self.0) })583 .expect("get_type should not fail");583 Ok(NixType::from_int(ty))584 NixType::from_int(ty)584 }585 }585 pub fn to_string(&self) -> Result<String> {586 pub fn to_string(&self) -> Result<String> {586 Ok(self.to_realised_string()?.as_str().to_owned())587 Ok(self.to_realised_string()?.as_str().to_owned())608 // nix_raw::real609 // nix_raw::real609 // }610 // }610 pub fn list_fields(&self) -> Result<Vec<String>> {611 pub fn list_fields(&self) -> Result<Vec<String>> {611 if !matches!(self.type_of()?, NixType::Attrs) {612 if !matches!(self.type_of(), NixType::Attrs) {612 bail!("invalid type: expected attrs");613 bail!("invalid type: expected attrs");613 }614 }614615625 Ok(out)626 Ok(out)626 }627 }627 pub fn get_elem(&self, v: usize) -> Result<Self> {628 pub fn get_elem(&self, v: usize) -> Result<Self> {628 if !matches!(self.type_of()?, NixType::List) {629 if !matches!(self.type_of(), NixType::List) {629 bail!("invalid type: expected list");630 bail!("invalid type: expected list");630 }631 }631 let len =632 let len =659 for f in b_fields.iter() {660 for f in b_fields.iter() {660 out.insert(f.as_str(), other.get_field(f)?);661 out.insert(f.as_str(), other.get_field(f)?);661 }662 }662 Self::new_attrs(out)663 Ok(Self::new_attrs(out))663 }664 }664 pub fn get_field(&self, name: impl AsFieldName) -> Result<Self> {665 pub fn get_field(&self, name: impl AsFieldName) -> Result<Self> {665 if !matches!(self.type_of()?, NixType::Attrs) {666 if !matches!(self.type_of(), NixType::Attrs) {666 bail!("invalid type: expected attrs");667 bail!("invalid type: expected attrs");667 }668 }668669675 .with_context(|| format!("getting field {:?}", name.to_field_name()))676 .with_context(|| format!("getting field {:?}", name.to_field_name()))676 }677 }677 pub fn call(&self, v: Value) -> Result<Self> {678 pub fn call(&self, v: Value) -> Result<Self> {679 let kind = self680 .functor_kind()681 .ok_or_else(|| anyhow!("can only call function or functor"))?;682683 let function = match kind {684 FunctorKind::Function => self.clone(),685 FunctorKind::Functor => {686 let f = self.get_field("__functor")?;678 if !matches!(self.type_of()?, NixType::Function) {687 assert_eq!(679 // TODO: Functors688 f.type_of(),689 NixType::Function,690 "invalid functor encountered"691 );692 f693 }680 bail!("invalid type: expected function");694 };681 }682695683 let out = Value::new_uninit()?;696 let out = Value::new_uninit();684 with_default_context(|c, es| unsafe { nix_raw::value_call(c, es, self.0, v.0, out.0) })?;697 with_default_context(|c, es| unsafe {698 nix_raw::value_call(c, es, function.0, v.0, out.0)699 })?;685700686 Ok(out)701 Ok(out)687 }702 }688 pub fn eval(v: &str) -> Result<Self> {703 pub fn eval(v: &str) -> Result<Self> {689 let s = CString::new(v).expect("expression shouldn't have internal NULs");704 let s = CString::new(v).expect("expression shouldn't have internal NULs");690 let out = Self::new_uninit()?;705 let out = Self::new_uninit();691 with_default_context(|c, es| unsafe {706 with_default_context(|c, es| unsafe {692 expr_eval_from_string(c, es, s.as_ptr(), c"/homeless-shelter".as_ptr(), out.0)707 expr_eval_from_string(c, es, s.as_ptr(), c"/homeless-shelter".as_ptr(), out.0)693 })?;708 })?;723 }738 }724739725 // Convert to string/evaluate derivations/etc740 // Convert to string/evaluate derivations/etc726 fn to_string_weak(&self) -> Result<String> {741 // fn to_string_weak(&self) -> Result<String> {727 // TODO742 // // TODO: For now, it works exactly like to_string, see the comment for fn force()728 self.to_string()743 // self.to_string()729 }744 // }730745731 fn is_derivation(&self) -> bool {746 fn is_derivation(&self) -> bool {732 if !matches!(self.type_of(), Ok(NixType::Attrs)) {747 if !matches!(self.type_of(), NixType::Attrs) {733 return false;748 return false;734 }749 }735 let Some(ty) = self.get_field("type").ok() else {750 let Some(ty) = self.get_field("type").ok() else {736 return false;751 return false;737 };752 };738 matches!(ty.to_string().as_deref(), Ok("derivation"))753 matches!(ty.to_string().as_deref(), Ok("derivation"))739 }754 }755 fn functor_kind(&self) -> Option<FunctorKind> {756 match self.type_of() {757 NixType::Attrs => self758 .has_field("__functor")759 .expect("has_field shouldn't fail for attrs")760 .then_some(FunctorKind::Functor),761 NixType::Function => Some(FunctorKind::Function),762 _ => None,763 }764 }765 pub fn is_function(&self) -> bool {766 self.functor_kind().is_some()767 }740}768}741769742impl From<String> for Value {770impl From<String> for Value {743 fn from(value: String) -> Self {771 fn from(value: String) -> Self {744 Value::new_str(&value).expect("todo: TryFrom")772 Value::new_str(&value)745 }773 }746}774}747impl From<bool> for Value {775impl From<bool> for Value {748 fn from(value: bool) -> Self {776 fn from(value: bool) -> Self {749 Value::new_bool(value).expect("todo: TryFrom")777 Value::new_bool(value)750 }778 }751}779}752impl From<&str> for Value {780impl From<&str> for Value {753 fn from(value: &str) -> Self {781 fn from(value: &str) -> Self {754 Value::new_str(&value).expect("todo: TryFrom")782 Value::new_str(value)755 }783 }756}784}757impl<T> From<Vec<T>> for Value785impl<T> From<Vec<T>> for Value758where786where759 T: Into<Value>,787 T: Into<Value>,760{788{761 fn from(value: Vec<T>) -> Self {789 fn from(value: Vec<T>) -> Self {762 Value::new_list(value).expect("todo: TryFrom")790 Value::new_list(value)763 }791 }764}792}765793793821794#[test_log::test]822#[test_log::test]795fn test_native() -> Result<()> {823fn test_native() -> Result<()> {824 init_libraries();825796 let mut fetch_settings = FetchSettings::new();826 let mut fetch_settings = FetchSettings::new();797 fetch_settings.set(c"warn-dirty", c"false");827 fetch_settings.set(c"warn-dirty", c"false");798 //828799829 let manifest = format!("{}/../../", env!("CARGO_MANIFEST_DIR"));800 let (mut r, _) = FlakeReference::new("/home/lach/build/fleet", &fetch_settings)?;830 let (mut r, _) = FlakeReference::new(&manifest, &fetch_settings)?;801 let locked = r.lock(&fetch_settings)?;831 let locked = r.lock(&fetch_settings)?;802 let attrs = locked.get_attrs(&mut FlakeSettings::new()?)?;832 let attrs = locked.get_attrs(&mut FlakeSettings::new()?)?;803833804 let builtins = Value::eval("builtins")?;834 let builtins = Value::eval("builtins")?;805 dbg!(builtins.type_of()?);835 assert_eq!(builtins.type_of(), NixType::Attrs);806836807 dbg!(attrs.type_of()?);837 assert_eq!(attrs.type_of(), NixType::Attrs);808 dbg!(attrs.list_fields()?);838 let test_data = nix_go!(attrs.testData);839809 dbg!(840 let test_string: String = nix_go_json!(test_data.testString);810 attrs841 assert_eq!(test_string, "hello");811 .get_field("packages")?812 .get_field("x86_64-linux")?813 .get_field("fleet")?814 .get_field("outPath")?815 .to_string()816 );817842818 Ok(())843 Ok(())819}844}820821// struct NixBuildTask(Value, oneshot::Sender<Result<HashMap<String, PathBuf>>>);822//823// #[derive(Clone)]824// pub struct NixBuildBatch {825// tx: mpsc::UnboundedSender<NixBuildTask>,826// }827//828// #[instrument(skip(values))]829// async fn build_multiple(name: String, values: Vec<Value>) -> Result<()> {830// let builtins = Value::eval("builtins")?;831// let drv = nix_go!(builtins.derivation(Obj {832// // FIXME: pass system from localSystem or fleet args833// // system,834// name,835// builder: "/bin/sh",836// // we want nothing from this derivation, it is only used to perform multiple builds at once.837// args: vec!["-c", "echo > $out"],838// preferLocalBuild: true,839// allowSubstitutes: false,840// buildInputs: values,841// }));842// drv.build()?;843// Ok(())844// }845//846// impl NixBuildBatch {847// fn new(name: String) -> Self {848// let (tx, mut rx) = mpsc::unbounded_channel::<NixBuildTask>();849//850// tokio::task::spawn(async move {851// let mut deps = vec![];852// let mut build_data = vec![];853// while let Some(task) = rx.recv().await {854// build_data.push(task.0.clone());855// deps.push(task);856// }857// if deps.is_empty() {858// return;859// }860// match build_multiple(name, build_data).await {861// Ok(_) => {862// for NixBuildTask(v, o) in deps {863// let _ = o.send(v.build());864// }865// }866// Err(e) => {867// for NixBuildTask(v, o) in deps {868// let s = v.to_string_weak();869// let s = match s {870// Ok(s) => s,871// Err(e) => {872// let _ = o.send(Err(e));873// continue;874// }875// };876// if PathBuf::from(s).exists() {877// let _ = o.send(v.build());878// } else {879// let _ = o.send(Err(e.clone()));880// }881// }882// }883// };884// });885// Self { tx }886// }887// pub async fn submit(self, task: Value) -> Result<HashMap<String, PathBuf>> {888// let Self { tx: task_tx } = self;889// let (tx, rx) = oneshot::channel();890// let _ = task_tx.send(NixBuildTask(task, tx));891// drop(task_tx);892// rx.await.expect("shoudn't be cancelled here")893// }894// }895845crates/nix-eval/src/logging.rsdiffbeforeafterboth3use std::sync::{LazyLock, Mutex};3use std::sync::{LazyLock, Mutex};445use tracing::{5use tracing::{6 Level, Metadata, Span, debug, debug_span, error, error_span, event, info, info_span, trace,6 Level, Span, debug, debug_span, error, error_span, info, info_span, trace, trace_span, warn,7 trace_span, warn, warn_span,7 warn_span,8};8};9#[cfg(feature = "indicatif")]9use tracing_indicatif::span_ext::IndicatifSpanExt as _;10use tracing_indicatif::span_ext::IndicatifSpanExt as _;101111#[derive(Debug)]12#[derive(Debug)]31}32}323333fn parse_path(path: &str) -> &str {34fn parse_path(path: &str) -> &str {34 let path = strip_prefix_suffix(path, "\x1b[35;1m", "\x1b[0m").unwrap_or(path);35 strip_prefix_suffix(path, "\x1b[35;1m", "\x1b[0m").unwrap_or(path)35 path36}36}373738fn parse_drv(drv: &str) -> &str {38fn parse_drv(drv: &str) -> &str {245 Debug,245 Debug,246 Vomit,246 Vomit,247}247}248impl Into<tracing::Level> for Verbosity {248impl From<Verbosity> for tracing::Level {249 fn into(self) -> tracing::Level {249 fn from(val: Verbosity) -> Self {250 match self {250 match val {251 Verbosity::Error => Level::ERROR,251 Verbosity::Error => Level::ERROR,252 Verbosity::Warn => Level::WARN,252 Verbosity::Warn => Level::WARN,253 Verbosity::Notice => Level::WARN,253 Verbosity::Notice => Level::WARN,280 }280 }281}281}282282283#[derive(Hash, PartialEq, Eq, Clone, Copy)]284enum MetadataKind {285 Span,286 Event,287}288// impl MetadataKind {289// fn kind(&self) -> Kind {290// match self {291// MetadataKind::Span => Kind::SPAN,292// MetadataKind::Event => Kind::EVENT,293// }294// }295// }296297#[derive(Hash, PartialEq, Eq)]298struct ForeignMetadataInfo {299 target: &'static str,300 level: Level,301 kind: MetadataKind,302 name: &'static str,303 module: Option<&'static str>,304 file: Option<&'static str>,305 line: Option<u32>,306 names: &'static [&'static str],307}308309struct FakeCallsite;310impl tracing::callsite::Callsite for FakeCallsite {311 fn set_interest(&self, interest: tracing::subscriber::Interest) {312 unreachable!()313 }314315 fn metadata(&self) -> &Metadata<'_> {316 unreachable!()317 }318}319const FAKE_CALLSITE: FakeCallsite = FakeCallsite;320321#[cfg(false)]322#[derive(Default)]323struct ForeignSpanData {324 interned: HashSet<&'static str>,325 metadatas: HashMap<ForeignMetadataInfo, &'static Metadata<'static>>,326}327#[cfg(false)]328impl ForeignSpanData {329 fn intern(&mut self, s: &str) -> &'static str {330 if let Some(v) = self.interned.get(s) {331 return *v;332 }333 let leaked: Box<str> = s.into();334 let leaked = Box::leak(leaked);335 self.interned.insert(leaked);336 return leaked;337 }338 fn alloc_metadata<'t>(339 &'t mut self,340 target: &'static str,341 level: Level,342 kind: MetadataKind,343 name: &'static str,344 module: Option<&'static str>,345 file: Option<&'static str>,346 line: Option<u32>,347 names: &'static [&'static str],348 ) -> &'static Metadata<'static> {349 let info = ForeignMetadataInfo {350 target,351 level,352 kind,353 name,354 module,355 file,356 line,357 names,358 };359 if let Some(v) = self.metadatas.get(&info) {360 return *v;361 }362 let fake = FakeCallsite;363 let metadata = Box::leak::<'static>(Box::new(Metadata::new(364 name,365 target,366 level,367 file,368 line,369 module,370 FieldSet::new(names, tracing::callsite::Identifier(&FAKE_CALLSITE)),371 kind.kind(),372 )));373374 let meta_raw = &raw const *metadata;375 let fields_raw = &raw const *metadata.fields();376377 // SAFETY: FieldSet struct should be inside of metadata struct... Which we assume here, but do not test378 // FIXME: Safety comment above might be invalidated at any time, this should actually be covered by unit test (or, better: runtime assertion... Somehow.)379 let fields_offset = unsafe { fields_raw.cast::<u8>().offset_from(meta_raw.cast()) };380 let field_set = unsafe {381 ((&raw mut *metadata).cast::<()>())382 .byte_offset(fields_offset)383 .cast::<FieldSet>()384 };385 // FIXME: metadata borrow here invalidates our &mut borrow of 'static Metadata, and 'static FieldSet so this construction should be replaced with raw pointers or idk.386 // Something should be better done inside of tracing crate itself, someting like interior mutability.387 let callsite = Box::leak(Box::new(tracing::callsite::DefaultCallsite::new(metadata)));388 unsafe { *field_set = FieldSet::new(names, tracing::callsite::Identifier(callsite)) };389390 tracing::callsite::register(&*callsite);391392 self.metadatas.insert(info, metadata);393 return metadata;394 }395}396397#[cfg(false)]398static FOREIGN_SPAN_DATA: LazyLock<Mutex<ForeignSpanData>> =399 LazyLock::new(|| Mutex::new(ForeignSpanData::default()));400static NIX_SPAN_MAPPING: LazyLock<Mutex<HashMap<u64, Span>>> =283static NIX_SPAN_MAPPING: LazyLock<Mutex<HashMap<u64, Span>>> =401 LazyLock::new(|| Mutex::new(HashMap::new()));284 LazyLock::new(|| Mutex::new(HashMap::new()));402285491 }374 }492 };375 };493 if !s.trim().is_empty() {376 if !s.trim().is_empty() {377 #[cfg(feature = "indicatif")]378 {494 span.pb_set_message(s);379 span.pb_set_message(s);380 }495 let _e = span.enter();381 let _e = span.enter();496 let level: Level = self.verbosity.into();382 let level: Level = self.verbosity.into();497 if level == Level::ERROR {383 if level == Level::ERROR {506 trace!(target: "nix", "{}", s)392 trace!(target: "nix", "{}", s)507 }393 }508 } else {394 } else {395 #[cfg(feature = "indicatif")]396 {509 span.pb_start();397 span.pb_start();510 }398 }399 }511 mapping.insert(self.activity_id, span);400 mapping.insert(self.activity_id, span);512 }401 }513 fn emit_result(&mut self, ty: u32) {402 fn emit_result(&mut self, ty: u32) {514 let mut mapping = NIX_SPAN_MAPPING.lock().expect("not poisoned");403 let mapping = NIX_SPAN_MAPPING.lock().expect("not poisoned");515404516 let Some(parent) = mapping.get(&self.activity_id) else {405 let Some(parent) = mapping.get(&self.activity_id) else {517 panic!("unexpected result for dead parent");406 panic!("unexpected result for dead parent");536 // parent.pb_set_message(phase);425 // parent.pb_set_message(phase);537 debug!(target: "nix::phase", phase)426 debug!(target: "nix::phase", phase)538 }427 }539 (ResultType::Progress, [Int(done), Int(expected), Int(_), Int(_)]) => {428 (ResultType::Progress, [Int(_done), Int(_expected), Int(_), Int(_)]) => {540 parent.pb_set_length(*expected as u64);429 #[cfg(feature = "indicatif")]430 {431 parent.pb_set_length(*_expected as u64);541 parent.pb_set_position(*done as u64);432 parent.pb_set_position(*_done as u64);433 }542 }434 }543 _ => warn!("unknown progress report: {:?}({:?})", &res, &self.fields),435 _ => warn!("unknown progress report: {:?}({:?})", &res, &self.fields),544 }436 }576 }468 }577}469}578579// fn start_activity(act: u64, lvl: u32, act_ty: u32, s: &str, parent: u32) {580// tracing::Span::new(meta, values)581// }582470583#[cxx::bridge]471#[cxx::bridge]584pub mod nix_logging_cxx {472pub mod nix_logging_cxx {crates/nix-eval/src/macros.rsdiffbeforeafterboth20 use $crate::{nix_expr_inner};20 use $crate::{nix_expr_inner};21 let mut out = std::collections::hash_map::HashMap::new();21 let mut out = std::collections::hash_map::HashMap::new();22 nix_expr_inner!(@obj(out) $($tt)*);22 nix_expr_inner!(@obj(out) $($tt)*);23 Value::new_attrs(out)?23 Value::new_attrs(out)24 }};24 }};25 (@field($o:ident) . $var:ident $($tt:tt)*) => {{25 (@field($o:ident) . $var:ident $($tt:tt)*) => {{26 $o.index_attr(stringify!($var));26 $o.index_attr(stringify!($var));crates/nix-eval/src/value.rsdiffbeforeafterbothno changes
flake.lockdiffbeforeafterboth104 "nixpkgs-regression": "nixpkgs-regression"104 "nixpkgs-regression": "nixpkgs-regression"105 },105 },106 "locked": {106 "locked": {107 "lastModified": 1756860322,107 "lastModified": 1757000273,108 "narHash": "sha256-mT01CpWVdqSm79L270dSkjdYbdc37r+Hq9vk4GTp7Ao=",108 "narHash": "sha256-9AKhwsSlegWnNFy8++OMNctrxJUUIE7nG4s4ZHmFPic=",109 "owner": "deltarocks",109 "path": "/home/lach/build/nix-src",110 "repo": "nix",111 "rev": "eba1f549ec21208cf98343f1351a95e2e6eb3fbb",110 "type": "path"112 "type": "github"111 },113 },112 "original": {114 "original": {115 "owner": "deltarocks",116 "ref": "fleet",113 "path": "/home/lach/build/nix-src",117 "repo": "nix",114 "type": "path"118 "type": "github"115 }119 }116 },120 },117 "nixpkgs": {121 "nixpkgs": {flake.nixdiffbeforeafterboth19 };19 };20 # DeterminateSystem's nix fork is controversial, but I don't mind it,20 # DeterminateSystem's nix fork is controversial, but I don't mind it,21 # and it has lazy-trees support which is useful for fleet.21 # and it has lazy-trees support which is useful for fleet.22 nix.url = "/home/lach/build/nix-src";22 nix.url = "github:deltarocks/nix/fleet";23 };23 };24 outputs =24 outputs =25 inputs:25 inputs:444445 fleetModules.tf = ./modules/extras/tf.nix;45 fleetModules.tf = ./modules/extras/tf.nix;464647 # Used to test nix-eval bindings48 testData = {47 testObj = {49 testObj = {48 v = "Hello";50 v = "Hello";49 };51 };50 testString = "hello";52 testString = "hello";53 };515452 # To be used with https://github.com/NixOS/nix/pull/889255 # To be used with https://github.com/NixOS/nix/pull/889253 schemas =56 schemas =