Monorepo for Tangled
tangled.org
1use crate::command::{CaptureOutput, OutKind, Spec, run_capture, spawn_streaming};
2use crate::nix_config::{SPINDLE_RUN_DIR, nix_executable};
3use crate::protocol::{self, Message, v1};
4use anyhow::{Context, Result};
5use std::fs;
6use std::path::{Path, PathBuf};
7use std::time::Duration;
8use tokio::sync::mpsc::Sender;
9use tracing::info;
10
11const USER_CONFIG_DIR: &str = "/run/spindle/user-config";
12const DEVSHELL_ENV_PATH: &str = "/run/spindle/devshell-env.sh";
13const DEVSHELL_DRV: &str = "/etc/spindle/devshell.drv";
14
15pub async fn run(id: String, req: v1::ActivateConfig, out: Sender<Message>) {
16 let config_key = req.config_key.clone();
17 let result = activate(&id, &req, &out).await;
18 let msg = Message {
19 id,
20 activate_config_result: Some(v1::ActivateConfigResult {
21 config_key,
22 toplevel: (result.as_ref())
23 .map(|p| p.to_string_lossy().into_owned())
24 .unwrap_or_default(),
25 error: protocol::error_or_empty(result.err().map(|e| format!("{e:#}"))),
26 }),
27 ..Default::default()
28 };
29 let _ = out.send(msg).await;
30}
31
32async fn activate(id: &str, req: &v1::ActivateConfig, out: &Sender<Message>) -> Result<PathBuf> {
33 let need_build = req.toplevel.is_empty();
34 let timeout = (req.timeout_seconds > 0)
35 .then(|| Duration::from_secs(u64::from(req.timeout_seconds)))
36 .or_else(|| need_build.then_some(Duration::from_secs(10 * 60)))
37 .unwrap_or(Duration::from_secs(2 * 60));
38
39 let toplevel = if need_build {
40 build_toplevel(id, req, timeout, out).await?
41 } else {
42 realise_toplevel(id, &req.toplevel, timeout, out).await?
43 };
44
45 if !toplevel.starts_with("/nix/store/") {
46 anyhow::bail!("config toplevel {toplevel:?} is not a nix store path");
47 }
48
49 switch_to_configuration(&toplevel, timeout).await?;
50 write_devshell_env(timeout).await?;
51 info!(
52 config_key = %req.config_key,
53 base_config_hash = %req.base_config_hash,
54 ?toplevel,
55 "activated NixOS config"
56 );
57 Ok(toplevel)
58}
59
60async fn write_devshell_env(timeout: Duration) -> Result<()> {
61 let drv = match fs::canonicalize(DEVSHELL_DRV) {
62 Ok(p) => p,
63 Err(_) => {
64 let _ = fs::remove_file(DEVSHELL_ENV_PATH);
65 return Ok(());
66 }
67 };
68
69 info!(
70 ?drv,
71 "running nix print-dev-env for dependencies devshell..."
72 );
73 let output = run_capture(
74 Spec::new(nix_executable())
75 .args([
76 "print-dev-env".into(),
77 "--show-trace".into(),
78 drv.into_os_string(),
79 ])
80 .cwd(SPINDLE_RUN_DIR)
81 .timeout(timeout),
82 )
83 .await?;
84
85 if !output.success() {
86 anyhow::bail!(
87 "nix print-dev-env failed: exit={} error={:?} output={}",
88 output.exit.exit_code,
89 output.exit.error,
90 output.combined_lossy(),
91 );
92 }
93
94 fs::write(DEVSHELL_ENV_PATH, &output.stdout)
95 .with_context(|| format!("write {DEVSHELL_ENV_PATH}"))?;
96 info!(path = %DEVSHELL_ENV_PATH, "wrote devshell env");
97 Ok(())
98}
99
100async fn build_toplevel(
101 id: &str,
102 req: &v1::ActivateConfig,
103 timeout: Duration,
104 out: &Sender<Message>,
105) -> Result<PathBuf> {
106 let user_config = (req.user_config.is_empty())
107 .then_some("{}")
108 .unwrap_or_else(|| &req.user_config);
109
110 info!("writing user config to {USER_CONFIG_DIR}/config.json");
111 write_user_config(user_config).context("write user config")?;
112
113 info!("running nix build command for user config toplevel...");
114 let output = run_streaming_stderr(
115 Spec::new(nix_executable())
116 .args([
117 "build",
118 "--no-link",
119 "--show-trace",
120 "--json",
121 "--file",
122 "/etc/spindle/nixos/default.nix",
123 ])
124 .cwd(SPINDLE_RUN_DIR)
125 .timeout(timeout),
126 id,
127 out,
128 )
129 .await?;
130
131 if !output.success() {
132 anyhow::bail!(
133 "nix config build failed: exit={} error={:?} output={}",
134 output.exit.exit_code,
135 output.exit.error,
136 output.combined_lossy(),
137 );
138 }
139
140 #[derive(Debug, serde::Deserialize)]
141 struct NixBuildResult {
142 outputs: NixBuildOutputs,
143 }
144 #[derive(Debug, serde::Deserialize)]
145 struct NixBuildOutputs {
146 out: PathBuf,
147 }
148 let [result] = serde_json::from_slice::<[NixBuildResult; 1]>(&output.stdout)
149 .context("parse nix build --json output")?;
150 Ok(result.outputs.out)
151}
152
153fn write_user_config(user_config: &str) -> Result<()> {
154 fs::create_dir_all(USER_CONFIG_DIR).with_context(|| format!("create {USER_CONFIG_DIR}"))?;
155
156 let config_path = format!("{USER_CONFIG_DIR}/config.json");
157 fs::write(&config_path, user_config).with_context(|| format!("write {config_path}"))?;
158 Ok(())
159}
160
161async fn realise_toplevel(
162 id: &str,
163 toplevel: &str,
164 timeout: Duration,
165 out: &Sender<Message>,
166) -> Result<PathBuf> {
167 if !toplevel.starts_with("/nix/store/") {
168 anyhow::bail!("cached config toplevel {toplevel:?} is not a nix store path");
169 }
170 let output = run_streaming_stderr(
171 Spec::new(nix_executable())
172 .args(["build", "--no-link", "--show-trace", toplevel])
173 .timeout(timeout),
174 id,
175 out,
176 )
177 .await?;
178 if !output.success() {
179 anyhow::bail!(
180 "realise cached config failed: exit={} error={:?} output={}",
181 output.exit.exit_code,
182 output.exit.error,
183 output.combined_lossy(),
184 );
185 }
186
187 Ok(PathBuf::from(toplevel))
188}
189
190// streams stderr but captures stdout
191async fn run_streaming_stderr(
192 spec: Spec,
193 id: &str,
194 out: &Sender<Message>,
195) -> Result<CaptureOutput> {
196 let running = spawn_streaming(spec)?;
197 let mut stdout = Vec::new();
198 let mut stderr = Vec::new();
199 let (mut events, exit_task) = running.into_parts();
200
201 while let Some(event) = events.recv().await {
202 match event.kind {
203 OutKind::Stdout => stdout.extend_from_slice(&event.data),
204 OutKind::Stderr => {
205 stderr.extend_from_slice(&event.data);
206 let data = String::from_utf8_lossy(&event.data).into_owned();
207 let _ = out
208 .send(Message {
209 id: id.to_owned(),
210 exec_stderr: Some(v1::ExecStderr { data }),
211 ..Default::default()
212 })
213 .await;
214 }
215 }
216 }
217
218 let exit = exit_task
219 .await
220 .unwrap_or_else(|error| Err(anyhow::anyhow!("command supervisor failed: {error}")))?;
221 Ok(CaptureOutput {
222 exit,
223 stdout,
224 stderr,
225 })
226}
227
228async fn switch_to_configuration(toplevel: &Path, timeout: Duration) -> Result<()> {
229 info!("switching to new configuration: {:?}", toplevel);
230 let switch = toplevel.join("bin/switch-to-configuration");
231 let output = run_capture(Spec::new(switch).args(["test"]).timeout(timeout)).await?;
232 if !output.success() {
233 anyhow::bail!(
234 "switch-to-configuration test failed: exit={} error={:?} output={}",
235 output.exit.exit_code,
236 output.exit.error,
237 output.combined_lossy(),
238 );
239 }
240 Ok(())
241}