Add load-config-file --path to load a different config (#3395)
Browse files at this commit6 changed files +35 -10
- docs/wiki/Integrating-niri.md +3 -0
- niri-config/src/binds.rs +2 -2
- niri-ipc/src/lib.rs +7 -1
- src/input/mod.rs +2 -2
- src/ipc/server.rs +9 -1
- src/utils/watcher.rs +12 -4
docs/wiki/Integrating-niri.mdmodified+3 -0
| @@ -11,6 +11,9 @@ When this file is present, niri *will not* automatically create a config at `~/. | ||
| 11 | 11 | |
| 12 | 12 | Keep in mind that we update the default config in new releases, so if you have a custom `/etc/niri/config.kdl`, you likely want to inspect and apply the relevant changes too. |
| 13 | 13 | |
| 14 | +The default configuration locations can be overridden with the `NIRI_CONFIG` environment variable. | |
| 15 | +You can also change the configuration path at runtime via the niri IPC or using the command `niri msg action load-config-file --path <path-to-config.kdl>`. | |
| 16 | + | |
| 14 | 17 | You can split the niri config file into multiple files using [`include`](./Configuration:-Include.md). |
| 15 | 18 | |
| 16 | 19 | ### Xwayland |
niri-config/src/binds.rsmodified+2 -2
| @@ -368,7 +368,7 @@ pub enum Action { | ||
| 368 | 368 | #[knuffel(skip)] |
| 369 | 369 | UnsetWindowUrgent(u64), |
| 370 | 370 | #[knuffel(skip)] |
| 371 | - LoadConfigFile, | |
| 371 | + LoadConfigFile(#[knuffel(argument)] Option<String>), | |
| 372 | 372 | #[knuffel(skip)] |
| 373 | 373 | MruAdvance { |
| 374 | 374 | direction: MruDirection, |
| @@ -699,7 +699,7 @@ impl From<niri_ipc::Action> for Action { | ||
| 699 | 699 | niri_ipc::Action::ToggleWindowUrgent { id } => Self::ToggleWindowUrgent(id), |
| 700 | 700 | niri_ipc::Action::SetWindowUrgent { id } => Self::SetWindowUrgent(id), |
| 701 | 701 | niri_ipc::Action::UnsetWindowUrgent { id } => Self::UnsetWindowUrgent(id), |
| 702 | - niri_ipc::Action::LoadConfigFile {} => Self::LoadConfigFile, | |
| 702 | + niri_ipc::Action::LoadConfigFile { path } => Self::LoadConfigFile(path), | |
| 703 | 703 | } |
| 704 | 704 | } |
| 705 | 705 | } |
niri-ipc/src/lib.rsmodified+7 -1
| @@ -936,7 +936,13 @@ pub enum Action { | ||
| 936 | 936 | /// |
| 937 | 937 | /// Can be useful for scripts changing the config file, to avoid waiting the small duration for |
| 938 | 938 | /// niri's config file watcher to notice the changes. |
| 939 | - LoadConfigFile {}, | |
| 939 | + LoadConfigFile { | |
| 940 | + /// Path of a new config file to load. | |
| 941 | + /// | |
| 942 | + /// If unset, reloads the current config file. | |
| 943 | + #[cfg_attr(feature = "clap", arg(long))] | |
| 944 | + path: Option<String>, | |
| 945 | + }, | |
| 940 | 946 | } |
| 941 | 947 | |
| 942 | 948 | /// Change in window or column size. |
src/input/mod.rsmodified+2 -2
| @@ -2318,9 +2318,9 @@ impl State { | ||
| 2318 | 2318 | } |
| 2319 | 2319 | self.niri.queue_redraw_all(); |
| 2320 | 2320 | } |
| 2321 | - Action::LoadConfigFile => { | |
| 2321 | + Action::LoadConfigFile(path) => { | |
| 2322 | 2322 | if let Some(watcher) = &self.niri.config_file_watcher { |
| 2323 | - watcher.load_config(); | |
| 2323 | + watcher.load_config(path); | |
| 2324 | 2324 | } |
| 2325 | 2325 | } |
| 2326 | 2326 | Action::MruConfirm => { |
src/ipc/server.rsmodified+9 -1
| @@ -463,7 +463,8 @@ async fn process(ctx: &ClientCtx, request: Request) -> Reply { | ||
| 463 | 463 | fn validate_action(action: &Action) -> Result<(), String> { |
| 464 | 464 | if let Action::Screenshot { path, .. } |
| 465 | 465 | | Action::ScreenshotScreen { path, .. } |
| 466 | - | Action::ScreenshotWindow { path, .. } = action | |
| 466 | + | Action::ScreenshotWindow { path, .. } | |
| 467 | + | Action::LoadConfigFile { path } = action | |
| 467 | 468 | { |
| 468 | 469 | if let Some(path) = path { |
| 469 | 470 | // Relative paths are resolved against the niri compositor's working directory, which |
| @@ -474,6 +475,13 @@ fn validate_action(action: &Action) -> Result<(), String> { | ||
| 474 | 475 | } |
| 475 | 476 | } |
| 476 | 477 | |
| 478 | + if let Action::LoadConfigFile { path: Some(path) } = action { | |
| 479 | + let p = Path::new(path); | |
| 480 | + if !p.is_file() { | |
| 481 | + return Err(format!("path does not point to a file: {path}")); | |
| 482 | + } | |
| 483 | + } | |
| 484 | + | |
| 477 | 485 | Ok(()) |
| 478 | 486 | } |
| 479 | 487 |
src/utils/watcher.rsmodified+12 -4
| @@ -14,7 +14,7 @@ use crate::niri::State; | ||
| 14 | 14 | const POLLING_INTERVAL: Duration = Duration::from_millis(500); |
| 15 | 15 | |
| 16 | 16 | pub struct Watcher { |
| 17 | - load_config: mpsc::Sender<()>, | |
| 17 | + load_config: mpsc::Sender<Option<String>>, | |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | struct WatcherInner { |
| @@ -67,7 +67,15 @@ impl Watcher { | ||
| 67 | 67 | |
| 68 | 68 | loop { |
| 69 | 69 | let mut should_load = match load_config_rx.recv_timeout(POLLING_INTERVAL) { |
| 70 | - Ok(()) => true, | |
| 70 | + Ok(path) => { | |
| 71 | + if let Some(path) = path { | |
| 72 | + inner = WatcherInner::new( | |
| 73 | + ConfigPath::Explicit(PathBuf::from(path)), | |
| 74 | + Vec::new(), | |
| 75 | + ); | |
| 76 | + } | |
| 77 | + true | |
| 78 | + } | |
| 71 | 79 | Err(mpsc::RecvTimeoutError::Disconnected) => break, |
| 72 | 80 | Err(mpsc::RecvTimeoutError::Timeout) => false, |
| 73 | 81 | }; |
| @@ -105,8 +113,8 @@ impl Watcher { | ||
| 105 | 113 | Self { load_config } |
| 106 | 114 | } |
| 107 | 115 | |
| 108 | - pub fn load_config(&self) { | |
| 109 | - let _ = self.load_config.send(()); | |
| 116 | + pub fn load_config(&self, path: Option<String>) { | |
| 117 | + let _ = self.load_config.send(path); | |
| 110 | 118 | } |
| 111 | 119 | } |
| 112 | 120 |