aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/basedir.rs12
-rw-r--r--src/run/var.rs19
2 files changed, 26 insertions, 5 deletions
diff --git a/src/basedir.rs b/src/basedir.rs
index d2ede87..3589b71 100644
--- a/src/basedir.rs
+++ b/src/basedir.rs
@@ -14,30 +14,36 @@ pub fn home() -> PathBuf {
pub mod xdg {
use super::*;
+ pub const DATA_HOME: &str = "XDG_DATA_HOME";
+
/// $XDG_DATA_HOME defines the base directory relative to which user-specific data files should be stored.
/// If $XDG_DATA_HOME is either not set or empty, a default equal to $HOME/.local/share should be used.
pub fn data_home() -> PathBuf {
- if let Some(dir) = env::var_os("XDG_DATA_HOME") {
+ if let Some(dir) = env::var_os(DATA_HOME) {
PathBuf::from(dir)
} else {
home().join(".local/share")
}
}
+ pub const CONFIG_HOME: &str = "XDG_CONFIG_HOME";
+
/// $XDG_CONFIG_HOME defines the base directory relative to which user-specific configuration files should be stored.
/// If $XDG_CONFIG_HOME is either not set or empty, a default equal to $HOME/.config should be used.
pub fn config_home() -> PathBuf {
- if let Some(dir) = env::var_os("XDG_CONFIG_HOME") {
+ if let Some(dir) = env::var_os(CONFIG_HOME) {
PathBuf::from(dir)
} else {
home().join(".config")
}
}
+ pub const STATE_HOME: &str = "XDG_STATE_HOME";
+
/// $XDG_STATE_HOME defines the base directory relative to which user-specific state files should be stored.
/// If $XDG_STATE_HOME is either not set or empty, a default equal to $HOME/.local/state should be used.
pub fn state_home() -> PathBuf {
- if let Some(dir) = env::var_os("XDG_STATE_HOME") {
+ if let Some(dir) = env::var_os(STATE_HOME) {
PathBuf::from(dir)
} else {
home().join(".local/state")
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, fn() -> 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
}
}