use std::{ env, fs::{read_dir, DirEntry}, io, path::PathBuf, process::{Command, Stdio}, }; use crate::fs::resolve_link; pub fn handle_session() -> io::Result<()> { println!(">>> Activating environment <<<"); let generation = env::var("NIXGREETY_GENERATION").unwrap(); Command::new(format!("{}/activate", generation)) .status() .expect("Failed to activate environment"); println!(">>> Creating session <<<"); Command::new(format!("{}/session/init", generation)) .status() .expect("Failed to create session"); println!(">>> Deactivating environment <<<"); let home = env::var("HOME").unwrap(); let profile_path = PathBuf::from(format!("{}/.local/state/nix/profiles/", home)); let mut generations: Vec = read_dir(profile_path) .expect("Failed to gather home-manager generations") .flatten() .collect(); generations.sort_by_key(|entry| entry.file_name()); let generation = generations .into_iter() .rev() .filter(|entry| { entry .file_name() .to_string_lossy() .starts_with("home-manager-") }) .find(|entry| { let entry_path = entry.path(); let resolved_path = resolve_link(&entry_path); let specialisation_path = resolved_path.join("specialisation"); specialisation_path.exists() }) .map(|entry| entry.path()) .unwrap_or(PathBuf::from("")); Command::new(format!("{}/activate", generation.display())) .status() .unwrap(); Ok(()) }