How to make a file end up in /etc/default/polkadot

I want to create a file containing startup-parameters which I would like to be able to change as part of my snap.

Seems like a trivial thing, but I can’t get it right. It swallows massive time from me.

I’ve read through https://snapcraft.io/docs/parts-environment-variables and tried to understand what goes on. Anyway.

I have a part defined like this:

files:
    plugin: dump
    source-type: local
    source: ./files
    organize:
      service-arguments: $SNAPCRAFT_PRIME/etc/default/polkadot

But the file ends up in ‘/snap/polkadot/current/root/parts/files/install/etc/default/polkadot’

I would like it to be found in a much better location. Like somewhere similar to /etc/default/polkadot which would make it easier to find for some poor bastard trying to understand the snap.

Even more generally, I would like to know how others, more experts, are producing startup-parameter handling such as to allow someone to reconfigure it dynamically. I feel I’m doing guess-work here (as usual with me snapping).

Any help getting this right, much appreciated.

Use an install hook to copy the file from $SNAP/etc/default to $SNAP_DATA/ …

If your app actually looks for the file in /etc/default you can use a layout to map the file from $SNAP_DATA into /etc/default as needed

In the end, I ended up mimicking the LXD charm buy creating a wrapper script as part of the app start command for my service that looks for the existence of the service-arguments file, creates it or uses it as input for start-up parameters. The “parts” and “apps” looks something like this:

parts:

 wrappers:
    plugin: dump
    source-type: local
    source: .
    stage:
      - wrappers/

apps:
  polkadot:
    command: wrappers/start-polkadot.sh $SNAP_DATA/service-arguments
    daemon: simple
    install-mode: disable
    refresh-mode: endure
    restart-condition: never
    plugs:
      - network
      - network-bind
    environment:
      LC_ALL: C.UTF-8
      LANG: C.UTF-8

And the “start-polkadot.sh” looks like this:

#!/bin/sh
set -eu

echo "=> Preparing the system  (${SNAP_REVISION})"

# shellcheck disable=SC2155
export SNAP_CURRENT="$(realpath "${SNAP}/..")/current"

# Create a config if its missing.
if [ ! -f "${SNAP_COMMON}/service-arguments" ]; then
    echo "==> Creating basic service argument file ${SNAP_COMMON}/service-arguments"
    echo "--name=Dwellir" > "${SNAP_COMMON}/service-arguments"
    chmod 0644 "${SNAP_COMMON}/service-arguments"
fi

# Read the system wide configuration file
echo "=> Using service argument file:  ${SNAP_COMMON}/service-arguments"

SERVICE_ARGS_FILE="$SNAP_COMMON/service-arguments"
SERVICE_ARGS=$(cat "$SERVICE_ARGS_FILE")

# Start the service with the specified parameters
POLKADOT="${SNAP}/bin/polkadot"
exec "${POLKADOT}" $SERVICE_ARGS

The arguments in the “service-arguments” file are space separated.

Attribution to the LXD people that has a really good example of a snap. I will use that future reference.