diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 75ab3984a..75e6399f9 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -278,6 +278,8 @@ /modules/services/etesync-dav.nix @Valodim +/modules/services/espanso.nix @lucasew + /modules/services/flameshot.nix @moredhel /modules/services/fluidsynth.nix @Valodim diff --git a/modules/misc/news.nix b/modules/misc/news.nix index 831e8d450..c03d991a2 100644 --- a/modules/misc/news.nix +++ b/modules/misc/news.nix @@ -2417,6 +2417,14 @@ in A new module is available: 'programs.eww'. ''; } + + { + time = "2022-02-17T23:11:13+00:00"; + condition = hostPlatform.isLinux; + message = '' + A new module is available: 'services.espanso'. + ''; + } ]; }; } diff --git a/modules/modules.nix b/modules/modules.nix index cf101a359..86e10eb83 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -178,6 +178,7 @@ let ./services/easyeffects.nix ./services/emacs.nix ./services/etesync-dav.nix + ./services/espanso.nix ./services/flameshot.nix ./services/fluidsynth.nix ./services/fnott.nix diff --git a/modules/services/espanso.nix b/modules/services/espanso.nix new file mode 100644 index 000000000..3d66a5987 --- /dev/null +++ b/modules/services/espanso.nix @@ -0,0 +1,86 @@ +{ pkgs, config, lib, ... }: + +let + + inherit (lib) + mkOption mkEnableOption mkIf maintainers literalExpression types platforms; + + inherit (lib.hm.assertions) assertPlatform; + + cfg = config.services.espanso; + + yaml = pkgs.formats.yaml { }; + +in { + meta.maintainers = with maintainers; [ lucasew ]; + + options = { + services.espanso = { + enable = mkEnableOption "Espanso: cross platform text expander in Rust"; + + package = mkOption { + type = types.package; + description = "Which espanso package to use"; + default = pkgs.espanso; + defaultText = literalExpression "pkgs.espanso"; + }; + + settings = mkOption { + type = yaml.type; + default = { matches = [ ]; }; + 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"; }; + }]; + } + ]; + } + ''; + description = '' + The Espanso configuration to use. See + + for a description of available options. + ''; + }; + }; + }; + + config = mkIf cfg.enable { + assertions = [ (assertPlatform "services.espanso" pkgs platforms.linux) ]; + + home.packages = [ cfg.package ]; + + xdg.configFile."espanso/default.yml".source = + yaml.generate "espanso-default.yml" cfg.settings; + + systemd.user.services.espanso = { + Unit = { Description = "Espanso: cross platform text expander in Rust"; }; + Service = { + Type = "exec"; + ExecStart = "${cfg.package}/bin/espanso daemon"; + Restart = "on-failure"; + }; + Install = { WantedBy = [ "default.target" ]; }; + }; + }; +} diff --git a/tests/default.nix b/tests/default.nix index 73e99639b..14c7773c5 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -137,6 +137,7 @@ import nmt { ./modules/services/devilspie2 ./modules/services/dropbox ./modules/services/emacs + ./modules/services/espanso ./modules/services/flameshot ./modules/services/fluidsynth ./modules/services/fnott diff --git a/tests/modules/services/espanso/basic-configuration.nix b/tests/modules/services/espanso/basic-configuration.nix new file mode 100644 index 000000000..34c6d533b --- /dev/null +++ b/tests/modules/services/espanso/basic-configuration.nix @@ -0,0 +1,45 @@ +{ ... }: + +{ + services.espanso = { + enable = true; + settings = { + 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"; }; + }]; + } + ]; + }; + }; + + test.stubs.espanso = { }; + + nmt.script = '' + serviceFile=home-files/.config/systemd/user/espanso.service + assertFileExists "$serviceFile" + assertFileContent "$serviceFile" ${./basic-configuration.service} + + configFile=home-files/.config/espanso/default.yml + assertFileExists "$configFile" + assertFileContent "$configFile" ${./basic-configuration.yaml} + ''; +} diff --git a/tests/modules/services/espanso/basic-configuration.service b/tests/modules/services/espanso/basic-configuration.service new file mode 100644 index 000000000..593196e59 --- /dev/null +++ b/tests/modules/services/espanso/basic-configuration.service @@ -0,0 +1,10 @@ +[Install] +WantedBy=default.target + +[Service] +ExecStart=@espanso@/bin/espanso daemon +Restart=on-failure +Type=exec + +[Unit] +Description=Espanso: cross platform text expander in Rust diff --git a/tests/modules/services/espanso/basic-configuration.yaml b/tests/modules/services/espanso/basic-configuration.yaml new file mode 100644 index 000000000..3789df04b --- /dev/null +++ b/tests/modules/services/espanso/basic-configuration.yaml @@ -0,0 +1,17 @@ +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 diff --git a/tests/modules/services/espanso/default.nix b/tests/modules/services/espanso/default.nix new file mode 100644 index 000000000..5df5fa3f1 --- /dev/null +++ b/tests/modules/services/espanso/default.nix @@ -0,0 +1 @@ +{ espanso-basic-configuration = ./basic-configuration.nix; }