git.delta.rocks / jrsonnet / refs/commits / d70668683ed2

difftreelog

fix primop registration

ynqztwqxYaroslav Bolyukin2026-01-06parent: #98314cf.patch.diff
in: trunk

5 files changed

modifiedcrates/fleet-base/src/opts.rsdiffbeforeafterboth
before · crates/fleet-base/src/opts.rs
1use std::{2	collections::BTreeMap,3	env::current_dir,4	ffi::OsString,5	str::FromStr,6	sync::{Arc, Mutex},7};89use anyhow::{Context, Result, bail};10use nix_eval::{11	FetchSettings, FlakeLockFlags, FlakeReference, FlakeReferenceParseFlags, FlakeSettings, Value,12	gc_now, nix_go, util::assert_warn,13};14use nom::{15	Parser,16	bytes::complete::take_while1,17	character::complete::char,18	combinator::{map, opt},19	multi::separated_list1,20	sequence::{preceded, separated_pair},21};2223use crate::{24	fleetdata::FleetData,25	host::{Config, ConfigHost, FleetConfigInternals},26};2728#[derive(Clone)]29pub enum HostItem {30	Host {31		name: String,32		attrs: BTreeMap<String, String>,33	},34	Tag {35		name: String,36		attrs: BTreeMap<String, String>,37	},38}39fn host_item_parser(input: &str) -> Result<HostItem, String> {40	fn err_to_string(err: nom::Err<nom::error::Error<&str>>) -> String {41		err.to_string()42	}4344	let (input, is_tag) = map(opt(char('@')), |c| c.is_some())45		.parse_complete(input)46		.map_err(err_to_string)?;47	let (input, name) = map(48		take_while1(|v| v != ',' && v != '?' && v != '@'),49		str::to_owned,50	)51	.parse_complete(input)52	.map_err(err_to_string)?;5354	let kw_item = separated_pair(55		map(take_while1(|v| v != '&' && v != '='), str::to_owned),56		char('='),57		map(take_while1(|v| v != '&'), str::to_owned),58	);59	let kw = map(separated_list1(char('&'), kw_item), |vec| {60		vec.into_iter().collect::<BTreeMap<_, _>>()61	});62	let mut opt_kw = map(opt(preceded(char('?'), kw)), Option::unwrap_or_default);6364	let (input, attrs) = opt_kw.parse_complete(input).map_err(err_to_string)?;6566	if !input.is_empty() {67		return Err(format!("unexpected trailing input: {input:?}"));68	}69	Ok(if is_tag {70		HostItem::Tag { name, attrs }71	} else {72		HostItem::Host { name, attrs }73	})74}7576// TODO: Rename to HostSelector77#[derive(clap::Parser, Clone)]78pub struct FleetOpts {79	/// All hosts except those would be skipped80	#[clap(long, number_of_values = 1, value_parser = host_item_parser)]81	pub only: Vec<HostItem>,8283	/// Hosts to skip84	#[clap(long, number_of_values = 1)]85	pub skip: Vec<String>,8687	/// Host, which should be threaten as current machine88	// TODO: Replace with connectivity refactor89	#[clap(long, default_value_t = hostname::get().expect("unknown hostname").to_str().expect("hostname is not utf-8").to_owned())]90	pub localhost: String,9192	/// Override detected system for host, to perform builds via93	/// binfmt-declared qemu instead of trying to crosscompile94	#[clap(long, default_value = env!("NIX_SYSTEM"))]95	pub local_system: String,9697	/// By default fleet continues on single derivation build failure98	/// this flag makes command fail immediately99	///100	/// Opposite of Nix's --keep-going101	#[clap(long)]102	pub fail_fast: bool,103}104105impl FleetOpts {106	pub async fn filter_skipped(107		&self,108		hosts: impl IntoIterator<Item = ConfigHost>,109	) -> Result<Vec<ConfigHost>> {110		let mut out = Vec::new();111		for host in hosts {112			if self.should_skip(&host).await? {113				continue;114			}115			out.push(host);116		}117		Ok(out)118	}119	pub async fn should_skip(&self, host: &ConfigHost) -> Result<bool> {120		if self.skip.iter().any(|h| h as &str == host.name) {121			return Ok(true);122		}123		if self.only.is_empty() {124			return Ok(false);125		}126		let mut have_group_matches = false;127		for item in self.only.iter() {128			match item {129				HostItem::Host { name, .. } if *name == host.name => {130					return Ok(false);131				}132				HostItem::Tag { .. } => {133					have_group_matches = true;134				}135				_ => {}136			}137		}138		if have_group_matches {139			let host_tags = host.tags().await?;140			for item in self.only.iter() {141				match item {142					HostItem::Tag { name, .. } if host_tags.contains(name) => {143						return Ok(false);144					}145					_ => {}146				}147			}148		}149		Ok(true)150	}151	pub async fn action_attr<T: FromStr>(&self, host: &ConfigHost, attr: &str) -> Result<Option<T>>152	where153		T::Err: Sync,154		anyhow::Error: From<T::Err>,155	{156		let str = self.action_attr_str(host, attr).await?;157		Ok(str.map(|v| T::from_str(&v)).transpose()?)158	}159	pub async fn action_attr_str(&self, host: &ConfigHost, attr: &str) -> Result<Option<String>> {160		if self.only.is_empty() {161			return Ok(None);162		}163		let mut have_group_matches = false;164		for item in self.only.iter() {165			match item {166				HostItem::Host { name, attrs }167					if *name == host.name && attrs.contains_key(attr) =>168				{169					return Ok(attrs.get(attr).cloned());170				}171				HostItem::Tag { attrs, .. } if attrs.contains_key(attr) => {172					have_group_matches = true;173				}174				_ => {}175			}176		}177		if have_group_matches {178			let host_tags = host.tags().await?;179			for item in self.only.iter() {180				match item {181					HostItem::Tag { name, attrs }182						if host_tags.contains(name) && attrs.contains_key(attr) =>183					{184						return Ok(attrs.get(attr).cloned());185					}186					_ => {}187				}188			}189		}190		Ok(None)191	}192	pub fn is_local(&self, host: &str) -> bool {193		self.localhost == host194	}195196	// TODO: Config should be detached from opts.197	pub async fn build(&self, nix_args: Vec<OsString>, assert: bool) -> Result<Config> {198		let cwd = current_dir()?;199		let mut directory = cwd.clone();200		let mut fleet_data_path = directory.join("fleet.nix");201		while !fleet_data_path.is_file() {202			// fleet.nix203			fleet_data_path.pop();204			if !directory.pop() || !fleet_data_path.pop() {205				bail!(206					"fleet.nix not found at {} or any of the parent directories",207					cwd.display()208				);209			}210			fleet_data_path.push("fleet.nix");211		}212		let bytes =213			std::fs::read_to_string(&fleet_data_path).context("reading fleet state (fleet.nix)")?;214		let data = Arc::new(Mutex::new(FleetData::from_str(&bytes)?));215216		let mut fetch_settings = FetchSettings::new();217		fetch_settings.set(c"warn-dirty", c"false");218219		let mut flake_settings = FlakeSettings::new()?;220		let mut parse = FlakeReferenceParseFlags::new(&flake_settings)?;221		// For some reason, lazy trees not being used when there is no base dir set222		parse.set_base_dir("/")?;223224		let (mut flake, _) = FlakeReference::new(225			directory226				.to_str()227				.ok_or_else(|| anyhow::anyhow!("fleet dir should have utf-8 path"))?,228			&flake_settings,229			&parse,230			&fetch_settings,231		)?;232233		let lock = FlakeLockFlags::new(&flake_settings)?;234235		let flake = flake.lock(&fetch_settings, &flake_settings, &lock)?;236237		let flake = flake.get_attrs(&mut flake_settings)?;238239		let builtins_field = Value::eval("builtins")?;240241		let fleet_root = flake.get_field("fleetConfigurations")?;242		let fleet_field = nix_go!(fleet_root.default(Obj {}));243244		let config_field = nix_go!(fleet_field.config);245246		if assert {247			assert_warn("fleet config evaluation", &config_field)248				.await249				.context("failed to verify assertions")?;250		}251252		let import = nix_go!(builtins_field.import);253		let overlays = nix_go!(config_field.nixpkgs.overlays);254		let nixpkgs = nix_go!(config_field.nixpkgs.buildUsing);255		let nixpkgs_imported = nix_go!(import(nixpkgs));256257		let default_pkgs = nix_go!(nixpkgs_imported(Obj {258			overlays,259			system: self.local_system.clone(),260		}));261262		if cfg!(debug_assertions) {263			gc_now();264		}265266		Ok(Config(Arc::new(FleetConfigInternals {267			directory,268			data,269			flake_outputs: flake,270			local_system: self.local_system.clone(),271			nix_args,272			config_field,273			default_pkgs,274			nixpkgs,275			localhost: self.localhost.to_owned(),276		})))277	}278}
after · crates/fleet-base/src/opts.rs
1use std::{2	collections::BTreeMap,3	env::current_dir,4	ffi::OsString,5	str::FromStr,6	sync::{Arc, Mutex},7};89use anyhow::{Context, Result, bail};10use nix_eval::{11	FetchSettings, FlakeLockFlags, FlakeReference, FlakeReferenceParseFlags, FlakeSettings, Value,12	gc_now, nix_go, util::assert_warn,13};14use nom::{15	Parser,16	bytes::complete::take_while1,17	character::complete::char,18	combinator::{map, opt},19	multi::separated_list1,20	sequence::{preceded, separated_pair},21};2223use crate::{24	fleetdata::FleetData,25	host::{Config, ConfigHost, FleetConfigInternals}, primops::init_primops,26};2728#[derive(Clone)]29pub enum HostItem {30	Host {31		name: String,32		attrs: BTreeMap<String, String>,33	},34	Tag {35		name: String,36		attrs: BTreeMap<String, String>,37	},38}39fn host_item_parser(input: &str) -> Result<HostItem, String> {40	fn err_to_string(err: nom::Err<nom::error::Error<&str>>) -> String {41		err.to_string()42	}4344	let (input, is_tag) = map(opt(char('@')), |c| c.is_some())45		.parse_complete(input)46		.map_err(err_to_string)?;47	let (input, name) = map(48		take_while1(|v| v != ',' && v != '?' && v != '@'),49		str::to_owned,50	)51	.parse_complete(input)52	.map_err(err_to_string)?;5354	let kw_item = separated_pair(55		map(take_while1(|v| v != '&' && v != '='), str::to_owned),56		char('='),57		map(take_while1(|v| v != '&'), str::to_owned),58	);59	let kw = map(separated_list1(char('&'), kw_item), |vec| {60		vec.into_iter().collect::<BTreeMap<_, _>>()61	});62	let mut opt_kw = map(opt(preceded(char('?'), kw)), Option::unwrap_or_default);6364	let (input, attrs) = opt_kw.parse_complete(input).map_err(err_to_string)?;6566	if !input.is_empty() {67		return Err(format!("unexpected trailing input: {input:?}"));68	}69	Ok(if is_tag {70		HostItem::Tag { name, attrs }71	} else {72		HostItem::Host { name, attrs }73	})74}7576// TODO: Rename to HostSelector77#[derive(clap::Parser, Clone)]78pub struct FleetOpts {79	/// All hosts except those would be skipped80	#[clap(long, number_of_values = 1, value_parser = host_item_parser)]81	pub only: Vec<HostItem>,8283	/// Hosts to skip84	#[clap(long, number_of_values = 1)]85	pub skip: Vec<String>,8687	/// Host, which should be threaten as current machine88	// TODO: Replace with connectivity refactor89	#[clap(long, default_value_t = hostname::get().expect("unknown hostname").to_str().expect("hostname is not utf-8").to_owned())]90	pub localhost: String,9192	/// Override detected system for host, to perform builds via93	/// binfmt-declared qemu instead of trying to crosscompile94	#[clap(long, default_value = env!("NIX_SYSTEM"))]95	pub local_system: String,9697	/// By default fleet continues on single derivation build failure98	/// this flag makes command fail immediately99	///100	/// Opposite of Nix's --keep-going101	#[clap(long)]102	pub fail_fast: bool,103}104105impl FleetOpts {106	pub async fn filter_skipped(107		&self,108		hosts: impl IntoIterator<Item = ConfigHost>,109	) -> Result<Vec<ConfigHost>> {110		let mut out = Vec::new();111		for host in hosts {112			if self.should_skip(&host).await? {113				continue;114			}115			out.push(host);116		}117		Ok(out)118	}119	pub async fn should_skip(&self, host: &ConfigHost) -> Result<bool> {120		if self.skip.iter().any(|h| h as &str == host.name) {121			return Ok(true);122		}123		if self.only.is_empty() {124			return Ok(false);125		}126		let mut have_group_matches = false;127		for item in self.only.iter() {128			match item {129				HostItem::Host { name, .. } if *name == host.name => {130					return Ok(false);131				}132				HostItem::Tag { .. } => {133					have_group_matches = true;134				}135				_ => {}136			}137		}138		if have_group_matches {139			let host_tags = host.tags().await?;140			for item in self.only.iter() {141				match item {142					HostItem::Tag { name, .. } if host_tags.contains(name) => {143						return Ok(false);144					}145					_ => {}146				}147			}148		}149		Ok(true)150	}151	pub async fn action_attr<T: FromStr>(&self, host: &ConfigHost, attr: &str) -> Result<Option<T>>152	where153		T::Err: Sync,154		anyhow::Error: From<T::Err>,155	{156		let str = self.action_attr_str(host, attr).await?;157		Ok(str.map(|v| T::from_str(&v)).transpose()?)158	}159	pub async fn action_attr_str(&self, host: &ConfigHost, attr: &str) -> Result<Option<String>> {160		if self.only.is_empty() {161			return Ok(None);162		}163		let mut have_group_matches = false;164		for item in self.only.iter() {165			match item {166				HostItem::Host { name, attrs }167					if *name == host.name && attrs.contains_key(attr) =>168				{169					return Ok(attrs.get(attr).cloned());170				}171				HostItem::Tag { attrs, .. } if attrs.contains_key(attr) => {172					have_group_matches = true;173				}174				_ => {}175			}176		}177		if have_group_matches {178			let host_tags = host.tags().await?;179			for item in self.only.iter() {180				match item {181					HostItem::Tag { name, attrs }182						if host_tags.contains(name) && attrs.contains_key(attr) =>183					{184						return Ok(attrs.get(attr).cloned());185					}186					_ => {}187				}188			}189		}190		Ok(None)191	}192	pub fn is_local(&self, host: &str) -> bool {193		self.localhost == host194	}195196	// TODO: Config should be detached from opts.197	pub async fn build(&self, nix_args: Vec<OsString>, assert: bool) -> Result<Config> {198		let cwd = current_dir()?;199		let mut directory = cwd.clone();200		let mut fleet_data_path = directory.join("fleet.nix");201		while !fleet_data_path.is_file() {202			// fleet.nix203			fleet_data_path.pop();204			if !directory.pop() || !fleet_data_path.pop() {205				bail!(206					"fleet.nix not found at {} or any of the parent directories",207					cwd.display()208				);209			}210			fleet_data_path.push("fleet.nix");211		}212		let bytes =213			std::fs::read_to_string(&fleet_data_path).context("reading fleet state (fleet.nix)")?;214		let data = Arc::new(Mutex::new(FleetData::from_str(&bytes)?));215216		init_primops(data.clone());217218		let mut fetch_settings = FetchSettings::new();219		fetch_settings.set(c"warn-dirty", c"false");220221		let mut flake_settings = FlakeSettings::new()?;222		let mut parse = FlakeReferenceParseFlags::new(&flake_settings)?;223		// For some reason, lazy trees not being used when there is no base dir set224		parse.set_base_dir("/")?;225226		let (mut flake, _) = FlakeReference::new(227			directory228				.to_str()229				.ok_or_else(|| anyhow::anyhow!("fleet dir should have utf-8 path"))?,230			&flake_settings,231			&parse,232			&fetch_settings,233		)?;234235		let lock = FlakeLockFlags::new(&flake_settings)?;236237		let flake = flake.lock(&fetch_settings, &flake_settings, &lock)?;238239		let flake = flake.get_attrs(&mut flake_settings)?;240241		let builtins_field = Value::eval("builtins")?;242243		let fleet_root = flake.get_field("fleetConfigurations")?;244		let fleet_field = nix_go!(fleet_root.default(Obj {}));245246		let config_field = nix_go!(fleet_field.config);247248		if assert {249			assert_warn("fleet config evaluation", &config_field)250				.await251				.context("failed to verify assertions")?;252		}253254		let import = nix_go!(builtins_field.import);255		let overlays = nix_go!(config_field.nixpkgs.overlays);256		let nixpkgs = nix_go!(config_field.nixpkgs.buildUsing);257		let nixpkgs_imported = nix_go!(import(nixpkgs));258259		let default_pkgs = nix_go!(nixpkgs_imported(Obj {260			overlays,261			system: self.local_system.clone(),262		}));263264		if cfg!(debug_assertions) {265			gc_now();266		}267268		Ok(Config(Arc::new(FleetConfigInternals {269			directory,270			data,271			flake_outputs: flake,272			local_system: self.local_system.clone(),273			nix_args,274			config_field,275			default_pkgs,276			nixpkgs,277			localhost: self.localhost.to_owned(),278		})))279	}280}
modifiedcrates/fleet-base/src/primops.rsdiffbeforeafterboth
--- a/crates/fleet-base/src/primops.rs
+++ b/crates/fleet-base/src/primops.rs
@@ -2,6 +2,7 @@
 use std::sync::{Arc, Mutex};
 
 use nix_eval::{NativeFn, Value};
