Editing snap files allows you to modify and customize snap content. Snaps are containerized software packages that provide applications for the Linux-based Snapcraft ecosystem. While snaps are read-only by default, there are methods to extract, modify, and repackage snaps to edit them.
What is a snap file?
A snap file is a self-contained software package that includes all the dependencies and required components for an application to run. Snaps are designed to work securely across Linux distributions like Ubuntu, Fedora, Arch, etc.
Some key features of snap files:
- All-in-one – Snap packages bundle all dependencies required to run the app, eliminating version conflicts
- Secure – Each snap runs isolated in a secure, sandboxed environment
- Auto-updating – Snaps auto-update themselves when new versions are released
- Cross-platform – Snaps work across Linux distributions like Ubuntu, Fedora, Arch, etc
- App store – Snaps are installed and updated via the Snap store
Snaps typically have the .snap file extension and are installed using the snapd daemon. Once installed, they expose confined apps to the user. Under the hood, a snap file consists of a SquashFS filesystem containing the app binaries, libraries, icons, and metadata.
Why edit a snap file?
There are a few reasons one may want to modify or edit a snap file:
- Customize the app – Change icons, themes, default settings, etc.
- Security research – Inspect workings of the app by unpacking its contents
- Modify app behavior – Alter functionality by editing binaries or code
- Fix bugs – Patch issues and rebuild the snap
- Bundle local data – Add custom data like configurations into the snap
- Learn internals – Understand snap structure by examining contents
However, editing snaps can break confinement and security measures. Modifications should be done carefully to avoid compromising the app sandbox.
Methods to edit a snap file
There are a few main methods to modify and edit the contents of a snap file:
1. Extract and repackage
This involves extracting the contents of the snap, making changes, then repackaging it back into a working snap file. Steps:
- Extract the contents:
unsquashfs sample.snap
- Edit files inside extract folder as required
- Repackage with squashfs
mksquashfs editted-extract/ sample-edited.snap -comp xz
- Install edited snap
sudo snap install --dangerous sample-edited.snap
This gives full access for modifications but requires repacking skills.
2. Mount snap folder as writable
The snap data folder can be manually mounted as writable to directly edit some snap contents:
- Find path of installed snap data. e.g. for snap “sample”
snap list sample
- Mount snap data folder as writable
sudo mount -o rw,remount /var/lib/snapd/snaps/sample/x1/
- Make edits directly inside mounted snap folder
- Unmount when done
sudo umount /var/lib/snapd/snaps/sample/x1/
This allows editing some snap components like configurations and scripts but not core app binaries.
3. Unpack, chroot, edit, repack
For substantial modifications, the snap can be unpacked, chrooted into, edited, and repacked:
- Extract snap contents
- Chroot into extract folder
sudo chroot editted-extract/
- Make required edits interactively within the chroot
- Exit chroot and repack the snap
The chroot better replicates the snap environment for edits. But chroot and rebuild requires expertise.
Editing snap content
Once inside the snap, there are many possible edits one can make:
- Change binaries and libraries
- Modify configuration files
- Alter startup scripts
- Add/edit default data files
- Modify icons, themes, graphics
- Edit desktop and MIME entries
- Replace splash screens
- Localize text and language
- Change version metadata
Care must be taken to not break confinement or application behavior when editing. Some trial and error may be needed to get a modified snap working properly.
Example: Editing a snap file
Let’s go through a hands-on example of editing a simple snap file:
- Install a sample snap, like the command-not-found snap:
sudo snap install command-not-found
- Extract the contents
unsquashfs /var/lib/snapd/snaps/command-not-found/x1/snap/command-not-found.snap
- Edit the bash script at /bin/command_not_found_handle. Remove the first line to disable not found packages check:
sed -i '1d' squashfs-root/bin/command_not_found_handle
- Repackage the edited snap
mksquashfs squashfs-root/ command-not-found-edited.snap -comp xz
- Install modified snap
sudo snap install --dangerous command-not-found-edited.snap
Now when an unknown command is run, the snap will no longer display info about missing packages. This demonstrates editing a snap by modifying its scripts. Similarly, other contents can be changed as well.
Important considerations
When editing snaps, keep in mind:
- Modifications may compromise security and isolation
- App behavior could break if improper changes are made
- Some snap parts like binaries are read-only even when mounted writable
- Multi-part snaps have interdependent components that may break
- Edited snaps cannot be re-published to the Snap store
- Backups should be kept in case anything breaks
- Test edited snaps thoroughly before deploying
So while possible, editing snaps requires care and expertise to avoid problems.
Conclusion
In summary, editing snap files involves extracting, modifying, and repackaging their contents. This allows customizing snaps but should be done with caution. The main methods are unsquashfs extract/modify/repack, mount snap folder writable, or chroot into snap for edits. Typical modifications include changing configs, binaries, default data, and metadata. With some expertise, snaps can be edited to alter app behavior, fix issues, add customizations etc. But backups and testing are recommended when editing snaps to avoid breakage.