Difference between revisions of "Sway"

From NixOS Wiki
Jump to: navigation, search
(Fix spelling)
m (rollback unauthorized mass edits)
Tag: Rollback
 
(42 intermediate revisions by 28 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.  
+
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://github.com/swaywm/sway/wiki/i3-Migration-Guide i3 migration guide]
 
[https://github.com/swaywm/sway/wiki/i3-Migration-Guide i3 migration guide]
  
 
== Installation ==
 
== Installation ==
 +
You can install Sway by enabling it in NixOS directly, or by using [[Home Manager]]. Note that if you enable Sway using NixOS (via <code>programs.sway.enable = true;</code> in <code>configuration.nix</code>), your Home Manager configurations for Sway will be ignored.
  
=== NixOS ===
+
=== Using NixOS ===
[https://search.nixos.org/options?query=sway NixOS options for sway]
+
Here is a minimal configuration:
 
 
 
<syntaxhighlight lang="nix">
 
<syntaxhighlight lang="nix">
programs.sway = {
+
{ config, pkgs, lib, ... }:
   enable = true;
+
{
  wrapperFeatures.gtk = true; # so that gtk works properly
+
   environment.systemPackages = with pkgs; [
  extraPackages = with pkgs; [
+
     grim # screenshot functionality
     swaylock
+
     slurp # screenshot functionality
     swayidle
+
     wl-clipboard # wl-copy and wl-paste for copy/paste from stdin / stdout
     wl-clipboard
+
     mako # notification system developed by swaywm maintainer
     mako # notification daemon
 
    alacritty # Alacritty is the default terminal in the config
 
    dmenu # Dmenu is the default in the config but i recommend wofi since its wayland native
 
 
   ];
 
   ];
};
 
  
</syntaxhighlight>
+
  # Enable the gnome-keyring secrets vault.
 +
  # Will be exposed through DBus to programs willing to store secrets.
 +
  services.gnome.gnome-keyring.enable = true;
  
=== Home Manager ===
+
  # enable sway window manager
[https://rycee.gitlab.io/home-manager/options.html#opt-wayland.windowManager.sway.enable Home Manager options for sway]
+
  programs.sway = {
 
+
    enable = true;
<syntaxhighlight lang="nix">
+
    wrapperFeatures.gtk = true;
wayland.windowManager.sway = {
+
  };
  enable = true;
+
}
  wrapperFeatures.gtk = true ;
 
};
 
home.packages = with pkgs; [
 
  swaylock
 
  swayidle
 
  wl-clipboard
 
  mako # notification daemon
 
  alacritty # Alacritty is the default terminal in the config
 
  dmenu # Dmenu is the default in the config but i recommend wofi since its wayland native
 
];
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Info ==
+
A few general comments:
 +
* There is some friction between GTK theming and sway. Currently the sway developers suggest using gsettings to set gtk theme attributes as described here [https://github.com/swaywm/sway/wiki/GTK-3-settings-on-Wayland]. There is currently a plan to allow GTK theme attributes to be set directly in the sway config.
 +
* Running sway as a systemd user service is not recommended [https://github.com/swaywm/sway/wiki/Systemd-integration#running-sway-itself-as-a---user-service] [https://github.com/swaywm/sway/issues/5160]
  
=== Clipboard ===
+
=== Using Home Manager ===
 
+
To set up Sway using [[Home Manager]], first you must enable [[Polkit]] in your nix configuration:
For clipboard support
+
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
<syntaxhighlight lang="nix">
+
security.polkit.enable = true;
environment.systemPackages = with pkgs; [ wl-clipboard ];
+
</nowiki>}}
 +
 +
Then you can enable Sway in your home manager configuration. Here is a minimal example:
 +
<syntaxhighlight lang="nix>
 +
  wayland.windowManager.sway = {
 +
    enable = true;
 +
    config = rec {
 +
      modifier = "Mod4";
 +
      # Use kitty as default terminal
 +
      terminal = "kitty";
 +
      startup = [
 +
        # Launch Firefox on start
 +
        {command = "firefox";}
 +
      ];
 +
    };
 +
  };
 
</syntaxhighlight>
 
</syntaxhighlight>
  
to use [https://github.com/brunelli/wl-clipboard-x11/ wl-clipboard-x11] which is a wrapper to use wl-clipboard as a drop-in replacement to X11 clipboard tools
+
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.
  
<syntaxhighlight lang="nix">
+
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]
nixpkgs.overlays = [
 
  (self: super: {
 
    wl-clipboard-x11 = super.stdenv.mkDerivation rec {
 
    pname = "wl-clipboard-x11";
 
    version = "5";
 
 
 
    src = super.fetchFromGitHub {
 
      owner = "brunelli";
 
      repo = "wl-clipboard-x11";
 
      rev = "v${version}";
 
      sha256 = "1y7jv7rps0sdzmm859wn2l8q4pg2x35smcrm7mbfxn5vrga0bslb";
 
    };
 
 
 
    dontBuild = true;
 
    dontConfigure = true;
 
    propagatedBuildInputs = [ super.wl-clipboard ];
 
    makeFlags = [ "PREFIX=$(out)" ];
 
    };
 
 
 
    xsel = self.wl-clipboard-x11;
 
    xclip = self.wl-clipboard-x11;
 
  })
 
];
 
</syntaxhighlight>
 
{{Note|This will recompile all packages that have xclip or xsel in their dependencies|warn}}
 
  
 
=== Brightness and volume ===
 
=== Brightness and volume ===
 
+
If you are on a laptop, you can set up brightness and volume function keys as follows:
Brightnessctl has worked better for me than light
 
 
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
 
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
environment.systemPackages = with pkgs; [ brightnessctl ];
 
 
users.users.yourusername.extraGroups = [ "video" ];
 
users.users.yourusername.extraGroups = [ "video" ];
# or
 
 
programs.light.enable = true;
 
programs.light.enable = true;
  
environment.systemPackages = with pkgs; [ pactl ];
 
 
</nowiki>}}
 
</nowiki>}}
  
