1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
// SPDX-FileCopyrightText: 2020 Serokell <https://serokell.io/>
//
// SPDX-License-Identifier: MPL-2.0
use log::{debug, info};
use std::collections::HashMap;
use std::path::Path;
use std::process::Stdio;
use thiserror::Error;
use tokio::process::Command;
#[derive(Error, Debug)]
pub enum PushProfileError {
#[error("Failed to run Nix show-derivation command: {0}")]
ShowDerivation(std::io::Error),
#[error("Nix show-derivation command resulted in a bad exit code: {0:?}")]
ShowDerivationExit(Option<i32>),
#[error("Nix show-derivation command output contained an invalid UTF-8 sequence: {0}")]
ShowDerivationUtf8(std::str::Utf8Error),
#[error("Failed to parse the output of nix show-derivation: {0}")]
ShowDerivationParse(serde_json::Error),
#[error("Nix show-derivation output is empty")]
ShowDerivationEmpty,
#[error("Failed to run Nix build command: {0}")]
Build(std::io::Error),
#[error("Nix build command resulted in a bad exit code: {0:?}")]
BuildExit(Option<i32>),
#[error(
"Activation script deploy-rs-activate does not exist in profile.\n\
Did you forget to use deploy-rs#lib.<...>.activate.<...> on your profile path?"
)]
DeployRsActivateDoesntExist,
#[error("Activation script activate-rs does not exist in profile.\n\
Is there a mismatch in deploy-rs used in the flake you're deploying and deploy-rs command you're running?")]
ActivateRsDoesntExist,
#[error("Failed to run Nix sign command: {0}")]
Sign(std::io::Error),
#[error("Nix sign command resulted in a bad exit code: {0:?}")]
SignExit(Option<i32>),
#[error("Failed to run Nix copy command: {0}")]
Copy(std::io::Error),
#[error("Nix copy command resulted in a bad exit code: {0:?}")]
CopyExit(Option<i32>),
#[error("The remote building option is not supported when using legacy nix")]
RemoteBuildWithLegacyNix,
#[error("Failed to run Nix path-info command: {0}")]
PathInfo(std::io::Error),
}
pub struct PushProfileData<'a> {
pub supports_flakes: bool,
pub check_sigs: bool,
pub repo: &'a str,
pub deploy_data: &'a super::DeployData<'a>,
pub deploy_defs: &'a super::DeployDefs,
pub keep_result: bool,
pub result_path: Option<&'a str>,
pub extra_build_args: &'a [String],
pub use_nom: bool,
}
pub async fn build_profile_locally(data: &PushProfileData<'_>, derivation_name: &str) -> Result<(), PushProfileError> {
info!(
"Building profile `{}` for node `{}`",
data.deploy_data.profile_name, data.deploy_data.node_name
);
let mut build_command = if data.supports_flakes {
Command::new(if data.use_nom {"nom"} else {"nix"})
} else {
Command::new(if data.use_nom {"nom-build"} else {"nix-build"})
};
if data.supports_flakes {
build_command.arg("build").arg(derivation_name)
} else {
build_command.arg(derivation_name)
};
match (data.keep_result, data.supports_flakes) {
(true, _) => {
let result_path = data.result_path.unwrap_or("./.deploy-gc");
build_command.arg("--out-link").arg(format!(
"{}/{}/{}",
result_path, data.deploy_data.node_name, data.deploy_data.profile_name
))
}
(false, false) => build_command.arg("--no-out-link"),
(false, true) => build_command.arg("--no-link"),
};
build_command.args(data.extra_build_args);
let build_exit_status = build_command
// Logging should be in stderr, this just stops the store path from printing for no reason
.stdout(Stdio::null())
.status()
.await
.map_err(PushProfileError::Build)?;
match build_exit_status.code() {
Some(0) => (),
a => return Err(PushProfileError::BuildExit(a)),
};
if !Path::new(
format!(
"{}/deploy-rs-activate",
data.deploy_data.profile.profile_settings.path
)
.as_str(),
)
.exists()
{
return Err(PushProfileError::DeployRsActivateDoesntExist);
}
if !Path::new(
format!(
"{}/activate-rs",
data.deploy_data.profile.profile_settings.path
)
.as_str(),
)
.exists()
{
return Err(PushProfileError::ActivateRsDoesntExist);
}
if let Ok(local_key) = std::env::var("LOCAL_KEY") {
info!(
"Signing key present! Signing profile `{}` for node `{}`",
data.deploy_data.profile_name, data.deploy_data.node_name
);
let sign_exit_status = Command::new("nix")
.arg("sign-paths")
.arg("-r")
.arg("-k")
.arg(local_key)
.arg(&data.deploy_data.profile.profile_settings.path)
.status()
.await
.map_err(PushProfileError::Sign)?;
match sign_exit_status.code() {
Some(0) => (),
a => return Err(PushProfileError::SignExit(a)),
};
}
Ok(())
}
pub async fn build_profile_remotely(data: &PushProfileData<'_>, derivation_name: &str) -> Result<(), PushProfileError> {
info!(
"Building profile `{}` for node `{}` on remote host",
data.deploy_data.profile_name, data.deploy_data.node_name
);
// TODO: this should probably be handled more nicely during 'data' construction
let hostname = match data.deploy_data.cmd_overrides.hostname {
Some(ref x) => x,
None => &data.deploy_data.node.node_settings.hostname,
};
let store_address = format!("ssh-ng://{}@{}", data.deploy_defs.ssh_user, hostname);
let ssh_opts_str = data.deploy_data.merged_settings.ssh_opts.join(" ");
// copy the derivation to remote host so it can be built there
let copy_command_status = Command::new("nix").arg("copy")
.arg("-s") // fetch dependencies from substitures, not localhost
.arg("--to").arg(&store_address)
.arg("--derivation").arg(derivation_name)
.env("NIX_SSHOPTS", ssh_opts_str.clone())
.stdout(Stdio::null())
.status()
.await
.map_err(PushProfileError::Copy)?;
match copy_command_status.code() {
Some(0) => (),
a => return Err(PushProfileError::CopyExit(a)),
};
let mut build_command = Command::new(if data.use_nom {"nom"} else {"nix"});
build_command
.arg("build").arg(derivation_name)
.arg("--eval-store").arg("auto")
.arg("--store").arg(&store_address)
.args(data.extra_build_args)
.env("NIX_SSHOPTS", ssh_opts_str.clone());
debug!("build command: {:?}", build_command);
let build_exit_status = build_command
// Logging should be in stderr, this just stops the store path from printing for no reason
.stdout(Stdio::null())
.status()
.await
.map_err(PushProfileError::Build)?;
match build_exit_status.code() {
Some(0) => (),
a => return Err(PushProfileError::BuildExit(a)),
};
Ok(())
}
pub async fn build_profile(data: PushProfileData<'_>) -> Result<(), PushProfileError> {
debug!(
"Finding the deriver of store path for {}",
&data.deploy_data.profile.profile_settings.path
);
// `nix-store --query --deriver` doesn't work on invalid paths, so we parse output of show-derivation :(
let mut show_derivation_command = Command::new("nix");
show_derivation_command
.arg("show-derivation")
.arg(&data.deploy_data.profile.profile_settings.path);
let show_derivation_output = show_derivation_command
.output()
.await
.map_err(PushProfileError::ShowDerivation)?;
match show_derivation_output.status.code() {
Some(0) => (),
a => return Err(PushProfileError::ShowDerivationExit(a)),
};
let derivation_info: HashMap<&str, serde_json::value::Value> = serde_json::from_str(
std::str::from_utf8(&show_derivation_output.stdout)
.map_err(PushProfileError::ShowDerivationUtf8)?,
)
.map_err(PushProfileError::ShowDerivationParse)?;
let &deriver = derivation_info
.keys()
.next()
.ok_or(PushProfileError::ShowDerivationEmpty)?;
// Since nix 2.15.0 'nix build <path>.drv' will build only the .drv file itself, not the
// derivation outputs, '^out' is used to refer to outputs explicitly
let new_deriver = &(deriver.to_owned().to_string() + "^out");
let path_info_output = Command::new("nix")
.arg("--experimental-features").arg("nix-command")
.arg("path-info")
.arg(&deriver)
.output().await
.map_err(PushProfileError::PathInfo)?;
let deriver = if std::str::from_utf8(&path_info_output.stdout).map(|s| s.trim()) == Ok(deriver) {
// In this case we're on 2.15.0 or newer, because 'nix path-infonix path-info <...>.drv'
// returns the same '<...>.drv' path.
// If 'nix path-info <...>.drv' returns a different path, then we're on pre 2.15.0 nix and
// derivation build result is already present in the /nix/store.
new_deriver
} else {
// If 'nix path-info <...>.drv' returns a different path, then we're on pre 2.15.0 nix and
// derivation build result is already present in the /nix/store.
//
// Alternatively, the result of the derivation build may not be yet present
// in the /nix/store. In this case, 'nix path-info' returns
// 'error: path '...' is not valid'.
deriver
};
if data.deploy_data.merged_settings.remote_build.unwrap_or(false) {
if !data.supports_flakes {
return Err(PushProfileError::RemoteBuildWithLegacyNix)
}
build_profile_remotely(&data, &deriver).await?;
} else {
build_profile_locally(&data, &deriver).await?;
}
Ok(())
}
pub async fn push_profile(data: PushProfileData<'_>) -> Result<(), PushProfileError> {
let ssh_opts_str = data
.deploy_data
.merged_settings
.ssh_opts
// This should provide some extra safety, but it also breaks for some reason, oh well
// .iter()
// .map(|x| format!("'{}'", x))
// .collect::<Vec<String>>()
.join(" ");
// remote building guarantees that the resulting derivation is stored on the target system
// no need to copy after building
if !data.deploy_data.merged_settings.remote_build.unwrap_or(false) {
info!(
"Copying profile `{}` to node `{}`",
data.deploy_data.profile_name, data.deploy_data.node_name
);
let mut copy_command = Command::new("nix");
copy_command.arg("copy");
if data.deploy_data.merged_settings.fast_connection != Some(true) {
copy_command.arg("--substitute-on-destination");
}
if !data.check_sigs {
copy_command.arg("--no-check-sigs");
}
let hostname = match data.deploy_data.cmd_overrides.hostname {
Some(ref x) => x,
None => &data.deploy_data.node.node_settings.hostname,
};
let copy_exit_status = copy_command
.arg("--to")
.arg(format!("ssh://{}@{}", data.deploy_defs.ssh_user, hostname))
.arg(&data.deploy_data.profile.profile_settings.path)
.env("NIX_SSHOPTS", ssh_opts_str)
.status()
.await
.map_err(PushProfileError::Copy)?;
match copy_exit_status.code() {
Some(0) => (),
a => return Err(PushProfileError::CopyExit(a)),
};
}
Ok(())
}
|