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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
use anyhow::Result;
use clap::{Parser, Subcommand};
use std::path::PathBuf;
mod commands;
mod config;
mod dag;
mod flow;
mod hooks;
mod registry;
mod secrets;
mod targeting;
#[derive(Parser)]
#[command(name = "fleet")]
#[command(about = "Node lifecycle CLI for NixOS fleet management", long_about = None)]
#[command(version)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Deploy NixOS configurations to nodes
Deploy {
/// Target nodes (names or @tag)
targets: Vec<String>,
/// Deploy to all nodes
#[arg(long)]
all: bool,
/// Dry run (build but don't activate)
#[arg(long)]
dry_run: bool,
/// Show nix evaluation trace
#[arg(long)]
show_trace: bool,
/// Skip deploy-rs flake checks
#[arg(long)]
skip_checks: bool,
},
/// Build NixOS configurations without activating
Build {
/// Target nodes (names or @tag)
targets: Vec<String>,
/// Build all nodes
#[arg(long)]
all: bool,
/// Show nix evaluation trace
#[arg(long)]
show_trace: bool,
},
/// Show closure diff between current and new configuration
Diff {
/// Target nodes (names or @tag)
targets: Vec<String>,
/// Diff all nodes
#[arg(long)]
all: bool,
},
/// Execute a command on remote nodes via SSH
Exec {
/// Target nodes (names or @tag)
targets: Vec<String>,
/// Execute on all nodes
#[arg(long)]
all: bool,
/// Command to execute (after --)
#[arg(last = true, required = true)]
cmd: Vec<String>,
},
/// Show status of remote nodes (generation, uptime, kernel)
Status {
/// Target nodes (names or @tag)
targets: Vec<String>,
/// Show status of all nodes (default if no targets given)
#[arg(long)]
all: bool,
},
/// Rollback nodes to previous NixOS generation
Rollback {
/// Target nodes (names or @tag)
targets: Vec<String>,
/// Rollback all nodes
#[arg(long)]
all: bool,
},
/// Reboot remote nodes
Reboot {
/// Target nodes (names or @tag)
targets: Vec<String>,
/// Reboot all nodes
#[arg(long)]
all: bool,
/// Skip confirmation prompt
#[arg(short = 'y', long)]
yes: bool,
},
/// Rebuild local system (auto-detects Darwin/NixOS from hostname)
Rebuild {
/// Show nix evaluation trace
#[arg(long)]
show_trace: bool,
/// Pass --option key value to darwin-rebuild/nixos-rebuild (repeatable)
#[arg(long = "nix-option", num_args = 2, value_names = ["KEY", "VALUE"], action = clap::ArgAction::Append)]
nix_options: Vec<String>,
},
/// Open interactive SSH session to a node
Ssh {
/// Node name
node: String,
},
/// Show node registry information
Info {
/// Output as JSON
#[arg(long)]
json: bool,
},
/// Check SSH connectivity to nodes
Ping {
/// Target nodes (names or @tag)
targets: Vec<String>,
/// Ping all nodes (default if no targets given)
#[arg(long)]
all: bool,
},
/// Run or list named DAG workflows
Flow {
#[command(subcommand)]
action: FlowAction,
},
/// Manage secrets (provision from 1Password, clean local files)
Secrets {
#[command(subcommand)]
action: SecretsAction,
},
}
#[derive(Subcommand)]
enum SecretsAction {
/// Provision secrets from configured providers (all or by name)
Sync {
/// Secret name (provisions all if omitted)
name: Option<String>,
},
/// Remove local secret files (all or by name)
Clean {
/// Secret name (cleans all if omitted)
name: Option<String>,
},
/// List configured secrets and their status
List,
}
#[derive(Subcommand)]
enum FlowAction {
/// List available flows
List,
/// Run a named flow
Run {
/// Flow name
name: String,
/// Target nodes (names or @tag) — used by steps without explicit targets
targets: Vec<String>,
/// Target all nodes
#[arg(long)]
all: bool,
/// Print execution plan without running
#[arg(long)]
dry_run: bool,
},
}
fn load_config() -> config::FleetConfig {
// Prefer local detection: walk up to find flake.nix
let dir = std::env::current_dir()
.ok()
.and_then(|cwd| commands::rebuild::find_flake_root(&cwd).ok())
.or_else(|| std::env::var("FLEET_FLAKE_DIR").map(PathBuf::from).ok())
.unwrap_or_else(|| PathBuf::from("."));
config::FleetConfig::load(&dir).unwrap_or_default()
}
fn main() -> Result<()> {
let cli = Cli::parse();
let config = load_config();
match cli.command {
Commands::Deploy {
targets,
all,
dry_run,
show_trace,
skip_checks,
} => {
secrets::provision_for_command(&config, "deploy")?;
let reg = registry::load_registry()?;
let resolved = targeting::resolve(®, &targets, all)?;
for (name, node) in &resolved.nodes {
hooks::run_pre(&config, "deploy", name, node)?;
}
commands::deploy::run(&resolved, dry_run, show_trace, skip_checks)?;
for (name, node) in &resolved.nodes {
hooks::run_post(&config, "deploy", name, node);
}
}
Commands::Build {
targets,
all,
show_trace,
} => {
let reg = registry::load_registry()?;
let resolved = targeting::resolve(®, &targets, all)?;
for (name, node) in &resolved.nodes {
hooks::run_pre(&config, "build", name, node)?;
}
commands::build::run(&resolved, show_trace)?;
for (name, node) in &resolved.nodes {
hooks::run_post(&config, "build", name, node);
}
}
Commands::Diff { targets, all } => {
let reg = registry::load_registry()?;
let resolved = targeting::resolve(®, &targets, all)?;
for (name, node) in &resolved.nodes {
hooks::run_pre(&config, "diff", name, node)?;
}
commands::diff::run(&resolved, &config)?;
for (name, node) in &resolved.nodes {
hooks::run_post(&config, "diff", name, node);
}
}
Commands::Exec { targets, all, cmd } => {
let reg = registry::load_registry()?;
let resolved = targeting::resolve(®, &targets, all)?;
for (name, node) in &resolved.nodes {
hooks::run_pre(&config, "exec", name, node)?;
}
commands::exec::run(&resolved, &cmd, &config)?;
for (name, node) in &resolved.nodes {
hooks::run_post(&config, "exec", name, node);
}
}
Commands::Status { targets, all } => {
let reg = registry::load_registry()?;
let all = all || targets.is_empty();
let resolved = targeting::resolve(®, &targets, all)?;
commands::status::run(&resolved, &config)?;
}
Commands::Rollback { targets, all } => {
let reg = registry::load_registry()?;
let resolved = targeting::resolve(®, &targets, all)?;
for (name, node) in &resolved.nodes {
hooks::run_pre(&config, "rollback", name, node)?;
}
commands::rollback::run(&resolved, &config)?;
for (name, node) in &resolved.nodes {
hooks::run_post(&config, "rollback", name, node);
}
}
Commands::Reboot { targets, all, yes } => {
let reg = registry::load_registry()?;
let resolved = targeting::resolve(®, &targets, all)?;
for (name, node) in &resolved.nodes {
hooks::run_pre(&config, "reboot", name, node)?;
}
commands::reboot::run(&resolved, yes, &config)?;
for (name, node) in &resolved.nodes {
hooks::run_post(&config, "reboot", name, node);
}
}
Commands::Rebuild {
show_trace,
nix_options,
} => {
secrets::provision_for_command(&config, "rebuild")?;
commands::rebuild::rebuild(show_trace, &nix_options)?;
}
Commands::Ssh { node } => {
let reg = registry::load_registry()?;
let resolved = targeting::resolve(®, &[node], false)?;
commands::ssh::run(&resolved, &config)?;
}
Commands::Info { json } => {
let reg = registry::load_registry()?;
commands::info::run(®, json)?;
}
Commands::Ping { targets, all } => {
let reg = registry::load_registry()?;
let all = all || targets.is_empty();
let resolved = targeting::resolve(®, &targets, all)?;
commands::ping::run(&resolved, &config)?;
}
Commands::Secrets { action } => match action {
SecretsAction::Sync { name } => match name {
Some(n) => secrets::sync_secret(&config, &n)?,
None => secrets::sync_all(&config)?,
},
SecretsAction::Clean { name } => match name {
Some(n) => secrets::clean_secret(&config, &n)?,
None => {
for secret_name in config.secrets.keys() {
secrets::clean_secret(&config, secret_name)?;
}
}
},
SecretsAction::List => {
if config.secrets.is_empty() {
println!("No secrets configured in fleet.yaml");
} else {
for (name, secret) in &config.secrets {
let target = secrets::expand_home_pub(&secret.path);
let status = if target.exists() {
"present".to_string()
} else {
"missing".to_string()
};
println!(
" {} ({}) -> {} [{}]",
name, secret.provider, target.display(), status
);
}
}
}
},
Commands::Flow { action } => match action {
FlowAction::List => {
commands::flow::list(&config)?;
}
FlowAction::Run {
name,
targets,
all,
dry_run,
} => {
// Registry is optional — Pangea-only flows don't need node targets
let reg = registry::load_registry().unwrap_or_default();
commands::flow::run(&config, ®, &name, &targets, all, dry_run)?;
}
},
}
Ok(())
}