1{ config, pkgs, lib, ... }:
2
3with lib;
4
5let
6 cfg = config.programs.sway;
7
8 wrapperOptions = types.submodule {
9 options =
10 let
11 mkWrapperFeature = default: description: mkOption {
12 type = types.bool;
13 inherit default;
14 example = !default;
15 description = lib.mdDoc "Whether to make use of the ${description}";
16 };
17 in {
18 base = mkWrapperFeature true ''
19 base wrapper to execute extra session commands and prepend a
20 dbus-run-session to the sway command.
21 '';
22 gtk = mkWrapperFeature false ''
23 wrapGAppsHook wrapper to execute sway with required environment
24 variables for GTK applications.
25 '';
26 };
27 };
28
29 defaultSwayPackage = pkgs.sway.override {
30 extraSessionCommands = cfg.extraSessionCommands;
31 extraOptions = cfg.extraOptions;
32 withBaseWrapper = cfg.wrapperFeatures.base;
33 withGtkWrapper = cfg.wrapperFeatures.gtk;
34 isNixOS = true;
35 };
36in {
37 options.programs.sway = {
38 enable = mkEnableOption (lib.mdDoc ''
39 Sway, the i3-compatible tiling Wayland compositor. You can manually launch
40 Sway by executing "exec sway" on a TTY. Copy /etc/sway/config to
41 ~/.config/sway/config to modify the default configuration. See
42 <https://github.com/swaywm/sway/wiki> and
43 "man 5 sway" for more information'');
44
45 package = mkOption {
46 type = with types; nullOr package;
47 default = defaultSwayPackage;
48 defaultText = literalExpression "pkgs.sway";
49 description = lib.mdDoc ''
50 Sway package to use. Will override the options
51 'wrapperFeatures', 'extraSessionCommands', and 'extraOptions'.
52 Set to <code>null</code> to not add any Sway package to your
53 path. This should be done if you want to use the Home Manager Sway
54 module to install Sway.
55 '';
56 };
57
58 wrapperFeatures = mkOption {
59 type = wrapperOptions;
60 default = { };
61 example = { gtk = true; };
62 description = lib.mdDoc ''
63 Attribute set of features to enable in the wrapper.
64 '';
65 };
66
67 extraSessionCommands = mkOption {
68 type = types.lines;
69 default = "";
70 example = ''
71 # SDL:
72 export SDL_VIDEODRIVER=wayland
73 # QT (needs qt5.qtwayland in systemPackages):
74 export QT_QPA_PLATFORM=wayland-egl
75 export QT_WAYLAND_DISABLE_WINDOWDECORATION="1"
76 # Fix for some Java AWT applications (e.g. Android Studio),
77 # use this if they aren't displayed properly:
78 export _JAVA_AWT_WM_NONREPARENTING=1
79 '';
80 description = lib.mdDoc ''
81 Shell commands executed just before Sway is started. See
82 <https://github.com/swaywm/sway/wiki/Running-programs-natively-under-wayland>
83 and <https://github.com/swaywm/wlroots/blob/master/docs/env_vars.md>
84 for some useful environment variables.
85 '';
86 };
87
88 extraOptions = mkOption {
89 type = types.listOf types.str;
90 default = [];
91 example = [
92 "--verbose"
93 "--debug"
94 "--unsupported-gpu"
95 ];
96 description = lib.mdDoc ''
97 Command line arguments passed to launch Sway. Please DO NOT report
98 issues if you use an unsupported GPU (proprietary drivers).
99 '';
100 };
101
102 extraPackages = mkOption {
103 type = with types; listOf package;
104 default = with pkgs; [
105 swaylock swayidle foot dmenu
106 ];
107 defaultText = literalExpression ''
108 with pkgs; [ swaylock swayidle foot dmenu ];
109 '';
110 example = literalExpression ''
111 with pkgs; [
112 i3status i3status-rust
113 termite rofi light
114 ]
115 '';
116 description = lib.mdDoc ''
117 Extra packages to be installed system wide. See
118 <https://github.com/swaywm/sway/wiki/Useful-add-ons-for-sway> and
119 <https://github.com/swaywm/sway/wiki/i3-Migration-Guide#common-x11-apps-used-on-i3-with-wayland-alternatives>
120 for a list of useful software.
121 '';
122 };
123
124 };
125
126 config = mkIf cfg.enable {
127 assertions = [
128 {
129 assertion = cfg.extraSessionCommands != "" -> cfg.wrapperFeatures.base;
130 message = ''
131 The extraSessionCommands for Sway will not be run if
132 wrapperFeatures.base is disabled.
133 '';
134 }
135 ];
136 environment = {
137 systemPackages = optional (cfg.package != null) cfg.package ++ cfg.extraPackages;
138 # Needed for the default wallpaper:
139 pathsToLink = optionals (cfg.package != null) [ "/share/backgrounds/sway" ];
140 etc = {
141 "sway/config.d/nixos.conf".source = pkgs.writeText "nixos.conf" ''
142 # Import the most important environment variables into the D-Bus and systemd
143 # user environments (e.g. required for screen sharing and Pinentry prompts):
144 exec dbus-update-activation-environment --systemd DISPLAY WAYLAND_DISPLAY SWAYSOCK XDG_CURRENT_DESKTOP
145 '';
146 } // optionalAttrs (cfg.package != null) {
147 "sway/config".source = mkOptionDefault "${cfg.package}/etc/sway/config";
148 };
149 };
150 security.polkit.enable = true;
151 security.pam.services.swaylock = {};
152 hardware.opengl.enable = mkDefault true;
153 fonts.enableDefaultFonts = mkDefault true;
154 programs.dconf.enable = mkDefault true;
155 # To make a Sway session available if a display manager like SDDM is enabled:
156 services.xserver.displayManager.sessionPackages = optionals (cfg.package != null) [ cfg.package ];
157 programs.xwayland.enable = mkDefault true;
158 # For screen sharing (this option only has an effect with xdg.portal.enable):
159 xdg.portal.extraPortals = [ pkgs.xdg-desktop-portal-wlr ];
160 };
161
162 meta.maintainers = with lib.maintainers; [ primeos colemickens ];
163}