diff options
| author | sodiboo <git@sodi.boo> | 2024-05-26 23:57:34 +0200 |
|---|---|---|
| committer | Ivan Molodetskikh <yalterz@gmail.com> | 2024-06-28 14:00:26 +0300 |
| commit | d180e60e0584e0c43682d7aca612c366f881dd85 (patch) | |
| tree | e240f28df0053bc61c090f5d76a34e164d91467f /src | |
| parent | 65addefd09acef059e1415e9ebdfcbb82f86d689 (diff) | |
| download | niri-d180e60e0584e0c43682d7aca612c366f881dd85.tar.gz niri-d180e60e0584e0c43682d7aca612c366f881dd85.tar.bz2 niri-d180e60e0584e0c43682d7aca612c366f881dd85.zip | |
Implement support for `$NIRI_CONFIG` environment variable
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"); |
