From fb34f840fba75ec1b77e94e76642bc0ee01584b9 Mon Sep 17 00:00:00 2001 From: Matt Rochford Date: Wed, 8 Jan 2025 16:54:49 -0800 Subject: [PATCH] core: apply config files under XDG_CONFIG_DIRS --- src/build/mdgen/ghostty_1_footer.md | 10 ++++++++- src/build/mdgen/ghostty_5_footer.md | 10 ++++++++- src/config/Config.zig | 32 ++++++++++++++++++++++------- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/build/mdgen/ghostty_1_footer.md b/src/build/mdgen/ghostty_1_footer.md index 86a8a8098..0ec61563d 100644 --- a/src/build/mdgen/ghostty_1_footer.md +++ b/src/build/mdgen/ghostty_1_footer.md @@ -2,7 +2,11 @@ _\$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_ @@ -23,6 +27,10 @@ for configuration files. : Default location for configuration files. +**XDG_CONFIG_DIRS** + +: Colon separated list of paths to load configuration files. + **LOCALAPPDATA** : **WINDOWS ONLY:** alternate location to search for configuration files. diff --git a/src/build/mdgen/ghostty_5_footer.md b/src/build/mdgen/ghostty_5_footer.md index 0c893dd07..339f33d69 100644 --- a/src/build/mdgen/ghostty_5_footer.md +++ b/src/build/mdgen/ghostty_5_footer.md @@ -2,7 +2,11 @@ _\$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_ @@ -15,6 +19,10 @@ for configuration files. : Default location for configuration files. +**XDG_CONFIG_DIRS** + +: Colon separated list of paths to load configuration files. + **LOCALAPPDATA** : **WINDOWS ONLY:** alternate location to search for configuration files. diff --git a/src/config/Config.zig b/src/config/Config.zig index 144796554..5c192d3ec 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -1439,8 +1439,10 @@ keybind: Keybinds = .{}, @"config-file": RepeatablePath = .{}, /// When this is true, the default configuration file paths will be loaded. -/// The default configuration file paths are currently only the XDG -/// config path ($XDG_CONFIG_HOME/ghostty/config). +/// The default configuration files are at ./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. /// 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 -/// configuration file is at `$XDG_CONFIG_HOME/ghostty/config`. +/// Load configurations from the default configuration files. +/// 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` /// is also loaded. pub fn loadDefaultFiles(self: *Config, alloc: Allocator) !void { - // Load XDG first - const xdg_path = try internal_os.xdg.config(alloc, .{ .subdir = "ghostty/config" }); + const config_subdir = "ghostty"; + 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); const xdg_action = self.loadOptionalFile(alloc, xdg_path); // On macOS load the app support directory as well 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); const app_support_action = self.loadOptionalFile(alloc, app_support_path);