fork/home-manager
git clone https://git.julia.blue/fork/home-manager
75790441
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7cfg = config . wayland . windowManager . sway ; 8 9commonOptions = import ./lib/options.nix { 10inherit config lib cfg pkgs ; 11moduleName = "sway" ; 12capitalModuleName = "Sway" ; 13}; 14 15configModule = types . submodule { 16options = { 17inherit ( commonOptions ) 18fonts window floating focus assigns workspaceLayout 19workspaceAutoBackAndForth modifier keycodebindings colors bars startup 20gaps menu terminal defaultWorkspace workspaceOutputAssign ; 21 22left = mkOption { 23type = types . str ; 24default = "h" ; 25description = "Home row direction key for moving left." ; 26}; 27 28down = mkOption { 29type = types . str ; 30default = "j" ; 31description = "Home row direction key for moving down." ; 32}; 33 34up = mkOption { 35type = types . str ; 36default = "k" ; 37description = "Home row direction key for moving up." ; 38}; 39 40right = mkOption { 41type = types . str ; 42default = "l" ; 43description = "Home row direction key for moving right." ; 44}; 45 46keybindings = mkOption { 47type = types . attrsOf ( types . nullOr types . str ); 48default = mapAttrs ( n :mkOptionDefault ) { 49" ${ cfg . config . modifier } +Return" = "exec ${ cfg . config . terminal } " ; 50" ${ cfg . config . modifier } +Shift+q" = "kill" ; 51" ${ cfg . config . modifier } +d" = "exec ${ cfg . config . menu } " ; 52 53" ${ cfg . config . modifier } + ${ cfg . config . left } " = "focus left" ; 54" ${ cfg . config . modifier } + ${ cfg . config . down } " = "focus down" ; 55" ${ cfg . config . modifier } + ${ cfg . config . up } " = "focus up" ; 56" ${ cfg . config . modifier } + ${ cfg . config . right } " = "focus right" ; 57 58" ${ cfg . config . modifier } +Left" = "focus left" ; 59" ${ cfg . config . modifier } +Down" = "focus down" ; 60" ${ cfg . config . modifier } +Up" = "focus up" ; 61" ${ cfg . config . modifier } +Right" = "focus right" ; 62 63" ${ cfg . config . modifier } +Shift+ ${ cfg . config . left } " = "move left" ; 64" ${ cfg . config . modifier } +Shift+ ${ cfg . config . down } " = "move down" ; 65" ${ cfg . config . modifier } +Shift+ ${ cfg . config . up } " = "move up" ; 66" ${ cfg . config . modifier } +Shift+ ${ cfg . config . right } " = "move right" ; 67 68" ${ cfg . config . modifier } +Shift+Left" = "move left" ; 69" ${ cfg . config . modifier } +Shift+Down" = "move down" ; 70" ${ cfg . config . modifier } +Shift+Up" = "move up" ; 71" ${ cfg . config . modifier } +Shift+Right" = "move right" ; 72 73" ${ cfg . config . modifier } +b" = "splith" ; 74" ${ cfg . config . modifier } +v" = "splitv" ; 75" ${ cfg . config . modifier } +f" = "fullscreen toggle" ; 76" ${ cfg . config . modifier } +a" = "focus parent" ; 77 78" ${ cfg . config . modifier } +s" = "layout stacking" ; 79" ${ cfg . config . modifier } +w" = "layout tabbed" ; 80" ${ cfg . config . modifier } +e" = "layout toggle split" ; 81 82" ${ cfg . config . modifier } +Shift+space" = "floating toggle" ; 83" ${ cfg . config . modifier } +space" = "focus mode_toggle" ; 84 85" ${ cfg . config . modifier } +1" = "workspace number 1" ; 86" ${ cfg . config . modifier } +2" = "workspace number 2" ; 87" ${ cfg . config . modifier } +3" = "workspace number 3" ; 88" ${ cfg . config . modifier } +4" = "workspace number 4" ; 89" ${ cfg . config . modifier } +5" = "workspace number 5" ; 90" ${ cfg . config . modifier } +6" = "workspace number 6" ; 91" ${ cfg . config . modifier } +7" = "workspace number 7" ; 92" ${ cfg . config . modifier } +8" = "workspace number 8" ; 93" ${ cfg . config . modifier } +9" = "workspace number 9" ; 94 95" ${ cfg . config . modifier } +Shift+1" = 96"move container to workspace number 1" ; 97" ${ cfg . config . modifier } +Shift+2" = 98"move container to workspace number 2" ; 99" ${ cfg . config . modifier } +Shift+3" = 100"move container to workspace number 3" ; 101" ${ cfg . config . modifier } +Shift+4" = 102"move container to workspace number 4" ; 103" ${ cfg . config . modifier } +Shift+5" = 104"move container to workspace number 5" ; 105" ${ cfg . config . modifier } +Shift+6" = 106"move container to workspace number 6" ; 107" ${ cfg . config . modifier } +Shift+7" = 108"move container to workspace number 7" ; 109" ${ cfg . config . modifier } +Shift+8" = 110"move container to workspace number 8" ; 111" ${ cfg . config . modifier } +Shift+9" = 112"move container to workspace number 9" ; 113 114" ${ cfg . config . modifier } +Shift+minus" = "move scratchpad" ; 115" ${ cfg . config . modifier } +minus" = "scratchpad show" ; 116 117" ${ cfg . config . modifier } +Shift+c" = "reload" ; 118" ${ cfg . config . modifier } +Shift+e" = 119"exec swaynag -t warning -m 'You pressed the exit shortcut. Do you really want to exit sway? This will end your Wayland session.' -b 'Yes, exit sway' 'swaymsg exit'" ; 120 121" ${ cfg . config . modifier } +r" = "mode resize" ; 122}; 123defaultText = "Default sway keybindings." ; 124description = '' 125An attribute set that assigns a key press to an action using a key symbol. 126See <https://i3wm.org/docs/userguide.html#keybindings>. 127 128Consider to use `lib.mkOptionDefault` function to extend or override 129default keybindings instead of specifying all of them from scratch. 130'' ; 131example = literalExpression '' 132let 133modifier = config.wayland.windowManager.sway.config.modifier; 134in lib.mkOptionDefault { 135"''${modifier}+Return" = "exec ${ cfg . config . terminal } "; 136"''${modifier}+Shift+q" = "kill"; 137"''${modifier}+d" = "exec ${ cfg . config . menu } "; 138} 139'' ; 140}; 141 142bindkeysToCode = mkOption { 143type = types . bool ; 144default = false ; 145example = true ; 146description = '' 147Whether to make use of {option}`--to-code` in keybindings. 148'' ; 149}; 150 151input = mkOption { 152type = types . attrsOf ( types . attrsOf types . str ); 153default = { }; 154example = { "*" = { xkb_variant = "dvorak" ; }; }; 155description = '' 156An attribute set that defines input modules. See 157{manpage}`sway-input(5)` 158for options. 159'' ; 160}; 161 162output = mkOption { 163type = types . attrsOf ( types . attrsOf types . str ); 164default = { }; 165example = { "HDMI-A-2" = { bg = "~/path/to/background.png fill" ; }; }; 166description = '' 167An attribute set that defines output modules. See 168{manpage}`sway-output(5)` 169for options. 170'' ; 171}; 172 173seat = mkOption { 174type = types . attrsOf ( types . attrsOf types . str ); 175default = { }; 176example = { "*" = { hide_cursor = "when-typing enable" ; }; }; 177description = '' 178An attribute set that defines seat modules. See 179{manpage}`sway-input(5)` 180for options. 181'' ; 182}; 183 184modes = mkOption { 185type = types . attrsOf ( types . attrsOf types . str ); 186default = { 187resize = { 188" ${ cfg . config . left } " = "resize shrink width 10 px" ; 189" ${ cfg . config . down } " = "resize grow height 10 px" ; 190" ${ cfg . config . up } " = "resize shrink height 10 px" ; 191" ${ cfg . config . right } " = "resize grow width 10 px" ; 192"Left" = "resize shrink width 10 px" ; 193"Down" = "resize grow height 10 px" ; 194"Up" = "resize shrink height 10 px" ; 195"Right" = "resize grow width 10 px" ; 196"Escape" = "mode default" ; 197"Return" = "mode default" ; 198}; 199}; 200description = '' 201An attribute set that defines binding modes and keybindings 202inside them 203 204Only basic keybinding is supported (bindsym keycomb action), 205for more advanced setup use 'sway.extraConfig'. 206'' ; 207}; 208}; 209}; 210 211wrapperOptions = types . submodule { 212options = let 213mkWrapperFeature = default :description : 214mkOption { 215type = types . bool ; 216inherit default ; 217example = ! default ; 218description = "Whether to make use of the ${ description } " ; 219}; 220in { 221base = mkWrapperFeature true '' 222base wrapper to execute extra session commands and prepend a 223dbus-run-session to the sway command. 224'' ; 225gtk = mkWrapperFeature false '' 226wrapGAppsHook wrapper to execute sway with required environment 227variables for GTK applications. 228'' ; 229}; 230}; 231 232commonFunctions = import ./lib/functions.nix { 233inherit config cfg lib ; 234moduleName = "sway" ; 235}; 236 237inherit ( commonFunctions ) 238keybindingsStr keycodebindingsStr modeStr assignStr barStr gapsStr 239floatingCriteriaStr windowCommandsStr colorSetStr windowBorderString 240fontConfigStr keybindingDefaultWorkspace keybindingsRest workspaceOutputStr ; 241 242startupEntryStr = { command , always , ...} :'' 243${ if always then "exec_always" else "exec" } ${ command } 244'' ; 245 246moduleStr = moduleType :name :attrs :'' 247${ moduleType } " ${ name } " { 248${ concatStringsSep "\n" 249( mapAttrsToList ( name : value : " ${ name } ${ value } " ) attrs )} 250} 251'' ; 252inputStr = moduleStr "input" ; 253outputStr = moduleStr "output" ; 254seatStr = moduleStr "seat" ; 255 256configFile = pkgs . writeText "sway.conf" ( concatStringsSep "\n" 257(( optional ( cfg . extraConfigEarly != "" ) cfg . extraConfigEarly ) 258++ ( if cfg . config != null then 259with cfg . config ; 260([ 261( fontConfigStr fonts ) 262"floating_modifier ${ floating . modifier } " 263( windowBorderString window floating ) 264"hide_edge_borders ${ window . hideEdgeBorders } " 265"focus_wrapping ${ focus . wrapping } " 266"focus_follows_mouse ${ focus . followMouse } " 267"focus_on_window_activation ${ focus . newWindow } " 268"mouse_warping ${ 269if builtins . isString ( focus . mouseWarping ) then 270focus . mouseWarping 271else if focus . mouseWarping then 272"output" 273else 274"none" 275} " 276"workspace_layout ${ workspaceLayout } " 277"workspace_auto_back_and_forth ${ 278lib . hm . booleans . yesNo workspaceAutoBackAndForth 279} " 280"client.focused ${ colorSetStr colors . focused } " 281"client.focused_inactive ${ colorSetStr colors . focusedInactive } " 282"client.unfocused ${ colorSetStr colors . unfocused } " 283"client.urgent ${ colorSetStr colors . urgent } " 284"client.placeholder ${ colorSetStr colors . placeholder } " 285"client.background ${ colors . background } " 286( keybindingsStr { 287keybindings = keybindingDefaultWorkspace ; 288bindsymArgs = 289lib . optionalString ( cfg . config . bindkeysToCode ) "--to-code" ; 290}) 291( keybindingsStr { 292keybindings = keybindingsRest ; 293bindsymArgs = 294lib . optionalString ( cfg . config . bindkeysToCode ) "--to-code" ; 295}) 296( keycodebindingsStr keycodebindings ) 297] ++ mapAttrsToList inputStr input 298++ mapAttrsToList outputStr output # outputs 299++ mapAttrsToList seatStr seat # seats 300++ mapAttrsToList ( modeStr cfg . config . bindkeysToCode ) modes # modes 301++ mapAttrsToList assignStr assigns # assigns 302++ map barStr bars # bars 303++ optional ( gaps != null ) gapsStr # gaps 304++ map floatingCriteriaStr floating . criteria # floating 305++ map windowCommandsStr window . commands # window commands 306++ map startupEntryStr startup # startup 307++ map workspaceOutputStr workspaceOutputAssign # custom mapping 308) 309else 310[ ]) ++ ( optional cfg . systemd . enable '' 311exec " ${ pkgs . dbus } /bin/dbus-update-activation-environment --systemd DISPLAY WAYLAND_DISPLAY SWAYSOCK XDG_CURRENT_DESKTOP XDG_SESSION_TYPE NIXOS_OZONE_WL; systemctl --user start sway-session.target"'' ) 312++ ( optional ( ! cfg . xwayland ) "xwayland disable" ) ++ [ cfg . extraConfig ])); 313 314defaultSwayPackage = pkgs . sway . override { 315extraSessionCommands = cfg . extraSessionCommands ; 316extraOptions = cfg . extraOptions ; 317withBaseWrapper = cfg . wrapperFeatures . base ; 318withGtkWrapper = cfg . wrapperFeatures . gtk ; 319}; 320 321in { 322meta . maintainers = with maintainers ; [ 323Scrumplex 324alexarice 325sumnerevans 326sebtm 327oxalica 328]; 329 330imports = let modulePath = [ "wayland" "windowManager" "sway" ]; 331in [ 332( mkRenamedOptionModule ( modulePath ++ [ "systemdIntegration" ]) 333( modulePath ++ [ "systemd" "enable" ])) 334]; 335 336options . wayland . windowManager . sway = { 337enable = mkEnableOption "sway wayland compositor" ; 338 339package = mkOption { 340type = with types ; nullOr package ; 341default = defaultSwayPackage ; 342defaultText = literalExpression " ${ pkgs . sway } " ; 343description = '' 344Sway package to use. Will override the options 345'wrapperFeatures', 'extraSessionCommands', and 'extraOptions'. 346Set to `null` to not add any Sway package to your 347path. This should be done if you want to use the NixOS Sway 348module to install Sway. 349'' ; 350}; 351 352systemd = { 353enable = mkOption { 354type = types . bool ; 355default = pkgs . stdenv . isLinux ; 356example = false ; 357description = '' 358Whether to enable {file}`sway-session.target` on 359sway startup. This links to 360{file}`graphical-session.target`. 361Some important environment variables will be imported to systemd 362and dbus user environment before reaching the target, including 363* {env}`DISPLAY` 364* {env}`WAYLAND_DISPLAY` 365* {env}`SWAYSOCK` 366* {env}`XDG_CURRENT_DESKTOP` 367'' ; 368}; 369 370xdgAutostart = mkEnableOption '' 371autostart of applications using 372{manpage}`systemd-xdg-autostart-generator(8)` 373'' ; 374}; 375 376xwayland = mkOption { 377type = types . bool ; 378default = true ; 379description = '' 380Enable xwayland, which is needed for the default configuration of sway. 381'' ; 382}; 383 384wrapperFeatures = mkOption { 385type = wrapperOptions ; 386default = { }; 387example = { gtk = true ; }; 388description = '' 389Attribute set of features to enable in the wrapper. 390'' ; 391}; 392 393extraSessionCommands = mkOption { 394type = types . lines ; 395default = "" ; 396example = '' 397export SDL_VIDEODRIVER=wayland 398# needs qt5.qtwayland in systemPackages 399export QT_QPA_PLATFORM=wayland 400export QT_WAYLAND_DISABLE_WINDOWDECORATION="1" 401# Fix for some Java AWT applications (e.g. Android Studio), 402# use this if they aren't displayed properly: 403export _JAVA_AWT_WM_NONREPARENTING=1 404'' ; 405description = '' 406Shell commands executed just before Sway is started. 407'' ; 408}; 409 410extraOptions = mkOption { 411type = types . listOf types . str ; 412default = [ ]; 413example = [ 414"--verbose" 415"--debug" 416"--unsupported-gpu" 417"--my-next-gpu-wont-be-nvidia" 418]; 419description = '' 420Command line arguments passed to launch Sway. Please DO NOT report 421issues if you use an unsupported GPU (proprietary drivers). 422'' ; 423}; 424 425config = mkOption { 426type = types . nullOr configModule ; 427default = { }; 428description = "Sway configuration options." ; 429}; 430 431extraConfig = mkOption { 432type = types . lines ; 433default = "" ; 434description = 435"Extra configuration lines to add to ~/.config/sway/config." ; 436}; 437 438extraConfigEarly = mkOption { 439type = types . lines ; 440default = "" ; 441description = 442"Like extraConfig, except lines are added to ~/.config/sway/config before all other configuration." ; 443}; 444}; 445 446config = mkIf cfg . enable ( mkMerge [ 447( mkIf ( cfg . config != null ) { 448warnings = ( optional ( isList cfg . config . fonts ) 449"Specifying sway.config.fonts as a list is deprecated. Use the attrset version instead." ) 450++ flatten ( map ( b : 451optional ( isList b . fonts ) 452"Specifying sway.config.bars[].fonts as a list is deprecated. Use the attrset version instead." ) 453cfg . config . bars ) ++ [ 454( mkIf cfg . config . focus . forceWrapping 455"sway.config.focus.forceWrapping is deprecated, use focus.wrapping instead." ) 456]; 457}) 458 459{ 460assertions = [ 461( hm . assertions . assertPlatform "wayland.windowManager.sway" pkgs 462platforms . linux ) 463]; 464 465home . packages = optional ( cfg . package != null ) cfg . package 466++ optional cfg . xwayland pkgs . xwayland ; 467 468xdg . configFile . "sway/config" = let 469swayPackage = if cfg . package == null then pkgs . sway else cfg . package ; 470in { 471source = configFile ; 472onChange = '' 473swaySocket="''${XDG_RUNTIME_DIR:-/run/user/$UID}/sway-ipc.$UID.$( ${ pkgs . procps } /bin/pgrep --uid $UID -x sway || true).sock" 474if [ -S "$swaySocket" ]; then 475${ swayPackage } /bin/swaymsg -s $swaySocket reload 476fi 477'' ; 478}; 479 480systemd . user . targets . sway-session = mkIf cfg . systemd . enable { 481Unit = { 482Description = "sway compositor session" ; 483Documentation = [ "man:systemd.special(7)" ]; 484BindsTo = [ "graphical-session.target" ]; 485Wants = [ "graphical-session-pre.target" ] 486++ optional cfg . systemd . xdgAutostart "xdg-desktop-autostart.target" ; 487After = [ "graphical-session-pre.target" ]; 488Before = 489optional cfg . systemd . xdgAutostart "xdg-desktop-autostart.target" ; 490}; 491}; 492 493systemd . user . targets . tray = { 494Unit = { 495Description = "Home Manager System Tray" ; 496Requires = [ "graphical-session-pre.target" ]; 497}; 498}; 499} 500]); 501}