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 swayPackage = 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 wrapperFeatures = mkOption {
46 type = wrapperOptions;
47 default = { };
48 example = { gtk = true; };
49 description = lib.mdDoc ''
50 Attribute set of features to enable in the wrapper.
51 '';
52 };
53
54 extraSessionCommands = mkOption {
55 type = types.lines;
56 default = "";
57 example = ''
58 # SDL:
59 export SDL_VIDEODRIVER=wayland
60 # QT (needs qt5.qtwayland in systemPackages):
61 export QT_QPA_PLATFORM=wayland-egl
62 export QT_WAYLAND_DISABLE_WINDOWDECORATION="1"
63 # Fix for some Java AWT applications (e.g. Android Studio),
64 # use this if they aren't displayed properly:
65 export _JAVA_AWT_WM_NONREPARENTING=1
66 '';
67 description = lib.mdDoc ''
68 Shell commands executed just before Sway is started. See
69 <https://github.com/swaywm/sway/wiki/Running-programs-natively-under-wayland>
70 and <https://github.com/swaywm/wlroots/blob/master/docs/env_vars.md>
71 for some useful environment variables.
72 '';
73 };
74
75 extraOptions = mkOption {
76 type = types.listOf types.str;
77 default = [];
78 example = [
79 "--verbose"
80 "--debug"
81 "--unsupported-gpu"
82 ];
83 description = lib.mdDoc ''
84 Command line arguments passed to launch Sway. Please DO NOT report
85 issues if you use an unsupported GPU (proprietary drivers).
86 '';
87 };
88
89 extraPackages = mkOption {
90 type = with types; listOf package;
91 default = with pkgs; [
92 swaylock swayidle foot dmenu
93 ];
94 defaultText = literalExpression ''
95 with pkgs; [ swaylock swayidle foot dmenu ];
96 '';
97 example = literalExpression ''
98 with pkgs; [
99 i3status i3status-rust
100 termite rofi light
101 ]
102 '';
103 description = lib.mdDoc ''
104 Extra packages to be installed system wide. See
105 <https://github.com/swaywm/sway/wiki/Useful-add-ons-for-sway> and
106 <https://github.com/swaywm/sway/wiki/i3-Migration-Guide#common-x11-apps-used-on-i3-with-wayland-alternatives>
107 for a list of useful software.
108 '';
109 };
110
111 };
112
113 config = mkIf cfg.enable {
114 assertions = [
115 {
116 assertion = cfg.extraSessionCommands != "" -> cfg.wrapperFeatures.base;
117 message = ''
118 The extraSessionCommands for Sway will not be run if
119 wrapperFeatures.base is disabled.
120 '';
121 }
122 ];
123 environment = {
124 systemPackages = [ swayPackage ] ++ cfg.extraPackages;
125 # Needed for the default wallpaper:
126 pathsToLink = [ "/share/backgrounds/sway" ];
127 etc = {
128 "sway/config".source = mkOptionDefault "${swayPackage}/etc/sway/config";
129 "sway/config.d/nixos.conf".source = pkgs.writeText "nixos.conf" ''
130 # Import the most important environment variables into the D-Bus and systemd
131 # user environments (e.g. required for screen sharing and Pinentry prompts):
132 exec dbus-update-activation-environment --systemd DISPLAY WAYLAND_DISPLAY SWAYSOCK XDG_CURRENT_DESKTOP
133 '';
134 };
135 };
136 security.polkit.enable = true;
137 security.pam.services.swaylock = {};
138 hardware.opengl.enable = mkDefault true;
139 fonts.enableDefaultFonts = mkDefault true;
140 programs.dconf.enable = mkDefault true;
141 # To make a Sway session available if a display manager like SDDM is enabled:
142 services.xserver.displayManager.sessionPackages = [ swayPackage ];
143 programs.xwayland.enable = mkDefault true;
144 # For screen sharing (this option only has an effect with xdg.portal.enable):
145 xdg.portal.extraPortals = [ pkgs.xdg-desktop-portal-wlr ];
146 };
147
148 meta.maintainers = with lib.maintainers; [ primeos colemickens ];
149}