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

difftreelog

feat create gc root per built system

Yaroslav Bolyukin2024-11-19parent: #353fd26.patch.diff
in: trunk

5 files changed

modifiedCargo.lockdiffbeforeafterboth
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -962,6 +962,7 @@
  "nixlike",
  "nom",
  "openssh",
+ "rand",
  "serde",
  "serde_json",
  "tempfile",
modifiedcmds/fleet/src/cmds/build_systems.rsdiffbeforeafterboth
--- a/cmds/fleet/src/cmds/build_systems.rs
+++ b/cmds/fleet/src/cmds/build_systems.rs
@@ -253,12 +253,12 @@
 
 async fn build_task(
 	config: Config,
-	host: String,
+	hostname: String,
 	build_attr: &str,
 	batch: Option<NixBuildBatch>,
 ) -> Result<PathBuf> {
 	info!("building");
-	let host = config.host(&host).await?;
+	let host = config.host(&hostname).await?;
 	// let action = Action::from(self.subcommand.clone());
 	let nixos = host.nixos_config().await?;
 	let drv = nix_go!(nixos.system.build[{ build_attr }]);
@@ -267,6 +267,21 @@
 		.get("out")
 		.ok_or_else(|| anyhow!("system build should produce \"out\" output"))?;
 
+	{
+		info!("adding gc root");
+		let mut cmd = config.local_host().cmd("nix").await?;
+		cmd.arg("build")
+			.comparg(
+				"--profile",
+				format!(
+					"/nix/var/nix/profiles/{}-{hostname}",
+					config.data().gc_root_prefix
+				),
+			)
+			.arg(out_output);
+		cmd.sudo().run_nix().await?;
+	}
+
 	Ok(out_output.clone())
 }
 
modifiedcrates/fleet-base/Cargo.tomldiffbeforeafterboth
before · crates/fleet-base/Cargo.toml
1[package]2name = "fleet-base"3edition = "2021"4version.workspace = true56[dependencies]7age.workspace = true8anyhow.workspace = true9better-command.workspace = true10chrono = "0.4.38"11clap = { workspace = true, features = ["derive"] }12fleet-shared.workspace = true13futures = "0.3.30"14hostname = "0.4.0"15itertools = "0.13.0"16nix-eval.workspace = true17nixlike.workspace = true18nom = "7.1.3"19openssh = "0.11.0"20serde.workspace = true21serde_json = "1.0.127"22tempfile.workspace = true23tokio.workspace = true24tokio-util = "0.7.11"25tracing.workspace = true
after · crates/fleet-base/Cargo.toml
1[package]2name = "fleet-base"3edition = "2021"4version.workspace = true56[dependencies]7age.workspace = true8anyhow.workspace = true9better-command.workspace = true10chrono = "0.4.38"11clap = { workspace = true, features = ["derive"] }12fleet-shared.workspace = true13futures = "0.3.30"14hostname = "0.4.0"15itertools = "0.13.0"16nix-eval.workspace = true17nixlike.workspace = true18nom = "7.1.3"19openssh = "0.11.0"20rand = "0.8.5"21serde.workspace = true22serde_json = "1.0.127"23tempfile.workspace = true24tokio.workspace = true25tokio-util = "0.7.11"26tracing.workspace = true
modifiedcrates/fleet-base/src/fleetdata.rsdiffbeforeafterboth
--- a/crates/fleet-base/src/fleetdata.rs
+++ b/crates/fleet-base/src/fleetdata.rs
@@ -6,6 +6,10 @@
 use age::Recipient;
 use chrono::{DateTime, Utc};
 use fleet_shared::SecretData;
+use rand::{
+	distributions::{Alphanumeric, DistString},
+	thread_rng,
+};
 use serde::{de::Error, Deserialize, Serialize};
 use serde_json::Value;
 
@@ -42,10 +46,17 @@
 	}
 }
 
+fn generate_gc_prefix() -> String {
+	let id = Alphanumeric.sample_string(&mut thread_rng(), 8);
+	format!("fleet-gc-{id}")
+}
+
 #[derive(Serialize, Deserialize)]
 #[serde(rename_all = "camelCase")]
 pub struct FleetData {
 	pub version: FleetDataVersion,
+	#[serde(default = "generate_gc_prefix")]
+	pub gc_root_prefix: String,
 
 	#[serde(default)]
 	pub hosts: BTreeMap<String, HostData>,
modifiedmodules/hosts.nixdiffbeforeafterboth
--- a/modules/hosts.nix
+++ b/modules/hosts.nix
@@ -16,6 +16,10 @@
             type = str;
             internal = true;
           };
+          gcRootPrefix = mkOption {
+            type = str;
+            internal = true;
+          };
           hosts = mkOption {
             type = attrsOf (submodule {
               options.encryptionKey = mkOption {