2025-02-22 00:30:17 +00:00
|
|
|
use std::{
|
|
|
|
env,
|
|
|
|
fs::{read_dir, DirEntry},
|
|
|
|
io,
|
|
|
|
path::PathBuf,
|
|
|
|
process::{Command, Stdio},
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::fs::resolve_link;
|
|
|
|
|
|
|
|
pub fn handle_session() -> io::Result<()> {
|
2025-02-22 05:36:08 +00:00
|
|
|
match env::var("NIXGREETY_SIMPLE") {
|
|
|
|
Ok(_) => handle_simple_session(),
|
|
|
|
Err(_) => handle_hm_session(),
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_hm_session() {
|
2025-02-22 00:30:17 +00:00
|
|
|
println!(">>> Activating environment <<<");
|
|
|
|
let generation = env::var("NIXGREETY_GENERATION").unwrap();
|
|
|
|
Command::new(format!("{}/activate", generation))
|
2025-02-22 05:05:06 +00:00
|
|
|
.status()
|
2025-02-22 00:30:17 +00:00
|
|
|
.expect("Failed to activate environment");
|
|
|
|
|
|
|
|
println!(">>> Creating session <<<");
|
|
|
|
Command::new(format!("{}/session/init", generation))
|
2025-02-22 05:05:06 +00:00
|
|
|
.status()
|
2025-02-22 00:30:17 +00:00
|
|
|
.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<DirEntry> = 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()))
|
2025-02-22 05:05:06 +00:00
|
|
|
.status()
|
2025-02-22 00:30:17 +00:00
|
|
|
.unwrap();
|
2025-02-22 05:36:08 +00:00
|
|
|
}
|
2025-02-22 00:30:17 +00:00
|
|
|
|
2025-02-22 05:36:08 +00:00
|
|
|
fn handle_simple_session() {
|
|
|
|
let generation = env::var("NIXGREETY_GENERATION").unwrap();
|
|
|
|
Command::new(&generation)
|
|
|
|
.status()
|
|
|
|
.expect("Failed to create session");
|
2025-02-22 00:30:17 +00:00
|
|
|
}
|