Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Compositors (Hyprland)

Platforms: NixOS only.

Hyprland Wayland compositor with automatic workspace generation, window rules, and UWSM integration.

Config format

Hyprland 0.5x reads Lua (~/.config/hypr/hyprland.lua); hyprlang is the legacy provider. The module pins configType = "lua" and expresses every setting as an hl.* call, so option values are structured attrsets rather than the comma-separated hyprlang strings they used to be. Monitors, workspace rules and window rules all take the shape of the hl.* function that consumes them. Key chords use Hyprland’s +-separated syntax ($mainMod + SHIFT + H), where $mainMod expands to a Lua local carrying the mainMod option.

Two knock-on effects worth knowing:

  • hyprctl dispatch is a shorthand for hl.dispatch(...) under the Lua provider, so its argument is a Lua expression: hyprctl dispatch 'hl.dsp.exit()', not hyprctl dispatch exit.
  • Home Manager also writes ~/.config/hypr/.luarc.json, pointing the Lua language server at the compositor’s own hl.* stubs for editor completion.

Package

The compositor and its portal come from the Hyprland flake this library locks, and a consuming flake declares no hyprland input. Upstream builds those packages against its own nixpkgs, which is what hyprland.cachix.org serves, and the module adds that cache to the host’s substituters.

package and portalPackage take another build. Set both from one source, since the portal is built against the compositor it ships with.

{pkgs, ...}: {
  othrys.desktop.compositors.hyprland = {
    package = pkgs.hyprland;
    portalPackage = pkgs.xdg-desktop-portal-hyprland;
  };
}

A consumer who tracks the Hyprland flake at another revision makes this library’s input follow theirs, with inputs.othrys.inputs.hyprland.follows = "hyprland", and leaves the options at their defaults.

