Skip to content

Commit

Permalink
Merge pull request #75247 from Elyhaka/sway
Browse files Browse the repository at this point in the history
sway: refactor with a wrapper

This moves the wrapper functionality from the NixOS module to a new package
(wrapper) that wraps the original sway package (sway-unwrapped). Therefore it's
now also possible to properly use Sway on non-NixOS systems out of the box.

The new submodule for the wrapperFeatures makes it easy to extend the
functionality which should become useful in the future.

This also introduces a GTK wrapper feature to fix issues with icon/GTK themes,
e.g. when running waybar or wofi. This should also work for #67704. If not, we
might have to add some additional dependencies/arguments for this case.
  • Loading branch information
primeos committed Dec 21, 2019
2 parents 7b6d5df + d467c59 commit b9b7738
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 30 deletions.
67 changes: 45 additions & 22 deletions nixos/modules/programs/sway.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@ with lib;

let
cfg = config.programs.sway;
swayPackage = pkgs.sway;

swayWrapped = pkgs.writeShellScriptBin "sway" ''
set -o errexit
if [ ! "$_SWAY_WRAPPER_ALREADY_EXECUTED" ]; then
export _SWAY_WRAPPER_ALREADY_EXECUTED=1
${cfg.extraSessionCommands}
fi
wrapperOptions = types.submodule {
options =
let
mkWrapperFeature = default: description: mkOption {
type = types.bool;
inherit default;
example = !default;
description = "Whether to make use of the ${description}";
};
in {
base = mkWrapperFeature true ''
base wrapper to execute extra session commands and prepend a
dbus-run-session to the sway command.
'';
gtk = mkWrapperFeature false ''
wrapGAppsHook wrapper to execute sway with required environment
variables for GTK applications.
'';
};
};

