Skip to content

linux: add dbus and systemd activation services#7433

Merged
mitchellh merged 5 commits intoghostty-org:mainfrom
jcollie:dbus-and-systemd-activation
Jun 24, 2025
Merged

linux: add dbus and systemd activation services#7433
mitchellh merged 5 commits intoghostty-org:mainfrom
jcollie:dbus-and-systemd-activation

Conversation

@jcollie
Copy link
Copy Markdown
Member

@jcollie jcollie commented May 24, 2025

No description provided.

@jcollie jcollie requested a review from a team as a code owner May 24, 2025 22:01
@jcollie jcollie force-pushed the dbus-and-systemd-activation branch 2 times, most recently from 0f2a2b9 to b0b8dd6 Compare May 24, 2025 22:22
Comment thread src/apprt/gtk/App.zig Outdated
@alaviss
Copy link
Copy Markdown
Contributor

alaviss commented May 25, 2025

As a general note, on Linux an user may change the application id by setting the class configuration variable. This meant that the D-Bus name of Ghostty would then differs from the well-known name specified by the desktop file and D-Bus service, which would break the feature.

@jcollie
Copy link
Copy Markdown
Member Author

jcollie commented May 25, 2025

As a general note, on Linux an user may change the application id by setting the class configuration variable. This meant that the D-Bus name of Ghostty would then differs from the well-known name specified by the desktop file and D-Bus service, which would break the feature.

TBF, the user can do a lot of things to break stuff. Probably the only way to deal with this is to add some documentation to the class config that tells users what might happen.

@mitchellh
Copy link
Copy Markdown
Contributor

I don't have time to review in depth but I'm also starting to wonder if we should just have a launched-from configuration to have the flag and then derive default behaviors from that. I agree the logic here is getting a little messy. But moreso, the performance cost to detect this stuff is getting a little nasty too.

Comment thread nix/package.nix
Comment thread src/os/dbus.zig
@jcollie
Copy link
Copy Markdown
Member Author

jcollie commented May 27, 2025

I don't have time to review in depth but I'm also starting to wonder if we should just have a launched-from configuration to have the flag and then derive default behaviors from that. I agree the logic here is getting a little messy. But moreso, the performance cost to detect this stuff is getting a little nasty too.

The main problem I think would be macOS - AFAIK you can't specify a CLI flag if you're launching Ghostty from the dock or even just double-clicking the app.

There are several macOS behaviors that depend on detecting if Ghostty was launched from the desktop vs some other way.

@alaviss
Copy link
Copy Markdown
Contributor

alaviss commented May 28, 2025

you can't specify a CLI flag if you're launching Ghostty from the dock or even just double-clicking the app.

I think this can be solved by adding a wrapper shell script that adds the CLI flag and have the .app bundle launches it instead of ghostty binary directly.

@jcollie
Copy link
Copy Markdown
Member Author

jcollie commented May 28, 2025

you can't specify a CLI flag if you're launching Ghostty from the dock or even just double-clicking the app.

I think this can be solved by adding a wrapper shell script that adds the CLI flag and have the .app bundle launches it instead of ghostty binary directly.

That does not strike me as a very robust solution. I think what might help is breaking some of this logic out into a separate function rather than a giant boolean expression in a if statement. I don't think the performance of the detection matters a whole lot since it's done once at startup, and if that information is needed outside of startup it should be done once at startup and saved in a global variable for future use.

@alaviss
Copy link
Copy Markdown
Contributor

alaviss commented May 28, 2025

I personally am not concerned about performance here, but code complexity and the use of heuristics. It would be more robust to have the execution site tell us directly what kind of behavior do they want than trying to guess what it is.

As a plus, if there's a switch, then it's trivial to perform manual testing of these behaviors.

@jcollie jcollie force-pushed the dbus-and-systemd-activation branch from b0b8dd6 to 13d5cd8 Compare May 29, 2025 16:36
@pluiedev
Copy link
Copy Markdown
Member

pluiedev commented May 29, 2025