+use tracing::info;
 
 use crate::fleetdata::{FleetData, FleetSecrets};
 
@@ -23,21 +24,9 @@
 struct FsSecretsBackend {}
 
 pub fn init_primops(secrets: Arc<Mutex<FleetData>>) {
-	NativeFn::new(
-		c"fleet_ensure_host_secret",
-		c"Ensure secret existence for a host, regenerating it in case of some mismatch",
-		[c"host", c"secret", c"generator"],
-		|[host, secret, generator]| {
-			todo!("ensure secret");
-			Ok(Value::new_attrs(HashMap::from_iter([(
-				"raw",
-				Value::new_str("rawData"),
-			)])))
-		},
-	)
-	.register();
+	info!("initializing primops");
 	NativeFn::new(
-		c"fleet_ensure_host_secret",
+		c"__fleetEnsureHostSecret",
 		c"Ensure secret existence for a host, regenerating it in case of some mismatch",
 		[c"host", c"secret", c"generator"],
 		|[host, secret, generator]| {
modifiedcrates/nix-eval/src/lib.rsdiffbeforeafterboth
--- a/crates/nix-eval/src/lib.rs
+++ b/crates/nix-eval/src/lib.rs
@@ -966,9 +966,9 @@
 		let mut args = args.into_iter().map(|v| v.as_ptr()).collect_vec();
 		args.push(null());
 		let args = args.as_mut_ptr();
-		let primop = with_default_context(|c, _| unsafe {
+		let primop = unsafe {
 			alloc_primop(
-				c,
+				null_mut(),
 				f,
 				N as i32,
 				name.as_ptr(),
@@ -976,14 +976,14 @@
 				doc.as_ptr(),
 				Box::into_raw(closure).cast(),
 			)
-		})
-		.expect("primop allocation should not fail");
+		};
+
+		assert!(!primop.is_null(), "primop allocation should not fail");
 
 		Self(primop)
 	}
 	pub fn register(self) {
-		with_default_context(|c, _| unsafe { register_primop(c, self.0) })
-			.expect("primop registration should not fail");
+		unsafe { register_primop(null_mut(), self.0) };
 	}
 }
 
@@ -999,6 +999,17 @@
 #[test_log::test]
 fn test_native() -> Result<()> {
 	init_libraries();
+	NativeFn::new(
+		c"__uppercaseSuffix2",
+		c"make string uppercase and add suffix",
+		[c"str", c"suffix"],
+		|[str, suffix]: [&Value; 2]| {
+			let str = str.to_string()?;
+			let suffix = suffix.to_string()?;
+			Ok(Value::new_str(&format!("{}{suffix}", str.to_uppercase())))
+		},
+	)
+	.register();
 
 	let mut fetch_settings = FetchSettings::new();
 	fetch_settings.set(c"warn-dirty", c"false");
@@ -1036,6 +1047,8 @@
 
 	let test_result: String = nix_go_json!(test_data.testPrimop(uppercase_suffix));
 	assert_eq!(test_result, "PREFIX_BODY_SUFFIX");
+	let test_result: String = nix_go_json!(builtins.uppercaseSuffix2("test")("suffix"));
+	assert_eq!(test_result, "TESTsuffix");
 
 	let nix_ctx = NixContext::new();
 	let store = GLOBAL_STATE.store.parse_path(s.as_c_str())?;
modifiedcrates/nix-eval/src/macros.rsdiffbeforeafterboth
--- a/crates/nix-eval/src/macros.rs
+++ b/crates/nix-eval/src/macros.rs
@@ -47,11 +47,7 @@
 		nix_expr_inner!(@field(out) $($tt)*);
 		out
 	}};
-	($v:literal) => {{
-		use $crate::macros::NixExprBuilder;
-		NixExprBuilder::string($v)
-	}};
-	({$v:expr}) => {{
+	($v:expr) => {{
 		$crate::Value::serialized(&$v)?
 	}}
 }
modifiedmodules/nixos/secrets.nixdiffbeforeafterboth
--- a/modules/nixos/secrets.nix
+++ b/modules/nixos/secrets.nix
@@ -103,7 +103,7 @@
         };
       };
       config = {
-        parts = builtins.fleet_ensure_host_secret sysConfig.networking.hostName secretName config.generator;
+        parts = builtins.fleetEnsureHostSecret sysConfig.networking.hostName secretName config.generator;
       };
     }
   );