Line 94: Line 70:
  
 
# Brightness
 
# Brightness
bindsym XF86MonBrightnessDown exec "brightnessctl set 2%-"
+
bindsym XF86MonBrightnessDown exec light -U 10
bindsym XF86MonBrightnessUp exec "brightnessctl set +2%"
+
bindsym XF86MonBrightnessUp exec light -A 10
  
 
# Volume
 
# Volume
Line 103: Line 79:
 
}}
 
}}
  
=== Autostart Sway ===
+
=== Systemd services ===
It is possible to use [[greetd]], a minimal login manager, to autostart Sway. In this example, <code>myuser</code> needs to be replaced with the username which should execute Sway:
+
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:
 
 
 
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
 
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
services.greetd = {
+
  # kanshi systemd service
  enable = true;
+
  systemd.user.services.kanshi = {
  settings = rec {
+
    description = "kanshi daemon";
    initial_session = {
+
    serviceConfig = {
       command = "${pkgs.sway}/bin/sway";
+
      Type = "simple";
      user = "myuser";
+
       ExecStart = ''${pkgs.kanshi}/bin/kanshi -c kanshi_config_file'';
 
     };
 
     };
    default_session = initial_session;
 
 
   };
 
   };
};
 
 
</nowiki>}}
 
</nowiki>}}
  
Note that this approach starts Sway without asking for a user password. See [[greetd]] for alternative configuration examples.
+
{{file|sway config|bash|
 +
# give sway a little time to startup before starting kanshi.
 +
exec sleep 5; systemctl --user start kanshi.service
 +
}}
 +
When you launch sway, the systemd service is started.  
  
Alternatively, using <code>loginShellInit</code> Sway will automatically get started after user login. Use <code>getty</code> if you also want to skip the login and automatically get into <code>tty1</code> with <code>myuser</code>:
+
== Troubleshooting ==
  
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
+
=== Cursor is too tiny on HiDPI displays ===
services.getty.autologinUser = "myuser";
+
 
 +
Using [[Home Manager]] try configuring a general mouse cursor size and theme
  
