desktop

package
v0.5.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2026 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package desktop discovers active graphical desktop sessions on the host so user-scoped actions (Flatpak --user installs, shell scripts that need a real $HOME and DBus session bus, etc.) can fan out to every currently-signed-in user instead of running under the agent's own root context.

r, _ := exec.NewRunner(exec.Direct) // the agent runs as root
m, err := desktop.New(r)
if err != nil { ... }
sessions, err := m.ActiveSessions(ctx)

The discovery contract is intentionally narrow: only sessions that

  • are local (Remote=no)
  • are graphical (Type ∈ {x11, wayland, mir})
  • are active (Active=yes)

count. SSH sessions, getty TTYs, headless sessions, and inactive graphical sessions are filtered out — they don't have a usable XDG_RUNTIME_DIR / DBus session bus that user-scoped commands need.

desktop is a single-implementation capability (design §3.8): it exposes the Manager interface for shape-uniformity with the rest of the SDK. There is no Backend argument — only the required Runner.

Locale handling

The loginctl PROBES (ActiveSessions and its helpers) run through the injected Runner, which forces the C locale; this is deliberate — the SDK parses loginctl's stderr to distinguish "no logind here" from a real fault, and that matching must be locale-stable. User-scoped commands are fanned out with desktop.RunAsRunner, which also runs through the Runner.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnvFor

func EnvFor(s Session) []string

EnvFor builds the minimum environment a user-scoped command needs to behave like it was launched from inside that user's desktop session: HOME, USER, LOGNAME, XDG_RUNTIME_DIR, plus DBUS_SESSION_BUS_ADDRESS (so commands that talk to the user's session bus — Flatpak's notification path, GNOME settings, etc. — find it without falling back to a fresh autolaunched bus).

PATH is not added here — callers append it (via UserPath) so they can pick a curated value rather than inherit the agent's PATH; RunAsRunner passes UserPath(s) as the trusted child PATH.

func RunAsRunner

func RunAsRunner(base pmexec.Runner, s Session) (pmexec.Runner, error)

RunAsRunner wraps a base [exec.Runner] so every command it runs executes AS the session's user via runuser, with that user's desktop session environment (HOME, USER, XDG_RUNTIME_DIR, the session bus address) and the curated per-user PATH.

It lets a capability built on the SDK Runner operate on behalf of a specific logged-in user without that capability knowing anything about runuser — most usefully a per-user Flatpak Manager:

r, _ := exec.NewRunner(exec.Direct) // the agent runs as root
ru, _ := desktop.RunAsRunner(r, session)
fp, _ := pkg.New(pkg.Flatpak, ru, pkg.WithUserScope())
fp.Install(ctx, pkg.InstallOptions{Remote: "flathub"}, "org.x.App") // installs for `session`

The base Runner MUST run as root: runuser performs the privilege DROP to the target user, so the wrapped command is never escalated again. The caller's command env is screened by the same hijack blocklist the Runner enforces.

func UserPath

func UserPath(s Session) string

UserPath returns the curated PATH a per-user command should run with. It is built from the session — never from the agent's (root's) PATH — so a user-scoped command does not inherit root-only entries, and the user's own bin dirs come first so ~/.local/bin shadows a system binary of the same name. sbin dirs are included because usr-merged distros put them on every user's default PATH and a user running an sbin binary does so unprivileged (their own UID); excluding them only breaks command resolution without any privilege benefit.

Types

type Manager

type Manager interface {
	// ActiveSessions returns every active local graphical session on the host,
	// ready for fanning a user-scoped command out to each. It returns an empty
	// slice (not an error) when loginctl is missing or no logind is reachable.
	ActiveSessions(ctx context.Context) ([]Session, error)
	// HomeUsers enumerates every Linux account whose home lives directly under
	// the configured home root (default /home). It is the primitive for
	// "do something for every offline user".
	HomeUsers(ctx context.Context) ([]Session, error)
	// UsersWithFlatpakInstall returns the subset of HomeUsers whose per-user
	// Flatpak repository contains appID.
	UsersWithFlatpakInstall(ctx context.Context, appID string) ([]Session, error)
}

Manager is the desktop discovery + user-scoped-command surface.

func New

func New(runner pmexec.Runner, opts ...Option) (Manager, error)

New builds a desktop Manager driven by runner. A nil runner is rejected (fail-closed). New is pure — it does not probe the host.

type Option

type Option func(*manager)

Option configures a Manager at construction.

func WithHomeRoot

func WithHomeRoot(dir string) Option

WithHomeRoot overrides the directory HomeUsers enumerates (default /home). Primarily for tests; production uses the default.

type Session

type Session struct {
	// ID is the systemd-logind session ID (e.g. "c1", "2"). Stable for
	// the lifetime of the session; not stable across logout/login.
	ID string
	// Username is the Linux account name (`loginctl show-session ... -p Name`).
	Username string
	// UID is the numeric user ID. Used to derive XDG_RUNTIME_DIR.
	UID int
	// GID is the user's primary group ID, looked up via os/user.
	GID int
	// Home is the user's home directory, looked up via os/user. Required
	// for Flatpak --user installs (which resolve everything against
	// $HOME) and for shell scripts that expect a sane working directory.
	Home string
	// RuntimeDir is /run/user/<UID>. Populated unconditionally — the
	// caller decides whether to verify it exists before invoking a
	// command that needs it (e.g. anything touching DBus).
	RuntimeDir string
	// Type is the session type (`x11`, `wayland`, `mir`). Always one of
	// the graphical types because non-graphical sessions are filtered
	// out before construction.
	Type string
}

Session is a single active graphical desktop session.

All fields are populated from systemd-logind (loginctl) plus a passwd lookup for Home / GID. Callers should treat the struct as immutable — the SDK constructs it once per ActiveSessions call.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL