2

Here are the important parts of my working snapcraft.yaml...

name: my-lib
apps:
  library-sample:
    command: library_sample $SNAP/arg_file.json

parts:
  library:
    source: https://github.com/the/sample.git
    plugin: cmake
    install: |
      cp -r samples/library_sample $SNAPCRAFT_PART_INSTALL/
      cp -r ../src/samples/src/arg_file.json $SNAPCRAFT_PART_INSTALL/
      cp --parents modules/dep_lib1/libdep_lib1.so $SNAPCRAFT_PART_INSTALL/
      cp --parents modules/dep_lib2/libdep_lib2.so $SNAPCRAFT_PART_INSTALL/

Ultimately, I would like arg_file.json to be in $SNAP_DATA, so I changed

cp -r ../src/samples/src/arg_file.json $SNAPCRAFT_PART_INSTALL/

to

cp -r ../src/samples/src/arg_file.json $SNAP_DATA/

However, this causes the file to disappear from my install directory (and ultimately from my installed snap). I feel certain this is because the $SNAP_DATA environment variable is not available to me from the install: scriptlet, but I don't know how I should work around this otherwise.

Anthon
  • 69,918
  • 32
  • 186
  • 246
Zak
  • 12,213
  • 21
  • 59
  • 105

1 Answers1

3

Snapcraft is a build-time tool. The install scriptlet you're using there runs when the snap is being created, not when the snap is installed.

$SNAP_DATA and its kind ($SNAP_COMMON, $SNAP_USER_DATA, etc.) are defined by snapd at run-time. In other words, there is no $SNAP_DATA defined when snapcraft is creating the snap.

If you want to place files into $SNAP_DATA when the snap is installed, you can do so by utilizing the configure hook, which runs upon installation (for reference, here is a tutorial of using the configure hook for health checks). However, since the configure hook runs after services start up, if you change your app to be a daemon this won't work (assuming your service needs files in $SNAP_DATA before it'll start up).

Another option that would work in that case would be to create shell script wrappers for your binaries and/or services to copy necessary files over to $SNAP_DATA before running the real commands. Here is an example wrapper for Apache used within the Nextcloud snap for doing something similar. Then you make your library-sample app call e.g. library_sample_wrapper instead of library_sample.

kyrofa
  • 1,759
  • 1
  • 14
  • 19