if [ "$DBUS_SESSION_BUS_ADDRESS" ]; then
export DBUS_SESSION_BUS_ADDRESS
exec ${swayPackage}/bin/sway "$@"
else
exec ${pkgs.dbus}/bin/dbus-run-session ${swayPackage}/bin/sway "$@"
fi
'';
swayJoined = pkgs.symlinkJoin {
name = "sway-joined";
paths = [ swayWrapped swayPackage ];
passthru.providedSessions = [ "sway" ];
swayPackage = pkgs.sway.override {
extraSessionCommands = cfg.extraSessionCommands;
withBaseWrapper = cfg.wrapperFeatures.base;
withGtkWrapper = cfg.wrapperFeatures.gtk;
};
in {
options.programs.sway = {
Expand All @@ -36,6 +41,15 @@ in {
Please have a look at the "extraSessionCommands" example for running
programs natively under Wayland'';

wrapperFeatures = mkOption {
type = wrapperOptions;
default = { };
example = { gtk = true; };
description = ''
Attribute set of features to enable in the wrapper.
'';
};

extraSessionCommands = mkOption {
type = types.lines;
default = "";
Expand All @@ -56,7 +70,7 @@ in {
extraPackages = mkOption {
type = with types; listOf package;
default = with pkgs; [
swaylock swayidle swaybg
swaylock swayidle
xwayland rxvt_unicode dmenu
];
defaultText = literalExample ''
Expand All @@ -76,8 +90,17 @@ in {
};

config = mkIf cfg.enable {
assertions = [
{
assertion = cfg.extraSessionCommands != "" -> cfg.wrapperFeatures.base;
message = ''
The extraSessionCommands for Sway will not be run if
wrapperFeatures.base is disabled.
'';
}
];
environment = {
systemPackages = [ swayJoined ] ++ cfg.extraPackages;
systemPackages = [ swayPackage ] ++ cfg.extraPackages;
etc = {
"sway/config".source = mkOptionDefault "${swayPackage}/etc/sway/config";
#"sway/security.d".source = mkOptionDefault "${swayPackage}/etc/sway/security.d/";
Expand All @@ -89,7 +112,7 @@ in {
fonts.enableDefaultFonts = mkDefault true;
programs.dconf.enable = mkDefault true;
# To make a Sway session available if a display manager like SDDM is enabled:
services.xserver.displayManager.sessionPackages = [ swayJoined ];
services.xserver.displayManager.sessionPackages = [ swayPackage ];
};

meta.maintainers = with lib.maintainers; [ gnidorah primeos colemickens ];
Expand Down
12 changes: 5 additions & 7 deletions pkgs/applications/window-managers/sway/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
, pkgconfig, scdoc
, wayland, libxkbcommon, pcre, json_c, dbus, libevdev
, pango, cairo, libinput, libcap, pam, gdk-pixbuf
, wlroots, wayland-protocols, swaybg
, wlroots, wayland-protocols
}:

stdenv.mkDerivation rec {
pname = "sway";
pname = "sway-unwrapped";
version = "1.2";

src = fetchFromGitHub {
Expand All @@ -22,7 +22,9 @@ stdenv.mkDerivation rec {
./load-configuration-from-etc.patch
];

nativeBuildInputs = [ pkgconfig meson ninja scdoc makeWrapper ];
nativeBuildInputs = [
pkgconfig meson ninja scdoc
];

buildInputs = [
wayland libxkbcommon pcre json_c dbus libevdev
Expand All @@ -37,10 +39,6 @@ stdenv.mkDerivation rec {
"-Dtray=enabled" "-Dman-pages=enabled"
];

postInstall = ''
wrapProgram $out/bin/sway --prefix PATH : "${swaybg}/bin"
'';

meta = with stdenv.lib; {
description = "i3-compatible tiling Wayland compositor";
homepage = https://swaywm.org;
Expand Down
50 changes: 50 additions & 0 deletions pkgs/applications/window-managers/sway/wrapper.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{ lib, stdenv
, sway-unwrapped, swaybg
, makeWrapper, symlinkJoin, writeShellScriptBin
, withBaseWrapper ? true, extraSessionCommands ? "", dbus
, withGtkWrapper ? false, wrapGAppsHook, gdk-pixbuf
}:

assert extraSessionCommands != "" -> withBaseWrapper;

with lib;

let
baseWrapper = writeShellScriptBin "sway" ''
set -o errexit
if [ ! "$_SWAY_WRAPPER_ALREADY_EXECUTED" ]; then
export _SWAY_WRAPPER_ALREADY_EXECUTED=1
${extraSessionCommands}
fi
if [ "$DBUS_SESSION_BUS_ADDRESS" ]; then
export DBUS_SESSION_BUS_ADDRESS
exec ${sway-unwrapped}/bin/sway "$@"
else
exec ${dbus}/bin/dbus-run-session ${sway-unwrapped}/bin/sway "$@"
fi
'';
in symlinkJoin {
name = "sway-${sway-unwrapped.version}";

paths = (optional withBaseWrapper baseWrapper)
++ [ sway-unwrapped ];

nativeBuildInputs = [ makeWrapper ]
++ (optional withGtkWrapper wrapGAppsHook);

buildInputs = optional withGtkWrapper gdk-pixbuf;

postBuild = ''
# We want to run wrapProgram manually to only wrap sway and add swaybg:
export dontWrapGApps=true
${optionalString withGtkWrapper "wrapGAppsHook"}
wrapProgram $out/bin/sway \
--prefix PATH : "${swaybg}/bin" ${optionalString withGtkWrapper ''\
"''${gappsWrapperArgs[@]}"
''}
'';

passthru.providedSessions = [ "sway" ];

inherit (sway-unwrapped) meta;
}
3 changes: 2 additions & 1 deletion pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19391,7 +19391,8 @@ in

wlroots = callPackage ../development/libraries/wlroots { };

sway = callPackage ../applications/window-managers/sway { };
sway-unwrapped = callPackage ../applications/window-managers/sway { };
sway = callPackage ../applications/window-managers/sway/wrapper.nix { };
swaybg = callPackage ../applications/window-managers/sway/bg.nix { };
swayidle = callPackage ../applications/window-managers/sway/idle.nix { };
swaylock = callPackage ../applications/window-managers/sway/lock.nix { };
Expand Down

0 comments on commit b9b7738

Please sign in to comment.