-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
interface.rs
361 lines (296 loc) · 8.78 KB
/
interface.rs
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
use std::path::PathBuf;
use anstyle::Style;
use clap::ValueEnum;
use clap::{builder::Styles, Args, Parser, Subcommand};
use crate::installable::Installable;
use crate::Result;
fn make_style() -> Styles {
Styles::plain().header(Style::new().bold()).literal(
Style::new()
.bold()
.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Yellow))),
)
}
#[derive(Parser, Debug)]
#[command(
version,
about,
long_about = None,
styles=make_style(),
propagate_version = false,
help_template = "
{name} {version}
{about-with-newline}
{usage-heading} {usage}
{all-args}{after-help}
"
)]
/// Yet another nix helper
pub struct Main {
#[arg(short, long, global = true)]
/// Show debug logs
pub verbose: bool,
#[command(subcommand)]
pub command: NHCommand,
}
#[derive(Subcommand, Debug)]
#[command(disable_help_subcommand = true)]
pub enum NHCommand {
Os(OsArgs),
Home(HomeArgs),
Darwin(DarwinArgs),
Search(SearchArgs),
Clean(CleanProxy),
#[command(hide = true)]
Completions(CompletionArgs),
}
impl NHCommand {
pub fn run(self) -> Result<()> {
match self {
NHCommand::Os(args) => args.run(),
NHCommand::Search(args) => args.run(),
NHCommand::Clean(proxy) => proxy.command.run(),
NHCommand::Completions(args) => args.run(),
NHCommand::Home(args) => args.run(),
NHCommand::Darwin(args) => args.run(),
}
}
}
#[derive(Args, Debug)]
#[clap(verbatim_doc_comment)]
/// NixOS functionality
///
/// Implements functionality mostly around but not exclusive to nixos-rebuild
pub struct OsArgs {
#[command(subcommand)]
pub subcommand: OsSubcommand,
}
#[derive(Debug, Subcommand)]
pub enum OsSubcommand {
/// Build and activate the new configuration, and make it the boot default
Switch(OsRebuildArgs),
/// Build the new configuration and make it the boot default
Boot(OsRebuildArgs),
/// Build and activate the new configuration
Test(OsRebuildArgs),
/// Build the new configuration
Build(OsRebuildArgs),
/// Load system in a repl
Repl(OsReplArgs),
}
#[derive(Debug, Args)]
pub struct OsRebuildArgs {
#[command(flatten)]
pub common: CommonRebuildArgs,
#[command(flatten)]
pub update_args: UpdateArgs,
/// When using a flake installable, select this hostname from nixosConfigurations
#[arg(long, short = 'H', global = true)]
pub hostname: Option<String>,
/// Explicitely select some specialisation
#[arg(long, short)]
pub specialisation: Option<String>,
/// Ignore specialisations
#[arg(long, short = 'S')]
pub no_specialisation: bool,
/// Extra arguments passed to nix build
#[arg(last = true)]
pub extra_args: Vec<String>,
/// Don't panic if calling nh as root
#[arg(short = 'R', long, env = "NH_BYPASS_ROOT_CHECK")]
pub bypass_root_check: bool,
}
#[derive(Debug, Args)]
pub struct CommonRebuildArgs {
/// Only print actions, without performing them
#[arg(long, short = 'n')]
pub dry: bool,
/// Ask for confirmation
#[arg(long, short)]
pub ask: bool,
#[command(flatten)]
pub installable: Installable,
/// Don't use nix-output-monitor for the build process
#[arg(long)]
pub no_nom: bool,
/// Path to save the result link, defaults to using a temporary directory
#[arg(long, short)]
pub out_link: Option<PathBuf>,
}
#[derive(Debug, Args)]
pub struct OsReplArgs {
#[command(flatten)]
pub installable: Installable,
/// When using a flake installable, select this hostname from nixosConfigurations
#[arg(long, short = 'H', global = true)]
pub hostname: Option<String>,
}
#[derive(Args, Debug)]
/// Searches packages by querying search.nixos.org
pub struct SearchArgs {
#[arg(long, short, default_value = "30")]
/// Number of search results to display
pub limit: u64,
#[arg(
long,
short,
env = "NH_SEARCH_CHANNEL",
default_value = "nixos-unstable"
)]
/// Name of the channel to query (e.g nixos-23.11, nixos-unstable, etc)
pub channel: String,
/// Name of the package to search
pub query: String,
}
#[derive(Debug, Clone, ValueEnum)]
pub enum SearchNixpkgsFrom {
Flake,
Path,
}
// Needed a struct to have multiple sub-subcommands
#[derive(Debug, Clone, Args)]
pub struct CleanProxy {
#[clap(subcommand)]
command: CleanMode,
}
#[derive(Debug, Clone, Subcommand)]
/// Enhanced nix cleanup
pub enum CleanMode {
/// Clean all profiles
All(CleanArgs),
/// Clean the current user's profiles
User(CleanArgs),
/// Clean a specific profile
Profile(CleanProfileArgs),
}
#[derive(Args, Clone, Debug)]
#[clap(verbatim_doc_comment)]
/// Enhanced nix cleanup
///
/// For --keep-since, see the documentation of humantime for possible formats: https://docs.rs/humantime/latest/humantime/fn.parse_duration.html
pub struct CleanArgs {
#[arg(long, short, default_value = "1")]
/// At least keep this number of generations
pub keep: u32,
#[arg(long, short = 'K', default_value = "0h")]
/// At least keep gcroots and generations in this time range since now.
pub keep_since: humantime::Duration,
/// Only print actions, without performing them
#[arg(long, short = 'n')]
pub dry: bool,
/// Ask for confimation
#[arg(long, short)]
pub ask: bool,
/// Don't run nix store --gc
#[arg(long)]
pub nogc: bool,
/// Don't clean gcroots
#[arg(long)]
pub nogcroots: bool,
}
#[derive(Debug, Clone, Args)]
pub struct CleanProfileArgs {
#[command(flatten)]
pub common: CleanArgs,
/// Which profile to clean
pub profile: PathBuf,
}
#[derive(Debug, Args)]
/// Home-manager functionality
pub struct HomeArgs {
#[command(subcommand)]
pub subcommand: HomeSubcommand,
}
#[derive(Debug, Subcommand)]
pub enum HomeSubcommand {
/// Build and activate a home-manager configuration
Switch(HomeRebuildArgs),
/// Build a home-manager configuration
Build(HomeRebuildArgs),
/// Load a home-manager configuration in a Nix REPL
Repl(HomeReplArgs),
}
#[derive(Debug, Args)]
pub struct HomeRebuildArgs {
#[command(flatten)]
pub common: CommonRebuildArgs,
#[command(flatten)]
pub update_args: UpdateArgs,
/// Name of the flake homeConfigurations attribute, like username@hostname
///
/// If unspecified, will try <username>@<hostname> and <username>
#[arg(long, short)]
pub configuration: Option<String>,
/// Extra arguments passed to nix build
#[arg(last = true)]
pub extra_args: Vec<String>,
/// Move existing files by backing up with this file extension
#[arg(long, short = 'b')]
pub backup_extension: Option<String>,
}
#[derive(Debug, Args)]
pub struct HomeReplArgs {
#[command(flatten)]
pub installable: Installable,
/// Name of the flake homeConfigurations attribute, like username@hostname
///
/// If unspecified, will try <username>@<hostname> and <username>
#[arg(long, short)]
pub configuration: Option<String>,
/// Extra arguments passed to nix repl
#[arg(last = true)]
pub extra_args: Vec<String>,
}
#[derive(Debug, Parser)]
/// Generate shell completion files into stdout
pub struct CompletionArgs {
/// Name of the shell
pub shell: clap_complete::Shell,
}
/// Nix-darwin functionality
///
/// Implements functionality mostly around but not exclusive to darwin-rebuild
#[derive(Debug, Args)]
pub struct DarwinArgs {
#[command(subcommand)]
pub subcommand: DarwinSubcommand,
}
#[derive(Debug, Subcommand)]
pub enum DarwinSubcommand {
/// Build and activate a nix-darwin configuration
Switch(DarwinRebuildArgs),
/// Build a nix-darwin configuration
Build(DarwinRebuildArgs),
/// Load a nix-darwin configuration in a Nix REPL
Repl(DarwinReplArgs),
}
#[derive(Debug, Args)]
pub struct DarwinRebuildArgs {
#[command(flatten)]
pub common: CommonRebuildArgs,
#[command(flatten)]
pub update_args: UpdateArgs,
/// When using a flake installable, select this hostname from darwinConfigurations
#[arg(long, short = 'H', global = true)]
pub hostname: Option<String>,
/// Extra arguments passed to nix build
#[arg(last = true)]
pub extra_args: Vec<String>,
}
#[derive(Debug, Args)]
pub struct DarwinReplArgs {
#[command(flatten)]
pub installable: Installable,
/// When using a flake installable, select this hostname from darwinConfigurations
#[arg(long, short = 'H', global = true)]
pub hostname: Option<String>,
}
#[derive(Debug, Args)]
pub struct UpdateArgs {
#[arg(short = 'u', long = "update")]
/// Update all flake inputs
pub update: bool,
#[arg(short = 'U', long = "update-input")]
/// Update a single flake input
pub update_input: Option<String>,
}