Snapcraft Layout bind-file results in an empty file at the specified location

Hello,

I’m trying to understand how the Layout is supposed to function. My ultimate goal is to make available a configuration file within $SNAP_DATA that a utility (one of two apps in my snapcraft.yaml) could then modify when called by a snap set command (from the outside world).

My currently configured Layouts work as expected for files and directories that I need to be in place in $SNAP_DATA during the snap build process; the main app launches and writes to them.

My Layout for the configuration file though, also works in that the bind-file results in the file being in the location specified by the Layout within $SNAP_DATA, but the file is empty (0 bytes).

I’ve read different posts about migrating a configuration file over from /etc, /home, $SNAP etc using an install-hook and/or configure-hook, but nothing has worked for me. What is the best practice to achieve this? A few posts explain away without the use of a Layout but I can’t tell if that’s only because the post preceded the intro of Layouts, or not. Thx in advance!

It would help if you could share your snapcraft.yaml and the code you used for the install hook

Hi @ogra,

I was able to achieve my interim step of getting a writable copy of my conf.json file deposited into $SNAP_DATA by using an install-hook and not a Layout nor configure-hook.

Here’s the install-hook that worked for me.

#!/bin/sh -e

Copy conf.json file to $SNAP_DATA

validate the file’s existence

if [ ! -f $SNAP/conf/conf.json ] ; then echo “ERROR: The file does NOT exist” exit 1 fi

If it exists, copy conf.json from $SNAP to $SNAP_DATA

mkdir -m 755 $SNAP_DATA/edge/conf cp $SNAP/conf/conf.json $SNAP_DATA/edge/conf

But I still have the question from my post → I had thought I could achieve this same result via bind-file Layout but it resulted in an empty file instead. For my own understanding, why would that happen?

This is a snippet of that Layout:

$SNAP/conf/conf.json bind-file: $SNAP_DATA/edge/conf/conf.json

This was the resulting empty file at the correct location:

user@ubuntu-server01:/var/snap/edgenoplugins/current/edge/conf$ ls -ltr total 0 -rwxr-xr-x 1 root root 0 Nov 7 15:52 conf.json

Thanks!