1use std::process::Command;1use std::{env::current_dir, process::Command};
22
3use 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}
2424
25#[derive(StructOpt)]
26enum Subcommand {25enum UploadAction {
27
28 Test,26 Test,
29
30 Boot,27 Boot,
31
32 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 }
38
39 pub(crate) fn should_switch_profile(&self) -> bool {
40 matches!(self, Self::Switch | Self::Test)
41 }
45}42}
43
44enum PackageAction {
45 SdImage,
46}
47
48enum Action {
49 Upload(Option<UploadAction>),
50 Package(PackageAction),
51}
52
53impl 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}
64
65#[derive(StructOpt, Clone)]
66enum Subcommand {
67
68 Upload,
69
70 Test,
71
72 Boot,
73
74 Switch,
75
76
77 SdImage,
78}
4679
47impl 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);
123
124 let action = Action::from(self.subcommand.clone());
125
126 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 config
103 .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 config
114 .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));
161
162 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 out
167 } else {
168 Command::new("nix")
169 };
170 nix_build
171 .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 }
187
188 nix_build.inherit_stdio().run()?;
189 }
190 };
119 }191 }
120 Ok(())192 Ok(())
121 }193 }