Skip to content

Commit

Permalink
fix: various small issues
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelcoeffic committed Nov 15, 2024
1 parent f19fee6 commit 070cb6d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/base_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
23 changes: 19 additions & 4 deletions src/image_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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<P: AsRef<Path>>(path: P) -> bool {
fs::symlink_metadata(path).is_ok_and(|f| f.file_type().is_symlink())
}

fn symlink_base<P: AsRef<Path>>(base_path: P) -> Result<(), io::Error> {
let base_link = Path::new("/nix/.base");
let gcroots = Path::new("/nix/var/nix/gcroots");
Expand Down
1 change: 0 additions & 1 deletion src/pid_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions src/shell.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
collections::HashMap,
ffi::{OsStr, OsString},
fs::create_dir_all,
os::unix::process::CommandExt,
process::{self, exit, Command},
};
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 070cb6d

Please sign in to comment.