Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelcoeffic committed Dec 18, 2024
1 parent 6c8c014 commit 2d7ef40
Showing 4 changed files with 42 additions and 20 deletions.
8 changes: 6 additions & 2 deletions src/bin/pkg.rs
Original file line number Diff line number Diff line change
@@ -65,8 +65,12 @@ fn main() -> Result<()> {
process::exit(1);
}
Some(pkg) => {
return dive::nixpkgs::install_package(pkg)
.context("failed to install package");
if let Err(err) = dive::nixpkgs::install_package(pkg)
.context("failed to install package")
{
eprintln!("error: {err}");
process::exit(1);
}
}
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -20,6 +20,8 @@ pub(crate) const USER_ENV_DIR: &str = "/nix/.env";
pub(crate) const BASE_DIR: &str = "/nix/.base";
pub(crate) const ETC_DIR: &str = "/nix/etc";

pub(crate) const SSL_CERTS: &str = "/nix/.base/etc/ssl/certs/ca-bundle.crt";

pub const BASE_PACKAGES: &[&str] = &[
"bash",
"cacert",
1 change: 0 additions & 1 deletion src/nixos.rs
Original file line number Diff line number Diff line change
@@ -149,7 +149,6 @@ pub(crate) fn run_gc() -> Result<()> {
let status = Command::new("nix")
.args(["store", "gc"])
.envs(ENV_VARS)
.stdout(Stdio::null())
.status()?;

if !status.success() {
51 changes: 34 additions & 17 deletions src/shell.rs
Original file line number Diff line number Diff line change
@@ -20,6 +20,9 @@ pub struct Shell {
}

impl Shell {
const NIX_ENVS: [&str; 2] = [crate::USER_ENV_DIR, crate::BASE_DIR];
const BIN_DIRS: [&str; 2] = ["bin", "sbin"];

pub fn new(name: &str) -> Self {
Shell {
name: name.to_owned(),
@@ -40,6 +43,17 @@ impl Shell {
self
}

pub fn spawn(self) -> Result<i32> {
match unsafe { fork()? } {
Fork::Child(_) => {
let err = self.exec();
log::error!("cannot execute shell: {err}");
exit(1);
}
Fork::Parent(child_pid) => wait_for_child(child_pid),
}
}

fn exec(self) -> std::io::Error {
//
// TODO: path HOME w/ user as defined by /etc/passwd
@@ -64,7 +78,7 @@ impl Shell {
};

// TODO: these variable except for TERM should be initialized in zshenv
let nix_bin_path = "/nix/.base/sbin:/nix/.base/bin:/nix/.bin";
let nix_bin_path = Self::get_bin_paths();
cmd.env("PATH", format!("{nix_bin_path}:{proc_path}"));

if let Ok(term) = std::env::var("TERM") {
@@ -85,39 +99,42 @@ impl Shell {
);
cmd.env("PROMPT", &prompt);

let nix_base = crate::BASE_DIR;
let data_dir = format!("/usr/local/share:/usr/share:{nix_base}/share");
let share_paths = Self::get_env_paths("share");
let data_dir = format!("{share_paths}:/usr/local/share:/usr/share");
cmd.envs([
("ZDOTDIR", crate::ETC_DIR),
("NIX_CONF_DIR", crate::ETC_DIR),
(
"NIX_SSL_CERT_FILE",
"/nix/.base/etc/ssl/certs/ca-bundle.crt",
),
("NIX_SSL_CERT_FILE", crate::SSL_CERTS),
("XDG_CACHE_HOME", crate::CACHE_HOME),
("XDG_CONFIG_HOME", crate::CONFIG_HOME),
("XDG_DATA_DIR", &data_dir),
]);

cmd.envs([
("TERMINFO_DIRS", format!("{nix_base}/share/terminfo")),
("LIBEXEC_PATH", format!("{nix_base}/libexec")),
("INFOPATH", format!("{nix_base}/share/info")),
("TERMINFO_DIRS", Self::get_env_paths("share/terminfo")),
("LIBEXEC_PATH", Self::get_env_paths("libexec")),
("INFOPATH", Self::get_env_paths("share/info")),
]);

let _ = create_dir_all(crate::CACHE_HOME);
cmd.exec()
}

pub fn spawn(self) -> Result<i32> {
match unsafe { fork()? } {
Fork::Child(_) => {
let err = self.exec();
log::error!("cannot execute shell: {err}");
exit(1);
fn get_bin_paths() -> String {
let res_len = Self::NIX_ENVS.len() * Self::BIN_DIRS.len();
let mut paths = Vec::with_capacity(res_len);

for env in Self::NIX_ENVS {
for dir in Self::BIN_DIRS {
paths.push(format!("{}/{}", env, dir));
}
Fork::Parent(child_pid) => wait_for_child(child_pid),
}

paths.join(":")
}

fn get_env_paths(dir: &str) -> String {
Self::NIX_ENVS.map(|e| format!("{e}/{dir}")).join(":")
}
}

0 comments on commit 2d7ef40

Please sign in to comment.