Skip to content

Commit

Permalink
feat(config): XDG_CONFIG_HOME and XDG_CACHE_HOME compliance (#1050)
Browse files Browse the repository at this point in the history
Co-authored-by: Ruben Arts <[email protected]>
  • Loading branch information
chawyehsu and ruben-arts authored Mar 25, 2024
1 parent 999db88 commit 5d26f2f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
11 changes: 6 additions & 5 deletions docs/advanced/explain_info_command.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ Pixi caches all previously downloaded packages in a cache folder.
This cache folder is shared between all pixi projects and globally installed tools.
Normally the locations would be:

| Platform | Value |
|----------|-----------------------------------------------------|
| Linux | `$XDG_CACHE_HOME/rattler` or `$HOME`/.cache/rattler |
| macOS | `$HOME`/Library/Caches/rattler |
| Windows | `{FOLDERID_LocalAppData}/rattler` |
1. XDG compliant cache folder when it's available (`$XDG_CACHE_HOME/pixi`
or `$HOME/.cache/pixi`)
2. Platform-specific default cache folder:
- Linux: `$XDG_CACHE_HOME/rattler` or `$HOME`/.cache/rattler
- macOS: `$HOME`/Library/Caches/rattler
- Windows: `%APPDATA%\rattler`

When your system is filling up you can easily remove this folder.
It will re-download everything it needs the next time you install a project.
Expand Down
14 changes: 9 additions & 5 deletions docs/advanced/global_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ Pixi supports some global configuration options, as well as project-scoped
configuration (that does not belong into the project file). The configuration is
loaded in the following order:

1. Global configuration folder (e.g. `~/.config/pixi/config.toml` on Linux,
dependent on XDG_CONFIG_HOME)
2. Global .pixi folder: `~/.pixi/config.toml` (or `$PIXI_HOME/config.toml` if
1. XDG compliant configuration folder (`$XDG_CONFIG_HOME/pixi/config.toml` or
`$HOME/.config/pixi/config.toml`)
2. Global configuration folder, depending on the OS:
- Linux: `$HOME/.config/pixi/config.toml`
- macOS: `$HOME/Library/Application Support/pixi/config.toml`
- Windows: `%APPDATA%\pixi\config.toml`
3. Global .pixi folder: `~/.pixi/config.toml` (or `$PIXI_HOME/config.toml` if
the `PIXI_HOME` environment variable is set)
3. Project-local .pixi folder: `$PIXI_PROJECT/.pixi/config.toml`
4. Command line arguments (`--tls-no-verify`, `--change-ps1=false` etc.)
4. Project-local .pixi folder: `$PIXI_PROJECT/.pixi/config.toml`
5. Command line arguments (`--tls-no-verify`, `--change-ps1=false` etc.)

!!! note
To find the locations where `pixi` looks for configuration files, run
Expand Down
6 changes: 3 additions & 3 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,9 @@ Global is the main entry point for the part of pixi that executes on the
global(system) level.

!!! tip
Binaries and environments installed globally are stored in `~/.pixi`
by default, this can be changed by setting the `PIXI_HOME` environment
variable.
Binaries and environments installed globally are stored in `~/.pixi`
by default, this can be changed by setting the `PIXI_HOME` environment
variable.

### `global install`

Expand Down
24 changes: 22 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,26 @@ pub fn home_path() -> Option<PathBuf> {

/// Returns the default cache directory.
/// Most important is the `PIXI_CACHE_DIR` environment variable.
/// If that is not set, the `RATTLER_CACHE_DIR` environment variable is used.
/// If that is not set, the default cache directory of [`rattler::default_cache_dir`] is used.
/// - If that is not set, the `RATTLER_CACHE_DIR` environment variable is used.
/// - If that is not set, `XDG_CACHE_HOME/pixi` is used when the directory exists.
/// - If that is not set, the default cache directory of [`rattler::default_cache_dir`] is used.
pub fn get_cache_dir() -> miette::Result<PathBuf> {
std::env::var("PIXI_CACHE_DIR")
.map(PathBuf::from)
.or_else(|_| std::env::var("RATTLER_CACHE_DIR").map(PathBuf::from))
.or_else(|_| {
let xdg_cache_pixi_dir = std::env::var_os("XDG_CACHE_HOME")
.map_or_else(
|| dirs::home_dir().map(|d| d.join(".cache")),
|p| Some(PathBuf::from(p)),
)
.map(|d| d.join("pixi"));

// Only use the xdg cache pixi directory when it exists
xdg_cache_pixi_dir
.and_then(|d| d.exists().then_some(d))
.ok_or_else(|| miette::miette!("could not determine xdg cache directory"))
})
.or_else(|_| {
rattler::default_cache_dir()
.map_err(|_| miette::miette!("could not determine default cache directory"))
Expand Down Expand Up @@ -160,7 +174,13 @@ impl Config {

/// Load the global config file from the home directory (~/.pixi/config.toml)
pub fn load_global() -> Config {
let xdg_config_home = std::env::var_os("XDG_CONFIG_HOME").map_or_else(
|| dirs::home_dir().map(|d| d.join(".config")),
|p| Some(PathBuf::from(p)),
);

let global_locations = vec![
xdg_config_home.map(|d| d.join("pixi").join(consts::CONFIG_FILE)),
dirs::config_dir().map(|d| d.join("pixi").join(consts::CONFIG_FILE)),
home_path().map(|d| d.join(consts::CONFIG_FILE)),
];
Expand Down

0 comments on commit 5d26f2f

Please sign in to comment.