Snapcraft hook support

A hook is an executable file that runs within a snap’s confined environment when a specific action occurs.

The filename of the executable is based on the name of the hook. If this file exists, snapd will execute the file when required by that hook’s action. See Supported snap hooks for more details on which hooks are supported.

Snapcraft can integrate hooks into a snap using two methods:

1. Using a project-wide snap/hooks directory

Hook executable can be placed in a snap/hooks directory and be automatically copied into the snap during Snapcraft’s prime step (see Parts lifecycle for details).

Hooks copied from snap/hooks cannot access, nor be affected by, a part’s environment. For example, if a snap includes a Python part, $PYTHONPATH will not be defined and accessible from the hook.

2. From within snapcraft.yaml

A part within snapcraft.yaml can generate or install hook executables into $SNAPCRAFT_PART_INSTALL/snap/hooks/. These are automatically copied into the snap during the prime step.

configure-hook:
    plugin: dump
    source: configure-again/
    organize:
        configure: snap/hooks/configure

Hooks that are generated from a part will include that part’s environment. Using the previous Python part example, this means $PYTHONPATH will be defined and accessible by the hook.

By default, hooks run with no plugs. If a hook needs more privileges, it one can use the top-level hooks attribute in snapcraft.yaml to request plugs:

hooks: # Top-level YAML attribute, parallel to `apps`
  configure: # Hook name, corresponds to executable name
    plugs: [network] # Or any other plugs required by this hook

Hooks are called with no parameters. See Using the snapctl tool for details on the internal command they can use to provide and retrieve data to and from snapd.