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?

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.

  • Thanks! I was thinking about option 1 as well but thought there would be something more like option 2, but could not find out what it is. I'll check it out during the day. Also thanks for pointing me to $SNAP_DATA. I had my doubts about it in the first place but did not want to ask several questions in the post... I hoped someone might point me to the proper $SNAP_* folder eventually if I picked the wrong one :) Also, I stumbled upon the answer you linked while doing initial research and I actually bookmarked it. My one and only bookmark from askubuntu :) – Jasko Jan 27 '17 at 6:54
  • I've just tried it and it's exactly what I was looking for! – Jasko Jan 27 '17 at 7:55
  • Excellent! And yes, good call on not asking multiple questions. – Kyle Jan 27 '17 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.