Compare commits

..
11 Commits
5 changed files with 149 additions and 33 deletions

No files matched your search

+11
View File
@@ -0,0 +1,11 @@
use std::{fs::read_link, path::PathBuf};
pub fn resolve_link(path: &PathBuf) -> PathBuf {
let mut location = path.clone();
while let Ok(next) = read_link(&location) {
let base_path = location.parent().unwrap();
let next_path = base_path.join(next);
location = next_path;
}
location
}
+15 -19
View File
@@ -3,7 +3,7 @@ use std::{
path::{Path, PathBuf},
};
use crate::{Session, User};
use crate::{fs::resolve_link, Session, User};
#[derive(Debug)]
struct PasswdUser {
@@ -52,25 +52,15 @@ pub fn gather_sessions(user: &User) -> Vec<Session> {
true => gather_hm_sessions(&hm_profile),
false => vec![Session {
name: String::from("Shell"),
init: user.shell.clone(),
generation: user.shell.to_str().unwrap().to_owned(),
env: vec![String::from("NIXGREETY_SIMPLE=1")],
}],
}
}
#[allow(clippy::ptr_arg)]
fn resolve_link(path: &PathBuf) -> PathBuf {
let mut location = path.clone();
while let Ok(next) = read_link(&location) {
let base_path = location.parent().unwrap();
let next_path = base_path.join(next);
location = next_path;
}
location
}
fn gather_hm_sessions(path: &PathBuf) -> Vec<Session> {
let generation = resolve_link(path);
let mut sessions = vec![gather_session_data(&generation)];
let mut sessions = vec![gather_session_data(&generation, &generation)];
sessions.append(&mut gather_specialisations(&generation));
@@ -87,7 +77,7 @@ fn gather_specialisations(path: &PathBuf) -> Vec<Session> {
.unwrap()
.flatten()
.map(|entry| {
let path = resolve_link(&entry.path());
let link_path = resolve_link(&entry.path());
// let name = entry
// .path()
// .file_name()
@@ -96,16 +86,22 @@ fn gather_specialisations(path: &PathBuf) -> Vec<Session> {
// .unwrap()
// .to_owned();
// Session { name, path }
gather_session_data(&path)
gather_session_data(&link_path, path)
})
.collect()
}
fn gather_session_data(path: &PathBuf) -> Session {
fn gather_session_data(path: &PathBuf, reset_path: &PathBuf) -> Session {
let session_path = path.join("session");
let env_file = read_to_string(session_path.join("env")).unwrap();
let name = read_to_string(session_path.join("name")).unwrap();
let init = session_path.join("init");
let generation = path.to_str().unwrap().to_owned();
let env = env_file.lines().map(String::from).collect();
Session { name, init }
Session {
name,
generation,
env,
}
}
+23 -3
View File
@@ -1,4 +1,4 @@
use std::{env, error::Error, os::unix::net::UnixStream};
use std::{env, error::Error, os::unix::net::UnixStream, process::Command};
use greetd_ipc::{codec::SyncCodec, AuthMessageType, ErrorType, Request, Response};
@@ -30,8 +30,28 @@ pub fn login(
return Ok(LoginResult::Success);
} else {
starting = true;
let cmd = vec![session.init.to_str().unwrap().to_owned()];
let env = vec![];
let cmd = vec![String::from("nixgreety session")];
let mut env = vec![format!(
"NIXGREETY_GENERATION={}",
session.generation.clone()
)];
env.append(&mut session.env.clone());
// Activate the generation before handing control back to greetd
// Command::new(format!(
// "su - -c \"{}/activate\" {}",
// session.generation, user.name
// ))
Command::new("su")
.args([
"-",
"-c",
&format!("{}/activate", session.generation),
&user.name,
])
.status()
.expect("Failed to activate environment");
Request::StartSession { cmd, env }.write_to(&mut stream)?;
}
}
+32 -11
View File
@@ -2,7 +2,6 @@ use std::{
env,
fmt::Display,
io,
os::unix::net::UnixStream,
path::PathBuf,
time::{Duration, Instant},
};
@@ -18,10 +17,27 @@ use ratatui::{
};
use tui_rain::Rain;
mod fs;
mod gather;
mod greetd;
mod session;
fn main() -> io::Result<()> {
let mut args = env::args();
let _ = args.next();
let mode = args.next();
match mode {
Some(mode) => match mode.as_str() {
"greeter" => draw_app(),
"session" => session::handle_session(),
_ => panic!("Unrecognized mode: {}", mode),
},
None => draw_app(),
}
}
fn draw_app() -> io::Result<()> {
let mut terminal = ratatui::init();
let app_result = App::default().run(&mut terminal);
ratatui::restore();
@@ -29,7 +45,7 @@ fn main() -> io::Result<()> {
}
struct App {
start_time: Instant,
exit: bool,
exit_time: Option<Instant>,
error: bool,
focus: Focus,
status: Status,
@@ -111,7 +127,8 @@ impl Display for User {
#[derive(PartialEq, Eq, Clone)]
struct Session {
name: String,
init: PathBuf,
generation: String,
env: Vec<String>,
}
impl Display for Session {
@@ -123,7 +140,7 @@ impl Display for Session {
impl Default for App {
fn default() -> Self {
let start_time = Instant::now();
let exit = false;
let exit_time = None;
let error = false;
let focus = Focus::User;
let status = Status::Nothing;
@@ -141,7 +158,7 @@ impl Default for App {
Self {
start_time,
exit,
exit_time,
error,
focus,
status,
@@ -155,7 +172,10 @@ impl Default for App {
impl App {
pub fn run(&mut self, terminal: &mut DefaultTerminal) -> io::Result<()> {
self.start_time = Instant::now();
while !self.exit {
while match self.exit_time {
Some(exit_time) => Instant::now() < exit_time,
None => true,
} {
terminal.draw(|frame| self.draw(frame))?;
self.handle_events()?;
}
@@ -175,8 +195,8 @@ impl App {
})
.with_bold_dim_effect(false)
.with_rain_density(tui_rain::RainDensity::Relative { sparseness: 60 })
.with_rain_speed(tui_rain::RainSpeed::Absolute { speed: 0.002 })
.with_rain_speed_variance(10000.0)
.with_rain_speed(tui_rain::RainSpeed::Absolute { speed: 0.004 })
.with_rain_speed_variance(4000.0)
.with_tail_lifespan(Duration::from_millis(2500))
.with_color(Color::Green)
.with_head_color(Color::White)
@@ -307,7 +327,7 @@ impl App {
}
fn exit(&mut self) {
self.exit = true;
self.exit_time = Some(Instant::now());
}
fn submit(&mut self) {
@@ -325,11 +345,12 @@ impl App {
{
LoginResult::Success => {
self.error = false;
self.status = Status::Success
self.status = Status::Success;
self.exit_time = Some(Instant::now() + Duration::from_millis(200));
}
LoginResult::Failure => {
self.error = true;
self.status = Status::Error
self.status = Status::Error;
}
};
}
+68
View File
@@ -0,0 +1,68 @@
use std::{
env,
fs::{read_dir, DirEntry},
io,
path::PathBuf,
process::{Command, Stdio},
};
use crate::fs::resolve_link;
pub fn handle_session() -> io::Result<()> {
match env::var("NIXGREETY_SIMPLE") {
Ok(_) => handle_simple_session(),
Err(_) => handle_hm_session(),
}
Ok(())
}
fn handle_hm_session() {
println!(">>> Activating environment <<<");
let generation = env::var("NIXGREETY_GENERATION").unwrap();
// Activating the generation is now handled before handing control back to greetd
// 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<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()))
.status()
.unwrap();
}
fn handle_simple_session() {
let generation = env::var("NIXGREETY_GENERATION").unwrap();
Command::new(&generation)
.status()
.expect("Failed to create session");
}