about summary refs log tree commit diff
path: root/nixos/modules/programs/wayland/sway.nix
blob: 31821a84a5bd7c851a38db0a71b41c36af3bb8c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
{ config, lib, pkgs, ... }:

let
  cfg = config.programs.sway;

  wayland-lib = import ./lib.nix { inherit lib; };
in
{
  options.programs.sway = {
    enable = lib.mkEnableOption ''
      Sway, the i3-compatible tiling Wayland compositor. You can manually launch
      Sway by executing "exec sway" on a TTY. Copy /etc/sway/config to
      ~/.config/sway/config to modify the default configuration. See
      <https://github.com/swaywm/sway/wiki> and
      "man 5 sway" for more information'';

    package = lib.mkPackageOption pkgs "sway" {
      nullable = true;
      extraDescription = ''
        If the package is not overridable with `extraSessionCommands`, `extraOptions`,
        `withBaseWrapper`, `withGtkWrapper`, `enableXWayland` and `isNixOS`,
        then the module options {option}`wrapperFeatures`, {option}`extraSessionCommands`,
        {option}`extraOptions` and {option}`xwayland` will have no effect.

        Set to `null` to not add any Sway package to your path.
        This should be done if you want to use the Home Manager Sway module to install Sway.
      '';
    } // {
      apply = p: if p == null then null else
        wayland-lib.genFinalPackage p {
          extraSessionCommands = cfg.extraSessionCommands;
          extraOptions = cfg.extraOptions;
          withBaseWrapper = cfg.wrapperFeatures.base;
          withGtkWrapper = cfg.wrapperFeatures.gtk;
          enableXWayland = cfg.xwayland.enable;
          isNixOS = true;
        };
    };

    wrapperFeatures = {
      base = lib.mkEnableOption ''
        the base wrapper to execute extra session commands and prepend a
        dbus-run-session to the sway command'' // { default = true; };
      gtk = lib.mkEnableOption ''
        the wrapGAppsHook wrapper to execute sway with required environment
        variables for GTK applications'';
    };

    extraSessionCommands = lib.mkOption {
      type = lib.types.lines;
      default = "";
      example = ''
        # SDL:
        export SDL_VIDEODRIVER=wayland
        # QT (needs qt5.qtwayland in systemPackages):
        export QT_QPA_PLATFORM=wayland-egl
        export QT_WAYLAND_DISABLE_WINDOWDECORATION="1"
        # Fix for some Java AWT applications (e.g. Android Studio),
        # use this if they aren't displayed properly:
        export _JAVA_AWT_WM_NONREPARENTING=1
      '';
      description = ''
        Shell commands executed just before Sway is started. See
        <https://github.com/swaywm/sway/wiki/Running-programs-natively-under-wayland>
        and <https://github.com/swaywm/wlroots/blob/master/docs/env_vars.md>
        for some useful environment variables.
      '';
    };

    extraOptions = lib.mkOption {
      type = lib.types.listOf lib.types.str;
      default = [];
      example = [
        "--verbose"
        "--debug"
        "--unsupported-gpu"
      ];
      description = ''
        Command line arguments passed to launch Sway. Please DO NOT report
        issues if you use an unsupported GPU (proprietary drivers).
      '';
    };

    xwayland.enable = lib.mkEnableOption "XWayland" // { default = true; };

    extraPackages = lib.mkOption {
      type = with lib.types; listOf package;
      default = with pkgs; [ swaylock swayidle foot dmenu wmenu ];
      defaultText = lib.literalExpression ''
        with pkgs; [ swaylock swayidle foot dmenu wmenu ];
      '';
      example = lib.literalExpression ''
        with pkgs; [ i3status i3status-rust termite rofi light ]
      '';
      description = ''
        Extra packages to be installed system wide. See
        <https://github.com/swaywm/sway/wiki/Useful-add-ons-for-sway> and
        <https://github.com/swaywm/sway/wiki/i3-Migration-Guide#common-x11-apps-used-on-i3-with-wayland-alternatives>
        for a list of useful software.
      '';
    };
  };

  config = lib.mkIf cfg.enable (lib.mkMerge [
    {
      assertions = [
        {
          assertion = cfg.extraSessionCommands != "" -> cfg.wrapperFeatures.base;
          message = ''
            The extraSessionCommands for Sway will not be run if wrapperFeatures.base is disabled.
          '';
        }
      ];

      environment = {
        systemPackages = lib.optional (cfg.package != null) cfg.package ++ cfg.extraPackages;

        # Needed for the default wallpaper:
        pathsToLink = lib.optional (cfg.package != null) "/share/backgrounds/sway";

        etc = {
          "sway/config.d/nixos.conf".source = pkgs.writeText "nixos.conf" ''
            # Import the most important environment variables into the D-Bus and systemd
            # user environments (e.g. required for screen sharing and Pinentry prompts):
            exec dbus-update-activation-environment --systemd DISPLAY WAYLAND_DISPLAY SWAYSOCK XDG_CURRENT_DESKTOP
          '';
        } // lib.optionalAttrs (cfg.package != null) {
          "sway/config".source = lib.mkOptionDefault "${cfg.package}/etc/sway/config";
        };
      };

      programs.gnupg.agent.pinentryPackage = lib.mkDefault pkgs.pinentry-gnome3;

      # To make a Sway session available if a display manager like SDDM is enabled:
      services.displayManager.sessionPackages = lib.optional (cfg.package != null) cfg.package;

      # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1050913
      xdg.portal.config.sway.default = lib.mkDefault [ "wlr" "gtk" ];
    }

    (import ./wayland-session.nix {
      inherit lib pkgs;
      xwayland = cfg.xwayland.enable;
    })
  ]);

  meta.maintainers = with lib.maintainers; [ primeos colemickens ];
}