I now use a combination of xdg + known-folders manually:
[target.'cfg(windows)'.dependencies]
known-folders = "1.2.0"
[target.'cfg(not(windows))'.dependencies]
xdg = "2.5.2"
to get the config directory: use anyhow::{Context, Result};
#[cfg(windows)]
fn get_config_base_dir() -> Result<PathBuf> {
use known_folders::{KnownFolder, get_known_folder_path};
get_known_folder_path(KnownFolder::RoamingAppData).context("unable to get config dir")
}
#[cfg(not(windows))]
fn get_config_base_dir() -> Result<PathBuf> {
let base_dirs = xdg::BaseDirectories::new().context("unable to get config dir")?;
Ok(base_dirs.get_config_home())
}
That's why the long-term future of app development is containers. It is not possible, on a human level, to convince people to lift even the lightest of fingers for the common good.
Consider https://specifications.freedesktop.org/basedir-spec/latest/
The XDG specification has been around for 22 years. It has real benefits for users. It's trivial to implement. Yet even in the year of our lord two thousand and twenty five I still see TypeScript developers complain that it's "too hard" to comply with "this BS" and just stick files in $HOME.
I've long given up on solving technical coordination problems by appealing to the universal goodness of humanity. The only thing that works is to sandbox applications at tightly as possible and direct all their access to the external world through narrow interfaces that do their best to limit shenanigans.
I wholeheartedly agree about the containers part though, just have everything within a folder in a container somewhere so I don't have to keep googling where X stores Y and still failing half the time.
but CLI tools are applications
My configurations are preferences, stored in ~/Library/Preferences.
Even better if you store those as property lists and hook into CFPreferences so I can manage them with configuration profiles and use the defaults command to query and modify my preferences without having to open the app or read some 4000 line long JSONC file with 20 lines of settings and ~4000 lines of bad documentation.
And if you're writing a cross-platform application, it's not necessarily correct to have a completely different file format on different OSes. Not all Windows applications should store all their preferences in the registry, either.
I'd honestly be fine if none of them did.
> This directory contains app-specific preference files. You should not create files in this directory yourself.
Also
> defaults command to query and modify my preferences without having to open the app or read some 4000 line long JSONC file with 20 lines of settings and ~4000 lines of bad documentation.
I'd prefer the convenience of an editor to read the real 5 lines and 5 lines of comments of the settings I've changed (instead of the made up 4000000) and having a diffable config rather than some binary plist nonsense and relying on a clunky defaults cli. I'd even be prepared to shed the complexity of profiles for this basic conveniences
Moreover, most versions of MacOS are certified Unix: https://www.opengroup.org/openbrand/register/
POSIX exists, because out of UNIX System V, every clone went down their own merry way.
the very first paragraphs on specifications.freedesktop.org says this:
> Freedesktop.org is a project to work on interoperability and shared base technology for free-software desktop environments for the X Window System (X11) and Wayland on Linux and other Unix-like operating systems. > We are not a formal standards body. The standards published on these pages are active or tentative (if marked as such) specifications which desktop environments may implement to improve mutual compatibility, share code and pool resources.
Deferring to XDG_CONFIG_HOME on MacOS if it exists makes a lot of sense as it conveys a clear intent from the user and the convention has grown popular. I’m not sure that the default ~/.config from the XDG specification is automatically better than ~/Library/Application Support by appeal to freedesktop.org’s authority.
And please don’t move configuration files around between releases without really being intentional about it.
The issue is that often everyone assumes that XDG_CONFIG_HOME is ~/.config
The idea of having a variable is that it could be anywhere,
This is a recurring pattern that systemd documentation is sold as "neutral" standard. The UAPI group is another example of this.
My personal practice when writing command line utiities for macOS is to use the macOS API to write settings (previously known as “preferences”) into ~/Library/Preferences, so they can interoperate with any GUI versions of them I might like to write, and for the utilities themselves to have command line options to control those settings. As a sibling comment suggests, you do need to avoid name space collisions. You can use a reverse DNS name, or some companies are big enough just to use their company name. They do appear as .plist files but are actually maintained by a daemon – which nicely avoids race problems with multiple processes updating settings.
If I were writing a portable utility which had a dotfile with a documented format the user was expected to edit, I would make it a dotfile under the home directory.
~/Library/Application Support is really more for per-user static data, like presets and templates, things that are known in a GUI by a string in some dialogue but not necessarily thought of by the user as a file.
I am pretty happy with this setup as it means that all my dotfiles are in a single root folder that I manage through a git repo.
JoshTriplett•2h ago
re•1h ago
> Put app-created support files in the Library/Application support/ directory. In general, this directory includes files that the app uses to run but that should remain hidden from the user. [emphasis added]
JoshTriplett•1h ago
Many good command-line tools manage their config files automatically, in addition to allowing the user to hand-edit them. I don't think that materially changes this, though: people may still expect to find them in `~/.config`.
joshka•1h ago
I wrote a top level thread that this should be fixed by adding an explicit "I want XDG even though I'm on macOS" setting somewhere. Probably another environment variable.
re•1h ago
> Probably another environment variable
Having any of the existing XDG_* environment variables set is an incredibly-clear indication that the user wants the XDG spec followed.
pjmlp•36m ago
Where is XDG on Open Group standards?
mbreese•1h ago
Much like the original author, my opinion is that you should do the least surprising to the user and if that’s not what the spec says, so be it.
JdeBP•1h ago
re•46m ago
tux3•1h ago
Those libraries aren't normally in the business of creating symlinks, and if previous discussions are any indications, convincing them to add XDG support at all - let alone by default - seems on the same level as pleading Vim/Emacs users to just try Emacs/Vim
eviks•35m ago