1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-11-08 11:36:05 +01:00
home-manager/modules/services/espanso.nix
Austin Horstman 7419250703
treewide: convert package options to use mkPackageOption (#7116)
This commit converts `package = mkOption` declarations throughout the
codebase to use the more modern and consistent `lib.mkPackageOption`
function.

Key changes:
- Simple package options: `mkOption { type = types.package; default = pkgs.foo; }`
  becomes `lib.mkPackageOption pkgs "foo" { }`
- Package set options: Uses correct package set as first argument with
  `pkgsText` parameter (e.g., `lib.mkPackageOption pkgs.vimPlugins "plugin" { pkgsText = "pkgs.vimPlugins"; }`)
- Removes redundant descriptions that just restate the package name
- Preserves examples and extra context where meaningful
- Handles submodule plugin options properly with `null` defaults

This modernizes the option declarations and makes them more consistent
with current nixpkgs patterns while maintaining full backward compatibility.

Signed-off-by: Austin Horstman <khaneliman12@gmail.com>
2025-05-23 00:42:38 -05:00

222 lines
5.6 KiB
Nix

{
pkgs,
config,
lib,
...
}:
let
inherit (lib)
mkOption
mkPackageOption
mkEnableOption
mkIf
maintainers
literalExpression
types
mkRemovedOptionModule
versionAtLeast
;
inherit (pkgs.stdenv.hostPlatform) isLinux;
cfg = config.services.espanso;
espansoVersion = cfg.package.version;
package-bin =
if isLinux && cfg.x11Support && cfg.waylandSupport then
pkgs.writeShellScriptBin "espanso" ''
if [ -n "$WAYLAND_DISPLAY" ]; then
${lib.meta.getExe cfg.package-wayland} "$@"
else
${lib.meta.getExe cfg.package} "$@"
fi
''
else if isLinux && cfg.waylandSupport then
cfg.package-wayland
else
cfg.package;
yaml = pkgs.formats.yaml { };
in
{
imports = [
(mkRemovedOptionModule [
"services"
"espanso"
"settings"
] "Use services.espanso.configs and services.espanso.matches instead.")
];
meta.maintainers = [
maintainers.lucasew
maintainers.bobvanderlinden
lib.hm.maintainers.liyangau
maintainers.n8henrie
maintainers.phanirithvij
];
options = {
services.espanso = {
enable = mkEnableOption "Espanso: cross platform text expander in Rust";
package = lib.mkPackageOption pkgs "espanso" { };
package-wayland =
mkPackageOption pkgs "espanso-wayland" {
nullable = true;
extraDescription = "Which `espanso` package to use when running under wayland.";
}
// {
default = if isLinux && cfg.waylandSupport then pkgs.espanso-wayland else null;
};
x11Support = mkOption {
type = types.bool;
description = "Whether to enable x11 support on linux";
default = isLinux;
defaultText = "`true` on linux";
};
waylandSupport = mkOption {
type = types.bool;
description = "Whether to enable wayland support on linux";
default = isLinux;
defaultText = "`true` on linux";
};
configs = mkOption {
type = yaml.type;
default = {
default = { };
};
example = literalExpression ''
{
default = {
show_notifications = false;
};
vscode = {
filter_title = "Visual Studio Code$";
backend = "Clipboard";
};
};
'';
description = ''
The Espanso configuration to use. See
<https://espanso.org/docs/configuration/basics/>
for a description of available options.
'';
};
matches = mkOption {
type = yaml.type;
default = {
default.matches = [ ];
};
example = literalExpression ''
{
base = {
matches = [
{
trigger = ":now";
replace = "It's {{currentdate}} {{currenttime}}";
}
{
trigger = ":hello";
replace = "line1\nline2";
}
{
regex = ":hi(?P<person>.*)\\.";
replace = "Hi {{person}}!";
}
];
};
global_vars = {
global_vars = [
{
name = "currentdate";
type = "date";
params = {format = "%d/%m/%Y";};
}
{
name = "currenttime";
type = "date";
params = {format = "%R";};
}
];
};
};
'';
description = ''
The Espanso matches to use. See
<https://espanso.org/docs/matches/basics/>
for a description of available options.
'';
};
};
};
config = mkIf cfg.enable {
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 one of x11 or wayland support must be enabled on linux.
'';
}
];
# conflicting to have cfg.package and cfg.package-wayland
home.packages = [ package-bin ];
xdg.configFile =
let
configFiles = lib.mapAttrs' (name: value: {
name = "espanso/config/${name}.yml";
value = {
source = yaml.generate "${name}.yml" value;
};
}) cfg.configs;
matchesFiles = lib.mapAttrs' (name: value: {
name = "espanso/match/${name}.yml";
value = {
source = yaml.generate "${name}.yml" value;
};
}) cfg.matches;
in
configFiles // matchesFiles;
systemd.user.services.espanso = {
Unit = {
Description = "Espanso: cross platform text expander in Rust";
};
Service = {
ExecStart = "${lib.meta.getExe package-bin} launcher";
Restart = "on-failure";
RestartSec = 3;
};
Install = {
WantedBy = [ "default.target" ];
};
};
launchd.agents.espanso = {
enable = true;
config = {
ProgramArguments = [
"${cfg.package}/bin/espanso"
"launcher"
];
EnvironmentVariables.PATH = "${cfg.package}/bin:/usr/bin:/bin:/usr/sbin:/sbin";
KeepAlive = {
Crashed = true;
SuccessfulExit = false;
};
RunAtLoad = true;
};
};
};
}