diff --git a/src/args.rs b/src/args.rs index 875b293..2ce5faa 100644 --- a/src/args.rs +++ b/src/args.rs @@ -26,6 +26,7 @@ pub fn get_args() -> clap::ArgMatches { .subcommand( command!() .name("create-tar") + .arg(arg!([dir] "root fs dir").required(true)) .about("Create a container tar image"), ) .subcommand( diff --git a/src/install/desktop.rs b/src/install/desktop.rs index 60b3c95..6d4c0f4 100644 --- a/src/install/desktop.rs +++ b/src/install/desktop.rs @@ -3,7 +3,7 @@ use crate::{config::InstallConfig, linux::install_file, pkg::install_pkgs, print use super::navos::setup_navos; pub fn setup_desktop(conf: &InstallConfig) { - setup_navos(); + setup_navos("/mnt"); install_pkgs(&["navos_desktop"]); print_status("Enable SDDM"); print_status("Set keyboard layout for SDDM"); diff --git a/src/install/mod.rs b/src/install/mod.rs index c2616cb..9b518ae 100644 --- a/src/install/mod.rs +++ b/src/install/mod.rs @@ -51,7 +51,7 @@ pub mod zram; use crate::{ config::InstallConfig, - pkg::{self, install_pkgs, pacstrap}, + pkg::{self, install_pkgs, pacstrap_at}, print_status, }; @@ -129,14 +129,14 @@ pub fn install(mut conf: InstallConfig, bare: bool) { pub fn install_mnt(conf: InstallConfig, bare: bool) { // Base Install - pacstrap(&conf.pkg); + pacstrap_at("/mnt", conf.pkg.kernel.as_ref(), &conf.pkg.pkg); genfstab(); // Configuration first_boot_values(&conf.general); if !matches!(conf.general.mode, crate::config::InstallMode::Base) { - setup_navos(); + setup_navos("/mnt"); } setup_skel(&conf.general); diff --git a/src/install/navos.rs b/src/install/navos.rs index 3e704dc..0fab60d 100644 --- a/src/install/navos.rs +++ b/src/install/navos.rs @@ -1,19 +1,25 @@ -use crate::{linux::arch_chroot, print_status}; +use crate::{linux::arch_chroot_at, print_status}; -pub fn setup_navos() { +pub fn setup_navos(root: &str) { + let root = std::path::PathBuf::from(root); // pacman.conf print_status("Copying pacman.conf"); - std::fs::copy("/etc/pacman.conf", "/mnt/etc/pacman.conf").unwrap(); + std::fs::copy("/etc/pacman.conf", root.join("etc").join("pacman.conf")).unwrap(); // import keys print_status("Importing pkg keys"); std::fs::write( - "/mnt/usr/share/pacman/keyrings/navos.gpg", + root.join("usr") + .join("share") + .join("pacman") + .join("keyrings") + .join("navos.gpg"), include_str!("../../navos.gpg"), ) .unwrap(); - arch_chroot( + arch_chroot_at( + root.display().to_string().as_str(), &[ "pacman-key", "--add", @@ -23,7 +29,8 @@ pub fn setup_navos() { false, ); - arch_chroot( + arch_chroot_at( + root.display().to_string().as_str(), &[ "pacman-key", "--lsign-key", @@ -35,5 +42,5 @@ pub fn setup_navos() { // remove os-release print_status("Removing os-release"); - let _ = std::fs::remove_file("/mnt/etc/os-release"); + let _ = std::fs::remove_file(root.join("etc").join("os-release")); } diff --git a/src/linux.rs b/src/linux.rs index ac41592..d3d2c97 100644 --- a/src/linux.rs +++ b/src/linux.rs @@ -94,6 +94,18 @@ pub fn run_command(cmd: &[&str], input: Option<&str>, inherit: bool) -> (String, (output, stderr) } +/// Runs a command in the chroot environment at `dir` +pub fn arch_chroot_at( + dir: &str, + cmd: &[&str], + input: Option<&str>, + inherit: bool, +) -> (String, String) { + let mut chroot_cmd = vec!["arch-chroot", dir]; + chroot_cmd.extend_from_slice(cmd); + run_command(&chroot_cmd, input, inherit) +} + /// Runs a command in the chroot environment at `/mnt` pub fn arch_chroot(cmd: &[&str], input: Option<&str>, inherit: bool) -> (String, String) { let mut chroot_cmd = vec!["arch-chroot", "/mnt"]; diff --git a/src/main.rs b/src/main.rs index 510a160..3a48efa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,22 @@ use navinstall::config::InstallConfig; +use navinstall::install::navos::setup_navos; use navinstall::install::{ drives::setup_disk_image, install, install_mnt, security::ensure_secure_boot, }; use navinstall::linux::is_root; +use navinstall::pkg::pacstrap_at; use navinstall::print::print_config; +use navinstall::print_status; use navinstall::{create_iso::create_iso, expect_yes}; use yansi::{Color, Paint}; +pub fn ensure_root() { + if !is_root() { + eprintln!("Error: You need root to install"); + std::process::exit(1); + } +} + fn main() { println!( "{}", @@ -53,9 +63,12 @@ fn main() { ); std::process::exit(0); } - Some(("create-tar", _)) => { - println!("Tar creation is not yet supported"); - unimplemented!() + Some(("create-tar", tar_options)) => { + ensure_root(); + let dir: &String = tar_options.get_one("dir").unwrap(); + print_status("Pacstrapping root fs"); + pacstrap_at(&dir, None, &[]); + setup_navos(dir.as_str()); } Some(("create-img", install_args)) => { let config_file: &String = install_args.get_one("config").unwrap(); @@ -66,10 +79,7 @@ fn main() { install_mnt(conf, false); } Some(("install", install_args)) => { - if !is_root() { - eprintln!("Error: You need root to install"); - std::process::exit(1); - } + ensure_root(); let config_file: &String = install_args.get_one("config").unwrap(); let force = install_args.get_flag("force"); diff --git a/src/pkg.rs b/src/pkg.rs index 4015421..ef3823d 100644 --- a/src/pkg.rs +++ b/src/pkg.rs @@ -1,7 +1,4 @@ -use crate::{ - config::PackageConfig, - linux::{arch_chroot, run_command}, -}; +use crate::linux::{arch_chroot, run_command}; pub const DESKTOP_PKG: [&str; 17] = [ // Desktop @@ -42,11 +39,11 @@ pub fn install_pkgs(pkg: &[&str]) { // PACSTRAP /// Initial system pacstrap -pub fn pacstrap(conf: &PackageConfig) { +pub fn pacstrap_at(dir: &str, kernel: Option<&String>, pkg: &[String]) { let mut cmd: Vec<&str> = vec![ "pacstrap", "-K", - "/mnt", + dir, "base", "linux-firmware", "git", @@ -60,7 +57,7 @@ pub fn pacstrap(conf: &PackageConfig) { ]; let linux_kernel = "linux".to_string(); - let kernel = conf.kernel.as_ref().unwrap_or(&linux_kernel); + let kernel = kernel.unwrap_or(&linux_kernel); let headers = format!("{kernel}-headers"); cmd.extend(&[kernel.as_str(), headers.as_str()]); @@ -70,9 +67,7 @@ pub fn pacstrap(conf: &PackageConfig) { } cmd.extend( - &conf - .pkg - .iter() + pkg.iter() .map(std::string::String::as_str) .collect::>(), );