Adding a manual flag would be nice I think, although I don't think the heuristics should entirely go away - for instance, someone can write a systemd service on their own without ever specifying via CLI that Ghostty should consider itself launched via systemd. This obviously wouldn't be an issue for the official systemd service of course, but it is something that could happen (especially when one uses a window manager that expects more user intervention and customization than normal desktop environments).

Instead, we should probably respect a launched-from flag of some sort, and if one isn't present we attempt to find out using heuristics just how the app was launched. That way we get the best of both worlds.

I do think the heuristics need a LOT more links pointing to official D-Bus/systemd documentation reaffirming the veracity and reliability of these environment variables, though... ;)

Comment thread src/build/GhosttyResources.zig Outdated
Comment thread src/os/systemd.zig Outdated
@alaviss
Copy link
Copy Markdown
Contributor

alaviss commented May 29, 2025

for instance, someone can write a systemd service on their own without ever specifying via CLI that Ghostty should consider itself launched via systemd

I think this is simply not a valid case. If they decided to write a service like that, then are they really expecting Ghostty to behave differently from when they run ghostty on their CLI?

This might also spell trouble for people using XDG autostart, since systemd would generate services for those using systemd-xdg-autostart-generator(8). In that case, Ghostty would behave radically different from when it is launched normally via the .desktop file, which is not something to be expected.

Copy link
Copy Markdown
Contributor

@mitchellh mitchellh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main problem I think would be macOS - AFAIK you can't specify a CLI flag if you're launching Ghostty from the dock or even just double-clicking the app.

The main entry point is different on macOS and we already use it to specify where it was launched from: https://github.com/ghostty-org/ghostty/blob/1ff91625981ef92add92624c01b6b9095c365ed1/macos/Sources/App/macOS/main.swift This could be modified easily.

I think what @pluiedev said is probably best: introduce the config option, and change the config defaults to auto-detect. This already only happens once (per config reload which is rare) and we get caching this way.

That approach would get rid of the performance issue, make testing easier because we can simulate different launch environments, centralize detection in one place, we can probably more easily unit test as well instead of littering it throughout the codebase, and it lets users have an escape hatch to manually override our behavior if we detect incorrectly.

I looked at where we call these functions and in most cases we easily have a config available. The one place we don't is the locale loading for macOS but we can add a special case there I think because we have to load the locale before configuration is loaded necessarily for error messages at least.

I can try to extract that myself in a separate PR so we don't confuse it with the dbus/systemd stuff in here.

Comment thread src/os/dbus.zig
Comment thread src/os/systemd.zig
mitchellh added a commit that referenced this pull request Jun 2, 2025
Related to #7433

This extracts our "launched from desktop" logic into a config option.
The default value is detection using the same logic as before, but now
this can be overridden by the user.

This also adds the systemd and dbus activation sources from #7433.

There are a number of reasons why we decided to do this:

  1. It automatically gets us caching since the configuration is only
     loaded once (per reload, a rare occurrence).

  2. It allows us to override the logic when testing. Previously, we
     had to do more complex environment faking to get the same
     behavior.

  3. It forces exhaustive switches in any desktop handling code, which
     will make it easier to ensure valid behaviors if we introduce new
     launch sources (as we are in #7433).

  4. It lowers code complexity since callsites don't need to have N
     `launchedFromX()` checks and can use a single value.
@mitchellh
Copy link
Copy Markdown
Contributor

All reviews, esp @jcollie, see #7503. You'll have to rebase your changes onto that, it just focuses on the launched-from flag.

mitchellh added a commit that referenced this pull request Jun 2, 2025
Related to #7433

This extracts our "launched from desktop" logic into a config option.
The default value is detection using the same logic as before, but now
this can be overridden by the user.

This also adds the systemd and dbus activation sources from #7433.

There are a number of reasons why we decided to do this:

1. It automatically gets us caching since the configuration is only
loaded once (per reload, a rare occurrence).

2. It allows us to override the logic when testing. Previously, we had
to do more complex environment faking to get the same behavior.

3. It forces exhaustive switches in any desktop handling code, which
will make it easier to ensure valid behaviors if we introduce new launch
sources (as we are in #7433).

4. It lowers code complexity since callsites don't need to have N
`launchedFromX()` checks and can use a single value.
@jcollie jcollie force-pushed the dbus-and-systemd-activation branch from 13d5cd8 to 4a049d7 Compare June 2, 2025 17:36
@jcollie
Copy link
Copy Markdown
Member Author

jcollie commented Jun 2, 2025

Patch has been rebased on top of #7503

@tristan957
Copy link
Copy Markdown
Member

Do the title of the PR and commit need to change? Also, will there be any accompanying documentation about this? I'm a pretty experienced Linux person, but I don't have any idea what I could do with this.

Copy link
Copy Markdown
Contributor

@mitchellh mitchellh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One simple change requested. Looking good!

Comment thread src/apprt/gtk/App.zig Outdated
Copy link
Copy Markdown
Contributor

@alaviss alaviss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some notes about the desktop and service files

Comment thread dist/linux/app.desktop Outdated
Comment thread dist/linux/dbus.service Outdated
Comment thread dist/linux/systemd.service Outdated
Comment thread dist/linux/dbus.service
Comment thread dist/linux/app.desktop
Type=Application
Comment=A terminal emulator
Exec=ghostty
TryExec=ghostty
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the Desktop Entry Specification, this key is used to detect whether the application is installed so as to either show or autostart it.

For Ghostty the only way this file made it to the user system is if it's installed, so this key doesn't do much.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only way the .desktop file makes sense is if it's installed somewhere so presumably the user has installed the Ghostty binary somewhere too.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I'm saying, this key does nothing useful and can be removed.

Comment thread dist/linux/app.desktop
@jcollie
Copy link
Copy Markdown
Member Author

jcollie commented Jun 2, 2025

Also, will there be any accompanying documentation about this? I'm a pretty experienced Linux person, but I don't have any idea what I could do with this.

It should "just work" if all the files get installed in the right place, as I would hope that they would when Ghostty is installed via a package. Not sure what we should document as I don't think that we need to document how .desktop and
the other .service files work - we don't document what users should do with the current .desktop file as that's more of a packager concern.

As far as the title of the PR is concerned I think removing the word option might be a good idea.

@alaviss
Copy link
Copy Markdown
Contributor

alaviss commented Jun 2, 2025

As noted in #7433 (comment), this might break when class is configured to a non-default value.

I think class documentation should be updated to reflect this change.

@jcollie
Copy link
Copy Markdown
Member Author

jcollie commented Jun 2, 2025

Added some documentation to the class config that explains how changing it might affect launching.

@jcollie jcollie changed the title linux: add option for dbus and systemd activation linux: add dbus and systemd activation services Jun 2, 2025
mitchellh added a commit that referenced this pull request Jun 3, 2025
This is done to match against the default application id when Ghostty is
built using debug configuration, preparing the Flatpak version for D-Bus
activation support (#7433).
@jcollie jcollie force-pushed the dbus-and-systemd-activation branch from 51e40ec to a96dc8a Compare June 10, 2025 19:59
@jcollie jcollie force-pushed the dbus-and-systemd-activation branch from a96dc8a to c1d04a6 Compare June 13, 2025 15:22
@mitchellh
Copy link
Copy Markdown
Contributor

@pluiedev's changes were resolved so I'm going to merge this. Thanks 😄

@mitchellh mitchellh merged commit 977cd53 into ghostty-org:main Jun 24, 2025
74 of 76 checks passed
@github-actions github-actions Bot added this to the 1.2.0 milestone Jun 24, 2025
@jcollie jcollie deleted the dbus-and-systemd-activation branch June 24, 2025 22:16
mitchellh added a commit that referenced this pull request Jun 24, 2025
Flatpak currently does not export systemd user units. As such, remove
references to it from D-Bus services to prevent D-Bus daemon from trying
to start a non-existent service.

Additionally, make sure that the D-Bus service name is correct for debug
builds.

Follow up to #7433
mitchellh added a commit that referenced this pull request Jun 25, 2025
Reverts two commits:

977cd53
820b7e4

These break build from source on Linux for two reasons:

1.) The systemd user service needs to be installed in the `share`
prefix, not the `lib` prefix. This lets it get picked up in `~/.local`
but is also correct for just standard FHS paths.

2.) The `ghostty` path in the systemd user service needs to be absolute.
We should interpolate in the build install prefix to form an absolute
path.
mitchellh added a commit that referenced this pull request Jun 25, 2025
Reverts two commits:

977cd53
820b7e4

These break build from source on Linux for two reasons:

1.) The systemd user service needs to be installed in the `share`
prefix, not the `lib` prefix. This lets it get picked up in `~/.local`
but is also correct for just standard FHS paths.

