3

I'm setting up a Debian system, with LXDE as the Desktop Environment (really, I'm just remastering a Raspberry Pi OS image).

I have an application that ships with a .desktop file. Cool!

Unfortunately that MyApplication.desktop file contains the following line that prevents the icon from being shown in the menu:

NoDisplay=true

Now, I would like to find a generic way to override just this single field in the MyApplication.desktop file for the entire system, without touching any other fields - think of it as an overlay.

Especially, I try to avoid a solution like this...

sed -e 's/^NoDisplay=true/NoDisplay=false/' /usr/share/applications/MyApplication.desktop \
    > /usr/local/share/applications/MyApplication.desktop

... as this would lose any changes of the MyApplication.desktop introduced by newer upstream versions of that file (and presumable leave the menu-entry intact, even if after I've uninstalled MyApplication from the system)

Obviously, the standard does not allow only partial .desktop files, e.g. this would not work:

$ cat /usr/share/applications/MyApplication.desktop
[Desktop Entry]
Name=My Cool Application
GenericName=My Application
Exec=myapp %f
Icon=myapp
Type=Application
Terminal=false
Categories=Application
NoDisplay=true

$ cat /usr/local/share/applications/MyApplication.desktop
[Desktop Entry]
NoDisplay=false

Similarily, there's no include mechanism:

$ cat /usr/local/share/applications/MyApplication.desktop
include=/usr/share/applications/MyApplication.desktop
[Desktop Entry]
NoDisplay=false

what are my options to achieve that?

2 Answers 2

2

Desktop entry (.desktop) files in /usr/share/applications would be overwritten on package upgrade. To override the file without losing changes, place a copy in ~/.local/share/applications and make your changes here.


Updated for selective, automated override of original desktop file on detecting changes

Goal: monitor for changes to desktop file and execute script to effect desired changes.

  1. Create script /usr/local/bin/update-myapp-desktop-file.sh to make desired changes to desktop file:
#!/usr/bin/env bash

sed -i 's/^NoDisplay=true/NoDisplay=false/' /usr/share/applications/MyApplication.desktop

Mark script as executable: chmod 755 /usr/local/bin/update-myapp-desktop-file.sh

  1. Create systemd service file /etc/systemd/system/myapp-desktop-file.service to run script:
[Unit] 
Description=Update MyApp desktop file

[Service]
ExecStart=/usr/local/bin/update-myapp-desktop-file.sh
  1. Create corresponding systemd path file /etc/systemd/system/myapp-desktop-file.path to monitor for changes to desktop file:
[Unit]
Description=Monitor for changes to MyApp desktop file

[Path]
PathModified=/usr/share/applications/MyApplication.desktop
Unit=myapp-desktop-file.service

[Install]
WantedBy=multi-user.target
  1. Verify, enable, and start systemd path file:
systemd-analyze verify /etc/systemd/system/myapp-desktop-file.*
systemctl enable myapp-desktop-file.path
systemctl start myapp-desktop-file.path
2
  • not a problem here, because i'm creating packages. (apart from that: it seems your answer doesn't address the actual problem, just how to make any solution to the problem permanent) Commented Aug 28, 2023 at 9:06
  • @umläute See updated answer, it's a workaround but I don't know any better. Commented Aug 28, 2023 at 19:02
1

I wouldn't worry too much about new versions of .desktop files - they rarely change (and when they do, the changes are usually very small and easy to hand-edit if required...or use diff and patch to merge any changes).

If you're concerned about it, then whenever you make an edited copy of a .desktop file into /usr/local/share/applications/, also make a backup of the original (e.g. from /usr/share/applications/file.desktop to /usr/share/applications/file.desktop.orig).

Then you can run a nightly or weekly cron job to diff them and mail you any output (or write it to a file for you to check whenever you remember :-).

The cron-job script would start from a base something like this:

for f in /usr/share/applications/*.desktop.orig; do
  bn="$(basename "$f" ".orig")
  diff -u "$f" "$bn"
done

or instead of copying the files you're about to change to the same name with '.orig' appended, copy them to, say, /usr/share/applications.backup/. Then you can diff them all with just a single recursive -r diff command (and because the backup dir is only a small subset of the original, pipe into grep -v to get rid of warnings about missing files):

diff -u -r /usr/share/applications.backup/ /usr/share/applications/ |
  grep -v '^Only in /usr/share/applications/`

Yet another alternative is to run git init in /usr/share/applications (needs to be run as root, because the dir is owned by root) and commit all the .desktop files in there, and remember to commit new ones as they get added. You can then use git sub-commands like git status, git diff, and git show to show you any differences or uncommitted changes or new or deleted files.

git is a great tool for managing config files like these, it's not just for programming source code. The etckeeper program, for example, uses git to keep a revision history of everything in /etc (with hooks for apt, yum, dnf, etc to auto-commit changes before and after an upgrade) - definitely worth installing if you haven't already.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.