From 227b97b27f0875e20c3c9a58bbae17018fbdbf23 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Thu, 9 Jan 2025 23:00:22 +0100 Subject: [PATCH] refactor --- src/install/desktop.rs | 29 +++++++++++++++++++++++ src/install/mod.rs | 53 +++++++----------------------------------- src/install/ssh.rs | 4 ++-- src/install/virt.rs | 31 ++++++++++++++++++++++++ src/linux.rs | 1 + src/main.rs | 32 ++++++++++++------------- 6 files changed, 87 insertions(+), 63 deletions(-) create mode 100644 src/install/desktop.rs create mode 100644 src/install/virt.rs diff --git a/src/install/desktop.rs b/src/install/desktop.rs new file mode 100644 index 0000000..3b3b2d3 --- /dev/null +++ b/src/install/desktop.rs @@ -0,0 +1,29 @@ +use crate::{ + config::InstallConfig, + linux::install_file, + pkg::{self, install_pkgs}, + print_status, +}; + +use super::navos::setup_navos; + +pub fn setup_desktop(conf: &InstallConfig) { + setup_navos(); + install_pkgs(&pkg::DESKTOP_PKG); + print_status("Enable SDDM"); + print_status("Set keyboard layout for SDDM"); + install_file( + "/mnt/usr/share/sddm/scripts/Xsetup", + &format!( + "#!/bin/sh\n# Xsetup\nsetxkbmap {},us\n", + conf.general.keyboard_layout + ), + 0o644, + ); + + std::os::unix::fs::symlink( + "/usr/lib/systemd/system/sddm.service", + "/mnt/etc/systemd/system/display-manager.service", + ) + .unwrap(); +} diff --git a/src/install/mod.rs b/src/install/mod.rs index 228483e..0fd2c54 100644 --- a/src/install/mod.rs +++ b/src/install/mod.rs @@ -4,6 +4,7 @@ // DRIVE SELECTION use boot::setup_bootloader; +use desktop::setup_desktop; use docker::setup_docker; use drives::{format_drives, mount_drives}; use first_boot::{first_boot_values, genfstab}; @@ -14,10 +15,12 @@ use security::{setup_secure_boot, setup_tpm_unlock}; use skel::setup_skel; use ssh::setup_ssh; use user::setup_users; +use virt::setup_virtualization; use yansi::{Color, Paint}; use zram::setup_zram; pub mod boot; +pub mod desktop; pub mod docker; pub mod drives; pub mod first_boot; @@ -28,13 +31,12 @@ pub mod security; pub mod skel; pub mod ssh; pub mod user; +pub mod virt; pub mod zram; use crate::{ - config::{InstallConfig, InstallMode}, - linux::{arch_chroot, install_file, systemd_service_enable}, + config::InstallConfig, pkg::{self, install_pkgs, pacstrap}, - print_status, }; pub fn uncomment_first_value_of(value: &str, file: &str) { @@ -91,31 +93,14 @@ pub fn install(conf: InstallConfig) { setup_skel(&conf.general); setup_users(&conf.user.as_ref().unwrap_or(&Vec::new())); - setup_ssh(conf.ssh); + setup_ssh(&conf.ssh); setup_bootloader(); match conf.general.mode { crate::config::InstallMode::Base => {} crate::config::InstallMode::Desktop => { - setup_navos(); - install_pkgs(&pkg::DESKTOP_PKG); - print_status("Enable SDDM"); - print_status("Set keyboard layout for SDDM"); - install_file( - "/mnt/usr/share/sddm/scripts/Xsetup", - &format!( - "#!/bin/sh\n# Xsetup\nsetxkbmap {},us\n", - conf.general.keyboard_layout - ), - 0o644, - ); - - std::os::unix::fs::symlink( - "/usr/lib/systemd/system/sddm.service", - "/mnt/etc/systemd/system/display-manager.service", - ) - .unwrap(); + setup_desktop(&conf); } crate::config::InstallMode::Server => { setup_navos(); @@ -127,29 +112,7 @@ pub fn install(conf: InstallConfig) { } if conf.pkg.virtualization.unwrap_or_default() { - let user_conf = if let Some(user_conf) = &conf.user { - user_conf.clone() - } else { - Vec::new() - }; - - install_pkgs(&["libvirt"]); - - if matches!(conf.general.mode, InstallMode::Desktop) { - install_pkgs(&["virt-manager"]); - } - - systemd_service_enable("libvirtd.service"); - - for user in user_conf { - if user.virtualization.unwrap_or_default() { - arch_chroot( - &vec!["usermod", "-a", "-G", "libvirt", user.name.as_str()], - None, - false, - ); - } - } + setup_virtualization(&conf); } if conf.pkg.docker.unwrap_or_default() { diff --git a/src/install/ssh.rs b/src/install/ssh.rs index f1685de..1157df9 100644 --- a/src/install/ssh.rs +++ b/src/install/ssh.rs @@ -4,7 +4,7 @@ use std::io::Write; /// Setup SSH on the system /// /// This should be done after `setup_users()` to ensure that the users directories exist. -pub fn setup_ssh(conf: Option) { +pub fn setup_ssh(conf: &Option) { if let Some(conf) = conf { install_pkgs(&["openssh"]); @@ -14,7 +14,7 @@ pub fn setup_ssh(conf: Option) { install_file("/mnt/etc/ssh/sshd_config", &content, 0o644); } - for key in &conf.key.unwrap_or_default() { + for key in conf.key.as_ref().unwrap_or(&Vec::new()) { for user in &key.users { let path = if user == "root" { std::fs::create_dir_all("/root/.ssh").unwrap(); diff --git a/src/install/virt.rs b/src/install/virt.rs new file mode 100644 index 0000000..1e1300a --- /dev/null +++ b/src/install/virt.rs @@ -0,0 +1,31 @@ +use crate::{ + config::{InstallConfig, InstallMode}, + linux::{arch_chroot, systemd_service_enable}, + pkg::install_pkgs, +}; + +pub fn setup_virtualization(conf: &InstallConfig) { + let user_conf = if let Some(user_conf) = &conf.user { + user_conf.clone() + } else { + Vec::new() + }; + + install_pkgs(&["libvirt"]); + + if matches!(conf.general.mode, InstallMode::Desktop) { + install_pkgs(&["virt-manager"]); + } + + systemd_service_enable("libvirtd.service"); + + for user in user_conf { + if user.virtualization.unwrap_or_default() { + arch_chroot( + &vec!["usermod", "-a", "-G", "libvirt", user.name.as_str()], + None, + false, + ); + } + } +} diff --git a/src/linux.rs b/src/linux.rs index d6fd2fc..3711f80 100644 --- a/src/linux.rs +++ b/src/linux.rs @@ -75,6 +75,7 @@ pub fn install_file(path: &str, content: &str, permissions: u32) { file.write_all(content.as_bytes()).unwrap(); let permissions = std::fs::Permissions::from_mode(permissions); + // TODO : Fix permission format print_status(&format!("Wrote file {path} [{permissions:#?}]")); std::fs::set_permissions(path, permissions).unwrap(); } diff --git a/src/main.rs b/src/main.rs index bdf8902..986bd0e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -97,20 +97,20 @@ pub fn read_conf(config_file: &str) -> InstallConfig { let config_content = std::fs::read_to_string(config_file); match config_content { - Ok(content) => match toml::from_str(&content) { - Ok(config) => config, - Err(e) => { - eprintln!( - "{} {}", - "Error: Could not deserialize TOML file.".paint(Color::Red), - e.paint(Color::Red) - ); - std::process::exit(1); - } - }, - Err(_) => { - eprintln!("{}", "Error: Could not read config file.".paint(Color::Red)); - std::process::exit(1); - } + Ok(content) => match toml::from_str(&content) { + Ok(config) => config, + Err(e) => { + eprintln!( + "{} {}", + "Error: Could not deserialize TOML file.".paint(Color::Red), + e.paint(Color::Red) + ); + std::process::exit(1); } -} \ No newline at end of file + }, + Err(_) => { + eprintln!("{}", "Error: Could not read config file.".paint(Color::Red)); + std::process::exit(1); + } + } +}