Not able to modify the Exec property in the desktop file

I am trying to set a default flag that would be passed when my snap app would be launched.
For this, I tried to update my desktop file from

[Desktop Entry]
...
Exec=/path/to/myApp %U

to

[Desktop Entry]
...
Exec=/path/to/myApp --my-default-flag %U

The app is built correctly. I checked the contents of the desktop file inside the snap package that was generated using

sudo mount -t squashfs /path/to/myApp.snap /some/mount-point

But once I install the package locally, the contents are overwritten to

[Desktop Entry]
...
Exec=env BAMF_DESKTOP_FILE_HINT=/var/lib/snapd/desktop/applications/myApp.desktop /snap/bin/my-app %U

Specific Details:
the custom flag I want to pass is actually --no-sandbox

Hi @harryi3t - this is done this way for security reasons.

What you can do is have a simple wrapper script:

#!/usr/bin/bash

exec ${SNAP}/path/to/myApp --my-default-flag "$@"

how and when exactly in the build process do you do this ?
while the Exec line is indeed re-written during build to add the necessary BAMF environment, it should still use the existing Exec line appended to the added environment bits …

here i’m modifying the Exec line of an upstream .desktop file from an override-build: command:

(and later use that file from a desktop: entry in the respective apps: section (line 34))

What happens if you use the following?

Exec=my-app --my-default-flag %U

You should be using a command that is provided by your snap in the Exec line, rather than a path to an executable distributed within the snap. The Exec line will still be rewritten, but it should include the additional parameters you’ve specified.

That should not affect the additional flags being passed

As @Saviq suggested, looks like snap would overwrite the Exec line removing all flags.

how and when exactly in the build process do you do this ?

It’s hardcoded in the desktop file. During build time, I just fix the path, the flags are hardcoded. As I have mentioned in the post, I have verified that the file is correctly getting packaged. After the snap package was generated, I was able to open it up and see the flag in that file. It is only after I install the app, that it is overwritten for security reasons

Also, in your case, you are not passing any additional flags.

This is the code that rewrites the exec line:

As you can see, it checks to see if the Exec line starts with one of the snap’s exported commands, and preserves the remainder of the line if it finds a match.

If your .desktop file in $SNAP/meta/gui reads something like Exec=/path/to/myApp, you won’t get a match because of that leading path. It needs to be one of the commands your snap exports to /snap/bin.

Thanks @Saviq for the workaround.

I have one doubt, how is VSCode able to do this?
I see this in their desktop file

Exec=env BAMF_DESKTOP_FILE_HINT=/var/lib/snapd/desktop/applications/code_code.desktop /snap/bin/code --force-user-env --no-sandbox --unity-launch %F

They are able to pass these additional flags: --force-user-env --no-sandbox --unity-launch
The first one is replaced during build time and rest two are hardcoded.

Thanks @jamesh!

That looks like the core issue. Will try it out. Thanks again.