2.) The `ghostty` path in the systemd user service needs to be absolute.
We should interpolate in the build install prefix to form an absolute
path.
jcollie added a commit to jcollie/ghostty that referenced this pull request Jun 25, 2025
This replaces ghostty-org#7433. The improvements are:

1) Install the systemd user service in the proper directory depending
on if it's a 'user' install or a 'system' install. This is controlled
either by using the `--system` build flag (as most packages will) or by
the `-Dsystem-package` flag.

2) Add the absolute path to the `ghostty` binary in the application
file, the DBus service, and the systemd user service. This is done so
that they do not depend on `ghostty` being in the `PATH` of whatever
is launching Ghostty. That `PATH` is not necessarily the same as the
`PATH` in a user shell (especially for DBus activation and systemd user
services).

3) Adjust the DBus bus name that is expected by the system depending on
the optimization level that Ghostty is compiled with.
jcollie added a commit to jcollie/ghostty that referenced this pull request Jun 26, 2025
This replaces ghostty-org#7433. The improvements are:

1) Install the systemd user service in the proper directory depending
on if it's a 'user' install or a 'system' install. This is controlled
either by using the `--system` build flag (as most packages will) or by
the `-Dsystem-package` flag.

2) Add the absolute path to the `ghostty` binary in the application
file, the DBus service, and the systemd user service. This is done so
that they do not depend on `ghostty` being in the `PATH` of whatever
is launching Ghostty. That `PATH` is not necessarily the same as the
`PATH` in a user shell (especially for DBus activation and systemd user
services).

3) Adjust the DBus bus name that is expected by the system depending on
the optimization level that Ghostty is compiled with.
mitchellh pushed a commit to jcollie/ghostty that referenced this pull request Jun 26, 2025
This replaces ghostty-org#7433. The improvements are:

1) Install the systemd user service in the proper directory depending
on if it's a 'user' install or a 'system' install. This is controlled
either by using the `--system` build flag (as most packages will) or by
the `-Dsystem-package` flag.

2) Add the absolute path to the `ghostty` binary in the application
file, the DBus service, and the systemd user service. This is done so
that they do not depend on `ghostty` being in the `PATH` of whatever
is launching Ghostty. That `PATH` is not necessarily the same as the
`PATH` in a user shell (especially for DBus activation and systemd user
services).

3) Adjust the DBus bus name that is expected by the system depending on
the optimization level that Ghostty is compiled with.
mitchellh added a commit that referenced this pull request Jun 26, 2025
This replaces #7433. The improvements are:

1) Install the systemd user service in the proper directory depending on
if it's a 'user' install or a 'system' install. This is controlled
either by using the `--system` build flag (as most packages will) or by
the `-Dsystem-package` flag.

2) Add the absolute path to the `ghostty` binary in the application
file, the DBus service, and the systemd user service. This is done so
that they do not depend on `ghostty` being in the `PATH` of whatever is
launching Ghostty. That `PATH` is not necessarily the same as the `PATH`
in a user shell (especially for DBus activation and systemd user
services).

3) Adjust the DBus bus name that is expected by the system depending on
the optimization level that Ghostty is compiled with.
jcollie added a commit to jcollie/ghostty that referenced this pull request Jul 16, 2025
The XDG Freedesktop Portal has a _major_ undocumented requirement for
programs that are launched/controlled by `systemd` to interact with the
Portal. The unit _must_ be named `app-<appid>.service`. The Portal uses
the systemd unit name figure out what the program's application ID is
and it will only look at unit names that begin with `app-`. I can find
no place that this is documented other than by inspecting the code or the
issue and PR that introduced this feature. See the following code:

