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
--- a/crates/fleet-base/src/host.rs
+++ b/crates/fleet-base/src/host.rs
@@ -23,8 +23,10 @@
 };
 
 pub struct FleetConfigInternals {
+	/// Fleet project directory, containing fleet.nix file.
+	pub directory: PathBuf,
+	/// builtins.currentSystem
 	pub local_system: String,
-	pub directory: PathBuf,
 	pub data: Mutex<FleetData>,
 	pub nix_args: Vec<OsString>,
 	/// fleet_config.config
@@ -34,6 +36,7 @@
 
 	/// import nixpkgs {system = local};
 	pub default_pkgs: Value,
+	/// inputs.nixpkgs
 	pub nixpkgs: Value,
 
 	pub nix_session: NixSession,
@@ -58,7 +61,7 @@
 	Su,
 }
 
-#[derive(Clone, PartialEq, Copy)]
+#[derive(Clone, PartialEq, Copy, Debug)]
 pub enum DeployKind {
 	/// NixOS => NixOS managed by fleet
 	UpgradeToFleet,
@@ -67,6 +70,10 @@
 	/// Remote host has /mnt, /mnt/boot mounted,
 	/// generated config is added to fleet configuration.
 	NixosInstall,
+	/// Remote host has some system and nix installed in multi-user mode (/nix is owned by root),
+	/// generated config is added to fleet configuration,
+	/// and /etc/NIXOS_LUSTRATE exists, fleet will perform the rest.
+	NixosLustrate,
 }
 
 impl FromStr for DeployKind {
@@ -302,7 +309,7 @@
 		nix.arg("copy").arg("--substitute-on-destination");
 
 		match self.deploy_kind().await? {
-			DeployKind::Fleet | DeployKind::UpgradeToFleet => {
+			DeployKind::Fleet | DeployKind::UpgradeToFleet | DeployKind::NixosLustrate => {
 				nix.comparg("--to", format!("ssh-ng://{}", self.name));
 			}
 			DeployKind::NixosInstall => {
modifiedcrates/fleet-base/src/opts.rsdiffbeforeafterboth
6 sync::{Arc, Mutex},6 sync::{Arc, Mutex},
7};7};
88
9use anyhow::{Context, Result};9use anyhow::{bail, Context, Result};
10use clap::Parser;10use clap::Parser;
11use nix_eval::{nix_go, util::assert_warn, NixSessionPool, Value};11use nix_eval::{nix_go, util::assert_warn, NixSessionPool, Value};
12use nom::{12use nom::{
182182
183 // TODO: Config should be detached from opts.183 // TODO: Config should be detached from opts.
184 pub async fn build(&self, nix_args: Vec<OsString>, assert: bool) -> Result<Config> {184 pub async fn build(&self, nix_args: Vec<OsString>, assert: bool) -> Result<Config> {
185 let directory = current_dir()?;185 let cwd = current_dir()?;
186 let mut directory = cwd.clone();
187 let mut fleet_data_path = directory.join("fleet.nix");
188 while !fleet_data_path.is_file() {
189 // fleet.nix
190 fleet_data_path.pop();
191 if !directory.pop() || !fleet_data_path.pop() {
192 bail!(
193 "fleet.nix not found at {} or any of the parent directories",
194 cwd.display()
195 );
196 }
197 fleet_data_path.push("fleet.nix");
198 }
199 let bytes =
200 std::fs::read_to_string(&fleet_data_path).context("reading fleet state (fleet.nix)")?;
201 let data: Mutex<FleetData> = nixlike::parse_str(&bytes)?;
186202
187 let pool = NixSessionPool::new(203 let pool = NixSessionPool::new(
188 directory.as_os_str().to_owned(),204 directory.as_os_str().to_owned(),
194210
195 let builtins_field = Value::binding(nix_session.clone(), "builtins").await?;211 let builtins_field = Value::binding(nix_session.clone(), "builtins").await?;
196
197 let mut fleet_data_path = directory.clone();
198 fleet_data_path.push("fleet.nix");
199 let bytes =
200 std::fs::read_to_string(fleet_data_path).context("reading fleet state (fleet.nix)")?;
201 let data: Mutex<FleetData> = nixlike::parse_str(&bytes)?;
202212
203 let fleet_root = Value::binding(nix_session.clone(), "fleetConfigurations").await?;213 let fleet_root = Value::binding(nix_session.clone(), "fleetConfigurations").await?;
204 let fleet_field = nix_go!(fleet_root.default({ data }));214 let fleet_field = nix_go!(fleet_root.default({ data }));