Can't get path to file right in autotools configflags

Hi,

I’m currently building my first snap for a customized Squid proxy server on a Raspberry Pi. By default, Squid assumes there are specific configuration files (squid.conf and mime.conf) under /etc. Additional build flags can be used to use a different configuration folder. Specifically, --sysconfdir can be used to change this directory. Now, if I add a configflags entry to my snapcraft.yaml to set this flag to /etc/squid, I can build a snap (in devmode) but that ends up looking at the actual /etc/squid, where there is no file. If I understand correctly, the executable should be looking in /snap/squid/current/etc/squid. But if I supply that for the --sysconfdir flag, the executable ends up looking in the right location but the asset itself is moved to /snap/squid/current/snap/squid/current/etc. So I can’t run the proxy server.

My snapcraft.yaml:

name: squid-sl
version: '0.3'
summary: Proxy server for the Scripting Languages exam
description: |
  Proxy server meant to block and log traffic during the Scripting Languages exam.

grade: devel # must be 'stable' to release into candidate/stable channels
confinement: devmode # use 'strict' once you have the right plugs and slots

apps:
  squid-sl:
    command: squid
    plugs:
      - network
      - network-bind
  squid-sl-service:
    command: squid
    daemon: simple
    restart-condition: always
    plugs:
      - network
      - network-bind

parts:
  squid:
    source: http://www.squid-cache.org/Versions/v3/3.5/squid-3.5.26.tar.gz
    plugin: autotools
    #configflags: ["--sysconfdir=/etc/squid"]
    configflags: ["--sysconfdir=/snap/squid-sl/current/etc/squid"]

Is this intended behavior? What’s the mechanism that’s causing this?

The --sysconfdir flag specifies the directory to install into but when snapcraft gets to the install phase it issues make DESTDIR=$SNAPCRAFT_PART_INSTALL install which will prefix all the directories specified via options or defaults with a folder, which will be used as the root of the snap when packaged into a squashfs filesystem.

When installed into the client computer snapd will mount the squashfs filesystem at /snap/squid-sl/1 or similar. The 1 is the build number from the store, and squid-sl is the name of the snap.

So when you use a flag to an autotools build of --prefix=/usr then the combination with snapcraft will set your application to install itself into your snap at <snap-dir>/usr which will create a filesystem with a top-level directory of usr. When mounted to the client system that will appear as /snap/<snap>/<build>/usr.

Snapd also creates a symlink to the current build in the /snap/<snap>/ directory called current so you can hijack this for an application which hardcodes it’s paths as yours appears to be doing. The trick is to specify a full snapd path to autotools, and use organize to strip the snapd prefix when staging the files into the squashfs package:

parts:
  squid:
    source: http://www.squid-cache.org/Versions/v3/3.5/squid-3.5.26.tar.gz
    plugin: autotools
    configflags:
      # set a sysconfdir so that the hardcoded paths are created with snapd prefix
      - --sysconfdir=/snap/squid-sl/current/etc/squid
      # install everything else into the snap as usr/
      # Will appear at /snap/squid-sl/current/usr
      - --prefix=/usr
    organize:
      # strip the snapd prefix from sysconfdir paths so that it installs as etc/ in the snap
      snap/squid-sl/current/etc: etc