How to add environment vars to lauchner

I understand that you can specify environment vars in snapcraft.yaml.
My reading seems to imply that these environment variables will be written to the loader.
I can see them in snap.yaml but the loader isn’t affected.

This is my yaml I have:

apps:
pi-gation-debug1:
command: tomcat-launch
plugs: [network, network-bind]

pi-gation:
command: tomcat-launch
daemon: simple
plugs: [network, network-bind]
environment:
PI4J_PLATFORM : Simulated
SimulatedPlatform : RaspberryPi GPIO Provider

I’m expecting the loader for pi-gation to contain these environment vars as exports, but no such luck.

What am I doing wrong?

Thanks in advance,
Brett

they’re not written into the command-pi-gation.wrapper or equivalent for your app. Instead they are managed internally by snapd before it calls that wrapper. This confused me at first, too. You can verify whether the environment variable is set correctly by running:

snap run --shell pi-gation
echo $PI4J_PLATFORM
exit
1 Like

Yes thanks eventually worked it.

Turns out I need to customise the env vars based on architecture so I ended up creating my own wrapper which set the vars:

For the record:

apps:
  pi-gation:
    command: pi-gation-launch
    daemon: simple
    plugs: [network, network-bind]

parts: 
  pi-gation-launch:
    plugin: dump
    source: snap/src/scripts
    after: [tomcat, pi-gation-webapp]

And the source for my wrapper is:

src/scripts/pi-gation-launch

#!/bin/sh
ARCH=`uname -m`
if [ "$SNAP_ARCH" != "arm64" ] && [ "$SNAP_ARCH" != "armhf" ]; then
	# Run the simulator as we are not on RPi hardware
	export PI4J_PLATFORM="Simulated"
	export SimulatedPlatform="RaspberryPi GPIO Provider"
fi
export pi_gation_db_username="`snapctl get pi-gation db-username`"
export pi_gation_db_password="`snapctl get pi-gation db-password`"
export pi_gation_https_port="`snapctl get pi-gation https-port`"
$SNAP/tomcat-launch