Unverified Commit e4ee7c28 authored by Niklas Korz's avatar Niklas Korz Committed by GitHub
Browse files

nixos/espanso: provide required capabilities for espanso-wayland (#423931)

parents 906b64f1 6267196d
Loading
Loading
Loading
Loading
+14 −1
Original line number Diff line number Diff line
@@ -25,10 +25,23 @@ in
  };

  config = lib.mkIf cfg.enable {
    security.wrappers.espanso = lib.mkIf (cfg.package.waylandSupport or false) {
      capabilities = "cap_dac_override+p";
      owner = "root";
      group = "root";
      source = lib.getExe (
        pkgs.espanso-wayland.override { securityWrapperPath = config.security.wrapperDir; }
      );
    };
    systemd.user.services.espanso = {
      description = "Espanso daemon";
      serviceConfig = {
        ExecStart = "${lib.getExe cfg.package} daemon";
        ExecStart = "${
          if (cfg.package.waylandSupport or false) then
            "${config.security.wrapperDir}/espanso"
          else
            lib.getExe cfg.package
        } daemon";
        Restart = "on-failure";
      };
      wantedBy = [ "default.target" ];
+4 −0
Original line number Diff line number Diff line
@@ -523,6 +523,10 @@ in
  ergo = runTest ./ergo.nix;
  ergochat = runTest ./ergochat.nix;
  ersatztv = handleTest ./ersatztv.nix { };
  espanso = import ./espanso.nix {
    inherit (pkgs) lib;
    inherit runTest;
  };
  esphome = runTest ./esphome.nix;
  etc = pkgs.callPackage ../modules/system/etc/test.nix { inherit evalMinimalConfig; };
  etcd = import ./etcd/default.nix { inherit pkgs runTest; };
+86 −0
Original line number Diff line number Diff line
{ lib, runTest }:
let
  makeTest =
    conf:
    runTest {
      name = "espanso";
      meta.maintainers = with lib.maintainers; [ n8henrie ];

      nodes.machine =
        let
          base =
            { pkgs, config, ... }:
            {
              imports = [ ./common/user-account.nix ];
              services.espanso.enable = true;
              system.activationScripts.espanso-config = {
                deps = [ "users" ];
                text =
                  let
                    confdir = "${config.users.users.alice.home}/.config/espanso";
                    espanso_conf =
                      let
                        settingsFormat = pkgs.formats.yaml { };
                      in
                      settingsFormat.generate "base.yaml" {
                        matches = [
                          {
                            trigger = ":nixostest";
                            replace = "My NixOS Test Passed!";
                          }
                        ];
                      };
                  in
                  ''
                    mkdir -p ${confdir}/{config,match}
                    touch ${confdir}/config/default.yml
                    cp ${espanso_conf} ${confdir}/match/base.yml
                    chown -R ${config.users.users.alice.name} ${confdir}
                  '';
              };
            };
        in
        lib.mkMerge [
          base
          conf
        ];

      enableOCR = true;
      testScript = ''
        machine.wait_for_unit("graphical.target")
        machine.wait_for_text("Espanso is running!")
        machine.send_chars(":nixostest")
        machine.wait_for_text("My NixOS Test Passed!")
      '';
    };
in
{
  x11 = makeTest {
    imports = [ ./common/x11.nix ];
    test-support.displayManager.auto.user = "alice";
    users.users.alice.extraGroups = [ "input" ];
  };
  wayland = makeTest (
    { pkgs, config, ... }:
    {
      programs.sway.enable = true;
      services = {
        greetd =
          let
            initial_session = {
              user = config.users.users.alice.name;
              command = lib.getExe pkgs.sway;
            };
          in
          {
            enable = true;
            settings = {
              inherit initial_session;
              default_session = initial_session;
            };
          };
        espanso.package = pkgs.espanso-wayland;
      };
    }
  );
}
+32 −10
Original line number Diff line number Diff line
@@ -20,11 +20,14 @@
  wl-clipboard,
  wxGTK32,
  makeWrapper,
  securityWrapperPath ? null,
  nix-update-script,
  stdenv,
  waylandSupport ? false,
  x11Support ? stdenv.hostPlatform.isLinux,
  testers,
  nixosTests,
  fetchpatch,
}:
# espanso does not support building with both X11 and Wayland support at the same time
assert stdenv.hostPlatform.isLinux -> x11Support != waylandSupport;
@@ -87,12 +90,28 @@ rustPlatform.buildRustPackage (finalAttrs: {
    xdotool
  ];

  postPatch = lib.optionalString stdenv.hostPlatform.isDarwin ''
  patches = [
    # remove when version > 2.3.0
    (fetchpatch {
      name = "fix-welcome-screen-expansion.patch";
      url = "https://github.com/espanso/espanso/commit/5d5fc84df695d628d1d9c3e7e3854c2991a64d64.patch";
      hash = "sha256-dhoqq0V8b8mGvZvPInHiHKGmGDDFO/SH5HqMY7EA134=";
    })
  ];

  postPatch =
    lib.optionalString stdenv.hostPlatform.isDarwin ''
      substituteInPlace scripts/create_bundle.sh \
        --replace-fail target/mac/ $out/Applications/ \
        --replace-fail /bin/echo ${coreutils}/bin/echo
      substituteInPlace espanso/src/path/macos.rs  espanso/src/path/linux.rs \
        --replace-fail '"/usr/local/bin/espanso"' '"${placeholder "out"}/bin/espanso"'
    ''
    + lib.optionalString (securityWrapperPath != null) ''
      substituteInPlace espanso/src/cli/daemon/mod.rs \
        --replace-fail \
          'std::env::current_exe().expect("unable to obtain espanso executable location");' \
          'std::ffi::OsString::from("${securityWrapperPath}/espanso");'
    '';

  # Some tests require networking
@@ -123,10 +142,13 @@ rustPlatform.buildRustPackage (finalAttrs: {
      '';

  passthru = {
    tests.version = testers.testVersion {
    inherit waylandSupport;
    tests = nixosTests.espanso // {
      version = testers.testVersion {
        package = finalAttrs.finalPackage;
        inherit (finalAttrs) version;
      };
    };
    updateScript = nix-update-script { };
  };