difftreelog
refactor drop unnecessary async/await
in: trunk
15 files changed
cmds/fleet/src/cmds/build_systems.rsdiffbeforeafterboth--- a/cmds/fleet/src/cmds/build_systems.rs
+++ b/cmds/fleet/src/cmds/build_systems.rs
@@ -30,9 +30,9 @@
async fn build_task(config: Config, hostname: String, build_attr: &str) -> Result<PathBuf> {
info!("building");
- let host = config.host(&hostname).await?;
+ let host = config.host(&hostname)?;
// let action = Action::from(self.subcommand.clone());
- let nixos = host.nixos_config().await?;
+ let nixos = host.nixos_config()?;
let drv = nix_go!(nixos.system.build[{ build_attr }]);
let out_output = spawn_blocking(move || drv.build("out"))
.await
@@ -59,7 +59,7 @@
impl BuildSystems {
pub async fn run(self, config: &Config, opts: &FleetOpts) -> Result<()> {
- let hosts = opts.filter_skipped(config.list_hosts().await?).await?;
+ let hosts = opts.filter_skipped(config.list_hosts()?)?;
let set = LocalSet::new();
let build_attr = self.build_attr.clone();
for host in hosts {
@@ -95,20 +95,20 @@
impl Deploy {
pub async fn run(self, config: &Config, opts: &FleetOpts) -> Result<()> {
- let hosts = opts.filter_skipped(config.list_hosts().await?).await?;
+ let hosts = opts.filter_skipped(config.list_hosts()?)?;
let set = LocalSet::new();
for host in hosts.into_iter() {
let config = config.clone();
let span = info_span!("deploy", host = field::display(&host.name));
let hostname = host.name.clone();
let opts = opts.clone();
- if let Some(deploy_kind) = opts.action_attr::<DeployKind>(&host, "deploy_kind").await? {
+ if let Some(deploy_kind) = opts.action_attr::<DeployKind>(&host, "deploy_kind")? {
host.set_deploy_kind(deploy_kind);
};
- if let Some(destination) = opts.action_attr::<String>(&host, "dest").await? {
+ if let Some(destination) = opts.action_attr::<String>(&host, "dest")? {
host.set_session_destination(destination);
};
- if let Some(legacy) = opts.action_attr::<bool>(&host, "legacy_ssh_store").await? {
+ if let Some(legacy) = opts.action_attr::<bool>(&host, "legacy_ssh_store")? {
host.set_legacy_ssh_store(legacy);
};
@@ -153,7 +153,7 @@
self.action,
&host,
remote_path,
- match opts.action_attr(&host, "specialisation").await {
+ match opts.action_attr(&host, "specialisation") {
Ok(v) => v,
_ => {
error!("unreachable? failed to get specialization");
cmds/fleet/src/cmds/info.rsdiffbeforeafterboth--- a/cmds/fleet/src/cmds/info.rs
+++ b/cmds/fleet/src/cmds/info.rs
@@ -35,7 +35,7 @@
let mut data = Vec::new();
match self.cmd {
InfoCmd::ListHosts { ref tagged } => {
- 'host: for host in config.list_hosts().await? {
+ 'host: for host in config.list_hosts()? {
if !tagged.is_empty() {
let config = &config.config_field;
let host_name = &host.name;
@@ -59,7 +59,7 @@
"at leas one of --external or --internal must be set"
);
let mut out = <BTreeSet<String>>::new();
- let host = config.system_config(&host).await?;
+ let host = config.system_config(&host)?;
if external {
let data: Vec<String> = nix_go_json!(host.network.externalIps);
out.extend(data);
cmds/fleet/src/cmds/rollback.rsdiffbeforeafterboth--- a/cmds/fleet/src/cmds/rollback.rs
+++ b/cmds/fleet/src/cmds/rollback.rs
@@ -75,7 +75,7 @@
impl RollbackSingle {
pub(crate) async fn run(&self, config: &Config, _opts: &FleetOpts) -> Result<()> {
- let host = config.host(&self.machine).await?;
+ let host = config.host(&self.machine)?;
match &self.action {
RollbackAction::ListTargets => {
let generations = list_all_generations(&host, config).await;
cmds/fleet/src/cmds/secrets/mod.rsdiffbeforeafterboth--- a/cmds/fleet/src/cmds/secrets/mod.rs
+++ b/cmds/fleet/src/cmds/secrets/mod.rs
@@ -1,24 +1,16 @@
use std::{
- collections::{BTreeMap, BTreeSet, HashSet},
- io::{self, Read, Write, stdin, stdout},
+ collections::{BTreeSet, HashSet},
+ io::{Read, Write, stdin, stdout},
path::PathBuf,
};
-use anyhow::{Context, Result, anyhow, bail, ensure};
-use chrono::{DateTime, Utc};
+use anyhow::{Context, Result, bail, ensure};
use clap::Parser;
-use fleet_base::{
- fleetdata::{FleetSecretData, FleetSecretDistribution, FleetSecretPart, encrypt_secret_data},
- host::Config,
- opts::FleetOpts,
- secret::{Expectations, RegenerationReason, secret_needs_regeneration},
-};
+use fleet_base::{host::Config, opts::FleetOpts};
use fleet_shared::SecretData;
-use nix_eval::{NixType, Value, nix_go, nix_go_json};
-use serde::Deserialize;
-use tabled::{Table, Tabled};
-use tokio::{fs::read, task::spawn_blocking};
-use tracing::{Instrument, error, info, info_span, warn};
+use tabled::Tabled;
+use tokio::fs::read;
+use tracing::{info, info_span, warn};
#[derive(Parser)]
pub enum Secret {
@@ -145,13 +137,7 @@
}
*/
-#[derive(Deserialize)]
-#[serde(rename_all = "camelCase")]
-enum GeneratorKind {
- Impure,
- Pure,
-}
-
+/*
async fn generate_pure(
_config: &Config,
_display_name: &str,
@@ -315,6 +301,7 @@
}
}
}
+*/
/*
async fn generate_shared(
config: &Config,
@@ -421,8 +408,8 @@
todo!("part of fleet-pusher")
}
Secret::ForceKeys => {
- for host in config.list_hosts().await? {
- if opts.should_skip(&host).await? {
+ for host in config.list_hosts()? {
+ if opts.should_skip(&host)? {
continue;
}
config.key(&host.name).await?;
@@ -467,7 +454,7 @@
let Some(identity_holder) = identity_holder else {
bail!("no available holder found");
};
- let host = config.host(identity_holder).await?;
+ let host = config.host(identity_holder)?;
host.decrypt(part.raw.clone()).await?
} else {
part.raw.data.clone()
@@ -619,7 +606,7 @@
}
Secret::List {} => {
let _span = info_span!("loading secrets").entered();
- let configured = config.list_configured_shared().await?;
+ let configured = config.list_configured_shared()?;
#[derive(Tabled)]
struct SecretDisplay {
#[tabled(rename = "Name")]
@@ -662,7 +649,7 @@
.host_secret(&machine, &name)
.context("secret not found")?;
if let Some(data) = secret.secret.parts.get(&part) {
- let host = config.host(&machine).await?;
+ let host = config.host(&machine)?;
let secret = host.decrypt(data.raw.clone()).await?;
String::from_utf8(secret).context("secret is not utf8")?
} else if add {
cmds/fleet/src/main.rsdiffbeforeafterboth--- a/cmds/fleet/src/main.rs
+++ b/cmds/fleet/src/main.rs
@@ -216,13 +216,10 @@
.map(|a| extra_args::parse_os(&a))
.transpose()?
.unwrap_or_default();
- let config = opts
- .fleet_opts
- .build(
- nix_args,
- matches!(opts.command, Opts::Deploy(_) | Opts::BuildSystems(_)),
- )
- .await?;
+ let config = opts.fleet_opts.build(
+ nix_args,
+ matches!(opts.command, Opts::Deploy(_) | Opts::BuildSystems(_)),
+ )?;
match run_command(&config, opts.fleet_opts, opts.command).await {
Ok(()) => {
crates/fleet-base/src/fleetdata.rsdiffbeforeafterboth--- a/crates/fleet-base/src/fleetdata.rs
+++ b/crates/fleet-base/src/fleetdata.rs
@@ -421,3 +421,14 @@
}
}
}
+
+#[derive(Debug)]
+pub struct Expectations {
+ pub owners: BTreeSet<String>,
+ pub generation_data: serde_json::Value,
+ pub parts: BTreeMap<String, GeneratorPart>,
+}
+#[derive(Deserialize, Debug, Clone)]
+pub struct GeneratorPart {
+ pub encrypted: bool,
+}
crates/fleet-base/src/host.rsdiffbeforeafterboth--- a/crates/fleet-base/src/host.rs
+++ b/crates/fleet-base/src/host.rs
@@ -471,10 +471,13 @@
cmd.run().await
}
}
+
+struct HostSecretDefinition(Value);
+
impl ConfigHost {
// TOCTOU is possible here in case if config is changed, but this case is not handled anywhere anyway,
// assuming getting tags always returns the same value.
- pub async fn tags(&self) -> Result<Vec<String>> {
+ pub fn tags(&self) -> Result<Vec<String>> {
if let Some(v) = self.groups.get() {
return Ok(v.clone());
}
@@ -487,7 +490,7 @@
Ok(tags)
}
- pub async fn nixos_config(&self) -> Result<Value> {
+ pub fn nixos_config(&self) -> Result<Value> {
if let Some(v) = self.nixos_config.get() {
return Ok(v.clone());
}
@@ -495,7 +498,7 @@
bail!("local host has no nixos_config");
};
let nixos_config = nix_go!(host_config.nixos.config);
- assert_warn("nixos config evaluation", &nixos_config).await?;
+ assert_warn("nixos config evaluation", &nixos_config)?;
let _ = self.nixos_config.set(nixos_config.clone());
@@ -522,7 +525,7 @@
}
/// Packages for this host, resolved with nixpkgs overlays
- pub async fn pkgs(&self) -> Result<Value> {
+ pub fn pkgs(&self) -> Result<Value> {
if let Some(value) = &self.pkgs_override {
return Ok(value.clone());
}
@@ -534,17 +537,29 @@
}
}
+pub struct SharedSecretDefinition(Value);
+impl SharedSecretDefinition {
+ pub fn expected_owners(&self) -> Result<BTreeSet<String>> {
+ let secret = &self.0;
+ Ok(nix_go_json!(secret.expectedOwners))
+ }
+ pub fn generator(&self) -> Result<Value> {
+ let secret = &self.0;
+ Ok(nix_go!(secret.generator))
+ }
+}
+
impl Config {
- pub async fn tagged_hostnames(&self, tag: &str) -> Result<Vec<String>> {
+ pub fn tagged_hostnames(&self, tag: &str) -> Result<Vec<String>> {
let config = &self.config_field;
let tagged: Vec<String> = nix_go_json!(config.taggedWith[{ tag }]);
Ok(tagged)
}
- pub async fn expand_owner_set(&self, owners: Vec<String>) -> Result<BTreeSet<String>> {
+ pub fn expand_owner_set(&self, owners: Vec<String>) -> Result<BTreeSet<String>> {
let mut out = BTreeSet::new();
for owner in owners {
if let Some(tag) = owner.strip_prefix('@') {
- let hosts = self.tagged_hostnames(tag).await?;
+ let hosts = self.tagged_hostnames(tag)?;
out.extend(hosts);
} else {
out.insert(owner);
@@ -574,7 +589,7 @@
}
}
- pub async fn host(&self, name: &str) -> Result<ConfigHost> {
+ pub fn host(&self, name: &str) -> Result<ConfigHost> {
let config = &self.config_field;
let host_config = nix_go!(config.hosts[{ name }]);
@@ -595,23 +610,23 @@
legacy_ssh_store: OnceCell::new(),
})
}
- pub async fn list_hosts(&self) -> Result<Vec<ConfigHost>> {
+ pub fn list_hosts(&self) -> Result<Vec<ConfigHost>> {
let config = &self.config_field;
let names = nix_go!(config.hosts).list_fields()?;
let mut out = vec![];
for name in names {
- out.push(self.host(&name).await?);
+ out.push(self.host(&name)?);
}
Ok(out)
}
// TODO: Replace usages with .host().nixos_config
- pub async fn system_config(&self, host: &str) -> Result<Value> {
+ pub fn system_config(&self, host: &str) -> Result<Value> {
let fleet_field = &self.config_field;
Ok(nix_go!(fleet_field.hosts[{ host }].nixos.config))
}
/// Shared secrets configured in fleet.nix or in flake
- pub async fn list_configured_shared(&self) -> Result<Vec<String>> {
+ pub fn list_configured_shared(&self) -> Result<Vec<String>> {
let config_field = &self.config_field;
nix_go!(config_field.sharedSecrets).list_fields()
}
@@ -659,6 +674,17 @@
data.secrets.get(secret).cloned()
}
+ pub fn secret_definition(&self, secret: &str) -> Result<Option<SharedSecretDefinition>> {
+ let config = &self.config_field;
+ let shared_secrets = nix_go!(config.secrets);
+ if !shared_secrets.has_field(secret)? {
+ return Ok(None);
+ }
+ Ok(Some(SharedSecretDefinition(nix_go!(
+ shared_secrets[secret]
+ ))))
+ }
+
// TODO: Should this be something modifiable from other processes?
// E.g terraform provider might want to update FleetData (e.g secrets),
// and current implementation assumes only one process holds current fleet.nix
crates/fleet-base/src/keys.rsdiffbeforeafterboth--- a/crates/fleet-base/src/keys.rs
+++ b/crates/fleet-base/src/keys.rs
@@ -12,10 +12,10 @@
pub fn cached_key(&self, host: &str) -> Option<String> {
let data = self.data();
let key = data.hosts.get(host).map(|h| &h.encryption_key);
- if let Some(key) = key {
- if key.is_empty() {
- return None;
- }
+ if let Some(key) = key
+ && key.is_empty()
+ {
+ return None;
}
key.cloned()
}
@@ -30,7 +30,7 @@
Ok(key)
} else {
warn!("Loading key for {}", host);
- let host = self.host(host).await?;
+ let host = self.host(host)?;
let mut cmd = host.cmd("cat").await?;
cmd.arg("/etc/ssh/ssh_host_ed25519_key.pub");
let key = cmd.run_string().await?;
@@ -47,7 +47,7 @@
}
pub async fn recipients(&self, hosts: Vec<String>) -> Result<Vec<Box<dyn Recipient>>> {
- let hosts = self.expand_owner_set(hosts).await?;
+ let hosts = self.expand_owner_set(hosts)?;
futures::stream::iter(hosts.iter())
.then(|m| self.recipient(m.as_ref()))
.try_collect::<Vec<_>>()
@@ -57,12 +57,7 @@
#[allow(dead_code)]
pub async fn orphaned_data(&self) -> Result<Vec<String>> {
let mut out = Vec::new();
- let host_names = self
- .list_hosts()
- .await?
- .into_iter()
- .map(|h| h.name)
- .collect_vec();
+ let host_names = self.list_hosts()?.into_iter().map(|h| h.name).collect_vec();
for hostname in self
.data()
.hosts
crates/fleet-base/src/opts.rsdiffbeforeafterboth--- a/crates/fleet-base/src/opts.rs
+++ b/crates/fleet-base/src/opts.rs
@@ -104,20 +104,20 @@
}
impl FleetOpts {
- pub async fn filter_skipped(
+ pub fn filter_skipped(
&self,
hosts: impl IntoIterator<Item = ConfigHost>,
) -> Result<Vec<ConfigHost>> {
let mut out = Vec::new();
for host in hosts {
- if self.should_skip(&host).await? {
+ if self.should_skip(&host)? {
continue;
}
out.push(host);
}
Ok(out)
}
- pub async fn should_skip(&self, host: &ConfigHost) -> Result<bool> {
+ pub fn should_skip(&self, host: &ConfigHost) -> Result<bool> {
if self.skip.iter().any(|h| h as &str == host.name) {
return Ok(true);
}
@@ -137,7 +137,7 @@
}
}
if have_group_matches {
- let host_tags = host.tags().await?;
+ let host_tags = host.tags()?;
for item in self.only.iter() {
match item {
HostItem::Tag { name, .. } if host_tags.contains(name) => {
@@ -149,15 +149,15 @@
}
Ok(true)
}
- pub async fn action_attr<T: FromStr>(&self, host: &ConfigHost, attr: &str) -> Result<Option<T>>
+ pub fn action_attr<T: FromStr>(&self, host: &ConfigHost, attr: &str) -> Result<Option<T>>
where
T::Err: Sync,
anyhow::Error: From<T::Err>,
{
- let str = self.action_attr_str(host, attr).await?;
+ let str = self.action_attr_str(host, attr)?;
Ok(str.map(|v| T::from_str(&v)).transpose()?)
}
- pub async fn action_attr_str(&self, host: &ConfigHost, attr: &str) -> Result<Option<String>> {
+ pub fn action_attr_str(&self, host: &ConfigHost, attr: &str) -> Result<Option<String>> {
if self.only.is_empty() {
return Ok(None);
}
@@ -176,7 +176,7 @@
}
}
if have_group_matches {
- let host_tags = host.tags().await?;
+ let host_tags = host.tags()?;
for item in self.only.iter() {
match item {
HostItem::Tag { name, attrs }
@@ -195,7 +195,7 @@
}
// TODO: Config should be detached from opts.
- pub async fn build(&self, nix_args: Vec<OsString>, assert: bool) -> Result<Config> {
+ pub fn build(&self, nix_args: Vec<OsString>, assert: bool) -> Result<Config> {
let cwd = current_dir()?;
let mut directory = cwd.clone();
let mut fleet_data_path = directory.join("fleet.nix");
@@ -248,7 +248,6 @@
if assert {
assert_warn("fleet config evaluation", &config_field)
- .await
.context("failed to verify assertions")?;
}
crates/fleet-base/src/primops.rsdiffbeforeafterboth--- a/crates/fleet-base/src/primops.rs
+++ b/crates/fleet-base/src/primops.rs
@@ -1,38 +1,168 @@
-use std::cell::OnceCell;
-use std::collections::{BTreeMap, HashMap};
-use std::sync::{Arc, Mutex, OnceLock};
+use std::collections::{BTreeMap, BTreeSet, HashMap};
+use std::sync::OnceLock;
-use anyhow::{Context, bail};
+use anyhow::{Context, bail, ensure};
+use fleet_shared::SecretData;
use itertools::Itertools;
use nix_eval::{NativeFn, Value, nix_go, nix_go_json};
use serde::Deserialize;
use tracing::{info, warn};
-use crate::fleetdata::{FleetData, FleetSecrets};
-use crate::host::Config;
+use crate::fleetdata::{
+ Expectations, FleetSecretData, FleetSecretDistribution, FleetSecretPart, GeneratorPart,
+};
+use crate::host::{Config, ConfigHost};
+use crate::secret::{RegenerationReason, secret_needs_regeneration};
+use anyhow::{Result, anyhow};
#[derive(thiserror::Error, Debug)]
enum Error {}
-struct Parts {
- encrypted: Vec<String>,
- public: Vec<String>,
+pub static PRIMOPS_DATA: OnceLock<Config> = OnceLock::new();
+
+#[derive(Deserialize)]
+#[serde(rename_all = "camelCase")]
+enum GeneratorKind {
+ Impure,
+ Pure,
}
-trait SecretsBackend {
- fn has_shared(&self, name: &str);
- fn has_host(&self, host: &str, name: &str);
- fn shared_parts(&self, name: &str) -> Parts;
- fn host_parts(&self, host: &str, name: &str) -> Parts;
+pub fn get_pkgs_and_generators(host_on: &ConfigHost, recipients: Vec<String>) -> Result<Value> {
+ info!("get pkgs");
+ let pkgs = host_on.pkgs()?;
+ let default_mk_secret_generators = nix_go!(pkgs.mkSecretGenerators);
+ let generators = nix_go!(default_mk_secret_generators(Obj { recipients }));
+ Ok(pkgs.clone().attrs_update(generators)?)
+}
+pub fn get_default_pkgs_and_generators(config: &Config) -> Result<Value> {
+ let host_on = config.local_host();
+ get_pkgs_and_generators(&host_on, vec![])
}
+pub fn call_package(config: &Config, pkgs: &Value, package: &Value) -> Result<Value> {
+ ensure!(
+ package.is_function(),
+ "package should be a function to be called with callPackage"
+ );
+ // No need to use nixpkgs.buildUsing, as only nixpkgs-lib is used.
+ let nixpkgs = &config.nixpkgs;
+ let call_package = nix_go!(nixpkgs.lib.callPackageWith(pkgs));
+ Ok(nix_go!(call_package(package)(Obj {})))
+}
+
+pub fn get_default_generator_drv(config: &Config, generator: &Value) -> Result<Value> {
+ let default_pkgs_and_generators = get_default_pkgs_and_generators(config)?;
+ let default_generator_drv = call_package(config, &default_pkgs_and_generators, generator)
+ .context("failed to initialize generator to get metadata")?;
+
+ Ok(default_generator_drv)
+}
+
+pub async fn generate(
+ config: &Config,
+ expectations: Expectations,
+ generator: &Value,
+ default_generator_drv: &Value,
+) -> Result<FleetSecretDistribution> {
+ let kind: GeneratorKind = nix_go_json!(default_generator_drv.generatorKind);
+
+ match kind {
+ GeneratorKind::Impure => {
+ let impure_on: Option<String> = nix_go_json!(default_generator_drv.impureOn);
-struct FsSecretsBackend {}
+ let host_on = if let Some(on) = &impure_on {
+ config
+ .host(on)
+ .context("failed to get secret generation target host")?
+ } else {
+ config.local_host()
+ };
+ let pkgs_and_generators =
+ get_pkgs_and_generators(&host_on, expectations.owners.iter().cloned().collect())
+ .context("failed to get pkgs for target host")?;
+ let generator = call_package(config, &pkgs_and_generators, generator)
+ .context("failed to evaluate generator for target host")?;
-pub static PRIMOPS_DATA: OnceLock<Config> = OnceLock::new();
+ let generator = generator
+ .build("out")
+ .context("failed to build generator for target host")?;
-#[derive(Deserialize, Debug)]
-struct GeneratorPart {
- encrypted: bool,
+ let generator = host_on
+ .remote_derivation(&generator)
+ .await
+ .context("failed to copy generator to target host")?;
+
+ // TODO: Remove destdir after everything is done
+ let out_parent = host_on
+ .mktemp_dir()
+ .await
+ .context("failed to prepare generator output dir on target host")?;
+ let out = format!("{out_parent}/out");
+ let mut generator_cmd = host_on.cmd(generator).await?;
+ generator_cmd.env("out", &out);
+ if impure_on.is_none() {
+ let project_path: String = config
+ .directory
+ .clone()
+ .into_os_string()
+ .into_string()
+ .map_err(|e| anyhow!("fleet project path is not utf-8: {e:?}"))?;
+ generator_cmd.env("FLEET_PROJECT", project_path);
+ };
+ generator_cmd
+ .run()
+ .await
+ .context("failed to run impure generator")?;
+
+ {
+ let marker = host_on.read_file_text(format!("{out}/marker")).await?;
+ ensure!(
+ marker == "SUCCESS",
+ "impure generator ended prematurely, secret generation failed"
+ );
+ }
+
+ let mut parts = BTreeMap::new();
+ for part in host_on.read_dir(&out).await? {
+ if part == "created_at" || part == "expires_at" || part == "marker" {
+ continue;
+ }
+ let contents: SecretData = host_on
+ .read_file_text(format!("{out}/{part}"))
+ .await?
+ .parse()
+ .map_err(|e| anyhow!("failed to decode secret {out:?} part {part:?}: {e}"))?;
+ parts.insert(part.to_owned(), FleetSecretPart { raw: contents });
+ }
+
+ let created_at = host_on.read_file_value(format!("{out}/created_at")).await?;
+ let expires_at = host_on
+ .read_file_value(format!("{out}/expires_at"))
+ .await
+ .ok();
+
+ let new_data = FleetSecretData {
+ created_at,
+ expires_at,
+ parts,
+ generation_data: expectations.generation_data.clone(),
+ };
+
+ let new_data = FleetSecretDistribution {
+ secret: new_data,
+ owners: expectations.owners.clone(),
+ _deprecated_managed: true,
+ };
+
+ if let Some(reason) = secret_needs_regeneration(&new_data, &expectations) {
+ bail!("newly generated secret needs to be regenerated: {reason}")
+ }
+
+ Ok(new_data)
+ }
+ GeneratorKind::Pure => {
+ bail!("pure generators are disabled for now")
+ }
+ }
}
pub fn init_primops() {
@@ -52,52 +182,61 @@
.get()
.expect("primops data should be set on init");
- info!("get pkgs");
- let nixpkgs = &config.nixpkgs;
- let default_pkgs = &config.default_pkgs;
- let default_mk_secret_generators = nix_go!(default_pkgs.mkSecretGenerators);
- let generators = nix_go!(default_mk_secret_generators(Obj {
- recipients: <Vec<String>>::new(),
- }));
- let pkgs_and_generators = default_pkgs.clone().attrs_update(generators)?;
+ let shared_def = config.secret_definition(&secret).context("failed to get shared secret definition")?;
- info!("call package");
- let call_package = nix_go!(nixpkgs.lib.callPackageWith(pkgs_and_generators));
- let default_generator = call_package
- .call(generator.clone())
- .context("calling callPackage with generator")?
- .call(Value::new_attrs(HashMap::new()))
- .context("providing extra callPackage args")?;
+ let (shared, generator, expected_owners) = if generator.is_string() {
+ assert_eq!(generator.to_string()?, "shared", "asserted by nixos type system");
+ let Some(shared_def) = shared_def else {
+ bail!("secret {secret} is defined on host {host} as shared, but there is no shared secret with same name defined at fleetConfiguration.secrets.{secret}.generator")
+ };
+ let expected_owners = shared_def.expected_owners()?;
+
+ ensure!(expected_owners.contains(&host), "secret {secret} does not define {host} as expected owner");
- info!("get parts");
- let mut parts: BTreeMap<String, GeneratorPart> = nix_go_json!(default_generator.parts);
- info!("got parts: {parts:?}");
+ (true, shared_def.generator()?, expected_owners)
+ } else {
+ if shared_def.is_some() {
+ bail!("hosts can only have their own generators for non-shared secrets, either set host secret generator to \"shared\", or remove shared secret generator at fleetConfiguration.secrets.{secret}.generator")
+ }
- let Some(existing) = config
- .host_secret(&host, &secret) else {
- bail!("missing secret {secret} for host {host}; secret needs regeneration")
+ (false, generator.clone(), BTreeSet::from_iter([host.clone()]))
};
- info!("got existing: {existing:?}");
+ let default_generator_drv = get_default_generator_drv(config, &generator).context("failed to evaluate default generator")?;
+ let expectations = Expectations {
+ parts: nix_go_json!(default_generator_drv.parts),
+ generation_data: nix_go_json!(default_generator_drv.generationData),
+ owners: expected_owners,
+ };
+
+ let reason: RegenerationReason = 'regenerate: {
+ let Some(existing) = config
+ .host_secret(&host, &secret) else {
+ break 'regenerate RegenerationReason::Missing;
+ };
+ if let Some(reason) = secret_needs_regeneration(&existing, &expectations) {
+ break 'regenerate reason;
+ }
- let mut out = HashMap::new();
+ let mut parts = expectations.parts.clone();
- for (part_name, part) in &existing.secret.parts {
- let Some(definition) = parts.remove(part_name) else {
- warn!("secret {secret} part {part_name} is stored, but not defined in nixos config, it will not be passed to nix");
- continue;
- };
- if definition.encrypted != part.raw.encrypted {
- bail!("secret {secret} part {part_name} is supposed to be {}, but it is {}; secret needs regeneration", if definition.encrypted {"encrypted"} else {"unencrypted"}, if part.raw.encrypted {"encrypted"} else {"unencrypted"});
+ let mut out = HashMap::new();
+ for (part_name, part) in &existing.secret.parts {
+ let Some(definition) = parts.remove(part_name) else {
+ warn!("secret {secret} part {part_name} is stored, but not defined in nixos config, it will not be passed to nix");
+ continue;
+ };
+ assert!(definition.encrypted != part.raw.encrypted, "encryption status is checked by secret_needs_regeneration");
+ out.insert(part_name.as_str(), Value::new_attrs(HashMap::from_iter([("raw", Value::new_str(&part.raw.to_string()))])));
}
- out.insert(part_name.as_str(), Value::new_attrs(HashMap::from_iter([("raw", Value::new_str(&part.raw.to_string()))])));
- }
- if !parts.is_empty(){
- let defs = parts.keys().collect_vec();
- bail!("secret parts are defined, but not stored: {defs:?}, secret needs regeneration")
- }
+ assert!(parts.is_empty(), "secret part is missing, secret_needs_regeneration should check that");
- Ok(Value::new_attrs(out))
+ return Ok(Value::new_attrs(out))
+ };
+
+ todo!()
+
+
},
)
.register();
crates/fleet-base/src/secret.rsdiffbeforeafterboth--- a/crates/fleet-base/src/secret.rs
+++ b/crates/fleet-base/src/secret.rs
@@ -1,16 +1,8 @@
-use std::collections::BTreeSet;
+use std::collections::{BTreeMap, BTreeSet};
use chrono::{DateTime, Utc};
-use crate::fleetdata::FleetSecretData;
-
-#[derive(Debug)]
-pub struct Expectations {
- pub owners: BTreeSet<String>,
- pub generation_data: serde_json::Value,
- pub public_parts: BTreeSet<String>,
- pub private_parts: BTreeSet<String>,
-}
+use crate::fleetdata::{Expectations, FleetSecretData, FleetSecretDistribution, GeneratorPart};
#[derive(thiserror::Error, Debug)]
pub enum RegenerationReason {
@@ -34,56 +26,62 @@
ExpectedPublic(String),
#[error("secret is expired at {0}")]
Expired(DateTime<Utc>),
+
+ #[error("secret is not generated for this host")]
+ Missing,
}
pub fn secret_needs_regeneration(
- secret: &FleetSecretData,
- owners: &BTreeSet<String>,
+ secret: &FleetSecretDistribution,
expectations: &Expectations,
) -> Option<RegenerationReason> {
- if !owners.is_empty() {
- let added: BTreeSet<String> = expectations.owners.difference(owners).cloned().collect();
- if !added.is_empty() {
- return Some(RegenerationReason::OwnersAdded(added));
- }
+ let added: BTreeSet<String> = expectations
+ .owners
+ .difference(&secret.owners)
+ .cloned()
+ .collect();
+ if !added.is_empty() {
+ return Some(RegenerationReason::OwnersAdded(added));
+ }
- let removed: BTreeSet<String> = owners.difference(&expectations.owners).cloned().collect();
- if !removed.is_empty() {
- return Some(RegenerationReason::OwnersRemoved(removed));
- }
+ let removed: BTreeSet<String> = secret
+ .owners
+ .difference(&expectations.owners)
+ .cloned()
+ .collect();
+ if !removed.is_empty() {
+ return Some(RegenerationReason::OwnersRemoved(removed));
}
- if secret.generation_data != expectations.generation_data {
+ if secret.secret.generation_data != expectations.generation_data {
return Some(RegenerationReason::GenerationData {
expected: expectations.generation_data.clone(),
- found: secret.generation_data.clone(),
+ found: secret.secret.generation_data.clone(),
});
}
- if !expectations.public_parts.is_empty() || !expectations.private_parts.is_empty() {
- let expected: BTreeSet<String> = expectations
- .public_parts
- .union(&expectations.private_parts)
- .cloned()
- .collect();
- let found: BTreeSet<String> = secret.parts.keys().cloned().collect();
+ let expected: BTreeSet<String> = expectations.parts.keys().cloned().collect();
+ let found: BTreeSet<String> = secret.secret.parts.keys().cloned().collect();
- if found != expected {
- return Some(RegenerationReason::PartList { expected, found });
- }
+ if found != expected {
+ return Some(RegenerationReason::PartList { expected, found });
+ }
- for (name, value) in secret.parts.iter() {
- if value.raw.encrypted {
- if !expectations.private_parts.contains(name) {
- return Some(RegenerationReason::ExpectedPrivate(name.clone()));
- }
- } else if !expectations.public_parts.contains(name) {
- return Some(RegenerationReason::ExpectedPublic(name.clone()));
+ for (name, value) in secret.secret.parts.iter() {
+ let expectation = expectations
+ .parts
+ .get(name)
+ .expect("found == expected checked");
+ if value.raw.encrypted {
+ if !expectation.encrypted {
+ return Some(RegenerationReason::ExpectedPrivate(name.clone()));
}
+ } else if expectation.encrypted {
+ return Some(RegenerationReason::ExpectedPublic(name.clone()));
}
}
- if let Some(expiration) = secret.expires_at {
+ if let Some(expiration) = secret.secret.expires_at {
// TODO: Leeway?
if expiration < Utc::now() {
return Some(RegenerationReason::Expired(expiration));
crates/nix-eval/src/lib.rsdiffbeforeafterboth1use std::borrow::Cow;2use std::cell::RefCell;3use std::ffi::{CStr, CString, c_char, c_int, c_uint, c_void};4use std::ptr::{null, null_mut};5use std::sync::LazyLock;6use std::{array, fmt, slice};7use std::{collections::HashMap, path::PathBuf};89use anyhow::{Context, anyhow, bail};10use itertools::Itertools;11use serde::Serialize;12use serde::de::DeserializeOwned;1314pub use anyhow::Result;15use tracing::{info, instrument, warn};1617use self::logging::{ErrorInfoBuilder, nix_logging_cxx};18use self::nix_cxx::set_fetcher_setting;19use self::nix_raw::{20 BindingsBuilder as c_bindings_builder, EvalState as c_eval_state, GC_SUCCESS,21 GC_allow_register_threads, GC_get_stack_base, GC_register_my_thread, GC_stack_base,22 GC_thread_is_registered, GC_unregister_my_thread, ListBuilder as c_list_builder, PrimOp,23 PrimOpFun, Store as c_store, StorePath as c_store_path, alloc_primop, alloc_value,24 bindings_builder_free, bindings_builder_insert, c_context, c_context_create, c_context_free,25 clear_err, copy_value, err_NIX_ERR_KEY, err_NIX_ERR_NIX_ERROR, err_NIX_ERR_OVERFLOW,26 err_NIX_ERR_UNKNOWN, err_code, err_info_msg, err_msg, eval_state_build,27 eval_state_builder_load, eval_state_builder_new, eval_state_builder_set_eval_setting,28 expr_eval_from_string, fetchers_settings, fetchers_settings_free, fetchers_settings_new,29 flake_lock, flake_lock_flags, flake_lock_flags_free, flake_lock_flags_new, flake_reference,30 flake_reference_and_fragment_from_string, flake_reference_parse_flags,31 flake_reference_parse_flags_free, flake_reference_parse_flags_new,32 flake_reference_parse_flags_set_base_directory, flake_settings, flake_settings_free,33 flake_settings_new, gc_now as gc_now_raw, get_attr_byname, get_attr_name_byidx, get_attrs_size,34 get_list_byidx, get_list_size, get_string, get_type, has_attr_byname, init_bool, init_int,35 init_primop, init_string, libexpr_init, libstore_init, libutil_init, list_builder_free,36 list_builder_insert, locked_flake, locked_flake_free, locked_flake_get_output_attrs,37 make_attrs, make_bindings_builder, make_list, make_list_builder, realised_string,38 realised_string_free, realised_string_get_buffer_size, realised_string_get_buffer_start,39 realised_string_get_store_path, realised_string_get_store_path_count, register_primop,40 set_err_msg, setting_set, state_free, store_open, store_parse_path, store_path_free,41 store_path_name, string_realise, value, value_call, value_decref, value_force, value_incref,42};4344// Contains macros helpers45pub mod logging;46#[doc(hidden)]47pub mod macros;48pub mod util;4950#[allow(51 non_upper_case_globals,52 non_camel_case_types,53 non_snake_case,54 dead_code55)]56mod nix_raw {57 include!(concat!(env!("OUT_DIR"), "/bindings.rs"));58}59#[cxx::bridge]60pub mod nix_cxx {61 unsafe extern "C++" {62 type nix_fetchers_settings;63 include!("nix-eval/src/lib.hh");6465 #[allow(clippy::missing_safety_doc)]66 unsafe fn set_fetcher_setting(67 settings: *mut nix_fetchers_settings,68 setting: *const c_char,69 value: *const c_char,70 );71 }72}7374#[derive(Debug, PartialEq, Eq)]75pub enum NixType {76 Thunk,77 Int,78 Float,79 Bool,80 String,81 Path,82 Null,83 Attrs,84 List,85 Function,86 External,87}88impl NixType {89 fn from_int(c: c_uint) -> Self {90 match c {91 0 => Self::Thunk,92 1 => Self::Int,93 2 => Self::Float,94 3 => Self::Bool,95 4 => Self::String,96 5 => Self::Path,97 6 => Self::Null,98 7 => Self::Attrs,99 8 => Self::List,100 9 => Self::Function,101 10 => Self::External,102 _ => unreachable!("unknown nix type: {c}"),103 }104 }105}106107enum FunctorKind {108 Function,109 Functor,110}111112#[derive(Debug)]113#[repr(i32)]114pub enum NixErrorKind {115 Unknown = err_NIX_ERR_UNKNOWN,116 Overflow = err_NIX_ERR_OVERFLOW,117 Key = err_NIX_ERR_KEY,118 Generic = err_NIX_ERR_NIX_ERROR,119}120impl NixErrorKind {121 fn from_int(v: c_int) -> Option<Self> {122 Some(match v {123 0 => return None,124 nix_raw::err_NIX_ERR_UNKNOWN => Self::Unknown,125 nix_raw::err_NIX_ERR_OVERFLOW => Self::Overflow,126 nix_raw::err_NIX_ERR_KEY => Self::Key,127 nix_raw::err_NIX_ERR_NIX_ERROR => Self::Generic,128 _ => {129 debug_assert!(false, "unexpected nix error kind: {v}");130 Self::Unknown131 }132 })133 }134}135136pub fn gc_now() {137 unsafe { gc_now_raw() };138}139140pub fn gc_register_my_thread() {141 assert_eq!(unsafe { GC_thread_is_registered() }, 0);142143 let mut sb = GC_stack_base {144 mem_base: null_mut(),145 };146 let r = unsafe { GC_get_stack_base(&mut sb) };147 if r as u32 != GC_SUCCESS {148 panic!("failed to get thread stack base");149 }150 unsafe { GC_register_my_thread(&sb) };151}152pub fn gc_unregister_my_thread() {153 assert_eq!(unsafe { GC_thread_is_registered() }, 1);154155 unsafe { GC_unregister_my_thread() };156}157158pub struct ThreadRegisterGuard {}159impl ThreadRegisterGuard {160 #[allow(clippy::new_without_default)]161 pub fn new() -> Self {162 gc_register_my_thread();163 Self {}164 }165}166impl Drop for ThreadRegisterGuard {167 fn drop(&mut self) {168 gc_unregister_my_thread();169 }170}171172#[repr(transparent)]173pub struct NixContext(*mut c_context);174impl NixContext {175 pub fn set_err(&mut self, err: NixErrorKind, msg: &CStr) {176 unsafe { set_err_msg(self.0, err as c_int, msg.as_ptr()) };177 }178 pub fn new() -> Self {179 let ctx = unsafe { c_context_create() };180 Self(ctx)181 }182 fn error_kind(&self) -> Option<NixErrorKind> {183 let code = unsafe { err_code(self.0) };184 NixErrorKind::from_int(code)185 }186 fn error<'t>(&self) -> Option<(Cow<'t, str>, Option<Box<ErrorInfoBuilder>>)> {187 if let NixErrorKind::Generic = self.error_kind()? {188 let ei = unsafe { logging::nix_logging_cxx::extract_error_info(self.0) };189 let mut err_out = String::new();190 unsafe {191 err_info_msg(192 null_mut(),193 self.0,194 Some(copy_nix_str),195 (&raw mut err_out).cast(),196 )197 };198 return Some((Cow::Owned(err_out), Some(ei)));199 };200201 // TODO: Can throw error (resulting in panic) if unable to retrieve error. Should be able to resolve by passing context as a first argument,202 // but it looks ugly203 let str = unsafe { err_msg(null_mut(), self.0, null_mut()) };204 Some((unsafe { CStr::from_ptr(str) }.to_string_lossy(), None))205 }206 fn clean_err(&mut self) {207 unsafe {208 clear_err(self.0);209 }210 }211212 fn bail_if_error(&self) -> Result<()> {213 if let Some((err, stack)) = self.error() {214 let mut e = Err(anyhow!("{err}"));215 if let Some(stack) = stack {216 for ele in stack.stack_frames {217 e = e.with_context(|| {218 if ele.pos.is_empty() {219 ele.msg220 } else {221 format!("{} at {}", ele.msg, ele.pos)222 }223 })224 }225 }226 return e.context("<nix frames>");227 };228 Ok(())229 }230231 fn run_in_context<T>(&mut self, f: impl FnOnce(*mut c_context) -> T) -> Result<T> {232 self.clean_err();233 let o = f(self.0);234 self.bail_if_error()?;235 self.clean_err();236 Ok(o)237 }238}239240impl Default for NixContext {241 fn default() -> Self {242 Self::new()243 }244}245impl Drop for NixContext {246 fn drop(&mut self) {247 unsafe {248 c_context_free(self.0);249 }250 }251}252struct GlobalState {253 // Store should be valid as long as EvalState is valid254 #[allow(dead_code)]255 store: Store,256 state: EvalState,257}258impl GlobalState {259 fn new() -> Result<Self> {260 let mut ctx = NixContext::new();261 let store = ctx262 .run_in_context(|c| unsafe { store_open(c, c"auto".as_ptr(), null_mut()) })263 .map(Store)?;264265 let builder = ctx.run_in_context(|c| unsafe { eval_state_builder_new(c, store.0) })?;266 ctx.run_in_context(|c| unsafe { eval_state_builder_load(c, builder) })?;267 ctx.run_in_context(|c| unsafe {268 eval_state_builder_set_eval_setting(269 c,270 builder,271 c"lazy-trees".as_ptr(),272 c"true".as_ptr(),273 )274 })?;275 ctx.run_in_context(|c| unsafe {276 eval_state_builder_set_eval_setting(277 c,278 builder,279 c"lazy-locks".as_ptr(),280 c"true".as_ptr(),281 )282 })?;283 let state = ctx284 .run_in_context(|c| unsafe { eval_state_build(c, builder) })285 .map(EvalState)?;286287 Ok(Self { store, state })288 }289}290291struct ThreadState {292 ctx: NixContext,293}294impl ThreadState {295 fn new() -> Result<Self> {296 let ctx = NixContext::new();297298 Ok(Self { ctx })299 }300}301302static GLOBAL_STATE: LazyLock<GlobalState> = LazyLock::new(|| {303 info!("initializing nix global state");304 GlobalState::new().expect("global state init shouldn't fail")305});306307thread_local! {308 static THREAD_STATE: RefCell<ThreadState> = RefCell::new(ThreadState::new().expect("thread state init shouldn't fail"));309}310fn with_default_context<T>(f: impl FnOnce(*mut c_context, *mut c_eval_state) -> T) -> Result<T> {311 let global = &GLOBAL_STATE.state;312 let (ctx, state) = THREAD_STATE.with_borrow_mut(|w| (w.ctx.0, global.0));313 let mut ctx = NixContext(ctx);314 let v = ctx.run_in_context(|c| f(c, state));315 // It is reused for thread316 std::mem::forget(ctx);317 v318}319320pub fn set_setting(s: &CStr, v: &CStr) -> Result<()> {321 with_default_context(|c, _| unsafe { setting_set(c, s.as_ptr(), v.as_ptr()) }).map(|_| ())322}323324pub struct FetchSettings(*mut fetchers_settings);325impl FetchSettings {326 pub fn new() -> Self {327 Self::try_new().expect("allocation should not fail")328 }329 fn try_new() -> Result<Self> {330 with_default_context(|c, _| unsafe { fetchers_settings_new(c) }).map(Self)331 }332 pub fn set(&mut self, setting: &CStr, value: &CStr) {333 unsafe {334 set_fetcher_setting(self.0.cast(), setting.as_ptr(), value.as_ptr());335 };336 }337}338unsafe impl Send for FetchSettings {}339unsafe impl Sync for FetchSettings {}340341impl Default for FetchSettings {342 fn default() -> Self {343 Self::new()344 }345}346347impl Drop for FetchSettings {348 fn drop(&mut self) {349 unsafe { fetchers_settings_free(self.0) };350 }351}352pub struct FlakeSettings(*mut flake_settings);353impl FlakeSettings {354 pub fn new() -> Result<Self> {355 with_default_context(|c, _| unsafe { flake_settings_new(c) }).map(Self)356 }357}358unsafe impl Send for FlakeSettings {}359unsafe impl Sync for FlakeSettings {}360impl Drop for FlakeSettings {361 fn drop(&mut self) {362 unsafe {363 flake_settings_free(self.0);364 }365 }366}367368pub struct FlakeReferenceParseFlags(*mut flake_reference_parse_flags);369impl FlakeReferenceParseFlags {370 pub fn new(settings: &FlakeSettings) -> Result<Self> {371 with_default_context(|c, _| unsafe { flake_reference_parse_flags_new(c, settings.0) })372 .map(Self)373 }374 pub fn set_base_dir(&mut self, dir: &str) -> Result<()> {375 with_default_context(|c, _| {376 unsafe {377 flake_reference_parse_flags_set_base_directory(378 c,379 self.0,380 dir.as_ptr().cast(),381 dir.len(),382 )383 };384 })385 }386}387impl Drop for FlakeReferenceParseFlags {388 fn drop(&mut self) {389 unsafe {390 flake_reference_parse_flags_free(self.0);391 }392 }393}394pub struct FlakeLockFlags(*mut flake_lock_flags);395impl FlakeLockFlags {396 pub fn new(settings: &FlakeSettings) -> Result<Self> {397 let o = with_default_context(|c, _| unsafe { flake_lock_flags_new(c, settings.0) })398 .map(Self)?;399 // with_default_context(|c, _| unsafe { flake_lock_flags_set_mode_virtual(c, o.0) })?;400401 Ok(o)402 }403}404impl Drop for FlakeLockFlags {405 fn drop(&mut self) {406 unsafe {407 flake_lock_flags_free(self.0);408 }409 }410}411412unsafe extern "C" fn copy_nix_str(start: *const c_char, n: c_uint, user_data: *mut c_void) {413 let s = unsafe { slice::from_raw_parts(start.cast::<u8>(), n as usize) };414 let s = std::str::from_utf8(s).expect("c string has invalid utf-8");415 unsafe { *user_data.cast::<String>() = s.to_owned() };416}417418struct Store(*mut c_store);419unsafe impl Send for Store {}420unsafe impl Sync for Store {}421422impl Store {423 fn parse_path(&self, path: &CStr) -> Result<StorePath> {424 with_default_context(|c, _| {425 StorePath(unsafe { store_parse_path(c, self.0, path.as_ptr()) })426 })427 }428}429430#[repr(transparent)]431pub struct EvalState(*mut c_eval_state);432unsafe impl Send for EvalState {}433unsafe impl Sync for EvalState {}434435impl Drop for EvalState {436 fn drop(&mut self) {437 unsafe {438 state_free(self.0);439 }440 }441}442443pub struct FlakeReference(*mut flake_reference);444impl FlakeReference {445 #[instrument(name = "new-flake-reference", skip(flake, parse, fetch))]446 pub fn new(447 s: &str,448 flake: &FlakeSettings,449 parse: &FlakeReferenceParseFlags,450 fetch: &FetchSettings,451 ) -> Result<(Self, String)> {452 let mut out = null_mut();453 let mut fragment = String::new();454 // let fetch_settings = fetcher_settings;455 with_default_context(|c, _| unsafe {456 flake_reference_and_fragment_from_string(457 c,458 fetch.0,459 flake.0,460 parse.0,461 s.as_ptr().cast(),462 s.len(),463 &mut out,464 Some(copy_nix_str),465 (&raw mut fragment).cast(),466 )467 })?;468 assert!(!out.is_null());469470 Ok((Self(out), fragment))471 }472 #[instrument(name = "lock-flake", skip(self, fetch, flake, lock))]473 pub fn lock(474 &mut self,475 fetch: &FetchSettings,476 flake: &FlakeSettings,477 lock: &FlakeLockFlags,478 ) -> Result<LockedFlake> {479 with_default_context(|c, es| unsafe { flake_lock(c, fetch.0, flake.0, es, lock.0, self.0) })480 .map(LockedFlake)481 }482}483unsafe impl Send for FlakeReference {}484unsafe impl Sync for FlakeReference {}485486pub struct LockedFlake(*mut locked_flake);487impl LockedFlake {488 pub fn get_attrs(&self, settings: &mut FlakeSettings) -> Result<Value> {489 with_default_context(|c, es| unsafe {490 locked_flake_get_output_attrs(c, settings.0, es, self.0)491 })492 .map(Value)493 }494}495unsafe impl Send for LockedFlake {}496unsafe impl Sync for LockedFlake {}497impl Drop for LockedFlake {498 fn drop(&mut self) {499 unsafe {500 locked_flake_free(self.0);501 };502 }503}504505type FieldName = [u8; 64];506fn init_field_name(v: &str) -> FieldName {507 let mut f = [0; 64];508 assert!(v.len() < 64, "max field name is 63 chars");509 assert!(510 v.bytes().all(|v| v != 0),511 "nul bytes are unsupported in field name"512 );513 f[0..v.len()].copy_from_slice(v.as_bytes());514 f515}516517pub struct RealisedString(*mut realised_string);518impl fmt::Debug for RealisedString {519 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {520 self.as_str().fmt(f)521 }522}523524impl RealisedString {525 pub fn as_str(&self) -> &str {526 let len = unsafe { realised_string_get_buffer_size(self.0) };527 let data: *const u8 = unsafe { realised_string_get_buffer_start(self.0) }.cast();528 let data = unsafe { slice::from_raw_parts(data, len) };529 std::str::from_utf8(data).expect("non-utf8 strings not supported")530 }531 pub fn path_count(&self) -> usize {532 unsafe { realised_string_get_store_path_count(self.0) }533 }534 pub fn path(&self, i: usize) -> String {535 assert!(i < self.path_count());536 let path = unsafe { realised_string_get_store_path(self.0, i) };537 let mut err_out = String::new();538 unsafe { store_path_name(path, Some(copy_nix_str), (&raw mut err_out).cast()) };539 err_out540 }541}542543unsafe impl Send for RealisedString {}544impl Drop for RealisedString {545 fn drop(&mut self) {546 unsafe { realised_string_free(self.0) }547 }548}549550#[repr(transparent)]551pub struct Value(*mut value);552553unsafe impl Send for Value {}554unsafe impl Sync for Value {}555556pub trait AsFieldName {557 fn as_field_name<T>(&self, v: impl FnOnce(FieldName) -> Result<T>) -> Result<T>;558 fn to_field_name(&self) -> Result<String>;559}560impl AsFieldName for Value {561 fn as_field_name<T>(&self, v: impl FnOnce(FieldName) -> Result<T>) -> Result<T> {562 let f = self.to_string()?;563 v(init_field_name(&f))564 }565 fn to_field_name(&self) -> Result<String> {566 self.to_string()567 }568}569impl<E> AsFieldName for E570where571 E: AsRef<str>,572{573 fn as_field_name<T>(&self, v: impl FnOnce(FieldName) -> Result<T>) -> Result<T> {574 let f = self.as_ref();575 v(init_field_name(f))576 }577 fn to_field_name(&self) -> Result<String> {578 Ok(self.as_ref().to_owned())579 }580}581582struct AttrsBuilder(*mut c_bindings_builder);583impl AttrsBuilder {584 fn new(capacity: usize) -> Self {585 with_default_context(|c, es| unsafe { make_bindings_builder(c, es, capacity) })586 .map(Self)587 .expect("alloc should not fail")588 }589 fn insert(&mut self, k: &impl AsFieldName, v: Value) {590 k.as_field_name(|name| {591 with_default_context(|c, _| unsafe {592 bindings_builder_insert(c, self.0, name.as_ptr().cast(), v.0);593 // bindings_builder_insert doesn't do incref594 })595 })596 .expect("builder insert shouldn't fail");597 }598}599impl Drop for AttrsBuilder {600 fn drop(&mut self) {601 unsafe { bindings_builder_free(self.0) };602 }603}604605struct ListBuilder(*mut c_list_builder, c_uint);606impl ListBuilder {607 fn new(capacity: usize) -> Self {608 with_default_context(|c, es| unsafe { make_list_builder(c, es, capacity) })609 .map(|l| Self(l, 0))610 .expect("alloc should not fail")611 }612}613impl ListBuilder {614 fn push(&mut self, v: Value) {615 with_default_context(|c, _| unsafe {616 list_builder_insert(617 c,618 self.0,619 {620 let v = self.1;621 self.1 += 1;622 v623 },624 v.0,625 )626 })627 .expect("list insert shouldn't fail");628 }629}630impl Drop for ListBuilder {631 fn drop(&mut self) {632 unsafe { list_builder_free(self.0) };633 }634}635636impl Value {637 pub fn new_primop(v: NativeFn) -> Self {638 let out = Self::new_uninit();639 with_default_context(|c, _| unsafe { init_primop(c, out.0, v.0) })640 .expect("primop initialization should not fail");641 out642 }643 pub fn new_attrs(v: HashMap<&str, Value>) -> Self {644 let out = Self::new_uninit();645 let mut b = AttrsBuilder::new(v.len());646 for (k, v) in v {647 b.insert(&k, v);648 }649 with_default_context(|c, _| unsafe { make_attrs(c, out.0, b.0) })650 .expect("attrs initialization should not fail");651652 out653 }654 fn new_list<T: Into<Self>>(v: Vec<T>) -> Self {655 let out = Self::new_uninit();656 let mut b = ListBuilder::new(v.len());657 for v in v {658 b.push(v.into());659 }660 with_default_context(|c, _| unsafe { make_list(c, b.0, out.0) })661 .expect("list initialization should not fail");662663 out664 }665 fn new_uninit() -> Self {666 let out = with_default_context(|c, es| unsafe { alloc_value(c, es) })667 .expect("value allocation should not fail");668 Self(out)669 }670 pub fn new_str(v: &str) -> Self {671 let s = CString::new(v).expect("string should not contain NULs");672 let out = Self::new_uninit();673 // String is copied, `s` is free to be dropped674 with_default_context(|c, _| unsafe { init_string(c, out.0, s.as_ptr()) })675 .expect("string initialization should not fail");676 out677 }678 pub fn new_int(i: i64) -> Self {679 let out = Self::new_uninit();680 with_default_context(|c, _| unsafe { init_int(c, out.0, i) })681 .expect("int initialization should not fail");682 out683 }684 pub fn new_bool(v: bool) -> Self {685 let out = Self::new_uninit();686 with_default_context(|c, _| unsafe { init_bool(c, out.0, v) })687 .expect("bool initialization should not fail");688 out689 }690 // TODO: As far as I can see, there is no way to get Thunks from nix public C api, so this function is useless691 // fn force(&mut self, st: &mut EvalState) -> Result<()> {692 // with_default_context(|c, _| unsafe { value_force(c, st.0, self.0) })?;693 // Ok(())694 // }695 pub fn type_of(&self) -> NixType {696 let ty = with_default_context(|c, _| unsafe { get_type(c, self.0) })697 .expect("get_type should not fail");698 NixType::from_int(ty)699 }700 fn builtin_to_string(&self) -> Result<Self> {701 let builtin = Self::eval("builtins.toString")?;702 builtin.call(self.clone())703 }704 fn force(&mut self, s: *mut nix_raw::EvalState) -> Result<()> {705 with_default_context(|c, _| unsafe { value_force(c, s, self.0) })?;706 Ok(())707 }708 pub fn to_string(&self) -> Result<String> {709 let ty = self.type_of();710 if !matches!(ty, NixType::String) {711 bail!("unexpected type: {ty:?}, expected string");712 }713 let mut str_out = String::new();714 with_default_context(|c, _| unsafe {715 get_string(c, self.0, Some(copy_nix_str), (&raw mut str_out).cast())716 })?;717718 Ok(str_out)719 }720 pub fn to_realised_string(&self) -> Result<RealisedString> {721 with_default_context(|c, es| unsafe { string_realise(c, es, self.0, false) })722 .map(RealisedString)723724 // let store_paths = unsafe { nix_raw::realised_string_get_store_path_count(str) };725 // for i in 0..store_paths {726 // let store_path = unsafe { nix_raw::realised_string_get_store_path(str, i) };727 // nix_raw::store_path_name(store_path, callback, user_data);728 // }729 // dbg!(store_paths);730 // todo!();731 }732733 pub fn has_field(&self, field: &str) -> Result<bool> {734 if !matches!(self.type_of(), NixType::Attrs) {735 bail!("invalid type: expected attrs");736 }737738 let f = init_field_name(field);739 with_default_context(|c, es| unsafe { has_attr_byname(c, self.0, es, f.as_ptr().cast()) })740 }741 // pub fn derivation_path(&self) {742 // nix_raw::real743 // }744 pub fn list_fields(&self) -> Result<Vec<String>> {745 if !matches!(self.type_of(), NixType::Attrs) {746 bail!("invalid type: expected attrs");747 }748749 let len = with_default_context(|c, _| unsafe { get_attrs_size(c, self.0) })?;750 let mut out = Vec::with_capacity(len as usize);751752 for i in 0..len {753 let name =754 with_default_context(|c, es| unsafe { get_attr_name_byidx(c, self.0, es, i) })?;755 let c = unsafe { CStr::from_ptr(name) };756 out.push(c.to_str().expect("nix field names are utf-8").to_owned());757 }758 Ok(out)759 }760 pub fn get_elem(&self, v: usize) -> Result<Self> {761 if !matches!(self.type_of(), NixType::List) {762 bail!("invalid type: expected list");763 }764 let len = with_default_context(|c, _| unsafe { get_list_size(c, self.0) })? as usize;765 if v >= len {766 bail!("oob list get: {v} >= {len}");767 }768769 with_default_context(|c, es| unsafe { get_list_byidx(c, self.0, es, v as u32) }).map(Self)770 }771 pub fn attrs_update(self, other: Value /*, ignore_errors: bool*/) -> Result<Self> {772 let attrs_update_fn = Self::eval("a: b: a // b")?;773774 attrs_update_fn775 .call(self)?776 .call(other)777 .context("attrs update")778 }779 pub fn get_field(&self, name: impl AsFieldName) -> Result<Self> {780 if !matches!(self.type_of(), NixType::Attrs) {781 bail!("invalid type: expected attrs");782 }783784 name.as_field_name(|name| {785 with_default_context(|c, es| unsafe {786 get_attr_byname(c, self.0, es, name.as_ptr().cast())787 })788 .map(Self)789 })790 .with_context(|| format!("getting field {:?}", name.to_field_name()))791 }792 pub fn call(&self, v: Value) -> Result<Self> {793 let kind = self794 .functor_kind()795 .ok_or_else(|| anyhow!("can only call function or functor"))?;796797 let function = match kind {798 FunctorKind::Function => self.clone(),799 FunctorKind::Functor => {800 let f = self801 .get_field("__functor")802 .context("getting functor value")?;803 assert_eq!(804 f.type_of(),805 NixType::Function,806 "invalid functor encountered"807 );808 f809 }810 };811812 let out = Value::new_uninit();813 with_default_context(|c, es| unsafe { value_call(c, es, function.0, v.0, out.0) })?;814815 Ok(out)816 }817 pub fn eval(v: &str) -> Result<Self> {818 let s = CString::new(v).expect("expression shouldn't have internal NULs");819 let out = Self::new_uninit();820 with_default_context(|c, es| unsafe {821 expr_eval_from_string(c, es, s.as_ptr(), c"/root".as_ptr(), out.0)822 })?;823 Ok(out)824 }825 pub fn build(&self, output: &str) -> Result<PathBuf> {826 if !self.is_derivation() {827 bail!("expected derivation to build")828 }829 let output_name = self830 .get_field("outputName")831 .context("getting output name field")?832 .to_string()?;833 let v = if output_name != output {834 let out = self.get_field(output).context("getting target output")?;835 if !out.is_derivation() {836 bail!("unknown output: {output}");837 }838 out839 } else {840 self.clone()841 };842 // to_string here blocks until the path is built843 let s = v.builtin_to_string()?;844 let rs = s.to_realised_string()?;845 let drv_path = rs.as_str().to_owned();846 Ok(PathBuf::from(drv_path))847 }848 pub fn as_json<T: DeserializeOwned>(&self) -> Result<T> {849 let to_json = Self::eval("builtins.toJSON")?;850 let s = to_json.call(self.clone())?.to_string()?;851 Ok(serde_json::from_str(&s)?)852 }853 pub fn serialized<T: Serialize>(v: &T) -> Result<Self> {854 Self::eval(&nixlike::serialize(v)?)855 }856857 // Convert to string/evaluate derivations/etc858 // fn to_string_weak(&self) -> Result<String> {859 // // TODO: For now, it works exactly like to_string, see the comment for fn force()860 // self.to_string()861 // }862863 fn is_derivation(&self) -> bool {864 if !matches!(self.type_of(), NixType::Attrs) {865 return false;866 }867 let Some(ty) = self.get_field("type").ok() else {868 return false;869 };870 matches!(ty.to_string().as_deref(), Ok("derivation"))871 }872 fn functor_kind(&self) -> Option<FunctorKind> {873 match self.type_of() {874 NixType::Attrs => self875 .has_field("__functor")876 .expect("has_field shouldn't fail for attrs")877 .then_some(FunctorKind::Functor),878 NixType::Function => Some(FunctorKind::Function),879 _ => None,880 }881 }882 pub fn is_function(&self) -> bool {883 self.functor_kind().is_some()884 }885 pub fn is_null(&self) -> bool {886 matches!(self.type_of(), NixType::Null)887 }888 pub fn is_string(&self) -> bool {889 matches!(self.type_of(), NixType::String)890 }891 pub fn is_attrs(&self) -> bool {892 matches!(self.type_of(), NixType::Attrs)893 }894}895896impl From<String> for Value {897 fn from(value: String) -> Self {898 Value::new_str(&value)899 }900}901impl From<bool> for Value {902 fn from(value: bool) -> Self {903 Value::new_bool(value)904 }905}906impl From<&str> for Value {907 fn from(value: &str) -> Self {908 Value::new_str(value)909 }910}911impl<T> From<Vec<T>> for Value912where913 T: Into<Value>,914{915 fn from(value: Vec<T>) -> Self {916 Value::new_list(value)917 }918}919920impl Clone for Value {921 fn clone(&self) -> Self {922 with_default_context(|c, _| unsafe { value_incref(c, self.0) })923 .expect("value incref should not fail");924 Self(self.0)925 }926}927impl Drop for Value {928 fn drop(&mut self) {929 with_default_context(|c, _| unsafe { value_decref(c, self.0) })930 .expect("value drop should not fail");931 }932}933934pub fn init_libraries() {935 unsafe { GC_allow_register_threads() };936937 let mut ctx = NixContext::new();938 ctx.run_in_context(|c| unsafe { libutil_init(c) })939 .expect("util init should not fail");940 ctx.run_in_context(|c| unsafe { libstore_init(c) })941 .expect("store init should not fail");942 ctx.run_in_context(|c| unsafe { libexpr_init(c) })943 .expect("expr init should not fail");944945 nix_logging_cxx::apply_tracing_logger();946}947948unsafe extern "C" fn nix_primop_closure_adapter<const N: usize>(949 user_data: *mut c_void,950 context: *mut c_context,951 state: *mut nix_raw::EvalState,952 args: *mut *mut value,953 ret: *mut value,954) {955 let user_closure: &UserClosure<N> = unsafe { &*user_data.cast_const().cast() };956 let mut e = None;957 let args: [&Value; N] = array::from_fn(|i| {958 let v: &mut Value = unsafe { &mut *args.add(i).cast() };959960 info!("forcing arg");961 if matches!(v.type_of(), NixType::Thunk)962 && let Err(err) = v.force(state)963 {964 e = Some(err);965 };966 v as &Value967 });968 info!("args forced");969 let ctx: &mut NixContext = unsafe { &mut *context.cast() };970971 if let Some(e) = e {972 warn!("set err = {e}");973 unsafe { init_int(context, ret, 0) };974 return ctx.set_err(975 NixErrorKind::Unknown,976 &CString::new(e.to_string()).expect("forcing argument value failed"),977 );978 }979980 let state: &EvalState = unsafe { std::mem::transmute(&state) };981982 match user_closure(state, args) {983 Ok(v) => {984 unsafe { copy_value(context, ret, v.0) };985 }986 Err(e) => {987 unsafe { init_int(context, ret, 0) };988 warn!("set err = {e:#?}");989 ctx.set_err(990 NixErrorKind::Unknown,991 &CString::new(e.to_string()).expect("error should not contain internal nuls"),992 );993 }994 }995}996997type UserClosure<const N: usize> = Box<dyn Fn(&EvalState, [&Value; N]) -> Result<Value>>;998999pub struct NativeFn(*mut PrimOp);1000impl NativeFn {1001 pub fn new<const N: usize>(1002 name: &'static CStr,1003 doc: &'static CStr,1004 args: [&'static CStr; N],1005 f: impl Fn(&EvalState, [&Value; N]) -> Result<Value> + 'static,1006 ) -> Self {1007 // Double-boxing to make it thin pointer, as vtable gets outside of first Box1008 let closure: Box<UserClosure<N>> = Box::new(Box::new(f));1009 let f: PrimOpFun = Some(nix_primop_closure_adapter::<N>);1010 let mut args = args.into_iter().map(|v| v.as_ptr()).collect_vec();1011 args.push(null());1012 let args = args.as_mut_ptr();1013 let primop = unsafe {1014 alloc_primop(1015 null_mut(),1016 f,1017 N as i32,1018 name.as_ptr(),1019 args,1020 doc.as_ptr(),1021 Box::into_raw(closure).cast(),1022 )1023 };10241025 assert!(!primop.is_null(), "primop allocation should not fail");10261027 Self(primop)1028 }1029 pub fn register(self) {1030 unsafe { register_primop(null_mut(), self.0) };1031 }1032}10331034struct StorePath(*mut c_store_path);1035impl StorePath {}10361037impl Drop for StorePath {1038 fn drop(&mut self) {1039 unsafe { store_path_free(self.0) }1040 }1041}10421043#[test_log::test]1044fn test_native() -> Result<()> {1045 init_libraries();1046 NativeFn::new(1047 c"__uppercaseSuffix2",1048 c"make string uppercase and add suffix",1049 [c"str", c"suffix"],1050 |_, [str, suffix]: [&Value; 2]| {1051 let str = str.to_string()?;1052 let suffix = suffix.to_string()?;1053 Ok(Value::new_str(&format!("{}{suffix}", str.to_uppercase())))1054 },1055 )1056 .register();10571058 let mut fetch_settings = FetchSettings::new();1059 fetch_settings.set(c"warn-dirty", c"false");10601061 let manifest = format!("git+file://{}/../../", env!("CARGO_MANIFEST_DIR"));1062 let flake = FlakeSettings::new()?;1063 let parse = FlakeReferenceParseFlags::new(&flake)?;1064 let (mut r, _) = FlakeReference::new(&manifest, &flake, &parse, &fetch_settings)?;1065 let lock = FlakeLockFlags::new(&flake)?;1066 let locked = r.lock(&fetch_settings, &flake, &lock)?;1067 let attrs = locked.get_attrs(&mut FlakeSettings::new()?)?;10681069 let builtins = Value::eval("builtins")?;1070 assert_eq!(builtins.type_of(), NixType::Attrs);10711072 assert_eq!(attrs.type_of(), NixType::Attrs);1073 let test_data = nix_go!(attrs.testData);10741075 let test_string: String = nix_go_json!(test_data.testString);1076 assert_eq!(test_string, "hello");10771078 let s = nix_go!(attrs.packages["x86_64-linux"].fleet.drvPath);1079 let s = CString::new(s.to_string()?).expect("path str is cstring");10801081 let uppercase_suffix = Value::new_primop(NativeFn::new(1082 c"uppercase_suffix",1083 c"make string uppercase and add suffix",1084 [c"str", c"suffix"],1085 |es, [str, suffix]: [&Value; 2]| {1086 let str = str.to_string()?;1087 let suffix = suffix.to_string()?;1088 Ok(Value::new_str(&format!("{}{suffix}", str.to_uppercase())))1089 },1090 ));10911092 let test_result: String = nix_go_json!(test_data.testPrimop(uppercase_suffix));1093 assert_eq!(test_result, "PREFIX_BODY_SUFFIX");1094 let test_result: String = nix_go_json!(builtins.uppercaseSuffix2("test")("suffix"));1095 assert_eq!(test_result, "TESTsuffix");10961097 let nix_ctx = NixContext::new();1098 let store = GLOBAL_STATE.store.parse_path(s.as_c_str())?;10991100 // nix_raw::store_get_fs_closure(1);11011102 Ok(())1103}11041105// pub struct GcAlloc;1106// unsafe impl GlobalAlloc for GcAlloc {1107// unsafe fn alloc(&self, l: Layout) -> *mut u8 {1108// let ptr = unsafe { GC_malloc(l.size()) };1109// ptr.cast()1110// }1111// unsafe fn dealloc(&self, ptr: *mut u8, _: Layout) {1112// // unsafe { GC_free(ptr.cast()) };1113// }1114//1115// unsafe fn realloc(&self, ptr: *mut u8, _: Layout, new_size: usize) -> *mut u8 {1116// let ptr = unsafe { GC_realloc(ptr.cast(), new_size) };1117// ptr.cast()1118// }1119// }1120//1121// #[global_allocator]1122// static GC: GcAlloc = GcAlloc;crates/nix-eval/src/util.rsdiffbeforeafterboth--- a/crates/nix-eval/src/util.rs
+++ b/crates/nix-eval/src/util.rs
@@ -1,23 +1,15 @@
use std::time::Instant;
use anyhow::bail;
-use serde::Deserialize;
use tracing::{debug, warn};
use crate::{Value, nix_go_json};
-
-#[derive(Deserialize, Debug)]
-struct Assertion {
- assertion: bool,
- message: String,
-}
#[tracing::instrument(level = "info", skip(val))]
-pub async fn assert_warn(action: &str, val: &Value) -> anyhow::Result<()> {
+pub fn assert_warn(action: &str, val: &Value) -> anyhow::Result<()> {
let before_errors = Instant::now();
let errors: Vec<String> = nix_go_json!(val.errors);
- // let assertions: Vec<Assertion> = nix_go_json!(val.assertions);
- debug!("errors evaluation took {:?} {errors:?} ", before_errors.elapsed());
+ debug!("errors evaluation took {:?}", before_errors.elapsed());
if !errors.is_empty() {
bail!(
"failed with error{}{}",
lib/default.nixdiffbeforeafterboth--- a/lib/default.nix
+++ b/lib/default.nix
@@ -160,7 +160,7 @@
mkImpureSecretGenerator,
}:
mkImpureSecretGenerator {
- # TODO: Escape prompt?
+ # TODO: Escape prompt/part (preferrably just use env) to prevent shell injection
script = ''
${kdePackages.kdialog}/bin/kdialog --inputbox "${prompt}" | gh private -o $out/${part}
'';
modules/secrets.nixdiffbeforeafterboth--- a/modules/secrets.nix
+++ b/modules/secrets.nix
@@ -89,6 +89,7 @@
# If set - script will be run on remote machine, otherwise it will be run with fleet project in CWD
# (Some secrets-encryption-in-git/managed PKI solution is expected)
impureOn ? null,
+ generationData ? null,
parts,
}:
(prev.writeShellScript "impureGenerator.sh" ''
@@ -117,7 +118,7 @@
'').overrideAttrs
(old: {
passthru = {
- inherit impureOn parts;
+ inherit impureOn parts generationData;
generatorKind = "impure";
};
});