fork/home-manager

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

Robert HelgessonMerge PR #421575790441

master
17.4 KiB501 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.wayland.windowManager.sway;
8
9  commonOptions = import ./lib/options.nix {
10    inherit config lib cfg pkgs;
11    moduleName = "sway";
12    capitalModuleName = "Sway";
13  };
14
15  configModule = types.submodule {
16    options = {
17      inherit (commonOptions)
18        fonts window floating focus assigns workspaceLayout
19        workspaceAutoBackAndForth modifier keycodebindings colors bars startup
20        gaps menu terminal defaultWorkspace workspaceOutputAssign;
21
22      left = mkOption {
23        type = types.str;
24        default = "h";
25        description = "Home row direction key for moving left.";
26      };
27
28      down = mkOption {
29        type = types.str;
30        default = "j";
31        description = "Home row direction key for moving down.";
32      };
33
34      up = mkOption {
35        type = types.str;
36        default = "k";
37        description = "Home row direction key for moving up.";
38      };
39
40      right = mkOption {
41        type = types.str;
42        default = "l";
43        description = "Home row direction key for moving right.";
44      };
45
46      keybindings = mkOption {
47        type = types.attrsOf (types.nullOr types.str);
48        default = 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        };
123        defaultText = "Default sway keybindings.";
124        description = ''
125          An attribute set that assigns a key press to an action using a key symbol.
126          See <https://i3wm.org/docs/userguide.html#keybindings>.
127
128          Consider to use `lib.mkOptionDefault` function to extend or override
129          default keybindings instead of specifying all of them from scratch.
130        '';
131        example = literalExpression ''
132          let
133            modifier = config.wayland.windowManager.sway.config.modifier;
134          in 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
142      bindkeysToCode = mkOption {
143        type = types.bool;
144        default = false;
145        example = true;
146        description = ''
147          Whether to make use of {option}`--to-code` in keybindings.
148        '';
149      };
150
151      input = mkOption {
152        type = types.attrsOf (types.attrsOf types.str);
153        default = { };
154        example = { "*" = { xkb_variant = "dvorak"; }; };
155        description = ''
156          An attribute set that defines input modules. See
157          {manpage}`sway-input(5)`
158          for options.
159        '';
160      };
161
162      output = mkOption {
163        type = types.attrsOf (types.attrsOf types.str);
164        default = { };
165        example = { "HDMI-A-2" = { bg = "~/path/to/background.png fill"; }; };
166        description = ''
167          An attribute set that defines output modules. See
168          {manpage}`sway-output(5)`
169          for options.
170        '';
171      };
172
173      seat = mkOption {
174        type = types.attrsOf (types.attrsOf types.str);
175        default = { };
176        example = { "*" = { hide_cursor = "when-typing enable"; }; };
177        description = ''
178          An attribute set that defines seat modules. See
179          {manpage}`sway-input(5)`
180          for options.
181        '';
182      };
183
184      modes = mkOption {
185        type = types.attrsOf (types.attrsOf types.str);
186        default = {
187          resize = {
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        };
200        description = ''
201          An attribute set that defines binding modes and keybindings
202          inside them
203
204          Only basic keybinding is supported (bindsym keycomb action),
205          for more advanced setup use 'sway.extraConfig'.
206        '';
207      };
208    };
209  };
210
211  wrapperOptions = types.submodule {
212    options = let
213      mkWrapperFeature = default: description:
214        mkOption {
215          type = types.bool;
216          inherit default;
217          example = !default;
218          description = "Whether to make use of the ${description}";
219        };
220    in {
221      base = mkWrapperFeature true ''
222        base wrapper to execute extra session commands and prepend a
223        dbus-run-session to the sway command.
224      '';
225      gtk = mkWrapperFeature false ''
226        wrapGAppsHook wrapper to execute sway with required environment
227        variables for GTK applications.
228      '';
229    };
230  };
231
232  commonFunctions = import ./lib/functions.nix {
233    inherit config cfg lib;
234    moduleName = "sway";
235  };
236
237  inherit (commonFunctions)
238    keybindingsStr keycodebindingsStr modeStr assignStr barStr gapsStr
239    floatingCriteriaStr windowCommandsStr colorSetStr windowBorderString
240    fontConfigStr keybindingDefaultWorkspace keybindingsRest workspaceOutputStr;
241
242  startupEntryStr = { command, always, ... }: ''
243    ${if always then "exec_always" else "exec"} ${command}
244  '';
245
246  moduleStr = moduleType: name: attrs: ''
247    ${moduleType} "${name}" {
248    ${concatStringsSep "\n"
249    (mapAttrsToList (name: value: "${name} ${value}") attrs)}
250    }
251  '';
252  inputStr = moduleStr "input";
253  outputStr = moduleStr "output";
254  seatStr = moduleStr "seat";
255
256  configFile = pkgs.writeText "sway.conf" (concatStringsSep "\n"
257    ((optional (cfg.extraConfigEarly != "") cfg.extraConfigEarly)
258      ++ (if cfg.config != null then
259        with 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 ${
269            if builtins.isString (focus.mouseWarping) then
270              focus.mouseWarping
271            else if focus.mouseWarping then
272              "output"
273            else
274              "none"
275          }"
276          "workspace_layout ${workspaceLayout}"
277          "workspace_auto_back_and_forth ${
278            lib.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 {
287            keybindings = keybindingDefaultWorkspace;
288            bindsymArgs =
289              lib.optionalString (cfg.config.bindkeysToCode) "--to-code";
290          })
291          (keybindingsStr {
292            keybindings = keybindingsRest;
293            bindsymArgs =
294              lib.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        )
309      else
310        [ ]) ++ (optional cfg.systemd.enable ''
311          exec "${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
314  defaultSwayPackage = pkgs.sway.override {
315    extraSessionCommands = cfg.extraSessionCommands;
316    extraOptions = cfg.extraOptions;
317    withBaseWrapper = cfg.wrapperFeatures.base;
318    withGtkWrapper = cfg.wrapperFeatures.gtk;
319  };
320
321in {
322  meta.maintainers = with maintainers; [
323    Scrumplex
324    alexarice
325    sumnerevans
326    sebtm
327    oxalica
328  ];
329
330  imports = let modulePath = [ "wayland" "windowManager" "sway" ];
331  in [
332    (mkRenamedOptionModule (modulePath ++ [ "systemdIntegration" ])
333      (modulePath ++ [ "systemd" "enable" ]))
334  ];
335
336  options.wayland.windowManager.sway = {
337    enable = mkEnableOption "sway wayland compositor";
338
339    package = mkOption {
340      type = with types; nullOr package;
341      default = defaultSwayPackage;
342      defaultText = literalExpression "${pkgs.sway}";
343      description = ''
344        Sway package to use. Will override the options
345        'wrapperFeatures', 'extraSessionCommands', and 'extraOptions'.
346        Set to `null` to not add any Sway package to your
347        path. This should be done if you want to use the NixOS Sway
348        module to install Sway.
349      '';
350    };
351
352    systemd = {
353      enable = mkOption {
354        type = types.bool;
355        default = pkgs.stdenv.isLinux;
356        example = false;
357        description = ''
358          Whether to enable {file}`sway-session.target` on
359          sway startup. This links to
360          {file}`graphical-session.target`.
361          Some important environment variables will be imported to systemd
362          and 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
370      xdgAutostart = mkEnableOption ''
371        autostart of applications using
372        {manpage}`systemd-xdg-autostart-generator(8)`
373      '';
374    };
375
376    xwayland = mkOption {
377      type = types.bool;
378      default = true;
379      description = ''
380        Enable xwayland, which is needed for the default configuration of sway.
381      '';
382    };
383
384    wrapperFeatures = mkOption {
385      type = wrapperOptions;
386      default = { };
387      example = { gtk = true; };
388      description = ''
389        Attribute set of features to enable in the wrapper.
390      '';
391    };
392
393    extraSessionCommands = mkOption {
394      type = types.lines;
395      default = "";
396      example = ''
397        export SDL_VIDEODRIVER=wayland
398        # needs qt5.qtwayland in systemPackages
399        export QT_QPA_PLATFORM=wayland
400        export 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:
403        export _JAVA_AWT_WM_NONREPARENTING=1
404      '';
405      description = ''
406        Shell commands executed just before Sway is started.
407      '';
408    };
409
410    extraOptions = mkOption {
411      type = types.listOf types.str;
412      default = [ ];
413      example = [
414        "--verbose"
415        "--debug"
416        "--unsupported-gpu"
417        "--my-next-gpu-wont-be-nvidia"
418      ];
419      description = ''
420        Command line arguments passed to launch Sway. Please DO NOT report
421        issues if you use an unsupported GPU (proprietary drivers).
422      '';
423    };
424
425    config = mkOption {
426      type = types.nullOr configModule;
427      default = { };
428      description = "Sway configuration options.";
429    };
430
431    extraConfig = mkOption {
432      type = types.lines;
433      default = "";
434      description =
435        "Extra configuration lines to add to ~/.config/sway/config.";
436    };
437
438    extraConfigEarly = mkOption {
439      type = types.lines;
440      default = "";
441      description =
442        "Like extraConfig, except lines are added to ~/.config/sway/config before all other configuration.";
443    };
444  };
445
446  config = mkIf cfg.enable (mkMerge [
447    (mkIf (cfg.config != null) {
448      warnings = (optional (isList cfg.config.fonts)
449        "Specifying sway.config.fonts as a list is deprecated. Use the attrset version instead.")
450        ++ flatten (map (b:
451          optional (isList b.fonts)
452          "Specifying sway.config.bars[].fonts as a list is deprecated. Use the attrset version instead.")
453          cfg.config.bars) ++ [
454            (mkIf cfg.config.focus.forceWrapping
455              "sway.config.focus.forceWrapping is deprecated, use focus.wrapping instead.")
456          ];
457    })
458
459    {
460      assertions = [
461        (hm.assertions.assertPlatform "wayland.windowManager.sway" pkgs
462          platforms.linux)
463      ];
464
465      home.packages = optional (cfg.package != null) cfg.package
466        ++ optional cfg.xwayland pkgs.xwayland;
467
468      xdg.configFile."sway/config" = let
469        swayPackage = if cfg.package == null then pkgs.sway else cfg.package;
470      in {
471        source = configFile;
472        onChange = ''
473          swaySocket="''${XDG_RUNTIME_DIR:-/run/user/$UID}/sway-ipc.$UID.$(${pkgs.procps}/bin/pgrep --uid $UID -x sway || true).sock"
474          if [ -S "$swaySocket" ]; then
475            ${swayPackage}/bin/swaymsg -s $swaySocket reload
476          fi
477        '';
478      };
479
480      systemd.user.targets.sway-session = mkIf cfg.systemd.enable {
481        Unit = {
482          Description = "sway compositor session";
483          Documentation = [ "man:systemd.special(7)" ];
484          BindsTo = [ "graphical-session.target" ];
485          Wants = [ "graphical-session-pre.target" ]
486            ++ optional cfg.systemd.xdgAutostart "xdg-desktop-autostart.target";
487          After = [ "graphical-session-pre.target" ];
488          Before =
489            optional cfg.systemd.xdgAutostart "xdg-desktop-autostart.target";
490        };
491      };
492
493      systemd.user.targets.tray = {
494        Unit = {
495          Description = "Home Manager System Tray";
496          Requires = [ "graphical-session-pre.target" ];
497        };
498      };
499    }
500  ]);
501}