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
253253
254async fn build_task(254async fn build_task(
255 config: Config,255 config: Config,
256 host: String,256 hostname: String,
257 build_attr: &str,257 build_attr: &str,
258 batch: Option<NixBuildBatch>,258 batch: Option<NixBuildBatch>,
259) -> Result<PathBuf> {259) -> Result<PathBuf> {
260 info!("building");260 info!("building");
261 let host = config.host(&host).await?;261 let host = config.host(&hostname).await?;
262 // let action = Action::from(self.subcommand.clone());262 // let action = Action::from(self.subcommand.clone());
263 let nixos = host.nixos_config().await?;263 let nixos = host.nixos_config().await?;
264 let drv = nix_go!(nixos.system.build[{ build_attr }]);264 let drv = nix_go!(nixos.system.build[{ build_attr }]);
267 .get("out")267 .get("out")
268 .ok_or_else(|| anyhow!("system build should produce \"out\" output"))?;268 .ok_or_else(|| anyhow!("system build should produce \"out\" output"))?;
269
270 {
271 info!("adding gc root");
272 let mut cmd = config.local_host().cmd("nix").await?;
273 cmd.arg("build")
274 .comparg(
275 "--profile",
276 format!(
277 "/nix/var/nix/profiles/{}-{hostname}",
278 config.data().gc_root_prefix
279 ),
280 )
281 .arg(out_output);
282 cmd.sudo().run_nix().await?;
283 }
269284
270 Ok(out_output.clone())285 Ok(out_output.clone())
271}286}
modifiedcrates/fleet-base/Cargo.tomldiffbeforeafterboth
--- a/crates/fleet-base/Cargo.toml
+++ b/crates/fleet-base/Cargo.toml
@@ -17,6 +17,7 @@
 nixlike.workspace = true
 nom = "7.1.3"
 openssh = "0.11.0"
+rand = "0.8.5"
 serde.workspace = true
 serde_json = "1.0.127"
 tempfile.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 {