Partly overriding a .desktop entry

Hi, I have tried to change the execution command picked up by desktop runners for Element using Home Manager like so:

  xdg.dataFile."applications/element-desktop.desktop".text = ''
    Exec=element-desktop --enable-features=UseOzonePlatform --ozone-platform=wayland
  '';

However, this blanks out all the other fields of the desktop entry such as the application name, icon… is there a way to keep them?

I also tried

  xdg.desktopEntries.element-desktop.settings = {
    Exec = ''
      element-desktop --enable-features=UseOzonePlatform --ozone-platform=wayland
    '';
  };

but I get the error that “xdg.desktopEntries.element-desktop.settings is used but not defined”.

In this case it looks like your goal is to run Element with support for Wayland. It’s not well documented yet but there’s now a built in way to do that:

If you are using Wayland you can choose to use the Ozone Wayland support in Chrome and several Electron apps by setting the environment variable NIXOS_OZONE_WL=1 (for example via environment.sessionVariables.NIXOS_OZONE_WL = "1" ). This is not enabled by default because Ozone Wayland is still under heavy development and behavior is not always flawless. Furthermore, not all Electron apps use the latest Electron versions.

Using Home Manager this would look like:

home.sessionVariables.NIXOS_OZONE_WL = "1";

This works because the Element package already includes a wrapper that automatically adds --enable-features=UseOzonePlatform and --ozone-platform=wayland when NIXOS_OZONE_WL is set.


To answer your question though, both xdg.dataFile and xdg.desktopEntries are intended for creating new files, and they’re totally unaware of any existing entries. The only way to modify the entry created by a package is to modify the package, and each package may be different so you must begin by reading its source.

This package uses makeDesktopItem and references the result in its installPhase, so you have to both override makeDesktopItem and update installPhase. The process is explained in detail here, but would look something like this:

element-desktop.overrideAttrs (e: rec {
  # Add arguments to the .desktop entry
  desktopItem = e.desktopItem.override (d: {
    exec = "${d.exec} --example-one --example-two";
  });

  # Update the install script to use the new .desktop entry
  installPhase = builtins.replaceStrings [ "${e.desktopItem}" ] [ "${desktopItem}" ] e.installPhase;
})

Since the ability to override makeDesktopItem is a recent addition, this probably won’t work until NixOS 22.11.

4 Likes

Thank you very much for the detailed answer!