environment.loginShellInit = ''
+
<syntaxhighlight lang="nix>
  if [ -z $DISPLAY ] && [ "$(tty)" = "/dev/tty1" ]; then
 
    exec sway
 
  fi
 
'';
 
</nowiki>}}
 
  
=== Polkit ===
+
home-manager.users.myUser = {
  
nix generated sway config
+
    home.pointerCursor = {
<syntaxhighlight lang="nix">
+
      name = "Adwaita";
${pkgs.polkit_gnome}/libexec/polkit-gnome-authentication-agent-1
+
      package = pkgs.gnome.adwaita-icon-theme;
</syntaxhighlight>
+
      size = 24;
 +
      x11 = {
 +
        enable = true;
 +
        defaultCursor = "Adwaita";
 +
      };
 +
    };
  
normal sway config
+
};
<syntaxhighlight lang="nix">
 
environment.systemPackages = with pkgs; [ polkit_gnome ];
 
environment.pathsToLink = [ "/libexec" ];
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
<syntaxhighlight lang="bash">
+
Replace <code>myUser</code> with your user running the graphical environment.
# NixOS
 
exec /run/current-system/sw/libexec/polkit-gnome-authentication-agent-1
 
# Home Manager | pathsToLink is not needed
 
exec ~/.nix-profile/libexec/polkit-gnome-authentication-agent-1
 
</syntaxhighlight>
 
  
 +
=== Missing fonts on 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
  
=== Theming ===
+
<syntaxhighlight lang="nix>
  
==== Gtk ====
+
  fonts = {
 +
    packages = with pkgs; [
 +
      noto-fonts
 +
      noto-fonts-cjk
 +
      noto-fonts-emoji
 +
      font-awesome
 +
      source-han-sans
 +
      source-han-sans-japanese
 +
      source-han-serif-japanese
 +
    ];
 +
    fontconfig.defaultFonts = {
 +
      serif = [ "Noto Serif" "Source Han Serif" ];
 +
      sansSerif = [ "Noto Sans" "Source Han Sans" ];
 +
    };
 +
  };
  
<syntaxhighlight lang="nix">
 
environment.systemPackages = with pkgs; [
 
  gtk-engine-murrine
 
  gtk_engines
 
  gsettings-desktop-schemas
 
  lxappearance
 
];
 
 
</syntaxhighlight>
 