Options

  options.othrys.desktop.compositors.hyprland = {
    enable = lib.mkEnableOption "Hyprland window manager";

    package = lib.mkOption {
      type = lib.types.package;
      default = locked.hyprland;
      defaultText = lib.literalMD "the `hyprland` package of the Hyprland flake this library locks";
      example = lib.literalExpression "pkgs.hyprland";
      description = ''
        The Hyprland package, used by the NixOS module and by the Home Manager
        module alike. The default is built by upstream against its own
        nixpkgs, which is what hyprland.cachix.org serves. Set `portalPackage`
        from the same source, since the portal is built against the
        compositor it ships with.
      '';
    };

    portalPackage = lib.mkOption {
      type = lib.types.package;
      default = locked.xdg-desktop-portal-hyprland;
      defaultText = lib.literalMD "the `xdg-desktop-portal-hyprland` package of the Hyprland flake this library locks";
      example = lib.literalExpression "pkgs.xdg-desktop-portal-hyprland";
      description = "The desktop portal for Hyprland, passed to `programs.hyprland.portalPackage`.";
    };

    monitors = lib.mkOption {
      type = lib.types.listOf (lib.types.submodule {
        freeformType = lib.types.attrsOf luaValue;
        options = {
          output = lib.mkOption {
            type = lib.types.str;
            description = "Connector name, or \"\" for the catch-all rule covering every unnamed output.";
            example = "DP-1";
          };
          mode = lib.mkOption {
            type = lib.types.str;
            default = "preferred";
            description = "Resolution and refresh rate.";
            example = "1920x1080@360";
          };
          position = lib.mkOption {
            type = lib.types.str;
            default = "auto";
            description = "Position in the monitor layout.";
            example = "0x0";
          };
          scale = lib.mkOption {
            type = lib.types.str;
            default = "auto";
            description = "Scale factor.";
            example = "1";
          };
        };
      });
      default = [{output = "";}];
      description = "hl.monitor() specs. Freeform keys (vrr, transform, mirror, disabled, ...) pass through.";
      example = [
        {
          output = "DP-1";
          mode = "1920x1080@360";
          position = "0x0";
          scale = "1";
        }
        {
          output = "HDMI-A-1";
          mode = "1920x1080@144";
          position = "-1920x0";
          scale = "1";
        }
      ];
    };

    workspaces = lib.mkOption {
      type = lib.types.listOf (lib.types.submodule {
        freeformType = lib.types.attrsOf luaValue;
        options.workspace = lib.mkOption {
          type = lib.types.str;
          description = "Workspace the rule applies to; Hyprland requires a string selector.";
          example = "1";
        };
      });
      default = [];
      description = "hl.workspace_rule() specs (auto-generated from `monitors` if empty).";
      example = [
        {
          workspace = "1";
          monitor = "DP-1";
          default = true;
        }
      ];
    };

    mainMod = lib.mkOption {
      type = lib.types.str;
      default = "SUPER";
      description = "Modifier bound to the `mainMod` Lua local used by all default keybindings.";
    };

    terminal = lib.mkOption {
      type = lib.types.str;
      default = "kitty";
      description = "Terminal emulator command bound to the `terminal` Lua local.";
    };

    browser = lib.mkOption {
      type = lib.types.str;
      default = config.othrys.desktop.defaultBrowser;
      defaultText = lib.literalExpression "config.othrys.desktop.defaultBrowser";
      description = ''
        Browser command bound to the `browser` Lua local. Defaults to the
        shared `othrys.desktop.defaultBrowser`. Set this for a
        Hyprland-specific override without changing Niri's browser shortcut.
      '';
    };

    input.keyboardLayout = lib.mkOption {
      type = lib.types.str;
      default = "us";
      description = "XKB keyboard layout.";
    };

    screenshots.directory = lib.mkOption {
      type = lib.types.str;
      default = "~/Pictures/Screenshots";
      description = "Directory the screenshot binds save into.";
    };

    # Curated actions with configurable chords. The module knows how to focus,
    # swap, screenshot, etc. Consumers only decide which keys trigger them.
    # Chords use Hyprland's `+`-separated syntax; `$mainMod` expands to the
    # `mainMod` option.
    binds = {
      focusLeft = bindOpt "$mainMod + H" "focus window left";
      focusRight = bindOpt "$mainMod + L" "focus window right";
      focusDown = bindOpt "$mainMod + J" "focus window down";
      focusUp = bindOpt "$mainMod + K" "focus window up";

      swapLeft = bindOpt "$mainMod + SHIFT + H" "swap window left";
      swapRight = bindOpt "$mainMod + SHIFT + L" "swap window right";
      swapDown = bindOpt "$mainMod + SHIFT + J" "swap window down";
      swapUp = bindOpt "$mainMod + SHIFT + K" "swap window up";

      killActive = bindOpt "$mainMod + Q" "close the active window";
      terminal = bindOpt "$mainMod + Return" "launch the terminal";
      browser = bindOpt "$mainMod + F" "launch the browser";

      fullscreen = bindOpt "$mainMod + M" "toggle fullscreen";
      toggleFloating = bindOpt "$mainMod + V" "toggle floating";
      pseudo = bindOpt "$mainMod + P" "toggle pseudotiling";
      pin = bindOpt "$mainMod + Y" "pin window";
      exit = bindOpt "$mainMod + SHIFT + E" "exit Hyprland";

      specialWorkspace = bindOpt "$mainMod + S" "toggle the special workspace";
      moveToSpecialWorkspace = bindOpt "$mainMod + SHIFT + S" "move window to the special workspace";
      workspacePrev = bindOpt "$mainMod + CTRL + left" "previous workspace";
      workspaceNext = bindOpt "$mainMod + CTRL + right" "next workspace";

      screenshot = bindOpt "Print" "screenshot the full screen";
      screenshotRegion = bindOpt "$mainMod + Print" "screenshot a region";
      screenshotRegionToClipboard = bindOpt "$mainMod + SHIFT + Print" "screenshot a region to the clipboard";
    };

    numberedWorkspaceBinds = lib.mkOption {
      type = lib.types.bool;
      default = true;
      description = "Generate $mainMod+1..0 workspace switch and $mainMod+SHIFT+1..0 move binds.";
    };

    extraBinds = lib.mkOption {
      type = lib.types.listOf (lib.types.submodule {
        options = {
          key = lib.mkOption {
            type = lib.types.str;
            description = "Key chord; `$mainMod` expands to the mainMod option.";
            example = "$mainMod + CTRL + 1";
          };
          dispatcher = lib.mkOption {
            type = lib.types.str;
            description = "Raw Lua dispatcher expression handed to hl.bind().";
            example = "hl.dsp.focus({ monitor = \"DP-1\" })";
          };
          opts = lib.mkOption {
            type = lib.types.attrsOf luaValue;
            default = {};
            description = "hl.bind() option table (locked, repeating, mouse, description, ...).";
          };
        };
      });
      default = [];
      description = "Extra keybindings (e.g., monitor focus for multi-monitor).";
    };

    windowRules = {
      workspaceAssignments = lib.mkOption {
        type = lib.types.listOf (lib.types.attrsOf luaValue);
        default = [];
        description = "hl.window_rule() specs pinning applications to workspaces, a per-fleet workflow choice, so no defaults.";
        example = [
          {
            match.class = "^(code-url-handler)$";
            workspace = "3 silent";
          }
          {
            match.class = "^(discord)$";
            workspace = "6 silent";
          }
        ];
      };

      presets = {
        steam = lib.mkOption {
          type = lib.types.bool;
          default = true;
          description = "Float Steam dialogs (Friends List, Settings, Steam Guard, ...) and tile the main window.";
        };
        jetbrains = lib.mkOption {
          type = lib.types.bool;
          default = true;
          description = "Focus/centering fixes for JetBrains IDE popup windows.";
        };
        floatCommonDialogs = lib.mkOption {
          type = lib.types.bool;
          default = true;
          description = "Float utility dialogs (pavucontrol, nm-connection-editor, calculator).";
        };
        pictureInPicture = lib.mkOption {
          type = lib.types.bool;
          default = true;
          description = "Float, pin, and corner-position Picture-in-Picture windows.";
        };
      };

      extra = lib.mkOption {
        type = lib.types.listOf (lib.types.attrsOf luaValue);
        default = [];
        description = "Additional hl.window_rule() specs appended after presets.";
      };
    };

    touchpad = {
      enable = lib.mkEnableOption "Touchpad support (for laptops)";
    };
  };

