Skip to content

Commit

Permalink
feat: use persistent flake dirs to speed things up
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelcoeffic committed Dec 19, 2024
1 parent 6a5ab9d commit 795b4ac
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/image_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,13 @@ impl BaseImageBuilder {
nixos::load_store_db("/nix/.reginfo")?;

// build base flake
let flake_dir = Path::new(crate::CACHE_HOME).join("base-flake");
let store_path = match &self.flake_dir {
None => nixos::build_flake_from_package_list(
"debug-shell",
"A debug shell",
crate::BASE_PACKAGES,
&flake_dir,
)?,
Some(flake_dir) => nixos::build_flake(flake_dir)?,
};
Expand All @@ -226,9 +228,7 @@ impl BaseImageBuilder {

fn do_package(&self) -> Result<()> {
if self.package_output.is_none() {
// no packaging, run garbage collection instead
log::info!("running nix garbage collector");
return nixos::run_gc();
return Ok(());
}

let output = self.package_output.as_ref().unwrap();
Expand Down
6 changes: 2 additions & 4 deletions src/nixos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::{

use anyhow::{bail, Context, Result};
use regex::Regex;
use tempfile::tempdir;

pub(crate) const ENV_VARS: [(&str, &str); 4] = [
("PATH", "/nix/.bin"),
Expand Down Expand Up @@ -64,13 +63,11 @@ pub fn build_flake_from_package_list(
name: &str,
description: &str,
packages: &[&str],
flake_dir: &Path,
) -> Result<PathBuf> {
let mut packages: Vec<&str> = Vec::from(packages);
packages.sort();

let flake_tmp = tempdir()?;
let flake_dir = flake_tmp.path();

let flake_template = include_str!("templates/flake.nix");
let re = Regex::new(r"\{\{\s*(\w+)\s*\}\}").unwrap();

Expand All @@ -85,6 +82,7 @@ pub fn build_flake_from_package_list(
});

log::debug!("writing flake.nix to {}", flake_dir.display());
fs::create_dir_all(flake_dir)?;
fs::write(flake_dir.join("flake.nix"), flake.as_ref())?;

log::debug!("building flake in {}", flake_dir.display());
Expand Down
2 changes: 2 additions & 0 deletions src/nixpkgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,12 @@ pub fn remove_package(name: &str) -> Result<()> {

fn build_user_env_flake(pkgs: &Packages) -> Result<()> {
let flake_pkgs: Vec<&str> = pkgs.iter().map(|p| p.name.as_ref()).collect();
let flake_dir = Path::new(crate::CACHE_HOME).join("env-flake");
let path = crate::nixos::build_flake_from_package_list(
"user-env",
"Dive installed packages",
&flake_pkgs,
&flake_dir,
)
.context("failed to build flake")?;
nixos::symlink_store_path(path, "user-env", crate::USER_ENV_DIR)?;
Expand Down

0 comments on commit 795b4ac

Please sign in to comment.