</syntaxhighlight>
open lxappearance and pick your themes
 
  
read [https://github.com/swaywm/sway/wiki/GTK-3-settings-on-Wayland GTK3 settings on Wayland]
+
=== Swaylock cannot be unlocked with the correct password ===
  
==== Qt ====
+
Add the following to your NixOS configuration.
  
<syntaxhighlight lang="nix">
+
<syntaxhighlight lang="nix>
programs.qt5ct.enable = true;
+
  security.pam.services.swaylock = {};
 
</syntaxhighlight>
 
</syntaxhighlight>
open qt5ct and pick your theme
 
 
=== Additional packages ===
 
 
 
{{app|waybar|Highly customizable Wayland bar for Sway and Wlroots based compositors|https://github.com/Alexays/Waybar|waybar}}
 
{{app|autotiling|Script for sway and i3 to automatically switch the horizontal / vertical window split orientation|https://github.com/nwg-piotr/autotiling|autotiling}}
 
{{app|gammastep|Reduces bluelight and saves your eyes|https://gitlab.com/chinstrap/gammastep|gammastep}}
 
{{app|clipman|Simple clipboard manager for Wayland|https://github.com/yory8/clipman/|clipman}}
 
{{app|wofi|Launcher/menu program for wlroots based wayland compositors such as sway|https://hg.sr.ht/~scoopta/wofi|wofi}}
 
{{app|flashfocus|Simple focus animations for tiling window managers|https://github.com/fennerm/flashfocus|flashfocus}}
 
{{app|wf-recorder|Screen recorder for wlroots-based compositors such as sway|https://github.com/ammen99/wf-recorder|wf-recorder}}
 
{{app|i3-ratiosplit|Configurable window size on creation|https://github.com/333fred/i3-ratiosplit|i3-ratiosplit}}
 
 
more packages here [https://github.com/swaywm/sway/wiki/i3-Migration-Guide i3 migration guide]
 
 
  
=== Systemd integration ===
+
The <code>programs.sway.enable</code> option does this automatically.
  
In an article on the sway wiki [https://github.com/swaywm/sway/wiki/Systemd-integration], a way to integrate Sway with systemd user services is proposed. Starting sway that way has some benefits:
+
=== Inferior performance compared to other distributions ===
  
* Services like Waybar, kanshi, redshift can depend on <code>graphical-session.target</code> and can therefore be started as their own user service, including convenient service management and logging.
+
Enabling realtime may improve latency and reduce stuttering, specially in high load scenarios.
  
 
<syntaxhighlight lang="nix">
 
<syntaxhighlight lang="nix">
{ config, pkgs, lib, ... }: {
+
security.pam.loginLimits = [
 
+
   { domain = "@users"; item = "rtprio"; type = "-"; value = 1; }
  programs.sway = {
+
];
    enable = true;
 
    extraPackages = with pkgs; [
 
      swaylock # lockscreen
 
      swayidle
 
      xwayland # for legacy apps
 
      waybar # status bar
 
      mako # notification daemon
 
      kanshi # autorandr
 
    ];
 
  };
 
 
 
   environment = {
 
    etc = {
 
      # Put config files in /etc. Note that you also can put these in ~/.config, but then you can't manage them with NixOS anymore!
 
      "sway/config".source = ./dotfiles/sway/config;
 
      "xdg/waybar/config".source = ./dotfiles/waybar/config;
 
      "xdg/waybar/style.css".source = ./dotfiles/waybar/style.css;
 
    };
 
  };
 
 
 
  systemd.user.targets.sway-session = {
 
    description = "Sway compositor session";
 
    documentation = [ "man:systemd.special(7)" ];
 
    bindsTo = [ "graphical-session.target" ];
 
    wants = [ "graphical-session-pre.target" ];
 
    after = [ "graphical-session-pre.target" ];
 
  };
 
 
 
  services.redshift = {
 
    enable = true;
 
    # Redshift with wayland support isn't present in nixos-19.09 atm. You have to cherry-pick the commit from https://github.com/NixOS/nixpkgs/pull/68285 to do that.
 
    package = pkgs.redshift-wlr;
 
  };
 
 
 
  programs.waybar.enable = true;
 
 
 
  systemd.user.services.kanshi = {
 
    description = "Kanshi output autoconfig ";
 
    wantedBy = [ "graphical-session.target" ];
 
    partOf = [ "graphical-session.target" ];
 
    serviceConfig = {
 
      # kanshi doesn't have an option to specifiy config file yet, so it looks
 
      # at .config/kanshi/config
 
      ExecStart = ''
 
        ${pkgs.kanshi}/bin/kanshi
 
      '';
 
      RestartSec = 5;
 
      Restart = "always";
 
    };
 
  };
 
 
 
}
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
And then simply add the following to the ''end'' of your sway configuration
+
Enabling this option allows any program run by the "users" group to request real-time priority.
<syntaxhighlight>
 
exec "systemctl --user import-environment; systemctl --user start sway-session.target"
 
</syntaxhighlight>
 
  
Note: swayidle will fail cryptically if it cannot find {{ic|sh}} in {{ic|PATH}}, so you must provide this if you create a service file for it. An example is below:
 
  
<syntaxhighlight lang="nix">
 
  systemd.user.services.swayidle = {
 
    description = "Idle Manager for Wayland";
 
    documentation = [ "man:swayidle(1)" ];
 
    wantedBy = [ "sway-session.target" ];
 
    partOf = [ "graphical-session.target" ];
 
    path = [ pkgs.bash ];
 
    serviceConfig = {
 
      ExecStart = '' ${pkgs.swayidle}/bin/swayidle -w -d \
 
        timeout 300 '${pkgs.sway}/bin/swaymsg "output * dpms off"' \
 
        resume '${pkgs.sway}/bin/swaymsg "output * dpms on"'
 
      '';
 
    };
 
  };
 
</syntaxhighlight>
 
 
[[Category:Window managers]]
 
[[Category:Window managers]]
 +
[[Category:Applications]]

Latest revision as of 11:00, 6 April 2024

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. i3 migration guide

Installation

You can install Sway by enabling it in NixOS directly, or by using Home Manager. Note that if you enable Sway using NixOS (via programs.sway.enable = true; in configuration.nix), your Home Manager configurations for Sway will be ignored.

Using NixOS

Here is a minimal configuration:

{ config, pkgs, lib, ... }:
{
  environment.systemPackages = with pkgs; [
    grim # screenshot functionality
    slurp # screenshot functionality
    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. 
  # Will be exposed through DBus to programs willing to store secrets.
  services.gnome.gnome-keyring.enable = true;

  # enable sway window manager
  programs.sway = {
    enable = true;
    wrapperFeatures.gtk = true;
  };
}

A few general comments:

  • There is some friction between GTK theming and sway. Currently the sway developers suggest using gsettings to set gtk theme attributes as described here [1]. There is currently a plan to allow GTK theme attributes to be set directly in the sway config.
  • Running sway as a systemd user service is not recommended [2] [3]

Using Home Manager

To set up Sway using Home Manager, first you must enable Polkit in your nix configuration:

Breeze-text-x-plain.png
/etc/nixos/configuration.nix
security.polkit.enable = true;


Then you can enable Sway in your home manager configuration. Here is a minimal example:

  wayland.windowManager.sway = {
    enable = true;
    config = rec {
      modifier = "Mod4";
      # Use kitty as default terminal
      terminal = "kitty"; 
      startup = [
        # Launch Firefox on start
        {command = "firefox";}
      ];
    };
  };

See 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 Dunst crashes if run as service

Brightness and volume

If you are on a laptop, you can set up brightness and volume function keys as follows:

Breeze-text-x-plain.png
/etc/nixos/configuration.nix
users.users.yourusername.extraGroups = [ "video" ];
programs.light.enable = true;


Breeze-text-x-plain.png
sway config
# Brightness
bindsym XF86MonBrightnessDown exec light -U 10
bindsym XF86MonBrightnessUp exec light -A 10

# Volume
bindsym XF86AudioRaiseVolume 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'


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 [4], which implements monitor hot swapping. It would be enabled as follows:

Breeze-text-x-plain.png
/etc/nixos/configuration.nix
  # kanshi systemd service
  systemd.user.services.kanshi = {
    description = "kanshi daemon";
    serviceConfig = {
      Type = "simple";
      ExecStart = ''${pkgs.kanshi}/bin/kanshi -c kanshi_config_file'';
    };
  };


Breeze-text-x-plain.png
sway config
# give sway a little time to startup before starting kanshi.
exec sleep 5; systemctl --user start kanshi.service

When you launch sway, the systemd service is started.

Troubleshooting

Cursor is too tiny on HiDPI displays

Using Home Manager try configuring a general mouse cursor size and theme

home-manager.users.myUser = {

    home.pointerCursor = {
      name = "Adwaita";
      package = pkgs.gnome.adwaita-icon-theme;
      size = 24;
      x11 = {
        enable = true;
        defaultCursor = "Adwaita";
      };
    };

};

Replace myUser with your user running the graphical environment.

Missing fonts on 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

  fonts = {
    packages = with pkgs; [
      noto-fonts
      noto-fonts-cjk
      noto-fonts-emoji
      font-awesome
      source-han-sans
      source-han-sans-japanese
      source-han-serif-japanese
    ];
    fontconfig.defaultFonts = {
      serif = [ "Noto Serif" "Source Han Serif" ];
      sansSerif = [ "Noto Sans" "Source Han Sans" ];
    };
  };

Swaylock cannot be unlocked with the correct password

Add the following to your NixOS configuration.

  security.pam.services.swaylock = {};

The programs.sway.enable option does this automatically.

Inferior performance compared to other distributions

Enabling realtime may improve latency and reduce stuttering, specially in high load scenarios.

security.pam.loginLimits = [
  { domain = "@users"; item = "rtprio"; type = "-"; value = 1; }
];

Enabling this option allows any program run by the "users" group to request real-time priority.