Sway: Difference between revisions

add instructions on how to fix issues with systemd user services not getting the environment
Phobos (talk | contribs)
mNo edit summary
 
(23 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Sway is a tiling Wayland compositor and a drop-in replacement for the i3 window manager for X11. It works with your existing i3 configuration and supports most of i3's features, plus a few extras.  
[https://swaywm.org/ Sway] is a tiling [[Wayland]] compositor and a drop-in replacement for the [[i3]] window manager for X11. It can work with an existing i3 configuration and supports most of i3's features, plus a few extras. For users migrating from i3, see the [https://github.com/swaywm/sway/wiki/i3-Migration-Guide i3 migration guide].
[https://github.com/swaywm/sway/wiki/i3-Migration-Guide i3 migration guide]


== Setup ==
== Setup ==
Line 7: Line 6:
=== Using NixOS ===
=== Using NixOS ===
Here is a minimal configuration:
Here is a minimal configuration:
<syntaxhighlight lang="nix">
 
{ config, pkgs, lib, ... }:
{{File|3={ config, pkgs, lib, ... }:
{
{
   environment.systemPackages = with pkgs; [
   environment.systemPackages = with pkgs; [
    grim # screenshot functionality
     wl-clipboard # Copy/Paste functionality.
    slurp # screenshot functionality
     mako # Notification utility.
     wl-clipboard # wl-copy and wl-paste for copy/paste from stdin / stdout
     mako # notification system developed by swaywm maintainer
   ];
   ];


   # Enable the gnome-keyring secrets vault.
   # Enables Gnome Keyring to store secrets for applications.  
  # Will be exposed through DBus to programs willing to store secrets.
   services.gnome.gnome-keyring.enable = true;
   services.gnome.gnome-keyring.enable = true;


   # enable Sway window manager
   # Enable Sway.
   programs.sway = {
   programs.sway = {
     enable = true;
     enable = true;
     wrapperFeatures.gtk = true;
     wrapperFeatures.gtk = true;
   };
   };
}
}|name=/etc/nixos/configuration.nix|lang=nix}}
</syntaxhighlight>By default, the Sway module in NixOS comes with a set of extra packages, including the <code>foot</code> terminal, <code>swayidle</code>, <code>swaylock</code>, and <code>wmenu</code>, which can be configured under <code>[https://search.nixos.org/options?show=programs.sway.extraPackages programs.sway.extraPackages]</code> option. You may also want to include <code>wl-clipboard</code> for clipboard functionality and <code>slurp</code> for screenshot region selection. Additionally, for a more customizable bar implementation than <code>sway-bar</code>, <code>waybar</code> can be enabled with <code>programs.waybar.enable</code>.
 
By default, the Sway module in NixOS comes with a set of extra packages, including [https://codeberg.org/dnkl/foot/ Foot] terminal, [[Swayidle]], [[Swaylock]], and [https://codeberg.org/adnano/wmenu/ wmenu], which may be configured under the <code>[https://search.nixos.org/options?show=programs.sway.extraPackages programs.sway.extraPackages]</code> option. You may also want to include <code>wl-clipboard</code> for clipboard functionality, as well as a screenshot utility such as [https://github.com/emersion/slurp Slurp] or [[Flameshot]] for screenshot region selection.  
 
Additionally, for a more customizable bar implementation than <code>sway-bar</code>, [[Waybar]] may be enabled with <code>programs.waybar.enable</code>.


The default Sway configuration is symlinked to <code>/etc/sway/config</code> and <code>/etc/sway/config.d/nixos.conf</code>. The latter file contains dbus and systemd configuration that is critical to using apps that depend on XDG desktop portals with Sway, and should be included in any custom configuration files.
The default Sway configuration is symlinked to <code>/etc/sway/config</code> and <code>/etc/sway/config.d/nixos.conf</code>. The latter file contains dbus and systemd configuration that is critical to using apps that depend on XDG desktop portals with Sway, and should be included in any custom configuration files.
Line 36: Line 35:


=== Using Home Manager ===
=== Using Home Manager ===
To set up Sway using [[Home Manager]], first you must enable [[Polkit]] in your NixOS configuration:
To set up Sway using [[Home Manager]], you must first enable [[Polkit]] in your NixOS configuration.
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
security.polkit.enable = true;
security.polkit.enable = true;
</nowiki>}}
</nowiki>}}
   
   
Then you can enable Sway in your home manager configuration. Here is a minimal example:
Then you may enable Sway in your Home Manager configuration. Here is a minimal example:
<syntaxhighlight lang="nix>
 
  wayland.windowManager.sway = {
{{File|3=wayland.windowManager.sway = {
     enable = true;
     enable = true;
     wrapperFeatures.gtk = true; # Fixes common issues with GTK 3 apps
     wrapperFeatures.gtk = true; # Fixes common issues with GTK 3 apps
Line 55: Line 54:
       ];
       ];
     };
     };
   };
   };|name=/etc/nixos/home.nix|lang=nix}}
