artifactize_tools/platform/environment.rs
1//! The environment of the programs artifactize starts: the person's own, as their terminal
2//! or desktop session gave it to artifactize.
3//!
4//! Only an app started from an AppImage runs with more. The bundle's AppRun and its hooks
5//! put the bundle's directories first in path lists such as `PATH` and `LD_LIBRARY_PATH` and
6//! set variables of their own; the app needs them to load its bundled libraries, while a
7//! system program it starts would load those libraries instead of its own and fail.
8//! Everywhere else this is the process environment unchanged.
9
10use std::{collections::BTreeMap, ffi::OsString};
11
12/// This process's environment as the person using artifactize has it, for a program it
13/// starts.
14pub fn user() -> BTreeMap<OsString, OsString> {
15 #[cfg(target_os = "linux")]
16 if let Some(bundle) = super::appimage::Bundle::current() {
17 return bundle.without(std::env::vars_os());
18 }
19 std::env::vars_os().collect()
20}
21
22/// One variable of [`user`]; `None` when it is unset there. Names compare as this system
23/// compares them.
24pub fn var(name: &str) -> Option<OsString> {
25 #[cfg(target_os = "linux")]
26 if let Some(bundle) = super::appimage::Bundle::current() {
27 return bundle
28 .without(std::env::vars_os())
29 .remove(std::ffi::OsStr::new(name));
30 }
31 std::env::var_os(name)
32}