From 27bfd3eefacfb2ad7544806df974bfb580eb2dcd Mon Sep 17 00:00:00 2001 From: Aaron Honeycutt Date: Thu, 10 Apr 2025 20:16:34 -0600 Subject: [PATCH] add mount feature for nix crate --- Cargo.toml | 2 +- src/main.rs | 77 +++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 05bc2ba..69aee11 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,6 @@ edition = "2021" [dependencies] curl = "0.4.47" -nix = "0.29.0" +nix = { version = "0.29.0", features = ["mount"] } rsfdisk = "0.1.0" sys_metrics = "0.2.7" diff --git a/src/main.rs b/src/main.rs index 0ec2d6e..873ce01 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,16 @@ use std::io; use std::io::Write; +use std::fs; use std::fs::File; use std::process::Command; +use std::path::Path; use sys_metrics::disks; use rsfdisk::fdisk::Fdisk; use rsfdisk::core::partition_table::PartitionTableKind; use rsfdisk::core::partition::{Guid, Partition, PartitionKind}; +use nix::mount::{mount, MsFlags}; +use std::os::unix::ffi::OsStrExt; use curl::easy::Easy; fn format_drive(drive_name: &str) -> rsfdisk::Result<()> { @@ -38,6 +42,7 @@ fn format_drive(drive_name: &str) -> rsfdisk::Result<()> { // .size_in_sectors(121_634_816) // Internal drive testing .size_in_sectors(499_033_071_61) + // replace static int with a variable .build()?; let _ = disk.partition_add(root)?; @@ -52,17 +57,71 @@ fn format_partitions() { .arg("-F32") .arg("-n") .arg("EFI") + // replace static path with a variable .arg("/dev/nvme1n1p1") .output() .expect("Failed to partition boot partition as FAT32"); let _root_partition = Command::new("mkfs.ext4") + // replace static path with a variable .arg("/dev/nvme1n1p2") .output() .expect("Failed to partition root partition as ext4"); } -// fn mount_parts() {} +fn mount_root() { + // replace static path with a variable + let root_source = Some(Path::new("/dev/nvme1n1p2")); + let root_target = Path::new("/mnt"); + + // unmounts root before hand to fix bugs + let _umount_root = Command::new("umount") + // replace static path with a variable + .arg("/dev/nvme1n1p2") + .output() + .expect("Failed to unmount root"); + + mount( + root_source, + root_target, + Some("ext4"), + MsFlags::empty(), + None::<&[u8]>, + ) + .expect("Failed to mount root partition"); + + // Deletes the boot directory + // Shouldn't be needed with a blank drive + // So just for testing + let _delete_boot_directory = Command::new("rm") + .arg("-r") + .arg("/mnt/boot") + .output() + .expect("Failed to delete boot directory"); + + // Creates the boot directory in /mnt/boot + let _create_boot_directory = Command::new("mkdir") + .arg("/mnt/boot") + .output() + .expect("Failed to create boot directory"); +} + +fn mount_boot() { + // replace static path with a variable + let boot_source = Some(Path::new("/dev/nvme1n1p1")); + let boot_target = Path::new("/mnt/boot"); + + mount( + boot_source, + boot_target, + Some("FAT32"), + MsFlags::empty(), + None::<&[u8]>, + ) + .expect("Failed to mount boot partition"); + + println!(""); +} fn grab_flake() { let mut easy = Easy::new(); @@ -151,6 +210,11 @@ fn main() { // Formatting the partitions format_partitions(); + // Mounting the partitions + mount_root(); + + // mount_boot(); + // Download nix files grab_flake(); grab_config(); @@ -192,14 +256,17 @@ fn main() { }).unwrap(); transfer.perform().unwrap(); } - break - } - "2" => { + let _nixos_install = Command::new("nixos-install") .arg("--flake") .arg("/mnt/etc/nixos#garrus") .output() - .expect("Fiale to execute command"); + .expect("Failed to execute command"); + + break + } + "2" => { + println!("Nix the world!"); }, "3" => { println!("Goodbye!");