</syntaxhighlight>


See [https://nix-community.github.io/home-manager/options.xhtml#opt-wayland.windowManager.sway.enable Home Manager's Options for Sway] for a complete list of configuration options.
See [https://nix-community.github.io/home-manager/options.xhtml#opt-wayland.windowManager.sway.enable Home Manager's Options for Sway] for a complete list of configuration options.


You might need to active dbus manually from .zshrc to use i.e: dunst, see [https://discourse.nixos.org/t/dunst-crashes-if-run-as-service/27671/2 Dunst crashes if run as service]
You may need to activate dbus manually from .zshrc to use i.e: dunst, see [https://discourse.nixos.org/t/dunst-crashes-if-run-as-service/27671/2 Dunst crashes if run as service]
 
{{Note|
It's recommended to enable a [[Secret Service]] provider, like GNOME Keyring:
{{file|home.nix|nix|<nowiki>
services.gnome-keyring.enable = true;
</nowiki>}}
}}


=== Systemd services ===
=== Systemd services ===
Kanshi is an output configuration daemon. As explained above, we don't run Sway itself as a systemd service. There are auxiliary daemons that we do want to run as systemd services, for example Kanshi [https://sr.ht/~emersion/kanshi/], which implements monitor hot swapping. It would be enabled as follows:
Kanshi is an output configuration daemon. As explained above, we don't run Sway itself as a systemd service. There are auxiliary daemons that we do want to run as systemd services, for example Kanshi [https://gitlab.freedesktop.org/emersion/kanshi], which implements monitor hot swapping. It would be enabled as follows:
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
   # kanshi systemd service
   # kanshi systemd service
Line 92: Line 83:
When you launch Sway, the systemd service is started.  
When you launch Sway, the systemd service is started.  


=== Using greeter ===
=== Using a greeter ===
Installing a greeter based on [https://search.nixos.org/options?channel=unstable&show=services.greetd.settings&from=0&size=50&sort=relevance&type=packages&query=greetd greetd] is the most straightforward way to launch Sway.
Installing a greeter based on [[greetd]] is the most straightforward way to launch Sway.


Tuigreet does not even need a separate compositor to launch.
==== TUIGreet ====
Tuigreet is a simple and lightweight option that does not require a separate compositor to launch.


{{file|||<nowiki>
{{file|||<nowiki>
Line 108: Line 100:
};                                                                       
};                                                                       
</nowiki>|name=/etc/nixos/configuration.nix|lang=nix}}
</nowiki>|name=/etc/nixos/configuration.nix|lang=nix}}
==== Regreet ====
[https://github.com/rharish101/ReGreet Regreet] is a clean and customizable GTK-based greeter written in Rust. It will automatically find Sway and remembers the last picked option. Additional configuration options may be found under [https://search.nixos.org/options?&query=regreet programs.regreet].
{{File|3=programs.regreet.enable = true;|name=/etc/nixos/configuration.nix|lang=nix}}


=== Automatic startup on boot ===
=== Automatic startup on boot ===
The snippet below will start Sway immediately on startup, without a greeter and '''without login prompt'''. Only consider using this in conjunction with [[Full Disk Encryption]]!
The snippet below will start Sway immediately on startup, without a greeter and '''without a login prompt'''. Only consider using this in conjunction with [[Full Disk Encryption]].


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 120: Line 116:
     [[ "$(tty)" == /dev/tty1 ]] && sway
     [[ "$(tty)" == /dev/tty1 ]] && sway
'';
'';
</syntaxhighlight>When launched directly from the TTY sway won't inherit the user environment. This can cause issues with systemd user services such as application launchers or swayidle. To fix this add the following to your home-manager configuration:{{file|home.nix|nix|<nowiki>
</syntaxhighlight>When launched directly from the TTY, Sway will not inherit the user environment. This may cause issues with systemd user services such as application launchers or [[Swayidle]]. To fix this, add the following to your Home Manager configuration:{{file|home.nix|nix|<nowiki>
  wayland.windowManager.sway.systemd.variables = ["--all"];
  wayland.windowManager.sway.systemd.variables = ["--all"];
</nowiki>}}
</nowiki>}}
=== Secret Service ===
It is recommended to enable a secret service provider such as [https://wiki.gnome.org/Projects/GnomeKeyring Gnome Keyring]. For more information on secret services check the [[Secret Service]] page.
Install and enable:
{{File|3=services.gnome.gnome-keyring.enable = true;|name=/etc/nixos/configuration.nix|lang=nix}}
In order to unlock the keyring through logins from greeters and screen locking utilities you will need to enable them through PAM.
{{File|3=security.pam.services = {
  greetd.enableGnomeKeyring = true;
  swaylock.enableGnomeKeyring = true;
  # If using a display manager such as GDM
  #gdm.enableGnomeKeyring = true;
};|name=/etc/nixos/configuration.nix|lang=nix}}


== Configuration ==
== Configuration ==
Sway can be configured for specific users using Home-Manager or manually through configuration files. Default is <code>/etc/sway/config</code> and custom user configuration in <code>~/.config/sway/config</code>.
Sway may be configured for specific users using Home Manager or manually through configuration files. The default location is <code>/etc/sway/config</code>, and custom user configuration in <code>~/.config/sway/config</code>.


=== Keyboard layout ===
=== Keyboard layout ===
Line 140: Line 150:


=== Brightness and volume ===
=== Brightness and volume ===
You can set up brightness and volume function keys as follows by binding the key codes to their corresponding commands in your sway config. The following configurations accomplish this using <code>light</code> and <code>pulseaudio</code>:
You may set the brightness and volume function keys by binding the key codes to their corresponding commands within your sway config. The following configurations accomplish this using <code>light</code> and <code>pulseaudio</code>:
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
users.users.yourusername.extraGroups = [ "video" ];
users.users.yourusername.extraGroups = [ "video" ];
Line 147: Line 157:
</nowiki>}}
</nowiki>}}


