From 2acae17fc235dce288c186d04b4f0750bdecc198 Mon Sep 17 00:00:00 2001 From: Jonas Maier Date: Thu, 14 May 2026 17:12:18 +0200 Subject: automatically set XDG_ variables for child processes --- src/run/var.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'src/run/var.rs') diff --git a/src/run/var.rs b/src/run/var.rs index d3e98f4..8e22f5c 100644 --- a/src/run/var.rs +++ b/src/run/var.rs @@ -1,5 +1,6 @@ use std::borrow::Cow; use std::collections::{HashMap, HashSet}; +use std::os::unix::ffi::OsStrExt; use std::sync::LazyLock; use std::time::Instant; @@ -19,7 +20,10 @@ impl Vars { magic: HashMap BString>, ) -> Self { for (var, val) in std::env::vars_os() { - simple.insert(var.into_encoded_bytes(), val.into_encoded_bytes()); + // explicitly added variables get priority. + if !simple.contains_key(var.as_bytes()) { + simple.insert(var.into_encoded_bytes(), val.into_encoded_bytes()); + } } let all = simple .keys() @@ -87,10 +91,15 @@ macro_rules! map { impl Default for Vars { fn default() -> Self { + use crate::basedir::xdg; + let simple = map! { b"PISH_VERSION" => crate::consts::PISH_VERSION.as_bytes().to_vec(), b"PISH_COMMIT" => crate::consts::PISH_COMMIT.as_bytes().to_vec(), b"PISH_DIRTY" => vec![crate::consts::PISH_DIRTY as u8 + b'0'], + xdg::DATA_HOME => xdg::data_home().as_os_str().as_bytes().to_vec(), + xdg::STATE_HOME => xdg::state_home().as_os_str().as_bytes().to_vec(), + xdg::CONFIG_HOME => xdg::config_home().as_os_str().as_bytes().to_vec(), }; let magic = map! { @@ -101,7 +110,13 @@ impl Default for Vars { // call it once such that lazylock gets initialized seconds_since_startup(); - Self::new(simple, magic) + let mut this = Self::new(simple.clone(), magic); + for var in simple.keys() { + if var.starts_with(b"XDG_") { + this.allow_export(var, true); + } + } + this } } -- cgit v1.2.3