Read config from /etc/niri/config.kdl too
Browse files at this commit4b7c16b04a7c80f5f9b6fcbc4a1d8c9448dffbdbParent: aafd5ab3 changed files +73 -56
- src/main.rs +69 -53
- src/ui/config_error_notification.rs +2 -1
- wiki/Configuration:-Overview.md +2 -2
src/main.rsmodified+69 -53
| @@ -90,10 +90,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |||
| 90 | Sub::Validate { config } => { | 90 | Sub::Validate { config } => { |
| 91 | tracy_client::Client::start(); | 91 | tracy_client::Client::start(); |
| 92 | 92 | ||
| 93 | - let path = config | 93 | + let (path, _, _) = config_path(config); |
| 94 | - .or_else(env_config_path) | ||
| 95 | - .or_else(default_config_path) | ||
| 96 | - .expect("error getting config path"); | ||
| 97 | Config::load(&path)?; | 94 | Config::load(&path)?; |
| 98 | info!("config is valid"); | 95 | info!("config is valid"); |
| 99 | return Ok(()); | 96 | return Ok(()); |
| @@ -114,56 +111,50 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |||
| 114 | 111 | ||
| 115 | // Load the config. | 112 | // Load the config. |
| 116 | let mut config_created = false; | 113 | let mut config_created = false; |
| 117 | - let path = cli.config.or_else(env_config_path); | 114 | + let (path, watch_path, create_default) = config_path(cli.config); |
| 118 | env::remove_var("NIRI_CONFIG"); | 115 | env::remove_var("NIRI_CONFIG"); |
| 119 | - let path = path.or_else(|| { | 116 | + if create_default { |
| 120 | - let default_path = default_config_path()?; | 117 | + let default_parent = path.parent().unwrap(); |
| 121 | - let default_parent = default_path.parent().unwrap(); | 118 | + |
| 122 | - | 119 | + match fs::create_dir_all(default_parent) { |
| 123 | - if let Err(err) = fs::create_dir_all(default_parent) { | 120 | + Ok(()) => { |
| 124 | - warn!( | 121 | + // Create the config and fill it with the default config if it doesn't exist. |
| 125 | - "error creating config directories {:?}: {err:?}", | 122 | + let new_file = File::options() |
| 126 | - default_parent | 123 | + .read(true) |
| 127 | - ); | 124 | + .write(true) |
| 128 | - return Some(default_path); | 125 | + .create_new(true) |
| 129 | - } | 126 | + .open(&path); |
| 130 | - | 127 | + match new_file { |
| 131 | - // Create the config and fill it with the default config if it doesn't exist. | 128 | + Ok(mut new_file) => { |
| 132 | - let new_file = File::options() | 129 | + let default = include_bytes!("../resources/default-config.kdl"); |
| 133 | - .read(true) | 130 | + match new_file.write_all(default) { |
| 134 | - .write(true) | 131 | + Ok(()) => { |
| 135 | - .create_new(true) | 132 | + config_created = true; |
| 136 | - .open(&default_path); | 133 | + info!("wrote default config to {:?}", &path); |
| 137 | - match new_file { | 134 | + } |
| 138 | - Ok(mut new_file) => { | 135 | + Err(err) => { |
| 139 | - let default = include_bytes!("../resources/default-config.kdl"); | 136 | + warn!("error writing config file at {:?}: {err:?}", &path) |
| 140 | - match new_file.write_all(default) { | 137 | + } |
| 141 | - Ok(()) => { | 138 | + } |
| 142 | - config_created = true; | ||
| 143 | - info!("wrote default config to {:?}", &default_path); | ||
| 144 | - } | ||
| 145 | - Err(err) => { | ||
| 146 | - warn!("error writing config file at {:?}: {err:?}", &default_path) | ||
| 147 | } | 139 | } |
| 140 | + Err(err) if err.kind() == io::ErrorKind::AlreadyExists => {} | ||
| 141 | + Err(err) => warn!("error creating config file at {:?}: {err:?}", &path), | ||
| 148 | } | 142 | } |
| 149 | } | 143 | } |
| 150 | - Err(err) if err.kind() == io::ErrorKind::AlreadyExists => {} | 144 | + Err(err) => { |
| 151 | - Err(err) => warn!("error creating config file at {:?}: {err:?}", &default_path), | 145 | + warn!( |
| 146 | + "error creating config directories {:?}: {err:?}", | ||
| 147 | + default_parent | ||
| 148 | + ); | ||
| 149 | + } | ||
| 152 | } | 150 | } |
| 153 | - | 151 | + } |
| 154 | - Some(default_path) | ||
| 155 | - }); | ||
| 156 | 152 | ||
| 157 | let mut config_errored = false; | 153 | let mut config_errored = false; |
| 158 | - let mut config = path | 154 | + let mut config = Config::load(&path) |
| 159 | - .as_deref() | 155 | + .map_err(|err| { |
| 160 | - .and_then(|path| match Config::load(path) { | 156 | + warn!("{err:?}"); |
| 161 | - Ok(config) => Some(config), | 157 | + config_errored = true; |
| 162 | - Err(err) => { | ||
| 163 | - warn!("{err:?}"); | ||
| 164 | - config_errored = true; | ||
| 165 | - None | ||
| 166 | - } | ||
| 167 | }) | 158 | }) |
| 168 | .unwrap_or_default(); | 159 | .unwrap_or_default(); |
| 169 | 160 | ||
| @@ -231,19 +222,17 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |||
| 231 | } | 222 | } |
| 232 | 223 | ||
| 233 | // Set up config file watcher. | 224 | // Set up config file watcher. |
| 234 | - let _watcher = if let Some(path) = path.clone() { | 225 | + let _watcher = { |
| 235 | let (tx, rx) = calloop::channel::sync_channel(1); | 226 | let (tx, rx) = calloop::channel::sync_channel(1); |
| 236 | - let watcher = Watcher::new(path.clone(), tx); | 227 | + let watcher = Watcher::new(watch_path.clone(), tx); |
| 237 | event_loop | 228 | event_loop |
| 238 | .handle() | 229 | .handle() |
| 239 | .insert_source(rx, move |event, _, state| match event { | 230 | .insert_source(rx, move |event, _, state| match event { |
| 240 | - calloop::channel::Event::Msg(()) => state.reload_config(path.clone()), | 231 | + calloop::channel::Event::Msg(()) => state.reload_config(watch_path.clone()), |
| 241 | calloop::channel::Event::Closed => (), | 232 | calloop::channel::Event::Closed => (), |
| 242 | }) | 233 | }) |
| 243 | .unwrap(); | 234 | .unwrap(); |
| 244 | - Some(watcher) | 235 | + watcher |
| 245 | - } else { | ||
| 246 | - None | ||
| 247 | }; | 236 | }; |
| 248 | 237 | ||
| 249 | // Spawn commands from cli and auto-start. | 238 | // Spawn commands from cli and auto-start. |
| @@ -335,6 +324,33 @@ fn default_config_path() -> Option<PathBuf> { | |||
| 335 | Some(path) | 324 | Some(path) |
| 336 | } | 325 | } |
| 337 | 326 | ||
| 327 | +fn system_config_path() -> PathBuf { | ||
| 328 | + PathBuf::from("/etc/niri/config.kdl") | ||
| 329 | +} | ||
| 330 | + | ||
| 331 | +/// Resolves and returns the config path to load, the config path to watch, and whether to create | ||
| 332 | +/// the default config at the path to load. | ||
| 333 | +fn config_path(cli_path: Option<PathBuf>) -> (PathBuf, PathBuf, bool) { | ||
| 334 | + if let Some(explicit) = cli_path.or_else(env_config_path) { | ||
| 335 | + return (explicit.clone(), explicit, false); | ||
| 336 | + } | ||
| 337 | + | ||
| 338 | + let system_path = system_config_path(); | ||
| 339 | + if let Some(path) = default_config_path() { | ||
| 340 | + if path.exists() { | ||
| 341 | + return (path.clone(), path, true); | ||
| 342 | + } | ||
| 343 | + | ||
| 344 | + if system_path.exists() { | ||
| 345 | + (system_path, path, false) | ||
| 346 | + } else { | ||
| 347 | + (path.clone(), path, true) | ||
| 348 | + } | ||
| 349 | + } else { | ||
| 350 | + (system_path.clone(), system_path, false) | ||
| 351 | + } | ||
| 352 | +} | ||
| 353 | + | ||
| 338 | fn notify_fd() -> anyhow::Result<()> { | 354 | fn notify_fd() -> anyhow::Result<()> { |
| 339 | let fd = match env::var("NOTIFY_FD") { | 355 | let fd = match env::var("NOTIFY_FD") { |
| 340 | Ok(notify_fd) => notify_fd.parse()?, | 356 | Ok(notify_fd) => notify_fd.parse()?, |
src/ui/config_error_notification.rsmodified+2 -1
| @@ -60,7 +60,8 @@ impl ConfigErrorNotification { | |||
| 60 | Animation::new(from, to, 0., c.animations.config_notification_open_close.0) | 60 | Animation::new(from, to, 0., c.animations.config_notification_open_close.0) |
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | - pub fn show_created(&mut self, created_path: Option<PathBuf>) { | 63 | + pub fn show_created(&mut self, created_path: PathBuf) { |
| 64 | + let created_path = Some(created_path); | ||
| 64 | if self.created_path != created_path { | 65 | if self.created_path != created_path { |
| 65 | self.created_path = created_path; | 66 | self.created_path = created_path; |
| 66 | self.buffers.borrow_mut().clear(); | 67 | self.buffers.borrow_mut().clear(); |
wiki/Configuration:-Overview.mdmodified+2 -2
| @@ -13,8 +13,8 @@ You can find documentation for various sections of the config on these wiki page | |||
| 13 | 13 | ||
| 14 | ### Loading | 14 | ### Loading |
| 15 | 15 | ||
| 16 | -Niri will load configuration from `$XDG_CONFIG_HOME/niri/config.kdl` or `~/.config/niri/config.kdl`. | 16 | +Niri will load configuration from `$XDG_CONFIG_HOME/niri/config.kdl` or `~/.config/niri/config.kdl`, falling back to `/etc/niri/config.kdl`. |
| 17 | -If that file is missing, niri will create it with the contents of [the default configuration file](https://github.com/YaLTeR/niri/blob/main/resources/default-config.kdl). | 17 | +If both of these files are missing, niri will create `$XDG_CONFIG_HOME/niri/config.kdl` with the contents of [the default configuration file](https://github.com/YaLTeR/niri/blob/main/resources/default-config.kdl), which are embedded into the niri binary at build time. |
| 18 | Please use the default configuration file as the starting point for your custom configuration. | 18 | Please use the default configuration file as the starting point for your custom configuration. |
| 19 | 19 | ||
| 20 | The configuration is live-reloaded. | 20 | The configuration is live-reloaded. |