difftreelog
feat build sd-image
in: trunk
3 files changed
cmds/fleet/src/cmds/build_systems.rsdiffbeforeafterboth1use std::process::Command;1use std::{env::current_dir, process::Command};223use crate::{command::CommandExt, host::Config, nix::SYSTEMS_ATTRIBUTE};3use crate::{command::CommandExt, host::Config, nix::SYSTEMS_ATTRIBUTE};4use anyhow::Result;4use anyhow::Result;19 #[structopt(long)]19 #[structopt(long)]20 privileged_build: bool,20 privileged_build: bool,21 #[structopt(subcommand)]21 #[structopt(subcommand)]22 subcommand: Option<Subcommand>,22 subcommand: Subcommand,23}23}242425#[derive(StructOpt)]26enum Subcommand {25enum UploadAction {27 /// Switch to built system until reboot28 Test,26 Test,29 /// Switch to built system after reboot30 Boot,27 Boot,31 /// test + boot32 Switch,28 Switch,33}29}34impl Subcommand {30impl UploadAction {35 fn should_switch_profile(&self) -> bool {36 matches!(self, Self::Test | Self::Switch)37 }38 fn name(&self) -> &'static str {31 fn name(&self) -> &'static str {39 match self {32 match self {40 Self::Test => "test",33 UploadAction::Test => "test",41 Self::Boot => "boot",34 UploadAction::Boot => "boot",42 Self::Switch => "switch",35 UploadAction::Switch => "switch",43 }36 }44 }37 }3839 pub(crate) fn should_switch_profile(&self) -> bool {40 matches!(self, Self::Switch | Self::Test)41 }45}42}4344enum PackageAction {45 SdImage,46}4748enum Action {49 Upload(Option<UploadAction>),50 Package(PackageAction),51}5253impl From<Subcommand> for Action {54 fn from(s: Subcommand) -> Self {55 match s {56 Subcommand::Upload => Self::Upload(None),57 Subcommand::Test => Self::Upload(Some(UploadAction::Test)),58 Subcommand::Boot => Self::Upload(Some(UploadAction::Boot)),59 Subcommand::Switch => Self::Upload(Some(UploadAction::Switch)),60 Subcommand::SdImage => Self::Package(PackageAction::SdImage),61 }62 }63}6465#[derive(StructOpt, Clone)]66enum Subcommand {67 /// Upload, but do not switch68 Upload,69 /// Upload + switch to built system until reboot70 Test,71 /// Upload + switch to built system after reboot72 Boot,73 /// Upload + test + boot74 Switch,7576 /// Build sd image77 SdImage,78}467947impl BuildSystems {80impl BuildSystems {48 pub fn run(self, config: &Config) -> Result<()> {81 pub fn run(self, config: &Config) -> Result<()> {88 let built = std::fs::canonicalize(built)?;121 let built = std::fs::canonicalize(built)?;89 info!("Built closure: {:?}", built);122 info!("Built closure: {:?}", built);123124 let action = Action::from(self.subcommand.clone());125126 match action {127 Action::Upload(action) => {90 if !config.is_local(host) {128 if !config.is_local(host) {91 info!("Uploading system closure");129 info!("Uploading system closure");92 Command::new("nix")130 Command::new("nix")96 .inherit_stdio()134 .inherit_stdio()97 .run()?;135 .run()?;98 }136 }99 if let Some(subcommand) = &self.subcommand {137 if let Some(action) = action {100 if subcommand.should_switch_profile() {138 if action.should_switch_profile() {101 info!("Switching generation");139 info!("Switching generation");102 config140 config103 .command_on(host, "nix-env", true)141 .command_on(host, "nix-env", true)112 switch_script.push("switch-to-configuration");150 switch_script.push("switch-to-configuration");113 config151 config114 .command_on(host, switch_script, true)152 .command_on(host, switch_script, true)115 .arg(subcommand.name())153 .arg(action.name())116 .inherit_stdio()154 .inherit_stdio()117 .run()?;155 .run()?;118 }156 }157 }158 Action::Package(PackageAction::SdImage) => {159 let mut out = current_dir()?;160 out.push(format!("sd-image-{}", host));161162 info!("Building sd image to {:?}", out);163 let mut nix_build = if self.privileged_build {164 let mut out = Command::new("sudo");165 out.arg("nix");166 out167 } else {168 Command::new("nix")169 };170 nix_build171 .args(&["build", "--impure", "--no-link", "--out-link"])172 .arg(&out)173 .arg(format!(174 "{}.{}.config.system.build.sdImage",175 SYSTEMS_ATTRIBUTE, host,176 ));177 if let Some(builders) = &self.builders {178 nix_build.arg("--builders").arg(builders);179 }180 if let Some(jobs) = &self.jobs {181 nix_build.arg("--max-jobs");182 nix_build.arg(format!("{}", jobs));183 }184 if !self.fail_fast {185 nix_build.arg("--keep-going");186 }187188 nix_build.inherit_stdio().run()?;189 }190 };119 }191 }120 Ok(())192 Ok(())121 }193 }lib/default.nixdiffbeforeafterboth22 inherit name;22 inherit name;23 value = nixpkgs.lib.nixosSystem {23 value = nixpkgs.lib.nixosSystem {24 system = configuredHosts.${name}.system;24 system = configuredHosts.${name}.system;25 modules = configuredHosts.${name}.modules;25 modules = configuredHosts.${name}.modules ++ (26 pkgs = import nixpkgs { system = configuredHosts.${name}.system; };26 if configuredHosts.${name}.system == "aarch64-linux" then [ (nixpkgs + "/nixos/modules/installer/sd-card/sd-image-aarch64-installer.nix") ]27 else [ ]28 );27 };29 };28 }30 }29 )31 )modules/nixos/fleetPkgs.nixdiffbeforeafterbothno syntactic changes