diff --git a/modules/lib/maintainers.nix b/modules/lib/maintainers.nix index 2d2f551d9..2ad490f11 100644 --- a/modules/lib/maintainers.nix +++ b/modules/lib/maintainers.nix @@ -400,4 +400,10 @@ github = "pedorich-n"; githubId = 15573098; }; + liyangau = { + name = "Li Yang"; + email = "d@aufomm.com"; + github = "liyangau"; + githubId = 71299093; + }; } diff --git a/modules/services/espanso.nix b/modules/services/espanso.nix index 3d66a5987..ec8ace5e8 100644 --- a/modules/services/espanso.nix +++ b/modules/services/espanso.nix @@ -1,19 +1,25 @@ { pkgs, config, lib, ... }: - let - inherit (lib) - mkOption mkEnableOption mkIf maintainers literalExpression types platforms; + mkOption mkEnableOption mkIf maintainers literalExpression types platforms + mkRemovedOptionModule versionAtLeast; inherit (lib.hm.assertions) assertPlatform; cfg = config.services.espanso; + espansoVersion = cfg.package.version; yaml = pkgs.formats.yaml { }; - in { - meta.maintainers = with maintainers; [ lucasew ]; - + imports = [ + (mkRemovedOptionModule [ "services" "espanso" "settings" ] + "Use services.espanso.configs and services.espanso.matches instead.") + ]; + meta.maintainers = [ + maintainers.lucasew + maintainers.bobvanderlinden + lib.hm.maintainers.liyangau + ]; options = { services.espanso = { enable = mkEnableOption "Espanso: cross platform text expander in Rust"; @@ -25,40 +31,67 @@ in { defaultText = literalExpression "pkgs.espanso"; }; - settings = mkOption { + configs = mkOption { type = yaml.type; - default = { matches = [ ]; }; + default = { default = { }; }; example = literalExpression '' { - matches = [ - { # Simple text replacement - trigger = ":espanso"; - replace = "Hi there!"; - } - { # Dates - trigger = ":date"; - replace = "{{mydate}}"; - vars = [{ - name = "mydate"; - type = "date"; - params = { format = "%m/%d/%Y"; }; - }]; - } - { # Shell commands - trigger = ":shell"; - replace = "{{output}}"; - vars = [{ - name = "output"; - type = "shell"; - params = { cmd = "echo Hello from your shell"; }; - }]; - } - ]; - } + default = { + show_notifications = false; + }; + vscode = { + filter_title = "Visual Studio Code$"; + backend = "Clipboard"; + }; + }; ''; description = '' The Espanso configuration to use. See - + + 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.*)\\."; + 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 + for a description of available options. ''; }; @@ -66,12 +99,28 @@ in { }; config = mkIf cfg.enable { - assertions = [ (assertPlatform "services.espanso" pkgs platforms.linux) ]; + assertions = [ + (assertPlatform "services.espanso" pkgs platforms.linux) + { + assertion = versionAtLeast espansoVersion "2"; + message = '' + The services.espanso module only supports Espanso version 2 or later. + ''; + } + ]; home.packages = [ cfg.package ]; - xdg.configFile."espanso/default.yml".source = - yaml.generate "espanso-default.yml" cfg.settings; + 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"; }; diff --git a/tests/modules/services/espanso/basic-configuration.nix b/tests/modules/services/espanso/basic-configuration.nix index 34c6d533b..6d04c53d2 100644 --- a/tests/modules/services/espanso/basic-configuration.nix +++ b/tests/modules/services/espanso/basic-configuration.nix @@ -3,31 +3,38 @@ { services.espanso = { enable = true; - settings = { - matches = [ - { # Simple text replacement - trigger = ":espanso"; - replace = "Hi there!"; - } - { # Dates - trigger = ":date"; - replace = "{{mydate}}"; - vars = [{ - name = "mydate"; + configs = { default = { show_notifications = false; }; }; + matches = { + base = { + matches = [ + { + trigger = ":now"; + replace = "It's {{currentdate}} {{currenttime}}"; + } + { + trigger = ":hello"; + replace = '' + line1 + line2''; + } + { + regex = ":hi(?P.*)\\."; + replace = "Hi {{person}}!"; + } + ]; + global_vars = [ + { + name = "currentdate"; type = "date"; - params = { format = "%m/%d/%Y"; }; - }]; - } - { # Shell commands - trigger = ":shell"; - replace = "{{output}}"; - vars = [{ - name = "output"; - type = "shell"; - params = { cmd = "echo Hello from your shell"; }; - }]; - } - ]; + params = { format = "%d/%m/%Y"; }; + } + { + name = "currenttime"; + type = "date"; + params = { format = "%R"; }; + } + ]; + }; }; }; @@ -38,8 +45,12 @@ assertFileExists "$serviceFile" assertFileContent "$serviceFile" ${./basic-configuration.service} - configFile=home-files/.config/espanso/default.yml + configFile=home-files/.config/espanso/config/default.yml assertFileExists "$configFile" assertFileContent "$configFile" ${./basic-configuration.yaml} + + matchFile=home-files/.config/espanso/match/base.yml + assertFileExists "$matchFile" + assertFileContent "$matchFile" ${./basic-matches.yaml} ''; } diff --git a/tests/modules/services/espanso/basic-configuration.yaml b/tests/modules/services/espanso/basic-configuration.yaml index 3789df04b..9dc9aa99d 100644 --- a/tests/modules/services/espanso/basic-configuration.yaml +++ b/tests/modules/services/espanso/basic-configuration.yaml @@ -1,17 +1 @@ -matches: -- replace: Hi there! - trigger: :espanso -- replace: '{{mydate}}' - trigger: :date - vars: - - name: mydate - params: - format: '%m/%d/%Y' - type: date -- replace: '{{output}}' - trigger: :shell - vars: - - name: output - params: - cmd: echo Hello from your shell - type: shell +show_notifications: false diff --git a/tests/modules/services/espanso/basic-matches.yaml b/tests/modules/services/espanso/basic-matches.yaml new file mode 100644 index 000000000..3cf98c989 --- /dev/null +++ b/tests/modules/services/espanso/basic-matches.yaml @@ -0,0 +1,18 @@ +global_vars: +- name: currentdate + params: + format: '%d/%m/%Y' + type: date +- name: currenttime + params: + format: '%R' + type: date +matches: +- replace: It's {{currentdate}} {{currenttime}} + trigger: :now +- replace: 'line1 + + line2' + trigger: :hello +- regex: :hi(?P.*)\. + replace: Hi {{person}}!