Jump to content

Sway: Difference between revisions

1,921 bytes removed ,  3 May
imported>Onny
(Clarify autologin without password)
 
(43 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. Where <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>:
=== Using 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.
 
Tuigreet does not even need a separate compositor to launch.


{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
services.getty.autologinUser = "myuser";
services.greetd = {                                                     
  enable = true;                                                       
  settings = {                                                         
    default_session = {                                                 
      command = "${pkgs.greetd.tuigreet}/bin/tuigreet --time --cmd sway";
      user = "greeter";                                                
    };                                                                 
  };                                                                   
};                                                                     
</nowiki>}}


environment.loginShellInit = ''
== Troubleshooting ==
  if [ -z $DISPLAY ] && [ "$(tty)" = "/dev/tty1" ]; then
    exec sway
  fi
'';
</nowiki>}}


=== Polkit ===
=== Cursor is too tiny on HiDPI displays ===


nix generated sway config
Using [[Home Manager]] try configuring a general mouse cursor size and theme
<syntaxhighlight lang="nix">
${pkgs.polkit_gnome}/libexec/polkit-gnome-authentication-agent-1
</syntaxhighlight>


normal sway config
<syntaxhighlight lang="nix>
<syntaxhighlight lang="nix">
environment.systemPackages = with pkgs; [ polkit_gnome ];
environment.pathsToLink = [ "/libexec" ];
</syntaxhighlight>


<syntaxhighlight lang="bash">
home-manager.users.myUser = {
# 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>


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


};
</syntaxhighlight>


=== Theming ===
Replace <code>myUser</code> with your user running the graphical environment.


==== Gtk ====
=== Missing fonts on Xorg applications ===


<syntaxhighlight lang="nix">
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
environment.systemPackages = with pkgs; [
  gtk-engine-murrine
  gtk_engines
  gsettings-desktop-schemas
  lxappearance
];
</syntaxhighlight>
open lxappearance and pick your themes


read [https://github.com/swaywm/sway/wiki/GTK-3-settings-on-Wayland GTK3 settings on Wayland]
<syntaxhighlight lang="nix>


==== Qt ====
  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">
programs.qt5ct.enable = true;
</syntaxhighlight>
</syntaxhighlight>
open qt5ct and pick your theme


=== Additional packages ===
=== Swaylock cannot be unlocked with the correct password ===


Add the following to your NixOS configuration.


{{app|waybar|Highly customizable Wayland bar for Sway and Wlroots based compositors|https://github.com/Alexays/Waybar|waybar}}
<syntaxhighlight lang="nix>
{{app|autotiling|Script for sway and i3 to automatically switch the horizontal / vertical window split orientation|https://github.com/nwg-piotr/autotiling|autotiling}}
  security.pam.services.swaylock = {};
{{app|gammastep|Reduces bluelight and saves your eyes|https://gitlab.com/chinstrap/gammastep|gammastep}}
</syntaxhighlight>
{{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]


The <code>programs.sway.enable</code> option does this automatically.


=== Systemd integration ===
=== Inferior performance compared to other distributions ===


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:
Enabling realtime may improve latency and reduce stuttering, specially in high load scenarios.
 
* 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.


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{ config, pkgs, lib, ... }: {
security.pam.loginLimits = [
  { domain = "@users"; item = "rtprio"; type = "-"; value = 1; }
];
</syntaxhighlight>


  programs.sway = {
Enabling this option allows any program run by the "users" group to request real-time priority.
    enable = true;
    extraPackages = with pkgs; [
      swaylock # lockscreen
      swayidle
      xwayland # for legacy apps
      waybar # status bar
      mako # notification daemon
      kanshi # autorandr
    ];
  };


  environment = {
=== WLR Error when trying to launch sway ===
    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 = {
When this happens on a new nixos system, enabling opengl in configuration.nix may fix this issue.
    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>
 
And then simply add the following to the ''end'' of your sway configuration
<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">
<syntaxhighlight lang="nix">
  systemd.user.services.swayidle = {
hardware.opengl.enable = true;
    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>
</syntaxhighlight>
[[Category:Window managers]]
[[Category:Window managers]]
[[Category:Applications]]
4

edits