-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
nixos.rs
227 lines (188 loc) · 6.78 KB
/
nixos.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
use color_eyre::eyre::{bail, Context};
use color_eyre::Result;
use tracing::{debug, info, warn};
use crate::commands;
use crate::commands::Command;
use crate::installable::Installable;
use crate::interface::OsSubcommand::{self};
use crate::interface::{self, OsRebuildArgs, OsReplArgs};
use crate::update::update;
const SYSTEM_PROFILE: &str = "/nix/var/nix/profiles/system";
const CURRENT_PROFILE: &str = "/run/current-system";
const SPEC_LOCATION: &str = "/etc/specialisation";
impl interface::OsArgs {
pub fn run(self) -> Result<()> {
use OsRebuildVariant::*;
match self.subcommand {
OsSubcommand::Boot(args) => args.rebuild(Boot),
OsSubcommand::Test(args) => args.rebuild(Test),
OsSubcommand::Switch(args) => args.rebuild(Switch),
OsSubcommand::Build(args) => args.rebuild(Build),
OsSubcommand::Repl(args) => args.run(),
}
}
}
#[derive(Debug)]
enum OsRebuildVariant {
Build,
Switch,
Boot,
Test,
}
impl OsRebuildArgs {
fn rebuild(self, variant: OsRebuildVariant) -> Result<()> {
use OsRebuildVariant::*;
let elevate = if self.bypass_root_check {
warn!("Bypassing root check, now running nix as root");
false
} else {
if nix::unistd::Uid::effective().is_root() {
bail!("Don't run nh os as root. I will call sudo internally as needed");
}
true
};
if self.update_args.update {
update(&self.common.installable, self.update_args.update_input)?;
}
let hostname = match &self.hostname {
Some(h) => h.to_owned(),
None => hostname::get()
.context("Failed to get hostname")?
.to_str()
.unwrap()
.to_owned(),
};
let out_path: Box<dyn crate::util::MaybeTempPath> = match self.common.out_link {
Some(ref p) => Box::new(p.clone()),
None => Box::new({
let dir = tempfile::Builder::new().prefix("nh-os").tempdir()?;
(dir.as_ref().join("result"), dir)
}),
};
debug!(?out_path);
let toplevel = toplevel_for(hostname, self.common.installable.clone());
commands::Build::new(toplevel)
.extra_arg("--out-link")
.extra_arg(out_path.get_path())
.extra_args(&self.extra_args)
.message("Building NixOS configuration")
.nom(!self.common.no_nom)
.run()?;
let current_specialisation = std::fs::read_to_string(SPEC_LOCATION).ok();
let target_specialisation = if self.no_specialisation {
None
} else {
current_specialisation.or_else(|| self.specialisation.to_owned())
};
debug!("target_specialisation: {target_specialisation:?}");
let target_profile = match &target_specialisation {
None => out_path.get_path().to_owned(),
Some(spec) => out_path.get_path().join("specialisation").join(spec),
};
target_profile.try_exists().context("Doesn't exist")?;
Command::new("nvd")
.arg("diff")
.arg(CURRENT_PROFILE)
.arg(&target_profile)
.message("Comparing changes")
.run()?;
if self.common.dry || matches!(variant, Build) {
return Ok(());
}
if self.common.ask {
info!("Apply the config?");
let confirmation = dialoguer::Confirm::new().default(false).interact()?;
if !confirmation {
bail!("User rejected the new config");
}
}
if let Test | Switch = variant {
// !! Use the target profile aka spec-namespaced
let switch_to_configuration =
target_profile.join("bin").join("switch-to-configuration");
let switch_to_configuration = switch_to_configuration.to_str().unwrap();
Command::new(switch_to_configuration)
.arg("test")
.message("Activating configuration")
.elevate(elevate)
.run()?;
}
if let Boot | Switch = variant {
Command::new("nix")
.elevate(elevate)
.args(["build", "--no-link", "--profile", SYSTEM_PROFILE])
.arg(out_path.get_path())
.run()?;
// !! Use the base profile aka no spec-namespace
let switch_to_configuration = out_path
.get_path()
.join("bin")
.join("switch-to-configuration");
Command::new(switch_to_configuration)
.arg("boot")
.elevate(elevate)
.message("Adding configuration to bootloader")
.run()?;
}
// Make sure out_path is not accidentally dropped
// https://docs.rs/tempfile/3.12.0/tempfile/index.html#early-drop-pitfall
drop(out_path);
Ok(())
}
}
pub fn toplevel_for<S: AsRef<str>>(hostname: S, installable: Installable) -> Installable {
let mut res = installable.clone();
let hostname = hostname.as_ref().to_owned();
let toplevel = ["config", "system", "build", "toplevel"]
.into_iter()
.map(String::from);
match res {
Installable::Flake {
ref mut attribute, ..
} => {
// If user explicitely selects some other attribute, don't push nixosConfigurations
if attribute.is_empty() {
attribute.push(String::from("nixosConfigurations"));
attribute.push(hostname);
}
attribute.extend(toplevel);
}
Installable::File {
ref mut attribute, ..
} => {
attribute.extend(toplevel);
}
Installable::Expression {
ref mut attribute, ..
} => {
attribute.extend(toplevel);
}
Installable::Store { .. } => {}
}
res
}
impl OsReplArgs {
fn run(self) -> Result<()> {
let mut target_installable = self.installable;
if matches!(target_installable, Installable::Store { .. }) {
bail!("Nix doesn't support nix store installables.");
}
let hostname = self
.hostname
.unwrap_or_else(|| hostname::get().unwrap().to_str().unwrap().to_string());
if let Installable::Flake {
ref mut attribute, ..
} = target_installable
{
if attribute.is_empty() {
attribute.push(String::from("nixosConfigurations"));
attribute.push(hostname);
}
}
Command::new("nix")
.arg("repl")
.args(target_installable.to_args())
.run()?;
Ok(())
}
}