2

I am trying to build an application release by using Snapcraft.io, and I have almost all working.
Snapcraft already compiles the source code, generates the .snap file, includes all the dependencies, and so on.
However, I am stuck at how I can initialize some configuration files in the SNAP_USER_DATA folder after the first app install.
I do not want to place the files in the default read-only path SNAP, as the default parameters should be modified by the user, also I need to generate some additional files, like server certificates.
So I need to copy some files, and also run a script after the first install.

Is this possible?

Thanks.

boraseoksoon
  • 2,164
  • 1
  • 20
  • 25
Alvaro Luis Bustamante
  • 8,157
  • 3
  • 28
  • 42

1 Answers1

1

Because snaps are installed as root, it's impossible to do exactly as you ask at install time, as $SNAP_USER_DATA is user-specific, so it'll always be root's. However, you can do this at install-time using a system-wide directory, such as $SNAP_DATA, using the install hook:

$ snapcraft init
Created snap/snapcraft.yaml.
Edit the file to your liking or run `snapcraft` to get started

Create the hook. In our case we'll just create a new file in $SNAP_DATA, but you could do whatever you wanted.

$ mkdir -p snap/hooks
$ echo "touch \$SNAP_DATA/foo" >> snap/hooks/install
$ chmod a+x snap/hooks/install

Build the snap.

$ snapcraft
Preparing to pull my-part 
Pulling my-part 
Preparing to build my-part 
Building my-part 
Staging my-part 
Priming my-part 
Snapping 'my-snap-name' |                                        
Snapped my-snap-name_0.1_amd64.snap

Install the snap. This will run the install hook.

$ sudo snap install my-snap-name_0.1_amd64.snap --devmode --dangerous
my-snap-name 0.1 installed

Notice a file was created in $SNAP_DATA.

$ ls /var/snap/my-snap-name/current
foo

The only way to get similar functionality for $SNAP_USER_DATA would be to wrap your real command in a script that creates the config. This command is then run by the user, which means you get the $SNAP_USER_DATA you intend. Of course, this isn't at install-time.

kyrofa
  • 1,759
  • 1
  • 14
  • 19