Skip to content

Commit e1eb23d

Browse files
authored
Merge pull request #1771 from cachix/fix-1769
devenv: don't create local project directories on `devenv init`
2 parents 7e999fc + b32a12f commit e1eb23d

File tree

3 files changed

+46
-31
lines changed

3 files changed

+46
-31
lines changed

devenv/src/cnix.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ use tracing::{debug, error, info, warn};
1616

1717
pub struct Nix {
1818
pub options: Options,
19-
pool: SqlitePool,
19+
pool: Option<SqlitePool>,
20+
database_url: String,
2021
// TODO: all these shouldn't be here
2122
config: config::Config,
2223
global_options: cli::GlobalOptions,
@@ -91,13 +92,11 @@ impl Nix {
9192
"sqlite:{}/nix-eval-cache.db",
9293
devenv_dotfile.to_string_lossy()
9394
);
94-
let pool = devenv_eval_cache::db::setup_db(database_url)
95-
.await
96-
.into_diagnostic()?;
9795

9896
Ok(Self {
9997
options,
100-
pool,
98+
pool: None,
99+
database_url,
101100
config,
102101
global_options,
103102
cachix_caches,
@@ -109,6 +108,19 @@ impl Nix {
109108
})
110109
}
111110

111+
// Defer creating local project state
112+
pub async fn assemble(&mut self) -> Result<()> {
113+
if self.pool.is_none() {
114+
self.pool = Some(
115+
devenv_eval_cache::db::setup_db(&self.database_url)
116+
.await
117+
.into_diagnostic()?,
118+
);
119+
}
120+
121+
Ok(())
122+
}
123+
112124
pub async fn develop(
113125
&self,
114126
args: &[&str],
@@ -365,8 +377,10 @@ impl Nix {
365377
let result = if self.global_options.eval_cache
366378
&& options.cache_output
367379
&& supports_eval_caching(&cmd)
380+
&& self.pool.is_some()
368381
{
369-
let mut cached_cmd = CachedCommand::new(&self.pool);
382+
let pool = self.pool.as_ref().unwrap();
383+
let mut cached_cmd = CachedCommand::new(pool);
370384

371385
cached_cmd.watch_path(self.devenv_root.join("devenv.yaml"));
372386
cached_cmd.watch_path(self.devenv_root.join("devenv.lock"));

devenv/src/devenv.rs

+24-23
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ impl Devenv {
107107
.expect("Failed to create DEVENV_HOME directory");
108108
std::fs::create_dir_all(&devenv_home_gc)
109109
.expect("Failed to create DEVENV_HOME_GC directory");
110-
std::fs::create_dir_all(&devenv_dot_gc).expect("Failed to create .devenv/gc directory");
111110

112111
let nix = cnix::Nix::new(
113112
options.config.clone(),
@@ -119,7 +118,7 @@ impl Devenv {
119118
devenv_root.clone(),
120119
)
121120
.await
122-
.unwrap(); // TODO: handle error
121+
.expect("Failed to initialize Nix");
123122

124123
Self {
125124
config: options.config,
@@ -175,16 +174,14 @@ impl Devenv {
175174
file.write_all(path.contents())
176175
})
177176
.expect("Failed to append to existing file");
178-
} else {
179-
if target_path.exists() && !EXISTING_REQUIRED_FILES.contains(&filename) {
180-
if let Some(utf8_contents) = path.contents_utf8() {
181-
confirm_overwrite(&target_path, utf8_contents.to_string())?;
182-
} else {
183-
bail!("Failed to read file contents as UTF-8");
184-
}
177+
} else if target_path.exists() && !EXISTING_REQUIRED_FILES.contains(&filename) {
178+
if let Some(utf8_contents) = path.contents_utf8() {
179+
confirm_overwrite(&target_path, utf8_contents.to_string())?;
185180
} else {
186-
std::fs::write(&target_path, path.contents()).expect("Failed to write file");
181+
bail!("Failed to read file contents as UTF-8");
187182
}
183+
} else {
184+
std::fs::write(&target_path, path.contents()).expect("Failed to write file");
188185
}
189186
}
190187

@@ -238,7 +235,7 @@ impl Devenv {
238235
cmd: &Option<String>,
239236
args: &[String],
240237
) -> Result<Vec<String>> {
241-
self.assemble(false)?;
238+
self.assemble(false).await?;
242239
let env = self.get_dev_environment(false).await?;
243240

244241
let mut develop_args = vec![
@@ -286,7 +283,7 @@ impl Devenv {
286283
}
287284

288285
pub async fn update(&mut self, input_name: &Option<String>) -> Result<()> {
289-
self.assemble(false)?;
286+
self.assemble(false).await?;
290287

291288
let msg = match input_name {
292289
Some(input_name) => format!("Updating devenv.lock with input {input_name}"),
@@ -310,7 +307,7 @@ impl Devenv {
310307
);
311308

312309
async move {
313-
self.assemble(false)?;
310+
self.assemble(false).await?;
314311

315312
let container_store_path = self
316313
.nix
@@ -410,8 +407,8 @@ impl Devenv {
410407
.await
411408
}
412409

413-
pub fn repl(&mut self) -> Result<()> {
414-
self.assemble(false)?;
410+
pub async fn repl(&mut self) -> Result<()> {
411+
self.assemble(false).await?;
415412
self.nix.repl()
416413
}
417414

@@ -460,7 +457,7 @@ impl Devenv {
460457
}
461458

462459
pub async fn search(&mut self, name: &str) -> Result<()> {
463-
self.assemble(false)?;
460+
self.assemble(false).await?;
464461

465462
let build_options = cnix::Options {
466463
logging: false,
@@ -531,7 +528,7 @@ impl Devenv {
531528
}
532529

533530
pub async fn tasks_run(&mut self, roots: Vec<String>) -> Result<()> {
534-
self.assemble(false)?;
531+
self.assemble(false).await?;
535532
if roots.is_empty() {
536533
bail!("No tasks specified.");
537534
}
@@ -569,7 +566,7 @@ impl Devenv {
569566
}
570567

571568
pub async fn test(&mut self) -> Result<()> {
572-
self.assemble(true)?;
569+
self.assemble(true).await?;
573570

574571
// collect tests
575572
let test_script = {
@@ -617,14 +614,14 @@ impl Devenv {
617614
}
618615

619616
pub async fn info(&mut self) -> Result<()> {
620-
self.assemble(false)?;
617+
self.assemble(false).await?;
621618
let output = self.nix.metadata().await?;
622619
println!("{}", output);
623620
Ok(())
624621
}
625622

626623
pub async fn build(&mut self, attributes: &[String]) -> Result<()> {
627-
self.assemble(false)?;
624+
self.assemble(false).await?;
628625
let attributes: Vec<String> = if attributes.is_empty() {
629626
// construct dotted names of all attributes that we need to build
630627
let build_output = self.nix.eval(&["build"]).await?;
@@ -671,7 +668,7 @@ impl Devenv {
671668
detach: &bool,
672669
log_to_file: &bool,
673670
) -> Result<()> {
674-
self.assemble(false)?;
671+
self.assemble(false).await?;
675672
if !self.has_processes().await? {
676673
error!("No 'processes' option defined: https://devenv.sh/processes/");
677674
bail!("No processes defined");
@@ -791,7 +788,7 @@ impl Devenv {
791788
Ok(())
792789
}
793790

794-
pub fn assemble(&mut self, is_testing: bool) -> Result<()> {
791+
pub async fn assemble(&mut self, is_testing: bool) -> Result<()> {
795792
if self.assembled {
796793
return Ok(());
797794
}
@@ -803,9 +800,13 @@ impl Devenv {
803800
$ devenv init
804801
"});
805802
}
803+
806804
fs::create_dir_all(&self.devenv_dot_gc)
807805
.unwrap_or_else(|_| panic!("Failed to create {}", self.devenv_dot_gc.display()));
808806

807+
// Initialise any Nix state
808+
self.nix.assemble().await?;
809+
809810
let mut flake_inputs = HashMap::new();
810811
for (input, attrs) in self.config.inputs.iter() {
811812
match config::FlakeInput::try_from(attrs) {
@@ -878,7 +879,7 @@ impl Devenv {
878879
}
879880

880881
pub async fn get_dev_environment(&mut self, json: bool) -> Result<DevEnv> {
881-
self.assemble(false)?;
882+
self.assemble(false).await?;
882883

883884
let gc_root = self.devenv_dot_gc.join("shell");
884885
let span = tracing::info_span!("building_shell", devenv.user_message = "Building shell",);

devenv/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ async fn main() -> Result<()> {
152152
Commands::Search { name } => devenv.search(&name).await,
153153
Commands::Gc {} => devenv.gc(),
154154
Commands::Info {} => devenv.info().await,
155-
Commands::Repl {} => devenv.repl(),
155+
Commands::Repl {} => devenv.repl().await,
156156
Commands::Build { attributes } => devenv.build(&attributes).await,
157157
Commands::Update { name } => devenv.update(&name).await,
158158
Commands::Up { process, detach } => devenv.up(process.as_deref(), &detach, &detach).await,
@@ -170,7 +170,7 @@ async fn main() -> Result<()> {
170170
},
171171

172172
// hidden
173-
Commands::Assemble => devenv.assemble(false),
173+
Commands::Assemble => devenv.assemble(false).await,
174174
Commands::PrintDevEnv { json } => devenv.print_dev_env(json).await,
175175
Commands::GenerateJSONSchema => {
176176
config::write_json_schema();

0 commit comments

Comments
 (0)