git.delta.rocks / jrsonnet / refs/commits / 33e3a6cc33fd

difftreelog

feat basic lustration helper

Lach2025-04-24parent: #bd11592.patch.diff
in: trunk

3 files changed

modifiedcmds/fleet/src/cmds/build_systems.rsdiffbeforeafterboth
--- a/cmds/fleet/src/cmds/build_systems.rs
+++ b/cmds/fleet/src/cmds/build_systems.rs
@@ -1,6 +1,6 @@
 use std::{env::current_dir, os::unix::fs::symlink, path::PathBuf, time::Duration};
 
-use anyhow::{anyhow, bail, Result};
+use anyhow::{anyhow, bail, Context, Result};
 use clap::{Parser, ValueEnum};
 use fleet_base::{
 	host::{Config, ConfigHost, DeployKind},
@@ -132,10 +132,10 @@
 	disable_rollback: bool,
 ) -> Result<()> {
 	let deploy_kind = host.deploy_kind().await?;
-	if deploy_kind == DeployKind::NixosInstall
+	if (deploy_kind == DeployKind::NixosInstall || deploy_kind == DeployKind::NixosLustrate)
 		&& !matches!(action, DeployAction::Boot | DeployAction::Upload)
 	{
-		bail!("nixos-install deploy kind only supports boot and upload actions");
+		bail!("{deploy_kind:?} deploy kind only supports boot and upload actions");
 	}
 
 	let mut failed = false;
@@ -184,6 +184,17 @@
 			}
 		}
 	}
+	if deploy_kind == DeployKind::NixosLustrate {
+		// Fleet could also create this file, but as this operation is potentially disruptive,
+		// make user do it themself.
+		if !host.file_exists("/etc/NIXOS_LUSTRATE").await? {
+			bail!("/etc/NIXOS_LUSTRATE should be created on remote host");
+		}
+		// Wanted by NixOS to recognize the system as NixOS.
+		let mut cmd = host.cmd("touch").await?;
+		cmd.arg("/etc/NIXOS");
+		cmd.sudo().run().await.context("creating /etc/NIXOS")?;
+	}
 	if deploy_kind == DeployKind::NixosInstall {
 		info!(
 			"running nixos-install to switch profile, install bootloader, and perform activation"
@@ -247,6 +258,9 @@
 			};
 			let switch_script = specialised.join("bin/switch-to-configuration");
 			let mut cmd = host.cmd(switch_script).in_current_span().await?;
+			if deploy_kind == DeployKind::NixosLustrate {
+				cmd.env("NIXOS_INSTALL_BOOTLOADER", "1");
+			}
 			cmd.env("FLEET_ONLINE_ACTIVATION", "1")
 				.arg(action.name().expect("upload.should_activate == false"));
 			if let Err(e) = cmd.sudo().run().in_current_span().await {
modifiedcrates/fleet-base/src/host.rsdiffbeforeafterboth
23};23};
2424
25pub struct FleetConfigInternals {25pub struct FleetConfigInternals {
26 /// Fleet project directory, containing fleet.nix file.
26 pub local_system: String,27 pub directory: PathBuf,
28 /// builtins.currentSystem
27 pub directory: PathBuf,29 pub local_system: String,
28 pub data: Mutex<FleetData>,30 pub data: Mutex<FleetData>,
29 pub nix_args: Vec<OsString>,31 pub nix_args: Vec<OsString>,
30 /// fleet_config.config32 /// fleet_config.config
3436
35 /// import nixpkgs {system = local};37 /// import nixpkgs {system = local};
36 pub default_pkgs: Value,38 pub default_pkgs: Value,
39 /// inputs.nixpkgs
37 pub nixpkgs: Value,40 pub nixpkgs: Value,
3841
39 pub nix_session: NixSession,42 pub nix_session: NixSession,
58 Su,61 Su,
59}62}
6063
61#[derive(Clone, PartialEq, Copy)]64#[derive(Clone, PartialEq, Copy, Debug)]
62pub enum DeployKind {65pub enum DeployKind {
63 /// NixOS => NixOS managed by fleet66 /// NixOS => NixOS managed by fleet
64 UpgradeToFleet,67 UpgradeToFleet,
67 /// Remote host has /mnt, /mnt/boot mounted,70 /// Remote host has /mnt, /mnt/boot mounted,
68 /// generated config is added to fleet configuration.71 /// generated config is added to fleet configuration.
69 NixosInstall,72 NixosInstall,
73 /// Remote host has some system and nix installed in multi-user mode (/nix is owned by root),
74 /// generated config is added to fleet configuration,
75 /// and /etc/NIXOS_LUSTRATE exists, fleet will perform the rest.
76 NixosLustrate,
70}77}
7178
72impl FromStr for DeployKind {79impl FromStr for DeployKind {
302 nix.arg("copy").arg("--substitute-on-destination");309 nix.arg("copy").arg("--substitute-on-destination");
303310
304 match self.deploy_kind().await? {311 match self.deploy_kind().await? {
305 DeployKind::Fleet | DeployKind::UpgradeToFleet => {312 DeployKind::Fleet | DeployKind::UpgradeToFleet | DeployKind::NixosLustrate => {
306 nix.comparg("--to", format!("ssh-ng://{}", self.name));313 nix.comparg("--to", format!("ssh-ng://{}", self.name));
307 }314 }
308 DeployKind::NixosInstall => {315 DeployKind::NixosInstall => {
modifiedcrates/fleet-base/src/opts.rsdiffbeforeafterboth
--- a/crates/fleet-base/src/opts.rs
+++ b/crates/fleet-base/src/opts.rs
@@ -6,7 +6,7 @@
 	sync::{Arc, Mutex},
 };
 
-use anyhow::{Context, Result};
+use anyhow::{bail, Context, Result};
 use clap::Parser;
 use nix_eval::{nix_go, util::assert_warn, NixSessionPool, Value};
 use nom::{
@@ -182,7 +182,23 @@
 
 	// TODO: Config should be detached from opts.
 	pub async fn build(&self, nix_args: Vec<OsString>, assert: bool) -> Result<Config> {
-		let directory = current_dir()?;
+		let cwd = current_dir()?;
+		let mut directory = cwd.clone();
+		let mut fleet_data_path = directory.join("fleet.nix");
+		while !fleet_data_path.is_file() {
+			// fleet.nix
+			fleet_data_path.pop();
+			if !directory.pop() || !fleet_data_path.pop() {
+				bail!(
+					"fleet.nix not found at {} or any of the parent directories",
+					cwd.display()
+				);
+			}
+			fleet_data_path.push("fleet.nix");
+		}
+		let bytes =
+			std::fs::read_to_string(&fleet_data_path).context("reading fleet state (fleet.nix)")?;
+		let data: Mutex<FleetData> = nixlike::parse_str(&bytes)?;
 
 		let pool = NixSessionPool::new(
 			directory.as_os_str().to_owned(),
@@ -193,12 +209,6 @@
 		let nix_session = pool.get().await?;
 
 		let builtins_field = Value::binding(nix_session.clone(), "builtins").await?;
-
-		let mut fleet_data_path = directory.clone();
-		fleet_data_path.push("fleet.nix");
-		let bytes =
-			std::fs::read_to_string(fleet_data_path).context("reading fleet state (fleet.nix)")?;
-		let data: Mutex<FleetData> = nixlike::parse_str(&bytes)?;
 
 		let fleet_root = Value::binding(nix_session.clone(), "fleetConfigurations").await?;
 		let fleet_field = nix_go!(fleet_root.default({ data }));