Skip to content

Commit

Permalink
Use XDG_DATA_HOME on *nix if no ~/.pkgx
Browse files Browse the repository at this point in the history
* Fixes #1103
* Ordering is backwards compatible
* Not allowing relative PKGX_DIR which is more sensible and same as pkgx^1
  • Loading branch information
mxcl committed Jan 30, 2025
1 parent ea07115 commit 6047ffa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
24 changes: 13 additions & 11 deletions crates/lib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,20 @@ fn get_pantry_dir() -> io::Result<PathBuf> {
}

fn get_pkgx_dir() -> io::Result<PathBuf> {
if let Ok(env_dir) = env::var("PKGX_DIR") {
let path = PathBuf::from(env_dir);
if !path.is_absolute() {
return Ok(env::current_dir()?.join(path));
} else {
if let Ok(path) = env::var("PKGX_DIR") {
let path = PathBuf::from(path);
if path.is_absolute() {
return Ok(path);
}
}
#[cfg(target_os = "macos")]
return Ok(dirs_next::home_dir().unwrap().join(".pkgx"));
#[cfg(target_os = "linux")]
return Ok(dirs_next::home_dir().unwrap().join(".pkgx"));
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
panic!("Unsupported platform")

let default = dirs_next::home_dir().map(|x| x.join(".pkgx"));

if default.clone().is_some_and(|x| x.exists()) {
Ok(default.unwrap())
} else if let Ok(xdg) = env::var("XDG_DATA_HOME") {
Ok(PathBuf::from(xdg).join("pkgx"))
} else {
Ok(default.unwrap())
}
}
19 changes: 14 additions & 5 deletions docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,20 @@ following [semver] syntax:

## Where does `pkgx` store files

* pkgs are downloaded to `~/.pkgx` (`$PKGX_DIR` overrides)
* runtime data like the [pantry] is stored in:
* `~/Library/Caches/pkgx` on Mac
* `${XDG_CACHE_HOME:-$HOME/.cache}/pkgx` on *nix
* `%LOCALAPPDATA%/pkgx` on Windows
Packages are downloaded to `$PKGX_DIR` if set. If not set:

* macOS
* `~/Library/Packages` if the directory exists
* `~/.pkgx` otherwise
* *nix
* `~/.pkgx` if the directory exists
* `${XDG_DATA_HOME:-$HOME/.local/share}/pkgx` otherwise

Some cache data is stored:

* `~/Library/Caches/pkgx` on Mac
* `${XDG_CACHE_HOME:-$HOME/.cache}/pkgx` on *nix
* `%LOCALAPPDATA%/pkgx` on Windows


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

0 comments on commit 6047ffa

Please sign in to comment.