Files
ghostty-rs/crates/ghostty-os/src/passwd.rs
T
ddidderr b681241359 feat(os): port Ghostty's Linux OS helpers
Ports ZIG/src/os for Linux into ghostty-os: XDG dirs, home dir, hostname,
env/PATH helpers, passwd lookup (via nix), shell detection, resources
dir discovery, string encodings (printf-q, URL percent), systemd
sd_notify, i18n via gettextrs, xdg-open, desktop/cgroup/kernel info,
temp dirs, URI parsing (port of std.Uri incl. Ghostty's extra options),
and a zbus-based Flatpak HostCommand implementation.

The crate stays free of unsafe. Consequences, all tracked as
TODO(integration) in docs/rewrite/notes/ghostty-os.md:
- locale::ensure_locale is a no-op (setlocale/setenv are unsafe; GTK
  calls setlocale itself); setenv/unsetenv are not exposed.
- passwd's Flatpak branch needs the termio pty and falls back to the
  sandbox entry for now.
- FlatpakHostCommand's D-Bus path is untested (no portal here).
Intentional fix: a leading '@' in NOTIFY_SOCKET is treated as an
abstract socket.

macOS/Windows/WASM modules are out of scope.

Test Plan:
- cargo test -p ghostty-os (88 tests)
- cargo clippy -p ghostty-os --all-targets -- -D warnings
2026-10-11 21:54:11 +02:00

59 lines
1.8 KiB
Rust

use crate::flatpak::is_flatpak;
/// Entry that is retrieved from the passwd API. This only contains the fields
/// we care about.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Entry {
pub shell: Option<String>,
pub home: Option<String>,
pub name: Option<String>,
}
/// Get the passwd entry for the currently executing user.
pub fn get() -> std::io::Result<Entry> {
let uid = nix::unistd::getuid();
let user = match nix::unistd::User::from_uid(uid) {
Ok(Some(user)) => user,
Ok(None) => {
// Future: let's check if a better shell is available like zsh
log::warn!("no pw entry to detect default shell, will default to 'sh'");
return Ok(Entry::default());
}
Err(e) => {
log::warn!("error retrieving pw entry code={e}");
return Ok(Entry::default());
}
};
// TODO(integration): Zig shells out to the host via
// FlatpakHostCommand + a pty (`getent passwd <name>`) when running in
// Flatpak, because the sandbox passwd entry only has a correct username.
// That needs the `ghostty-termio` Pty; until then we use the sandbox
// entry and warn.
if is_flatpak() {
log::warn!(
"flatpak detected, but host passwd lookup is not implemented; \
using sandbox passwd entry"
);
}
Ok(Entry {
shell: Some(user.shell.to_string_lossy().into_owned()),
home: Some(user.dir.to_string_lossy().into_owned()),
name: Some(user.name),
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn get_entry() {
// We should be able to get an entry
let entry = get().unwrap();
assert!(entry.shell.is_some());
assert!(entry.home.is_some());
}
}