core: apply config files under XDG_CONFIG_DIRS

This commit is contained in:
Matt Rochford
2025-01-08 16:54:49 -08:00
parent ff0c805e3e
commit fb34f840fb
3 changed files with 43 additions and 9 deletions

View File

@ -2,7 +2,11 @@
_\$XDG_CONFIG_HOME/ghostty/config_ _\$XDG_CONFIG_HOME/ghostty/config_
: Location of the default configuration file. : Location of the default user configuration file.
_\$XDG_CONFIG_DIRS/ghostty/config_
: Location of the default system configuration files.
_\$LOCALAPPDATA/ghostty/config_ _\$LOCALAPPDATA/ghostty/config_
@ -23,6 +27,10 @@ for configuration files.
: Default location for configuration files. : Default location for configuration files.
**XDG_CONFIG_DIRS**
: Colon separated list of paths to load configuration files.
**LOCALAPPDATA** **LOCALAPPDATA**
: **WINDOWS ONLY:** alternate location to search for configuration files. : **WINDOWS ONLY:** alternate location to search for configuration files.

View File

@ -2,7 +2,11 @@
_\$XDG_CONFIG_HOME/ghostty/config_ _\$XDG_CONFIG_HOME/ghostty/config_
: Location of the default configuration file. : Location of the default user configuration file.
_\$XDG_CONFIG_DIRS/ghostty/config_
: Location of the default system configuration files.
_\$LOCALAPPDATA/ghostty/config_ _\$LOCALAPPDATA/ghostty/config_
@ -15,6 +19,10 @@ for configuration files.
: Default location for configuration files. : Default location for configuration files.
**XDG_CONFIG_DIRS**
: Colon separated list of paths to load configuration files.
**LOCALAPPDATA** **LOCALAPPDATA**
: **WINDOWS ONLY:** alternate location to search for configuration files. : **WINDOWS ONLY:** alternate location to search for configuration files.

View File

@ -1439,8 +1439,10 @@ keybind: Keybinds = .{},
@"config-file": RepeatablePath = .{}, @"config-file": RepeatablePath = .{},
/// When this is true, the default configuration file paths will be loaded. /// When this is true, the default configuration file paths will be loaded.
/// The default configuration file paths are currently only the XDG /// The default configuration files are at ./ghostty/config,
/// config path ($XDG_CONFIG_HOME/ghostty/config). /// in each of the colon seperated directories in $XDG_CONFIG_DIRS, and,
/// the xdg user config directory $XDG_CONFIG_HOME
/// (/etc/xdg and ~/.config if these environment variables are not set).
/// ///
/// If this is false, the default configuration paths will not be loaded. /// If this is false, the default configuration paths will not be loaded.
/// This is targeted directly at using Ghostty from the CLI in a way /// This is targeted directly at using Ghostty from the CLI in a way
@ -2921,20 +2923,36 @@ fn writeConfigTemplate(path: []const u8) !void {
); );
} }
/// Load configurations from the default configuration files. The default /// Load configurations from the default configuration files.
/// configuration file is at `$XDG_CONFIG_HOME/ghostty/config`. /// The default system configuration files are at `$XDG_CONFIG_DIRS/ghostty/config`.
/// The default user configuration file is at `$XDG_CONFIG_HOME/ghostty/config`.
/// ///
/// On macOS, `$HOME/Library/Application Support/$CFBundleIdentifier/config` /// On macOS, `$HOME/Library/Application Support/$CFBundleIdentifier/config`
/// is also loaded. /// is also loaded.
pub fn loadDefaultFiles(self: *Config, alloc: Allocator) !void { pub fn loadDefaultFiles(self: *Config, alloc: Allocator) !void {
// Load XDG first const config_subdir = "ghostty";
const xdg_path = try internal_os.xdg.config(alloc, .{ .subdir = "ghostty/config" }); const config_file = "config";
// Load XDG system config first
var it = internal_os.xdg.DirIterator.init(.config);
while (it.next()) |xdg_dir| {
const config_path = try std.fs.path.join(alloc, &[_][]const u8{
xdg_dir,
config_subdir,
config_file,
});
defer alloc.free(config_path);
_ = self.loadOptionalFile(alloc, config_path);
}
// Load XDG user config next
const xdg_config_dir = try internal_os.xdg.config(alloc, .{ .subdir = config_subdir });
defer alloc.free(xdg_config_dir);
const xdg_path = try std.fs.path.join(alloc, &[_][]const u8{ xdg_config_dir, config_file });
defer alloc.free(xdg_path); defer alloc.free(xdg_path);
const xdg_action = self.loadOptionalFile(alloc, xdg_path); const xdg_action = self.loadOptionalFile(alloc, xdg_path);
// On macOS load the app support directory as well // On macOS load the app support directory as well
if (comptime builtin.os.tag == .macos) { if (comptime builtin.os.tag == .macos) {
const app_support_path = try internal_os.macos.appSupportDir(alloc, "config"); const app_support_path = try internal_os.macos.appSupportDir(alloc, config_file);
defer alloc.free(app_support_path); defer alloc.free(app_support_path);
const app_support_action = self.loadOptionalFile(alloc, app_support_path); const app_support_action = self.loadOptionalFile(alloc, app_support_path);