difftreelog
feat create gc root per built system
in: trunk
5 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -962,6 +962,7 @@
"nixlike",
"nom",
"openssh",
+ "rand",
"serde",
"serde_json",
"tempfile",
cmds/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())
}
crates/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
crates/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>,
modules/hosts.nixdiffbeforeafterboth1{2 lib,3 fleetLib,4 ...5}: let6 inherit (fleetLib.modules) mkFleetGeneratorDefault;7 inherit (fleetLib.types) mkHostsType mkDataType;8 inherit (lib.options) mkOption;9 inherit (lib.types) str listOf attrsOf submodule;10in {11 options = {12 data = mkOption {13 type = mkDataType {14 options = {15 version = mkOption {16 type = str;17 internal = true;18 };19 hosts = mkOption {20 type = attrsOf (submodule {21 options.encryptionKey = mkOption {22 type = str;23 description = "Rage SSH encryption key for secrets.";24 };25 });26 };27 };28 };29 description = ''30 Configuration provided from outside.31 Usually used to persist fleet data between runs.32 '';33 };34 hosts = mkOption {35 type = mkHostsType ({config, ...}: {36 options = {37 system = mkOption {38 description = "Type of the system.";39 type = str;40 example = "x86_64-linux";41 };42 tags = mkOption {43 description = "Host tag. In CLI, you can refer to all hosts having this tag using @tag syntax.";44 type = listOf str;45 };46 network = mkOption {47 type = submodule {48 options = {49 internalIps = mkOption {50 description = "Internal ips";51 type = listOf str;52 default = [];53 };54 externalIps = mkOption {55 description = "External ips";56 type = listOf str;57 default = [];58 };59 };60 };61 description = "Network definition of host";62 };63 };64 config = {65 nixos.networking.hostName = mkFleetGeneratorDefault config._module.args.name;66 tags = ["all"];67 };68 _file = ./meta.nix;69 });70 default = {};71 description = "Configurations of individual hosts";72 };73 };74 _file = ./meta.nix;75}