Keybindings

          bind = let
            b = cfg.binds;
          in
            lib.optionals cfg.numberedWorkspaceBinds (
              map (n:
                hlLua.mkBind {
                  key = "$mainMod + ${toString (lib.mod n 10)}";
                  dispatcher = "hl.dsp.focus({ workspace = ${toString n} })";
                }) (lib.range 1 10)
              ++ map (n:
                hlLua.mkBind {
                  key = "$mainMod + SHIFT + ${toString (lib.mod n 10)}";
                  dispatcher = "hl.dsp.window.move({ workspace = ${toString n} })";
                }) (lib.range 1 10)
            )
            ++ mkBind b.focusLeft ''hl.dsp.focus({ direction = "left" })''
            ++ mkBind b.focusRight ''hl.dsp.focus({ direction = "right" })''
            ++ mkBind b.focusDown ''hl.dsp.focus({ direction = "down" })''
            ++ mkBind b.focusUp ''hl.dsp.focus({ direction = "up" })''
            ++ mkBind b.swapLeft ''hl.dsp.window.swap({ direction = "left" })''
            ++ mkBind b.swapRight ''hl.dsp.window.swap({ direction = "right" })''
            ++ mkBind b.swapDown ''hl.dsp.window.swap({ direction = "down" })''
            ++ mkBind b.swapUp ''hl.dsp.window.swap({ direction = "up" })''
            ++ mkBind b.killActive "hl.dsp.window.close()"
            ++ mkBind b.terminal "hl.dsp.exec_cmd(terminal)"
            ++ mkBind b.browser "hl.dsp.exec_cmd(browser)"
            ++ mkBind b.fullscreen "hl.dsp.window.fullscreen()"
            ++ mkBind b.toggleFloating "hl.dsp.window.float()"
            ++ mkBind b.pseudo "hl.dsp.window.pseudo()"
            ++ mkBind b.pin "hl.dsp.window.pin()"
            ++ mkBind b.exit "hl.dsp.exit()"
            ++ mkBind b.specialWorkspace ''hl.dsp.workspace.toggle_special("magic")''
            ++ mkBind b.moveToSpecialWorkspace ''hl.dsp.window.move({ workspace = "special:magic" })''
            ++ mkBind b.workspacePrev ''hl.dsp.focus({ workspace = "e-1" })''
            ++ mkBind b.workspaceNext ''hl.dsp.focus({ workspace = "e+1" })''
            ++ mkBind b.screenshot (execCmd "grim ${screenshotFile}")
            ++ mkBind b.screenshotRegion (execCmd ''grim -g "$(slurp)" ${screenshotFile}'')
            ++ mkBind b.screenshotRegionToClipboard (execCmd ''grim -g "$(slurp)" - | wl-copy'')
            # Drag binds, where `mouse` is exclusive with locked/repeating.
            ++ [
              (hlLua.mkBind {
                key = "$mainMod + mouse:272";
                dispatcher = "hl.dsp.window.drag()";
                opts.mouse = true;
              })
              (hlLua.mkBind {
                key = "$mainMod + mouse:273";
                dispatcher = "hl.dsp.window.resize()";
                opts.mouse = true;
              })
              (hlLua.mkBind {
                key = "$mainMod + mouse_down";
                dispatcher = ''hl.dsp.focus({ workspace = "e+1" })'';
              })
              (hlLua.mkBind {
                key = "$mainMod + mouse_up";
                dispatcher = ''hl.dsp.focus({ workspace = "e-1" })'';
              })
            ]
            # Media and brightness keys stay live on the lock screen and
            # repeat while held.
            ++ map (spec:
              hlLua.mkBind {
                inherit (spec) key dispatcher;
                opts = {
                  locked = true;
                  repeating = true;
                };
              }) [
              {
                key = "XF86AudioRaiseVolume";
                dispatcher = execCmd "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+";
              }
              {
                key = "XF86AudioLowerVolume";
                dispatcher = execCmd "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%-";
              }
              {
                key = "XF86AudioMute";
                dispatcher = execCmd "wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle";
              }
              {
                key = "XF86AudioMicMute";
                dispatcher = execCmd "wpctl set-mute @DEFAULT_AUDIO_SOURCE@ toggle";
              }
              {
                key = "XF86MonBrightnessUp";
                dispatcher = execCmd "brightnessctl s 10%+";
              }
              {
                key = "XF86MonBrightnessDown";
                dispatcher = execCmd "brightnessctl s 10%-";
              }
            ]
            ++ map (spec:
              hlLua.mkBind {
                inherit (spec) key dispatcher;
                opts.locked = true;
              }) [
              {
                key = "XF86AudioNext";
                dispatcher = execCmd "playerctl next";
              }
              {
                key = "XF86AudioPrev";
                dispatcher = execCmd "playerctl previous";
              }
              {
                key = "XF86AudioPlay";
                dispatcher = execCmd "playerctl play-pause";
              }
              {
                key = "XF86AudioPause";
                dispatcher = execCmd "playerctl play-pause";
              }
            ]
            ++ map hlLua.mkBind cfg.extraBinds;

