diff --git a/README.md b/README.md index 33145eb..ce0c8ce 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,13 @@ cargo build --release # Optional: Install system-wide cargo install --path . -# Optional: Build static binary +# Optional: Build x86_64 static binary +export CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER=x86_64-linux-gnu-gcc +export CC=x86_64-linux-gnu-gcc +rustup target add x86_64-unknown-linux-musl +cargo build --target=x86_64-unknown-linux-musl + +# Optional: Build arm64 static binary export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER=aarch64-linux-gnu-gcc export CC=aarch64-linux-gnu-gcc rustup target add aarch64-unknown-linux-musl diff --git a/src/base_image.rs b/src/base_image.rs index 0699d5c..6f3cb79 100644 --- a/src/base_image.rs +++ b/src/base_image.rs @@ -96,7 +96,7 @@ where log::info!("removing current base image"); for dir in dest.read_dir()? { let path = dir?.path(); - if let Err(err) = chmod(&path, |mode| mode | 0o700) { + if let Err(err) = chmod_dirs(&path, |mode| mode | 0o700) { log::error!( "could not fix permissions for {}: {}", path.display(), diff --git a/src/image_builder.rs b/src/image_builder.rs index f82ace9..2ec6598 100644 --- a/src/image_builder.rs +++ b/src/image_builder.rs @@ -97,7 +97,7 @@ impl BaseImageBuilder { } pub fn build_base_with_arch(&self, arch: &str) -> Result<()> { - install_nix(arch, &self.nix_dir)?; + install_nix(arch, &self.nix_dir).context("could not install Nix")?; log::info!("building base image"); let tmp = tempdir()?; @@ -406,6 +406,17 @@ pub fn chmod(path: &Path, func: fn(u32) -> u32) -> Result<(), io::Error> { Ok(()) } +pub fn chmod_dirs(path: &Path, func: fn(u32) -> u32) -> Result<(), io::Error> { + if fs::symlink_metadata(path)?.file_type().is_dir() { + for entry in path.read_dir()? { + chmod_dirs(&entry?.path(), func)?; + } + chmod_apply(path, func)?; + } + + Ok(()) +} + pub fn progress_bar(len: u64) -> ProgressBar { ProgressBar::new(len).with_style( ProgressStyle::with_template( @@ -503,16 +514,20 @@ where let gcroots = dest.join("var/nix/gcroots"); fs::create_dir_all(&gcroots)?; - if !nix_bin.exists() { + if !symlink_exists(&nix_bin) { let nix_store_path = find_nix(&nix_store, NIX_VERSION)?; let nix_path = Path::new("/nix/store").join(nix_store_path); - symlink(nix_path.join("bin"), nix_bin)?; - symlink(&nix_path, gcroots.join("nix"))?; + symlink(nix_path.join("bin"), nix_bin).unwrap(); + symlink(&nix_path, gcroots.join("nix")).unwrap(); } Ok(()) } +fn symlink_exists>(path: P) -> bool { + fs::symlink_metadata(path).is_ok_and(|f| f.file_type().is_symlink()) +} + fn symlink_base>(base_path: P) -> Result<(), io::Error> { let base_link = Path::new("/nix/.base"); let gcroots = Path::new("/nix/var/nix/gcroots"); diff --git a/src/pid_file.rs b/src/pid_file.rs index 813201d..258c109 100644 --- a/src/pid_file.rs +++ b/src/pid_file.rs @@ -24,7 +24,6 @@ impl PidFile { impl Drop for PidFile { fn drop(&mut self) { - log::debug!("removing {}", self.path.display()); if let Err(err) = fs::remove_file(&self.path) { log::error!("while removing: {}", err); } diff --git a/src/shell.rs b/src/shell.rs index 2c5c92d..87a9d06 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -1,6 +1,7 @@ use std::{ collections::HashMap, ffi::{OsStr, OsString}, + fs::create_dir_all, os::unix::process::CommandExt, process::{self, exit, Command}, }; @@ -100,6 +101,7 @@ impl Shell { ("INFOPATH", format!("{nix_base}/share/info")), ]); + let _ = create_dir_all("/nix/.cache"); let err = cmd.exec(); bail!("cannot exec: {}", err) }