{{file|sway config|bash|
{{file|||


# Brightness
# Brightness
Line 157: Line 167:
bindsym XF86AudioLowerVolume exec 'pactl set-sink-volume @DEFAULT_SINK@ -1%'
bindsym XF86AudioLowerVolume exec 'pactl set-sink-volume @DEFAULT_SINK@ -1%'
bindsym XF86AudioMute exec 'pactl set-sink-mute @DEFAULT_SINK@ toggle'
bindsym XF86AudioMute exec 'pactl set-sink-mute @DEFAULT_SINK@ toggle'
}}
|name=Sway Config|lang=bash}}Or alternatively in Home Manager:
{{File|3=wayland.windowManager.sway = {
  enable = true;
  config = {
    keybindings = {
      # Brightness Controls
      "XF86MonBrightnessDown" = "exec light -U 10";
      "XF86MonBrightnessUp" = "exec light -A 10";
       
      # Volume Controls
      "XF86AudioRaiseVolume" = "exec pactl set-sink-volume @DEFAULT_SINK@ +1%";
      "XF86AudioLowerVolume" = "exec pactl set-sink-volume @DEFAULT_SINK@ -1%";
      "XF86AudioMute" = "exec pactl set-sink-mute @DEFAULT_SINK@ toggle";
    };
  };
};|name=/etc/nixos/home.nix|lang=nix}}For an on screen display for audio and brightness, check [[swayosd]].
 
=== Input ===
 
