forge
Explore
Log in

Don't you dare go Hollow

Implement support for `$NIRI_CONFIG` environment variable

Browse files at this commit

d180e60e0584e0c43682d7aca612c366f881dd85sodibooParent: 65addef
src/cli.rsmodified+6 -0
@@ -13,6 +13,9 @@ use crate::utils::version;
13 #[command(subcommand_help_heading = "Subcommands")]13 #[command(subcommand_help_heading = "Subcommands")]
14 pub struct Cli {14 pub struct Cli {
15 /// Path to config file (default: `$XDG_CONFIG_HOME/niri/config.kdl`).15 /// Path to config file (default: `$XDG_CONFIG_HOME/niri/config.kdl`).
16+ ///
17+ /// This can also be set with the `NIRI_CONFIG` environment variable. If both are set, the
18+ /// command line argument takes precedence.
16 #[arg(short, long)]19 #[arg(short, long)]
17 pub config: Option<PathBuf>,20 pub config: Option<PathBuf>,
18 /// Import environment globally to systemd and D-Bus, run D-Bus services.21 /// Import environment globally to systemd and D-Bus, run D-Bus services.
@@ -43,6 +46,9 @@ pub enum Sub {
43 /// Validate the config file.46 /// Validate the config file.
44 Validate {47 Validate {
45 /// Path to config file (default: `$XDG_CONFIG_HOME/niri/config.kdl`).48 /// Path to config file (default: `$XDG_CONFIG_HOME/niri/config.kdl`).
49+ ///
50+ /// This can also be set with the `NIRI_CONFIG` environment variable. If both are set, the
51+ /// command line argument takes precedence.
46 #[arg(short, long)]52 #[arg(short, long)]
47 config: Option<PathBuf>,53 config: Option<PathBuf>,
48 },54 },
src/main.rsmodified+10 -1
@@ -91,6 +91,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
91 tracy_client::Client::start();91 tracy_client::Client::start();
92 92
93 let path = config93 let path = config
94+ .or_else(env_config_path)
94 .or_else(default_config_path)95 .or_else(default_config_path)
95 .expect("error getting config path");96 .expect("error getting config path");
96 Config::load(&path)?;97 Config::load(&path)?;
@@ -113,7 +114,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
113 114
114 // Load the config.115 // Load the config.
115 let mut config_created = false;116 let mut config_created = false;
116- let path = cli.config.or_else(|| {117+ let path = cli.config.or_else(env_config_path);
118+ env::remove_var("NIRI_CONFIG");
119+ let path = path.or_else(|| {
117 let default_path = default_config_path()?;120 let default_path = default_config_path()?;
118 let default_parent = default_path.parent().unwrap();121 let default_parent = default_path.parent().unwrap();
119 122
@@ -315,6 +318,12 @@ fn import_environment() {
315 }318 }
316 }319 }
317 320
321+fn env_config_path() -> Option<PathBuf> {
322+ env::var_os("NIRI_CONFIG")
323+ .filter(|x| !x.is_empty())
324+ .map(PathBuf::from)
325+}
326+
318 fn default_config_path() -> Option<PathBuf> {327 fn default_config_path() -> Option<PathBuf> {
319 let Some(dirs) = ProjectDirs::from("", "", "niri") else {328 let Some(dirs) = ProjectDirs::from("", "", "niri") else {
320 warn!("error retrieving home directory");329 warn!("error retrieving home directory");
wiki/Configuration:-Overview.mdmodified+5 -0
@@ -25,6 +25,11 @@ You can run `niri validate` to parse the config and see any errors.
25 25
26 To use a different config file path, pass it in the `--config` or `-c` argument to `niri`.26 To use a different config file path, pass it in the `--config` or `-c` argument to `niri`.
27 27
28+You can also set `$NIRI_CONFIG` to the path of the config file.
29+`--config` always takes precedence.
30+If `--config` or `$NIRI_CONFIG` doesn't point to a real file, the config will not be loaded.
31+If `$NIRI_CONFIG` is set to an empty string, it is ignored and the default config location is used instead.
32+
28 ### Syntax33 ### Syntax
29 34
30 The config is written in [KDL].35 The config is written in [KDL].