Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config: apply xdg system config files #4845

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

mjrochford
Copy link
Contributor

@mjrochford mjrochford commented Jan 9, 2025

fixes: #4506

Applies the config files inside the XDG_CONFIG_DIRS with the first file taking precedence.

@mjrochford mjrochford changed the title core: Config apply xdg system config files Config: apply xdg system config files Jan 9, 2025
Copy link
Contributor

@mitchellh mitchellh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for building on @jcollie's work here and keeping the commit credits. I made some notes.

// We must reverse the order of the iterator because the first xdg config
// dir must takes importance per the spec https://specifications.freedesktop.org/basedir-spec/latest/#variables
var xdg_config_dirs = std.ArrayList([]const u8).init(alloc);
defer xdg_config_dirs.deinit();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of all this machinery, the DirIterator could do a reverse iteration using std.mem.lastIndexOf to implement your own iterator. I would recommend this route.

@mitchellh
Copy link
Contributor

Also please note my feedback on @jcollie's PR, it applies here as well.

@mjrochford
Copy link
Contributor Author

mjrochford commented Jan 11, 2025

Re. API design

Current API

os.xdg.config(alloc, .{ .subdir = "ghostty/config" });
os.xdg.cache(alloc, .{ .subdir = "ghostty/config" });

This API

To get all the xdg dirs in order from least to most important
user dirs come last after all others have been consumed

var it = os.xdg.Dir.config.iter(alloc, .{ .subdir = "ghostty/config" });

Single directory access is possible ala

os.xdg.UserDir.config.path(alloc, .{ .subdir = "ghostty" });
os.xdg.UserDir.data.path(alloc, .{ .subdir = "ghostty" });
os.xdg.UserDir.cache.path(alloc, .{ .subdir = "ghostty" });
os.xdg.UserDir.state.path(alloc, .{ .subdir = "ghostty" });

Ditto for system directories

os.xdg.SystemDir.config.iter(alloc, .{ .subdir = "ghostty" });

I've just gone ahead and used splitBackwardsScalar instead of splitScalar, I think wrapping an std iterator is better here.
Least important first makes sense to me for the Config use case, not sure about other cases.

Better API

os.programdirs.config.iter(alloc)

I think splitting XDG/Windows/MacOS known directories logic into their own modules and unifying these into a programdirs enum could make sense something akin to ProjectDirs from this stack overflow post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Load configuration from XDG_CONFIG_DIRS
3 participants