Using launchers

Launchers are small programs that modify the runtime environment to make a snapped application function properly. They’re typically scripts that modify environment variables before running the application itself.

A launcher can be bundled into the packaging source tree, or included as an independent part that sources a remote repository.

NOTE: Many Snapcraft Extensions add launchers to modify the runtime environment. Using extensions is often preferred over manually adding a launcher.

How to use

To use a launcher, include it in the snap and insert its definition, in the proper order, in the command chain in the app stanza:

apps:
  _app_name_:
    adapter: full
    command: _app_command_
    command-chain:
      - _launcher_

Launchers can be stacked to apply multiple modifications before starting the app.

apps:
  _app_name_:
    adapter: full
    command: _app_command_
    command-chain:
      - _a_launcher_
      - _b_launcher_

Stacking launchers works because at the end of each launcher, it runs the arguments it received. So a_launcher will run b_launcher app_command and b_launcher will run app_command.

When a launcher depends on another launcher(e.g. perl-launch), both need to be included in a snap and be invoked in the proper order in the command chain.

How to make

  1. Identify the proper component that requires patching.
  2. Check out the component’s documentation or code to find out whether its behavior can be changed via environment variables or other means. If none can be found patch it and/or ask the component’s author to implement one.
  3. Implement a launcher that changes the environment variables, etc.
  4. At the end of the launcher script, exec the arguments of the launcher. This will execute the next launcher in the chain or the command itself. In bash, you can do this by adding the line exec "$@".
  5. (Recommended) write documentation regarding the usage of your launcher at Latest doc topics - snapcraft.io.

List of known launchers

2 Likes

Thanks for writing this page, @Lin-Buo-Ren !

It might be best to remove the command: _launcher_ _app_command_ syntax. I think we should recommend command-chain instead

  • it integrates nicer with extensions
  • It ensures the launchers are also run when snap run --shell is used
  • it’s cleaner in snapcraft.yaml

Keeping the “old” way around in the docs adds complexity without much benefit.

1 Like

Sure, I’ve dropped them.

1 Like