forge
Explore
Log in

Don't you dare go Hollow

Add load-config-file --path to load a different config (#3395)

Browse files at this commit

549148d27779d024255a84535b42b947f1c2a113Unverifiedジムワルド and Ivan MolodetskikhParent: 189917c
* ipc: allow load-config to relocate the path of the config

* doc: add info about alternative configuration paths and relocating

Co-authored-by: Ivan Molodetskikh <yalterz@gmail.com>

* Update docs/wiki/Integrating-niri.md

* Update niri-ipc/src/lib.rs

* Update src/ipc/server.rs

---------
docs/wiki/Integrating-niri.mdmodified+3 -0
@@ -11,6 +11,9 @@ When this file is present, niri *will not* automatically create a config at `~/.
1111
1212 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.
1313
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+
1417 You can split the niri config file into multiple files using [`include`](./Configuration:-Include.md).
1518
1619 ### Xwayland
niri-config/src/binds.rsmodified+2 -2
@@ -368,7 +368,7 @@ pub enum Action {
368368 #[knuffel(skip)]
369369 UnsetWindowUrgent(u64),
370370 #[knuffel(skip)]
371- LoadConfigFile,
371+ LoadConfigFile(#[knuffel(argument)] Option<String>),
372372 #[knuffel(skip)]
373373 MruAdvance {
374374 direction: MruDirection,
@@ -699,7 +699,7 @@ impl From<niri_ipc::Action> for Action {
699699 niri_ipc::Action::ToggleWindowUrgent { id } => Self::ToggleWindowUrgent(id),
700700 niri_ipc::Action::SetWindowUrgent { id } => Self::SetWindowUrgent(id),
701701 niri_ipc::Action::UnsetWindowUrgent { id } => Self::UnsetWindowUrgent(id),
702- niri_ipc::Action::LoadConfigFile {} => Self::LoadConfigFile,
702+ niri_ipc::Action::LoadConfigFile { path } => Self::LoadConfigFile(path),
703703 }
704704 }
705705 }
niri-ipc/src/lib.rsmodified+7 -1
@@ -936,7 +936,13 @@ pub enum Action {
936936 ///
937937 /// Can be useful for scripts changing the config file, to avoid waiting the small duration for
938938 /// 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+ },
940946 }
941947
942948 /// Change in window or column size.
src/input/mod.rsmodified+2 -2
@@ -2318,9 +2318,9 @@ impl State {
23182318 }
23192319 self.niri.queue_redraw_all();
23202320 }
2321- Action::LoadConfigFile => {
2321+ Action::LoadConfigFile(path) => {
23222322 if let Some(watcher) = &self.niri.config_file_watcher {
2323- watcher.load_config();
2323+ watcher.load_config(path);
23242324 }
23252325 }
23262326 Action::MruConfirm => {
src/ipc/server.rsmodified+9 -1
@@ -463,7 +463,8 @@ async fn process(ctx: &ClientCtx, request: Request) -> Reply {
463463 fn validate_action(action: &Action) -> Result<(), String> {
464464 if let Action::Screenshot { path, .. }
465465 | Action::ScreenshotScreen { path, .. }
466- | Action::ScreenshotWindow { path, .. } = action
466+ | Action::ScreenshotWindow { path, .. }
467+ | Action::LoadConfigFile { path } = action
467468 {
468469 if let Some(path) = path {
469470 // Relative paths are resolved against the niri compositor's working directory, which
@@ -474,6 +475,13 @@ fn validate_action(action: &Action) -> Result<(), String> {
474475 }
475476 }
476477
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+
477485 Ok(())
478486 }
479487
src/utils/watcher.rsmodified+12 -4
@@ -14,7 +14,7 @@ use crate::niri::State;
1414 const POLLING_INTERVAL: Duration = Duration::from_millis(500);
1515
1616 pub struct Watcher {
17- load_config: mpsc::Sender<()>,
17+ load_config: mpsc::Sender<Option<String>>,
1818 }
1919
2020 struct WatcherInner {
@@ -67,7 +67,15 @@ impl Watcher {
6767
6868 loop {
6969 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+ }
7179 Err(mpsc::RecvTimeoutError::Disconnected) => break,
7280 Err(mpsc::RecvTimeoutError::Timeout) => false,
7381 };
@@ -105,8 +113,8 @@ impl Watcher {
105113 Self { load_config }
106114 }
107115
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);
110118 }
111119 }
112120