Quick Reference

BindingAction
Super + 1-0Switch workspace
Super + Shift + 1-0Move window to workspace
Super + H/J/K/LFocus left/down/up/right
Super + Shift + H/J/K/LSwap window
Super + QKill window
Super + ReturnTerminal
Super + FBrowser
Super + MFullscreen
Super + VToggle floating
Super + SScratchpad
PrintScreenshot (full)
Super + PrintScreenshot (region)

Window Rules

          window_rule =
            cfg.windowRules.workspaceAssignments
            ++ lib.optionals cfg.windowRules.presets.steam [
              {
                match = {
                  class = "^(steam)$";
                  title = "^(Steam)$";
                };
                tile = true;
              }
              {
                match = {
                  class = "^(steam)$";
                  title = "^(Friends List).*$";
                };
                float = true;
              }
              {
                match = {
                  class = "^(steam)$";
                  title = "^(.*Settings)$";
                };
                float = true;
              }
              {
                match = {
                  class = "^(steam)$";
                  title = "^(Steam Guard.*)$";
                };
                float = true;
              }
              {
                match = {
                  class = "^(steam)$";
                  title = "^(Steam - News.*)$";
                };
                float = true;
              }
              {
                match = {
                  class = "^(steam)$";
                  title = "^(Install.*)$";
                };
                float = true;
              }
            ]
            ++ lib.optionals cfg.windowRules.presets.jetbrains [
              {
                match = {
                  class = "^(jetbrains-toolbox)$";
                  float = false;
                };
                no_initial_focus = true;
              }
              {
                match = {
                  class = "^(jetbrains-.*)$";
                  float = false;
                };
                no_initial_focus = true;
              }
              {
                match = {
                  class = "^(jetbrains-.*)$";
                  title = "^$";
                  initial_title = "^$";
                  float = false;
                };
                no_initial_focus = true;
              }
              {
                match = {
                  class = "^(jetbrains-.*)$";
                  initial_title = "(.+)";
                  float = false;
                };
                center = true;
              }
              {
                match = {
                  class = "^(jetbrains-.*)$";
                  title = "^$";
                  initial_title = "^$";
                  float = false;
                };
                center = true;
              }
              {
                match = {
                  class = "^(jetbrains-.*)$";
                  title = "^win(.*)$";
                  initial_title = "^win.*$";
                  float = false;
                };
                no_initial_focus = true;
              }
            ]
            ++ lib.optionals cfg.windowRules.presets.floatCommonDialogs [
              {
                match.class = "^(pavucontrol)$";
                float = true;
              }
              {
                match.class = "^(nm-connection-editor)$";
                float = true;
              }
              {
                match.title = "^(Volume Control)$";
                float = true;
              }
              {
                match.class = "^(org.gnome.Calculator)$";
                float = true;
              }
            ]
            # One rule now that effects share a spec, instead of the four
            # separate hyprlang lines this replaces.
            ++ lib.optional cfg.windowRules.presets.pictureInPicture {
              match.title = "^(Picture-in-Picture)$";
              float = true;
              pin = true;
              size = "640 360";
              move = "100%-660 100%-380";
            }
            ++ cfg.windowRules.extra;

Monitor Configuration

Monitors are configured per-host as hl.monitor() specs. output is required; mode, position, and scale default to preferred/auto/auto, and any other hl.monitor() field (vrr, transform, mirror, disabled, …) passes through:

othrys.desktop.compositors.hyprland.monitors = [
  {
    output = "DP-1";
    mode = "1920x1080@360";
    position = "0x0";
    scale = "1";
  }
  {output = "HDMI-A-1";} # preferred mode, auto position/scale
];

An empty output is the catch-all rule matching every unnamed connector, so it is the module default ([{output = "";}]) and the usual external-display fallback.

Workspaces auto-generate based on monitor count:

  • Single monitor: all 10 workspaces on it
  • Dual monitor: workspaces 1-5 on primary, 6-10 on secondary

Named monitors get a monitor: binding in the generated hl.workspace_rule() calls, while the catch-all rule does not, since pinning workspaces to it is meaningless. Set workspaces to override the generated rules wholesale.