Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

espanso: add wayland support #5930

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 59 additions & 9 deletions modules/services/espanso.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,22 @@ let
mkOption mkEnableOption mkIf maintainers literalExpression types
mkRemovedOptionModule versionAtLeast;

inherit (pkgs.stdenv.hostPlatform) isDarwin isLinux;

cfg = config.services.espanso;
espansoVersion = cfg.package.version;

package-bin = if isLinux then
pkgs.writeShellScriptBin "espanso" ''
if [ -n "$WAYLAND_DISPLAY" ]; then
${lib.meta.getExe cfg.package-wayland} "$@"
else
${lib.meta.getExe cfg.package} "$@"
fi
''
else
cfg.package;

yaml = pkgs.formats.yaml { };
in {
imports = [
Expand All @@ -26,10 +39,38 @@ in {
package = mkOption {
type = types.package;
description = "Which espanso package to use";
default = pkgs.espanso;
default = if isDarwin || cfg.x11Support then
pkgs.espanso
else
pkgs.espanso-wayland;
defaultText = literalExpression "pkgs.espanso";
};

package-wayland = mkOption {
type = types.package;
description =
"Which espanso package to use when running under wayland on linux";
default = if isLinux && cfg.waylandSupport then
pkgs.espanso-wayland
else
pkgs.espanso;
defaultText = literalExpression "pkgs.espanso-wayland";
};

x11Support = mkOption {
type = types.bool;
description = "Whether to enable x11 support on linux";
default = isLinux;
defaultText = literalExpression "enabled on linux";
};

waylandSupport = mkOption {
type = types.bool;
description = "Whether to enable wayland support on linux";
default = isLinux;
defaultText = literalExpression "enabled on linux";
};

configs = mkOption {
type = yaml.type;
default = { default = { }; };
Expand Down Expand Up @@ -98,14 +139,23 @@ in {
};

config = mkIf cfg.enable {
assertions = [{
assertion = versionAtLeast espansoVersion "2";
message = ''
The services.espanso module only supports Espanso version 2 or later.
'';
}];
assertions = [
{
assertion = versionAtLeast espansoVersion "2";
message = ''
The services.espanso module only supports Espanso version 2 or later.
'';
}
{
assertion = isLinux -> (cfg.x11Support || cfg.waylandSupport);
message = ''
In services.espanso at least x11 or wayland support must be enabled on linux.
'';
}
];

home.packages = [ cfg.package ];
# conflicting to have cfg.package and cfg.package-wayland
home.packages = [ package-bin ];

xdg.configFile = let
configFiles = lib.mapAttrs' (name: value: {
Expand All @@ -121,7 +171,7 @@ in {
systemd.user.services.espanso = {
Unit = { Description = "Espanso: cross platform text expander in Rust"; };
Service = {
ExecStart = "${cfg.package}/bin/espanso launcher";
ExecStart = "${lib.meta.getExe package-bin} launcher";
Restart = "on-failure";
RestartSec = 3;
};
Expand Down
15 changes: 12 additions & 3 deletions tests/modules/services/espanso/basic-configuration.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{ ... }:
espansoExtraArgs:
{ config, ... }:

{
services.espanso = {
Expand Down Expand Up @@ -36,14 +37,22 @@
];
};
};
};
} // espansoExtraArgs;

test.stubs.espanso = { };

nmt.script = ''
serviceFile=home-files/.config/systemd/user/espanso.service
expectedServiceFile=${./basic-configuration.service}
assertFileExists "$serviceFile"
assertFileContent "$serviceFile" ${./basic-configuration.service}
assertFileRegex "$serviceFile" 'ExecStart=.*/bin/espanso launcher'
if [[ $(uname) == "Linux" ]]; then
grep -v "/bin/espanso launcher" "$(_abs $serviceFile)" > espanso-service.actual
grep -v "/bin/espanso launcher" "$expectedServiceFile" > espanso-service.expected
assertFileContent "$(realpath espanso-service.actual)" espanso-service.expected
else
assertFileContent "$serviceFile" "$expectedServiceFile"
fi

configFile=home-files/.config/espanso/config/default.yml
assertFileExists "$configFile"
Expand Down
12 changes: 11 additions & 1 deletion tests/modules/services/espanso/default.nix
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
{ espanso-basic-configuration = ./basic-configuration.nix; }
{
espanso-basic-configuration = import ./basic-configuration.nix { };
espanso-basic-configuration-wayland = import ./basic-configuration.nix {
waylandSupport = true;
x11Support = false;
};
espanso-basic-configuration-x11 = import ./basic-configuration.nix {
waylandSupport = false;
x11Support = true;
};
}
Loading