See also: https://github.com/gentoo/gentoo/pull/47042 (pre-review discussion +contains example usage by way of xfce4-polkit) Ensures, via OnlyShowIn=<env>, that any generated autostart file is only, but surely, going to be started in its designated environment. This is important because we want to avoid having more than one polkit agent autostarting, and racing, in the same environment. It is then the desktop environments' responsibility to pull in their designated polkit-agent which works out of the box by matching the `DesktopNames` entry in the env session file. Which most of them already do for a very long time, except Xfce. Any WM without its own polkit-agent binary may set up an autostart file this way too, provided it has XDG autostart capability. Catch PN=polkit-gnome to keep existing filename in an attempt to preserve existing user modifications. Signed-off-by: Andreas Sturmlechner <astu…@gentoo.org> --- eclass/pkagent-autostart.eclass | 132 ++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 eclass/pkagent-autostart.eclass diff --git a/eclass/pkagent-autostart.eclass b/eclass/pkagent-autostart.eclass new file mode 100644 index 000000000000..2bcacda8b2b7 --- /dev/null +++ b/eclass/pkagent-autostart.eclass @@ -0,0 +1,132 @@ +# Copyright 2026 Gentoo Authors +# Distributed under the terms of the GNU General Public License v2 + +# @ECLASS: pkagent-autostart.eclass +# @MAINTAINER: +# free…@gentoo.org +# @SUPPORTED_EAPIS: 8 9 +# @BLURB: Installs aptly configured autostart desktop file for the chosen +# environment. May be used by environments that do not come with their own +# polkit-agent, thus reuse one of the known implementations. + +case ${EAPI} in + 8|9) ;; + *) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;; +esac + +if [[ -z ${_PKAGENT_AUTOSTART_ECLASS} ]]; then +_PKAGENT_AUTOSTART_ECLASS=1 + +# @FUNCTION: make_pkagent_autostart_entry +# @USAGE: <env> <pkagent> +# @DESCRIPTION: +# Make a session specific .desktop file and install it in /etc/xdg/autostart. +# See also: +# - https://specifications.freedesktop.org/menu/latest/onlyshowin-registry.html +# - https://specifications.freedesktop.org/desktop-entry/latest-single/#key-onlyshowin +# +# @CODE +# env: If set, should either correspond with registered values for Desktop +# Environments, or be made sure to match an existing session file's +# DesktopNames entry. Value of env is passed as OnlyShowIn=<value>. +# Non-registered environments shall be prefixed by the string "X-" to +# pass desktop-file-utils validation, but will work either way. +# If empty, will set Hidden=true instead of OnlyShowIn. Also affects +# desktop filename: 'polkit-${env,,}-authentication-agent.desktop' +# pkagent: Target (known) polkit-agent package to choose from for Exec +# If unknown, value of pkagent is directly passed as Exec=<value>, +# which must be either in PATH or be an absolute path to a binary. +# @CODE +# +# Example usage: +# @CODE +# XFCE polkit-gnome +# @CODE +make_pkagent_autostart_entry() { + local execpath fname name onlyshowin + if [[ $# -ne 2 ]]; then + die "${FUNCNAME[0]} must be passed exactly two arguments" + fi + + # Registered OnlyShowIn values + case ${1} in + COSMIC | \ + GNOME | \ + GNOME-Classic | \ + GNOME-Flashback | \ + KDE | \ + LXDE | \ + LXQt | \ + MATE | \ + Razor | \ + ROX | \ + TDE | \ + Unity | \ + XFCE | \ + EDE | \ + Cinnamon | \ + Pantheon | \ + Phosh | \ + DDE | \ + Endless | \ + Old | \ + X-*) + onlyshowin="OnlyShowIn=${1};" + name="${PN/#X-/}" + ;; + "") + onlyshowin="Hidden=true" + name="${PN/#polkit/}" + name="${name/--/-}" + ;; + *) + onlyshowin="OnlyShowIn=${1};" + name="${PN/#polkit/}" + elog "${1} is not a registered OnlyShowIn value in the Desktop Application" + elog "Autostart Specification. It will not do anything without a corresponding" + elog "DesktopNames entry from a session desktop file." + ;; + esac + + # Known 'standard' polkit-agent packages + case ${2} in + lxqt-policykit) + execpath="/usr/bin/lxqt-policykit-agent" ;; + lxsession) + execpath="/usr/bin/lxpolkit" ;; + mate-polkit) + execpath="/usr/libexec/polkit-mate-authentication-agent-1" ;; + polkit-gnome) + execpath="/usr/libexec/polkit-gnome-authentication-agent-1" ;; + polkit-kde-agent) + execpath="/usr/libexec/polkit-kde-authentication-agent-1" ;; + *) + execpath="${2}" ;; + esac + + # preserve existing filename for a de facto legacy standard in GTK world + if [[ ${PN} == polkit-gnome ]]; then + fname="polkit-gnome-authentication-agent-1.desktop" + else + fname="polkit-${name,,}-authentication-agent.desktop" + fi + + cat > "${T}/${fname}" <<- _EOF_ || die + [Desktop Entry] + Name=PolicyKit Authentication Agent + Comment=PolicyKit Authentication Agent + Exec=${execpath} + Icon=dialog-password + Terminal=false + Type=Application + ${onlyshowin} + NoDisplay=true + _EOF_ + + ( + insinto /etc/xdg/autostart + doins "${T}/${fname}" + ) +} + +fi
-- 2.56.1