Placing Default Config Files

Hello Team,

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 config.json
Is it possible to do that ?

Yes there are a few options.

  1. Use layouts to make the app inside a snap see config files such as /etc/myapp/config.conf really be located at $SNAP_DATA/etc/myapp/config.conf (or possibly $SNAP_USER_DATA . This has the advantage of not needing to modify your app to look at different config files. See https://docs.snapcraft.io/snap-layouts for more details
  2. Patch or modify the app to use files located inside $SNAP_DATA/etc by default instead of /etc using a command line flag or possibly an environment variable. How to do this depends on your specific app, if an environment variable is an option you can specify the environment variable for an app in your snap with the environment keyword.
1 Like

While you can use layouts and all like ian pointed out, to initially ship the file you need to put it into the snap itself (meaning the readonly bits) … what i usually do in such cases is to prefix the default configs with “default-” …

i.e. having it in $SNAP/data/default-config.conf … and then have a wrapper script that puts it in place in $SNAP_USER_DATA at first launch.

in cases where a global config is needed i put it into $SNAP_DATA from an install hook script. in either case you either need to ship a default config in the readonly snap or you need to generate a default config during first start or install.

1 Like

Thanks @ogra and @ijohnson !!
@ogra is it good idea to run a service which will copy a default file from $SNAP to $SNAP_DATA directory.

while you can surely do it with a service, if you actually want global config (i.e. not user specific config that would need to be copied on application startup) I would do it from an install hook instead of having something (pointlessly) kick in on every boot …

1 Like

Thanks @ogra I got your point, it is better to write a wrapper instead of service which will tried to copy on every boot :slight_smile:

As suggested, i had wrote a shell script under snap/hooks/install which copied the default_config.json file from $SNAP to $SNAP_DATA directory successfully when snap is in devmode. But when the same i build in classic mode confinement, then after successfully installation of the snap i observe that service getting failed but the same is working with devmode confinement. Its seems that script is not able to copy the default_config.json at $SNAP_DATA. I am not able to understand that what’s going wrong, any pointer in this will help me to understand the problem as well as to move forward. Thanks, i appreciate your suggestion here :slight_smile:

What’s the intended way to inform folks who install a snap that they should modify a config file inside $SNAP_DATA rather than in /etc proper?

Is there some helper which’ll give the path of say, $SNAP_DATA/etc/foo.conf for snap named foo?

I don’t think there’s anything specially crafted for that, but you can always add a snippet about editing the config file to the description of the snap or to a help message in your snap.

i personally either tell my users about that in the package description

… or (better) simply (hah) translate all config options from a config file into snap config hook options so they can use snap set/get to operate the config (which also helps when you pre-install a snap in an ubuntu core appliance image since you can set all defaults from the gadget snap there)

The configuration file in my case is a long list of domain names of any size, so I don’t really think snap config options make sense (unless it’s an option pointing to a config file).

I think I’ll probably just add a description but I do find this a bit odd. I guess the path will always be /var/snap/SNAPNAME/common/ (or /current/).

right, for system-wide/global configs this is the case …

user specific configs typically live in $SNAP_USER_DATA with is pointing to ~/snap/<snapname>/current