hyprwm / Hyprland

Hyprland is an independent, highly customizable, dynamic tiling Wayland compositor that doesn't sacrifice on its looks.
https://hyprland.org
BSD 3-Clause "New" or "Revised" License
21.19k stars 893 forks source link

Allow selecting a config file using an environment variable #3141

Open Hubro opened 1 year ago

Hubro commented 1 year ago

Description

I'm currently launching Hyprland with a script that ends with these lines:

if [[ -f "$HOME/.config/hypr/config-$(hostname).conf" ]]; then
    CONFIG_PATH="$HOME/.config/hypr/config-$(hostname).conf"
else
    CONFIG_PATH="$HOME/.config/hypr/hyprland.conf"
fi

echo
echo "Launching hyprland with config: $CONFIG_PATH"
echo

Hyprland --config "$CONFIG_PATH" "$@" 2>&1 | tee $HOME/.hyprland.log

Today I attempted to switch to a display manager (LightDM) and I got stuck on the fact that LightDM will just run Hyprland with no arguments, so there's no way for me to specify a config file.

I use separate config files for separate machines, since they have different display configurations.

Is it currently possible to make Hyprland use a specific config file by setting an environment variable? I searched to the best of my ability, but I couldn't find any mention of such an environment variable.

Would this be possible to add? It would unlock the ability to use display managers for those of us who need separate config files per host.

tchofy commented 1 year ago

If I understood your question properly, all you need to do is either modify the hyprland.desktop file in /usr/share/wayland-sessions (downside, it will likely get overwritten after an update), or even better, create a new .desktop entry and modify the Exec= to point to your script instead, then it'll show up on your login manager.

Hubro commented 1 year ago

@tchofy True, I should probably do that, but it feels like a workaround whereas using an environment variable feels more like a proper solution. That way I don't have to modify or add any system files, which would be more work any time I replicate my setup to another machine.

Good idea though, I'll do this for now.

memchr commented 1 year ago

config_from_env.patch

diff --git a/src/main.cpp b/src/main.cpp
index 3508043..3c63054 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -79,6 +79,16 @@ int main(int argc, char** argv) {
         }
     }

+    static const char* configPathEnv = getenv("HYPRLAND_CONFIG");
+    if (configPathEnv) {
+        if (!std::filesystem::exists(configPathEnv)) {
+            std::cerr << "[ ERROR ] Config path '" << configPathEnv << "' doesn't exist!\n";
+            return 1;
+        }
+        configPath = configPathEnv;
+        Debug::log(LOG, "Using config location specified in HYPRLAND_CONFIG: '%s'", configPath.c_str());
+    }
+
     if (!ignoreSudo && Init::isSudo()) {
         std::cerr << "[ ERROR ] Hyprland was launched with superuser priveleges, but the privileges check is not omitted.\n";
         std::cerr << "          Hint: Use the --i-am-really-stupid flag to omit that check.\n";

Build with

git apply config_from_env.patch
meson setup build
ninja -C build

You can test with

HYPRLAND_CONFIG=/path/to/config Hyprland
izmyname commented 1 month ago

config_from_env.patch

diff --git a/src/main.cpp b/src/main.cpp
index 3508043..3c63054 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -79,6 +79,16 @@ int main(int argc, char** argv) {
         }
     }

+    static const char* configPathEnv = getenv("HYPRLAND_CONFIG");
+    if (configPathEnv) {
+        if (!std::filesystem::exists(configPathEnv)) {
+            std::cerr << "[ ERROR ] Config path '" << configPathEnv << "' doesn't exist!\n";
+            return 1;
+        }
+        configPath = configPathEnv;
+        Debug::log(LOG, "Using config location specified in HYPRLAND_CONFIG: '%s'", configPath.c_str());
+    }
+
     if (!ignoreSudo && Init::isSudo()) {
         std::cerr << "[ ERROR ] Hyprland was launched with superuser priveleges, but the privileges check is not omitted.\n";
         std::cerr << "          Hint: Use the --i-am-really-stupid flag to omit that check.\n";

Build with

git apply config_from_env.patch
meson setup build
ninja -C build

You can test with

HYPRLAND_CONFIG=/path/to/config Hyprland

Bump. Prolly, worth merging it into the main?