git.delta.rocks / jrsonnet / refs/commits / 4ad5065059c4

difftreelog

source

src/cmds/build_systems.rs2.8 KiBsourcehistory
1use std::process::Command;23use crate::{command::CommandExt, host::Config, nix::SYSTEMS_ATTRIBUTE};4use anyhow::Result;5use clap::Clap;6use log::info;78#[derive(Clap)]9#[clap(group = clap::ArgGroup::new("target"))]10pub struct BuildSystems {11	/// --builders arg for nix12	#[clap(long)]13	builders: Option<String>,14	/// Jobs to run locally15	#[clap(long)]16	jobs: Option<usize>,17	/// Do not continue on error18	#[clap(long)]19	fail_fast: bool,20	#[clap(long)]21	privileged_build: bool,22	#[clap(subcommand)]23	subcommand: Option<Subcommand>,24}2526#[derive(Clap)]27enum Subcommand {28	/// Switch to built system until reboot29	Test,30	/// Switch to built system after reboot31	Boot,32	/// test + boot33	Switch,34}35impl Subcommand {36	fn should_switch_profile(&self) -> bool {37		matches!(self, Self::Test | Self::Switch)38	}39	fn name(&self) -> &'static str {40		match self {41			Self::Test => "test",42			Self::Boot => "boot",43			Self::Switch => "switch",44		}45	}46}4748impl BuildSystems {49	pub fn run(self, config: &Config) -> Result<()> {50		println!("Build");51		let hosts = config.list_hosts()?;5253		for host in hosts.iter() {54			if config.should_skip(host) {55				continue;56			}57			info!("Building host {}", host);58			let built = {59				let dir = tempfile::tempdir()?;60				dir.path().to_owned()61			};6263			let mut nix_build = if self.privileged_build {64				let mut out = Command::new("sudo");65				out.arg("nix");66				out67			} else {68				Command::new("nix")69			};70			nix_build71				.args(&["build", "--impure", "--no-link", "--out-link"])72				.arg(&built)73				.arg(format!(74					"{}.{}.config.system.build.toplevel",75					SYSTEMS_ATTRIBUTE, host,76				));7778			if let Some(builders) = &self.builders {79				println!("Using builders: {}", builders);80				nix_build.arg("--builders").arg(builders);81			}82			if let Some(jobs) = &self.jobs {83				nix_build.arg("--max-jobs");84				nix_build.arg(format!("{}", jobs));85			}86			if !self.fail_fast {87				nix_build.arg("--keep-going");88			}8990			nix_build.inherit_stdio().run()?;91			let built = std::fs::canonicalize(built)?;92			info!("Built closure: {:?}", built);93			if !config.is_local(host) {94				info!("Uploading system closure");95				Command::new("nix")96					.args(&["copy", "--to"])97					.arg(format!("ssh://root@{}", host))98					.arg(&built)99					.inherit_stdio()100					.run()?;101			}102			if let Some(subcommand) = &self.subcommand {103				if subcommand.should_switch_profile() {104					info!("Switching generation");105					config106						.command_on(host, "nix-env", true)107						.args(&["-p", "/nix/var/nix/profiles/system", "--set"])108						.arg(&built)109						.inherit_stdio()110						.run()?;111				}112				info!("Executing activation script");113				let mut switch_script = built.clone();114				switch_script.push("bin");115				switch_script.push("switch-to-configuration");116				config117					.command_on(host, switch_script, true)118					.arg(subcommand.name())119					.inherit_stdio()120					.run()?;121			}122		}123		Ok(())124	}125}