1

I am trying to package my app to be able to install it using snap. App is already compiled as executable and has one config file and one certificate file which I package inside the snap.

Once installation is complete I can see everything in /snap/myapp/x1 but as x1 part will be changing after updates I'd like to take out resource files and put them to a more convenient location.

For testing purposes I tied resource files to be pulled from the same folder where my app is running and I found out that I needed to put them to /var/lib/snapd/void folder for app to work. This does not seem as a good location to start with.

As user will be able to change the config files I'd like to keep them at say $SNAP_COMMON so it would be great if I could somehow get these files to be moved over to that location during installation. Any thoughts?

muru
  • 207,228
Jasko
  • 115
  • 7

1 Answers1

3

For the purpose of this response I'm going to assume the application contained within your snap has command: my-binary.

You really have two options:

  1. Wrap your application in a script that ensures the config file has been migrated before running. For example, if you wrote a script called run-my-binary that looks like this:

    #!/bin/sh
    
    # Migrate config if necessary
    if [ ! -d $SNAP_COMMON/my.config ]; then
        cp $SNAP/conf/default.conf $SNAP_COMMON/my.config
    fi
    
    my-binary
    

    Change your snapcraft.yaml to install this script, and change your app to command: run-my-binary.

  2. Use the configure hook, which runs upon initial install (among other times). That can be a shell script that looks very similar to the wrapper.

Note that while I wanted to answer the question you asked, I don't really recommend putting config files in $SNAP_COMMON since it's specifically unversioned-- it won't be backed up when you upgrade/rollback. I recommend using $SNAP_DATA instead. For more information see this answer.

kyrofa
  • 7,502