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 . xsession . windowManager . i3 ; 8 9commonOptions = import ./lib/options.nix { 10inherit config lib cfg pkgs ; 11moduleName = "i3" ; 12}; 13 14configModule = types . submodule { 15options = { 16inherit ( commonOptions ) 17fonts window floating focus assigns modifier workspaceLayout 18workspaceAutoBackAndForth keycodebindings colors bars startup gaps menu 19terminal defaultWorkspace workspaceOutputAssign ; 20 21keybindings = mkOption { 22type = types . attrsOf ( types . nullOr types . str ); 23default = mapAttrs ( n :mkOptionDefault ) { 24" ${ cfg . config . modifier } +Return" = "exec ${ cfg . config . terminal } " ; 25" ${ cfg . config . modifier } +Shift+q" = "kill" ; 26" ${ cfg . config . modifier } +d" = "exec ${ cfg . config . menu } " ; 27 28" ${ cfg . config . modifier } +Left" = "focus left" ; 29" ${ cfg . config . modifier } +Down" = "focus down" ; 30" ${ cfg . config . modifier } +Up" = "focus up" ; 31" ${ cfg . config . modifier } +Right" = "focus right" ; 32 33" ${ cfg . config . modifier } +Shift+Left" = "move left" ; 34" ${ cfg . config . modifier } +Shift+Down" = "move down" ; 35" ${ cfg . config . modifier } +Shift+Up" = "move up" ; 36" ${ cfg . config . modifier } +Shift+Right" = "move right" ; 37 38" ${ cfg . config . modifier } +h" = "split h" ; 39" ${ cfg . config . modifier } +v" = "split v" ; 40" ${ cfg . config . modifier } +f" = "fullscreen toggle" ; 41 42" ${ cfg . config . modifier } +s" = "layout stacking" ; 43" ${ cfg . config . modifier } +w" = "layout tabbed" ; 44" ${ cfg . config . modifier } +e" = "layout toggle split" ; 45 46" ${ cfg . config . modifier } +Shift+space" = "floating toggle" ; 47" ${ cfg . config . modifier } +space" = "focus mode_toggle" ; 48 49" ${ cfg . config . modifier } +a" = "focus parent" ; 50 51" ${ cfg . config . modifier } +Shift+minus" = "move scratchpad" ; 52" ${ cfg . config . modifier } +minus" = "scratchpad show" ; 53 54" ${ cfg . config . modifier } +1" = "workspace number 1" ; 55" ${ cfg . config . modifier } +2" = "workspace number 2" ; 56" ${ cfg . config . modifier } +3" = "workspace number 3" ; 57" ${ cfg . config . modifier } +4" = "workspace number 4" ; 58" ${ cfg . config . modifier } +5" = "workspace number 5" ; 59" ${ cfg . config . modifier } +6" = "workspace number 6" ; 60" ${ cfg . config . modifier } +7" = "workspace number 7" ; 61" ${ cfg . config . modifier } +8" = "workspace number 8" ; 62" ${ cfg . config . modifier } +9" = "workspace number 9" ; 63" ${ cfg . config . modifier } +0" = "workspace number 10" ; 64 65" ${ cfg . config . modifier } +Shift+1" = 66"move container to workspace number 1" ; 67" ${ cfg . config . modifier } +Shift+2" = 68"move container to workspace number 2" ; 69" ${ cfg . config . modifier } +Shift+3" = 70"move container to workspace number 3" ; 71" ${ cfg . config . modifier } +Shift+4" = 72"move container to workspace number 4" ; 73" ${ cfg . config . modifier } +Shift+5" = 74"move container to workspace number 5" ; 75" ${ cfg . config . modifier } +Shift+6" = 76"move container to workspace number 6" ; 77" ${ cfg . config . modifier } +Shift+7" = 78"move container to workspace number 7" ; 79" ${ cfg . config . modifier } +Shift+8" = 80"move container to workspace number 8" ; 81" ${ cfg . config . modifier } +Shift+9" = 82"move container to workspace number 9" ; 83" ${ cfg . config . modifier } +Shift+0" = 84"move container to workspace number 10" ; 85 86" ${ cfg . config . modifier } +Shift+c" = "reload" ; 87" ${ cfg . config . modifier } +Shift+r" = "restart" ; 88" ${ cfg . config . modifier } +Shift+e" = 89"exec i3-nagbar -t warning -m 'Do you want to exit i3?' -b 'Yes' 'i3-msg exit'" ; 90 91" ${ cfg . config . modifier } +r" = "mode resize" ; 92}; 93defaultText = "Default i3 keybindings." ; 94description = '' 95An attribute set that assigns a key press to an action using a key symbol. 96See <https://i3wm.org/docs/userguide.html#keybindings>. 97 98Consider to use `lib.mkOptionDefault` function to extend or override 99default keybindings instead of specifying all of them from scratch. 100'' ; 101example = literalExpression '' 102let 103modifier = config.xsession.windowManager.i3.config.modifier; 104in lib.mkOptionDefault { 105"''${modifier}+Return" = "exec i3-sensible-terminal"; 106"''${modifier}+Shift+q" = "kill"; 107"''${modifier}+d" = "exec ''${pkgs.dmenu}/bin/dmenu_run"; 108} 109'' ; 110}; 111 112modes = mkOption { 113type = types . attrsOf ( types . attrsOf types . str ); 114default = { 115resize = { 116"Left" = "resize shrink width 10 px or 10 ppt" ; 117"Down" = "resize grow height 10 px or 10 ppt" ; 118"Up" = "resize shrink height 10 px or 10 ppt" ; 119"Right" = "resize grow width 10 px or 10 ppt" ; 120"Escape" = "mode default" ; 121"Return" = "mode default" ; 122}; 123}; 124description = '' 125An attribute set that defines binding modes and keybindings 126inside them 127 128Only basic keybinding is supported (bindsym keycomb action), 129for more advanced setup use 'i3.extraConfig'. 130'' ; 131}; 132}; 133}; 134 135commonFunctions = import ./lib/functions.nix { 136inherit config cfg lib ; 137moduleName = "i3" ; 138}; 139 140inherit ( commonFunctions ) 141keybindingsStr keycodebindingsStr modeStr assignStr barStr gapsStr 142floatingCriteriaStr windowCommandsStr colorSetStr windowBorderString 143fontConfigStr keybindingDefaultWorkspace keybindingsRest workspaceOutputStr ; 144 145startupEntryStr = { command , always , notification , workspace , ...} : 146concatStringsSep " " [ 147( if always then "exec_always" else "exec" ) 148( if ( notification && workspace == null ) then "" else "--no-startup-id" ) 149( if ( workspace == null ) then 150command 151else 152"i3-msg 'workspace ${ workspace } ; exec ${ command } '" ) 153]; 154 155configFile = pkgs . writeText "i3.conf" ( concatStringsSep "\n" 156(( if cfg . config != null then 157with cfg . config ; 158([ 159( fontConfigStr fonts ) 160"floating_modifier ${ floating . modifier } " 161( windowBorderString window floating ) 162"hide_edge_borders ${ window . hideEdgeBorders } " 163"focus_wrapping ${ focus . wrapping } " 164"focus_follows_mouse ${ lib . hm . booleans . yesNo focus . followMouse } " 165"focus_on_window_activation ${ focus . newWindow } " 166"mouse_warping ${ if focus . mouseWarping then "output" else "none" } " 167"workspace_layout ${ workspaceLayout } " 168"workspace_auto_back_and_forth ${ 169lib . hm . booleans . yesNo workspaceAutoBackAndForth 170} " 171"client.focused ${ colorSetStr colors . focused } " 172"client.focused_inactive ${ colorSetStr colors . focusedInactive } " 173"client.unfocused ${ colorSetStr colors . unfocused } " 174"client.urgent ${ colorSetStr colors . urgent } " 175"client.placeholder ${ colorSetStr colors . placeholder } " 176"client.background ${ colors . background } " 177( keybindingsStr { keybindings = keybindingDefaultWorkspace ; }) 178( keybindingsStr { keybindings = keybindingsRest ; }) 179( keycodebindingsStr keycodebindings ) 180] ++ mapAttrsToList ( modeStr false ) modes 181++ mapAttrsToList assignStr assigns ++ map barStr bars 182++ optional ( gaps != null ) gapsStr 183++ map floatingCriteriaStr floating . criteria 184++ map windowCommandsStr window . commands ++ map startupEntryStr startup 185++ map workspaceOutputStr workspaceOutputAssign ) 186else 187[ ]) ++ [ cfg . extraConfig ])); 188 189# Validates the i3 configuration 190checkI3Config = 191pkgs . runCommandLocal "i3-config" { buildInputs = [ cfg . package ]; } '' 192# We have to make sure the wrapper does not start a dbus session 193export DBUS_SESSION_BUS_ADDRESS =1 194 195# A zero exit code means i3 successfully validated the configuration 196i3 -c ${ configFile } -C -d all || { 197echo "i3 configuration validation failed" 198echo "For a verbose log of the failure, run 'i3 -c ${ configFile } -C -d all'" 199exit 1 200}; 201cp ${ configFile } $ out 202'' ; 203 204in { 205meta . maintainers = with maintainers ; [ sumnerevans sebtm ]; 206 207options = { 208xsession . windowManager . i3 = { 209enable = mkEnableOption "i3 window manager" ; 210 211package = mkPackageOption pkgs "i3" { }; 212 213config = mkOption { 214type = types . nullOr configModule ; 215default = { }; 216description = "i3 configuration options." ; 217}; 218 219extraConfig = mkOption { 220type = types . lines ; 221default = "" ; 222description = 223"Extra configuration lines to add to ~/.config/i3/config." ; 224}; 225}; 226}; 227 228config = mkIf cfg . enable ( mkMerge [ 229{ 230assertions = [ 231( hm . assertions . assertPlatform "xsession.windowManager.i3" pkgs 232platforms . linux ) 233]; 234 235home . packages = [ cfg . package ]; 236 237xsession . windowManager . command = " ${ cfg . package } /bin/i3" ; 238 239xdg . configFile . "i3/config" = { 240source = checkI3Config ; 241onChange = '' 242# There may be several sockets after log out/log in, but the old ones 243# will fail with "Connection refused". 244for i3Socket in ''${XDG_RUNTIME_DIR:-/run/user/$UID}/i3/ipc-socket.*; do 245if [[ -S $i3Socket ]]; then 246${ cfg . package } /bin/i3-msg -s $i3Socket reload >/dev/null |& grep -v "Connection refused" || true 247fi 248done 249'' ; 250}; 251} 252 253( mkIf ( cfg . config != null ) { 254warnings = ( optional ( isList cfg . config . fonts ) 255"Specifying i3.config.fonts as a list is deprecated. Use the attrset version instead." ) 256++ flatten ( map ( b : 257optional ( isList b . fonts ) 258"Specifying i3.config.bars[].fonts as a list is deprecated. Use the attrset version instead." ) 259cfg . config . bars ) ++ [ 260( mkIf ( any ( s :s . workspace != null ) cfg . config . startup ) 261( "'xsession.windowManager.i3.config.startup.*.workspace' is deprecated, " 262+ "use 'xsession.windowManager.i3.config.assigns' instead." 263+ "See https://github.com/nix-community/home-manager/issues/265." )) 264( mkIf cfg . config . focus . forceWrapping 265( "'xsession.windowManager.i3.config.focus.forceWrapping' is deprecated, " 266+ "use 'xsession.windowManager.i3.config.focus.wrapping' instead." )) 267]; 268}) 269]); 270}