https://github.com/flatpak/xdg-desktop-portal/blob/7d4d48cf079147c8887da17ec6c3954acd5a285c/src/xdp-utils.c#L152-L220

This may fix many people's issues with getting global shortcuts
to work.

Note that this is a breaking change if you have been using Ghostty
compiled from source since ghostty-org#7433 was merged. You will need to ensure
that any Ghosty systemd unit files _not_ prefixed with `app-` are
deleted.

Originally discussed on Discord:

https://discord.com/channels/1005603569187160125/1394845362186879026

Co-authored-by: ambareeshbalaji@gmail.com
jcollie added a commit to jcollie/ghostty that referenced this pull request Jul 16, 2025
The XDG Freedesktop Portal has a _major_ undocumented requirement for
programs that are launched/controlled by `systemd` to interact with the
Portal. The unit _must_ be named `app-<appid>.service`. The Portal uses
the systemd unit name figure out what the program's application ID is
and it will only look at unit names that begin with `app-`. I can find
no place that this is documented other than by inspecting the code or the
issue and PR that introduced this feature. See the following code:

https://github.com/flatpak/xdg-desktop-portal/blob/7d4d48cf079147c8887da17ec6c3954acd5a285c/src/xdp-utils.c#L152-L220

This may fix many people's issues with getting global shortcuts
to work.

Note that this is a breaking change if you have been using Ghostty
compiled from source since ghostty-org#7433 was merged. You will need to ensure
that any Ghosty systemd unit files _not_ prefixed with `app-` are
deleted.

Originally discussed on Discord:

https://discord.com/channels/1005603569187160125/1394845362186879026

Co-authored-by: ambareeshbalaji@gmail.com
jcollie added a commit to jcollie/ghostty that referenced this pull request Jul 16, 2025
The XDG Freedesktop Portal has a _major_ undocumented requirement for
programs that are launched/controlled by `systemd` to interact with the
Portal. The unit _must_ be named `app-<appid>.service`. The Portal uses
the systemd unit name figure out what the program's application ID is
and it will only look at unit names that begin with `app-`. I can find
no place that this is documented other than by inspecting the code or the
issue and PR that introduced this feature. See the following code:

https://github.com/flatpak/xdg-desktop-portal/blob/7d4d48cf079147c8887da17ec6c3954acd5a285c/src/xdp-utils.c#L152-L220

This may fix many people's issues with getting global shortcuts
to work.

Note that this is a breaking change if you have been using Ghostty
compiled from source since ghostty-org#7433 was merged. You will need to ensure
that any Ghosty systemd unit files _not_ prefixed with `app-` are
deleted.

Original discussion/PR in the XDG Desktop Portal repository:

flatpak/xdg-desktop-portal#579
flatpak/xdg-desktop-portal#719

Originally discussed on Discord:

https://discord.com/channels/1005603569187160125/1394845362186879026

Co-authored-by: ambareeshbalaji@gmail.com
jcollie added a commit that referenced this pull request Jul 16, 2025
The XDG Freedesktop Portal has a _major_ undocumented requirement for
programs that are launched/controlled by `systemd` to interact with the
Portal. The unit _must_ be named `app-<appid>.service`. The Portal uses
the systemd unit name figure out what the program's application ID is
and it will only look at unit names that begin with `app-`. I can find
no place that this is documented other than by inspecting the code or
the
issue and PR that introduced this feature. See the following code:


https://github.com/flatpak/xdg-desktop-portal/blob/7d4d48cf079147c8887da17ec6c3954acd5a285c/src/xdp-utils.c#L152-L220

This may fix many people's issues with getting global shortcuts
to work.

Note that this is a breaking change if you have been using Ghostty
compiled from source since
#7433 was merged. You will
need to ensure
that any Ghosty systemd unit files _not_ prefixed with `app-` are
deleted.

Original discussion/PR in the XDG Desktop Portal repository:

flatpak/xdg-desktop-portal#579
flatpak/xdg-desktop-portal#719

Originally discussed on Discord:

https://discord.com/channels/1005603569187160125/1394845362186879026

Co-authored-by: ambareeshbalaji@gmail.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants