fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
10.3 KiB270 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.xsession.windowManager.i3;
8
9  commonOptions = import ./lib/options.nix {
10    inherit config lib cfg pkgs;
11    moduleName = "i3";
12  };
13
14  configModule = types.submodule {
15    options = {
16      inherit (commonOptions)
17        fonts window floating focus assigns modifier workspaceLayout
18        workspaceAutoBackAndForth keycodebindings colors bars startup gaps menu
19        terminal defaultWorkspace workspaceOutputAssign;
20
21      keybindings = mkOption {
22        type = types.attrsOf (types.nullOr types.str);
23        default = 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        };
93        defaultText = "Default i3 keybindings.";
94        description = ''
95          An attribute set that assigns a key press to an action using a key symbol.
96          See <https://i3wm.org/docs/userguide.html#keybindings>.
97
98          Consider to use `lib.mkOptionDefault` function to extend or override
99          default keybindings instead of specifying all of them from scratch.
100        '';
101        example = literalExpression ''
102          let
103            modifier = config.xsession.windowManager.i3.config.modifier;
104          in 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
112      modes = mkOption {
113        type = types.attrsOf (types.attrsOf types.str);
114        default = {
115          resize = {
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        };
124        description = ''
125          An attribute set that defines binding modes and keybindings
126          inside them
127
128          Only basic keybinding is supported (bindsym keycomb action),
129          for more advanced setup use 'i3.extraConfig'.
130        '';
131      };
132    };
133  };
134
135  commonFunctions = import ./lib/functions.nix {
136    inherit config cfg lib;
137    moduleName = "i3";
138  };
139
140  inherit (commonFunctions)
141    keybindingsStr keycodebindingsStr modeStr assignStr barStr gapsStr
142    floatingCriteriaStr windowCommandsStr colorSetStr windowBorderString
143    fontConfigStr keybindingDefaultWorkspace keybindingsRest workspaceOutputStr;
144
145  startupEntryStr = { command, always, notification, workspace, ... }:
146    concatStringsSep " " [
147      (if always then "exec_always" else "exec")
148      (if (notification && workspace == null) then "" else "--no-startup-id")
149      (if (workspace == null) then
150        command
151      else
152        "i3-msg 'workspace ${workspace}; exec ${command}'")
153    ];
154
155  configFile = pkgs.writeText "i3.conf" (concatStringsSep "\n"
156    ((if cfg.config != null then
157      with 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 ${
169          lib.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)
186    else
187      [ ]) ++ [ cfg.extraConfig ]));
188
189  # Validates the i3 configuration
190  checkI3Config =
191    pkgs.runCommandLocal "i3-config" { buildInputs = [ cfg.package ]; } ''
192      # We have to make sure the wrapper does not start a dbus session
193      export DBUS_SESSION_BUS_ADDRESS=1
194
195      # A zero exit code means i3 successfully validated the configuration
196      i3 -c ${configFile} -C -d all || {
197        echo "i3 configuration validation failed"
198        echo "For a verbose log of the failure, run 'i3 -c ${configFile} -C -d all'"
199        exit 1
200      };
201      cp ${configFile} $out
202    '';
203
204in {
205  meta.maintainers = with maintainers; [ sumnerevans sebtm ];
206
207  options = {
208    xsession.windowManager.i3 = {
209      enable = mkEnableOption "i3 window manager";
210
211      package = mkPackageOption pkgs "i3" { };
212
213      config = mkOption {
214        type = types.nullOr configModule;
215        default = { };
216        description = "i3 configuration options.";
217      };
218
219      extraConfig = mkOption {
220        type = types.lines;
221        default = "";
222        description =
223          "Extra configuration lines to add to ~/.config/i3/config.";
224      };
225    };
226  };
227
228  config = mkIf cfg.enable (mkMerge [
229    {
230      assertions = [
231        (hm.assertions.assertPlatform "xsession.windowManager.i3" pkgs
232          platforms.linux)
233      ];
234
235      home.packages = [ cfg.package ];
236
237      xsession.windowManager.command = "${cfg.package}/bin/i3";
238
239      xdg.configFile."i3/config" = {
240        source = checkI3Config;
241        onChange = ''
242          # There may be several sockets after log out/log in, but the old ones
243          # will fail with "Connection refused".
244          for i3Socket in ''${XDG_RUNTIME_DIR:-/run/user/$UID}/i3/ipc-socket.*; do
245            if [[ -S $i3Socket ]]; then
246              ${cfg.package}/bin/i3-msg -s $i3Socket reload >/dev/null |& grep -v "Connection refused" || true
247            fi
248          done
249        '';
250      };
251    }
252
253    (mkIf (cfg.config != null) {
254      warnings = (optional (isList cfg.config.fonts)
255        "Specifying i3.config.fonts as a list is deprecated. Use the attrset version instead.")
256        ++ flatten (map (b:
257          optional (isList b.fonts)
258          "Specifying i3.config.bars[].fonts as a list is deprecated. Use the attrset version instead.")
259          cfg.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}