Skip to content

Commit 6047ffa

Browse files
committed
Use XDG_DATA_HOME on *nix if no ~/.pkgx
* Fixes #1103 * Ordering is backwards compatible * Not allowing relative PKGX_DIR which is more sensible and same as pkgx^1
1 parent ea07115 commit 6047ffa

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

crates/lib/src/config.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,20 @@ fn get_pantry_dir() -> io::Result<PathBuf> {
4242
}
4343

4444
fn get_pkgx_dir() -> io::Result<PathBuf> {
45-
if let Ok(env_dir) = env::var("PKGX_DIR") {
46-
let path = PathBuf::from(env_dir);
47-
if !path.is_absolute() {
48-
return Ok(env::current_dir()?.join(path));
49-
} else {
45+
if let Ok(path) = env::var("PKGX_DIR") {
46+
let path = PathBuf::from(path);
47+
if path.is_absolute() {
5048
return Ok(path);
5149
}
5250
}
53-
#[cfg(target_os = "macos")]
54-
return Ok(dirs_next::home_dir().unwrap().join(".pkgx"));
55-
#[cfg(target_os = "linux")]
56-
return Ok(dirs_next::home_dir().unwrap().join(".pkgx"));
57-
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
58-
panic!("Unsupported platform")
51+
52+
let default = dirs_next::home_dir().map(|x| x.join(".pkgx"));
53+
54+
if default.clone().is_some_and(|x| x.exists()) {
55+
Ok(default.unwrap())
56+
} else if let Ok(xdg) = env::var("XDG_DATA_HOME") {
57+
Ok(PathBuf::from(xdg).join("pkgx"))
58+
} else {
59+
Ok(default.unwrap())
60+
}
5961
}

docs/faq.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,20 @@ following [semver] syntax:
214214

215215
## Where does `pkgx` store files
216216

217-
* pkgs are downloaded to `~/.pkgx` (`$PKGX_DIR` overrides)
218-
* runtime data like the [pantry] is stored in:
219-
* `~/Library/Caches/pkgx` on Mac
220-
* `${XDG_CACHE_HOME:-$HOME/.cache}/pkgx` on *nix
221-
* `%LOCALAPPDATA%/pkgx` on Windows
217+
Packages are downloaded to `$PKGX_DIR` if set. If not set:
218+
219+
* macOS
220+
* `~/Library/Packages` if the directory exists
221+
* `~/.pkgx` otherwise
222+
* *nix
223+
* `~/.pkgx` if the directory exists
224+
* `${XDG_DATA_HOME:-$HOME/.local/share}/pkgx` otherwise
225+
226+
Some cache data is stored:
227+
228+
* `~/Library/Caches/pkgx` on Mac
229+
* `${XDG_CACHE_HOME:-$HOME/.cache}/pkgx` on *nix
230+
* `%LOCALAPPDATA%/pkgx` on Windows
222231

223232

224233
## What happens if two packages provide the same named program?

0 commit comments

Comments
 (0)