Experimental support for hidden snap directory
Snap user data is currently stored under ~/snap
where each snap has a self-named subdirectory that contains:
- numbered directories for recent and current revisions, used to store versioned data
- a symlink called ‘current’ that points to the installed revision directory
- a ‘common’ directory to store non-versioned data accessible across revisions
This means the directory structure for snap user data currently looks something like the following:
~/snap
├── snap_name
│ ├── 152
│ │ └── options.cfg
│ ├── 159
│ │ └── options.cfg
│ ├── common
│ │ ├── client.crt
│ │ └── client.key
│ └── current -> 159
Snaps use these directories to store application data, caches, and configuration files, but having them visible to users may result in a poor user experience.
To help with this, we’re happy to announce a new experimental flag, experimental.hidden-snap-folder
, that allows snap directories to be moved from ~/snap
to ~/.snap/data
(note the dot before ‘snap’ to hide the directory). This location was chosen because ~/.snap
is already being used to store user credentials.
Snaps installed after the flag has been set will store their data in ~/.snap/data
by default, while previously installed snaps will have their data migrated to ~/.snap/data
when they next refresh.
After data has been migrated to the new location, the SNAP_USER_DATA
and SNAP_USER_COMMON
environment variables are updated to the subdirectories of the new hidden location, ~/.snap/data/<snap_name>/<rev>
and ~/.snap/data/<snap_name>/common
, respectively.
Enabling hidden snap directory
To enable the experimental hidden snap directory feature, snapd must currently be installed from its ‘edge’ channel. You can check your current version with:
$ snap info snapd
The installed version (which you can see at the bottom of the output) should be the same as the version in the ‘latest/edge’ channel. If it’s not, you can install the latest snapd version from edge with:
$ sudo snap refresh --channel=latest/edge snapd
You can then enable the experimental hidden snap directory feature with:
$ sudo snap set system experimental.hidden-snap-folder=true
To disable the hidden snap directory feature you can run:
$ sudo snap unset system experimental.hidden-snap-folder
Note that disabling the flag does not currently move the data back to ~/snap
. This must instead be done manually. The 2.55 snapd release will include the ability to automatically revert the migration with the next refresh by disabling the flag. With the snapd version currently on edge, however, if you wish to revert the snap directory, you must:
- disable the
experimental.hidden-snap-folder
flag - manually backup any data under
~/.snap/data
you want to keep - remove the affected snaps
- restore their backed up data to
~/snap
- re-install the snaps
Planned Work
One of the problems with the current state of snap user data storage is that snaps map both the HOME
and SNAP_USER_DATA
environment variables to the versioned directory under ~/snap/<snap_name>
or ~/.snap/data/<snap_name>
.
This means that user-facing data written to $HOME
will often be stored alongside application data, such as caches and configuration files, written to $SNAP_USER_DATA
. Note that this isn’t universally true, since snaps can override environment variables through their snapcraft.yaml.
In order to address these shortcomings, we intend to introduce a new directory, ~/Snap
(note the upper-case S), meant solely for user-facing data.
This new directory will be pointed to by a new SNAP_USER_HOME
environment variable as well as the HOME
environment variable.
To further encourage snaps to separate user-facing from application data, some of the environment variables defined by the XDG Base Directory Specification will be mapped to directories under $SNAP_USER_DATA
. In particular, the XDG_DATA_HOME
, XDG_CONFIG_HOME
and XDG_CACHE_HOME
environment variables will be mapped to $SNAP_USER_DATA/data
, $SNAP_USER_DATA/config
and $SNAP_USER_DATA/cache
, respectively.
Since snaps may override environment variables or use arbitrary locations to write application data, snap developers need to ensure that the snap application is writing to the correct location. To make this transition as easy as possible, we intend to couple the new ~/Snap
directory migration with changes being made for the upcoming core22 base. This should hopefully minimise the effort required by snap developers as they’ll already be testing their snaps against the new base.