diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/cli.rs | 6 | ||||
| -rw-r--r-- | src/main.rs | 11 |
2 files changed, 16 insertions, 1 deletions
@@ -13,6 +13,9 @@ use crate::utils::version; #[command(subcommand_help_heading = "Subcommands")] pub struct Cli { /// Path to config file (default: `$XDG_CONFIG_HOME/niri/config.kdl`). + /// + /// This can also be set with the `NIRI_CONFIG` environment variable. If both are set, the + /// command line argument takes precedence. #[arg(short, long)] pub config: Option<PathBuf>, /// Import environment globally to systemd and D-Bus, run D-Bus services. @@ -43,6 +46,9 @@ pub enum Sub { /// Validate the config file. Validate { /// Path to config file (default: `$XDG_CONFIG_HOME/niri/config.kdl`). + /// + /// This can also be set with the `NIRI_CONFIG` environment variable. If both are set, the + /// command line argument takes precedence. #[arg(short, long)] config: Option<PathBuf>, }, diff --git a/src/main.rs b/src/main.rs index fb399f3a..ad201b2d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -91,6 +91,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { tracy_client::Client::start(); let path = config + .or_else(env_config_path) .or_else(default_config_path) .expect("error getting config path"); Config::load(&path)?; @@ -113,7 +114,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { // Load the config. let mut config_created = false; - let path = cli.config.or_else(|| { + let path = cli.config.or_else(env_config_path); + env::remove_var("NIRI_CONFIG"); + let path = path.or_else(|| { let default_path = default_config_path()?; let default_parent = default_path.parent().unwrap(); @@ -315,6 +318,12 @@ fn import_environment() { } } +fn env_config_path() -> Option<PathBuf> { + env::var_os("NIRI_CONFIG") + .filter(|x| !x.is_empty()) + .map(PathBuf::from) +} + fn default_config_path() -> Option<PathBuf> { let Some(dirs) = ProjectDirs::from("", "", "niri") else { warn!("error retrieving home directory"); |