==== Touchpad ====
See the [https://www.mankier.com/5/sway-input sway-input man page] for options.
{{File|3=wayland.windowManager.sway =
  {
    enable = true;
    config.input = {
      "type:touchpad" = {
        # Enables or disables tap for specified input device.
        tap = "enabled";
        # Enables or disables natural (inverted) scrolling for the specified input device.
        natural_scroll = "enabled";
        # Enables or disables disable-while-typing for the specified input device.
        dwt = "enabled";
      };
    };
  };|name=/etc/nixos/home.nix|lang=nix}}


== Troubleshooting ==
== Troubleshooting ==
Line 175: Line 219:


In ~/.config/sway/config
In ~/.config/sway/config
<syntaxhighlight>
<syntaxhighlight>seat "*" xcursor_theme Adwaita 32</syntaxhighlight>
seat "*" xcursor_theme Adwaita 32
</syntaxhighlight>


==== With Home Manager ====
==== With Home Manager ====


Using [[Home Manager]] try configuring a general mouse cursor size and theme. The reason that your cursor might be missing in some applications, is because <code>XCURSOR_THEME</code>is missing, which will cause applications relying on <code>XWAYLAND</code> to misbehave. Setting <code>sway.enable = true</code>, combined with the <code>name</code>, <code>package</code> and size will set the correct environment variables, which sway will then use.
Using [[Home Manager]] you may configure the mouse cursor size and theme. The reason that your cursor might be missing in some applications, is because <code>XCURSOR_THEME</code> is missing, which will cause applications relying on <code>XWAYLAND</code> to misbehave. Setting <code>sway.enable = true</code>, combined with the <code>name</code>, <code>package</code> and size will set the correct environment variables, which sway will then use.


<syntaxhighlight lang="nix">home-manager.users.myUser = {
{{File|3=home.pointerCursor = {
     home.pointerCursor = {
     name = "Adwaita";
      name = "Adwaita";
    package = pkgs.adwaita-icon-theme;
      package = pkgs.adwaita-icon-theme;
    size = 24;
      size = 24;
    x11 = {
      x11 = {
      enable = true;
        enable = true;
      defaultCursor = "Adwaita";
        defaultCursor = "Adwaita";
      };
      sway.enable = true;
     };
     };
};</syntaxhighlight>


Replace <code>myUser</code> with your user running the graphical environment.
  sway.enable = true;
 
};|name=/etc/nixos/home.nix|lang=nix}}


=== Missing fonts on Xorg applications ===
=== Missing fonts in Xorg applications ===


If fonts for certain languages are missing in Xorg applications (e.g. Japanese fonts don't appear in Discord) even though they're in the system, you can set them as default fonts in your configuration file.
If fonts for certain languages are missing in Xorg applications (e.g. Japanese fonts don't appear in Discord) even though they're in the system, you can set them as default fonts in your configuration file.


<syntaxhighlight lang="nix>
<syntaxhighlight lang="nix>
Line 246: Line 286:
=== WLR Error when trying to launch Sway ===
=== WLR Error when trying to launch Sway ===


When this happens on a new nixos system, enabling opengl in configuration.nix may fix this issue.   
When this happens on a new NixOS system, enabling OpenGL in configuration.nix may fix this issue.   


{{Note|<code>hardware.opengl</code> was renamed to <code>hardware.graphics</code> in NixOS 24.11.}}
{{Note|<code>hardware.opengl</code> was renamed to <code>hardware.graphics</code> in NixOS 24.11.}}
Line 253: Line 293:
hardware.graphics.enable = true;
hardware.graphics.enable = true;
</syntaxhighlight>
</syntaxhighlight>
=== Systemd user services missing environment variables (PATH, etc) ===
When sway is launched with out display manager systemd won't inherit the users environment variables. To fix this add the following to your home-manager configuration:
{{File|3=wayland.windowManager.sway.systemd.variables = ["--all"];|name=home.nix|lang=nix}}


=== Touchscreen input bound to the wrong monitor in multi-monitor setups ===
=== Touchscreen input bound to the wrong monitor in multi-monitor setups ===
Line 278: Line 322:
This occurs because GTK apps make blocking calls to freedesktop portals to be displayed. If Sway is not integrated with dbus and systemd, it will not be able to communicate via the <code>org.freedesktop.portal.Desktop</code> portal. To fix this, see the [[Sway#Using NixOS|description]] of default Sway configurations earlier. Adding the following to your sway configuration, if it is not already present, may resolve the issue:
This occurs because GTK apps make blocking calls to freedesktop portals to be displayed. If Sway is not integrated with dbus and systemd, it will not be able to communicate via the <code>org.freedesktop.portal.Desktop</code> portal. To fix this, see the [[Sway#Using NixOS|description]] of default Sway configurations earlier. Adding the following to your sway configuration, if it is not already present, may resolve the issue:
  include /etc/sway/config.d/*
  include /etc/sway/config.d/*
===dbus-issues: no icons in tray, can't open files from Nautilus with the right program===
If you're using Gnome-Apps like Nautilus on NixOS with Sway, you might run into issue with settings standard applications to open files from Nautilus (e.g. there being no way of linking PDF to your preferred PDF-reader). You might  also experience tray icons not showing up in your bar.
This is fixed by running <code>dbus-update-activation-environment --all</code> after starting your session. Make it permanent by adding <code>exec dbus-update-activation-environment --all</code> to your sway config file.


== Tips and tricks ==
== Tips and tricks ==
Line 314: Line 364:
</syntaxhighlight>
</syntaxhighlight>


=== Screen sharing with Firefox, Chromium ===
=== Screenshots ===
<syntaxhighlight lang="nix">
Screenshots using grim, slurp, and [https://github.com/XodTech/grimshot grimshot] for selection screenshots/full screen screenshots.
{ pkgs, ... }:
 
Install tools
{{File|3=environment.systemPackages = with pkgs; [
  grim
  slurp
  sway-contrib.grimshot
];|name=/etc/nixos/configuration.nix|lang=nix}}
Example Home Manager configuration.
{{File|3=wayland.windowManager.sway = {
  enable = true;
  config = let
    modifier = config.wayland.windowManager.sway.config.modifier;
    in {
    modifier = "Mod4";
    keybindings = lib.mkOptionDefault {
 
      # Super + Shift + S
      # Screenshot a selection that saves to ~/Screenshots and copies to clipboard.
      "${modifier}+Shift+s" = "exec selection=$(slurp) && grim -g \"$selection\" - {{!}} tee ~/Screenshots/$(date +%Y-%m-%d_%H-%M-%S).png {{!}} wl-copy";
     
      # Print Screen Button
      # Screenshot the currently focused screen, save to ~/Screenshots and copy to clipboard.
      "Print" = "exec grimshot save output - {{!}} tee ~/Screenshots/$(date +%Y-%m-%d_%H-%M-%S).png {{!}} wl-copy";
 
    };
  };
};|name=/etc/nixos/home.nix|lang=nix}}
 
=== Screen sharing ===
{{File|3={ pkgs, ... }:
{
{
   # xdg portal + pipewire = screensharing
   # xdg portal + pipewire = screensharing
Line 328: Line 407:
     pulse.enable = true;
     pulse.enable = true;
   };
   };
}
}|name=/etc/nixos/configuration.nix|lang=nix}}{{Tip|Make sure that you do not have conflicting definitions for xdg.portal in Home Manager.}}
</syntaxhighlight>
 
=== Auto mounting ===
 
==== USB storage devices (e.g. Flash Drives) ====
You can use [https://github.com/coldfix/udiskie udiskie] to automatically mount external storage medias.
 
You will need to install and enable [https://www.freedesktop.org/wiki/Software/udisks/ udisks2].
{{File|3=services.udisks2.enable = true;|name=/etc/nixos/configuration.nix|lang=nix}}
Then, in Home Manager you can enable udiskie.
{{File|3=services.udiskie.enable = true;|name=/etc/nixos/home.nix|lang=nix}}
Udiskie will automatically mount attached USB storage media.
 
See related info on [[USB storage devices]].
 
==== MTP (Android Phone Storage) ====
File managers that support [https://wiki.gnome.org/Projects/gvfs GVfs], such as [[Thunar]], can mount MTP devices using GVfs. See the page on [[MTP]] for related information.
{{File|3=services.gvfs.enable = true;|name=/etc/nixos/configuration.nix|lang=nix}}
 
=== SwayFX ===
[https://github.com/WillPower3309/swayfx SwayFX] is a fork of Sway that adds eye-candy effects, installing it is as simple as replacing your Sway package with SwayFX. Check the [[SwayFX]] page on the wiki for additional details.
{{File|3=wayland.windowManager.sway = {
  enable = true;
  package = pkgs.swayfx;
 
  # Needed to build without errors.
  checkConfig = false;
   
  # SwayFX options must be configured through extraConfig.
  extraConfig = ''
    shadows enable
    corner_radius 11
    blur_radius 7
    blur_passes 2
  '';
 
};|name=/etc/nixos/home.nix|lang=nix}}


=== Screen dimming with wl-gammarelay-rs ===
=== Screen dimming with wl-gammarelay-rs ===
Add <code>wl-gammarelay-rs</code> to programs.sway.extraPackages, then add the following to sway config:
Add <code>wl-gammarelay-rs</code> to programs.sway.extraPackages, then add the following to sway config:
<syntaxhighlight>
<syntaxhighlight>