XDG_RUNTIME_DIR unbound variable error with snapcraft core20 and lxc

I get this error on ubuntu 20.04 (my snapcraft.yaml has core20 as well), I run “snapcraft --use-lxd”:

Pulling alsa-mixin 
/bin/bash: line 46: XDG_RUNTIME_DIR: unbound variable
Failed to run 'override-pull': Exit code was 1.

I did not have this issue with core18. I have tried adding

lxc exec $containername -- bash -c "export XDG_RUNTIME_DIR=/run/user/$UID" in my pre snapcraft script. I also tried adding “export XDG_RUNTIME_DIR=/run/user/$UID” right above where XDG_RUNTIME_DIR gets called in my snapcraft.yaml file.

The part in my yaml file where it is getting called:

    plugin: nil
    source: https://github.com/diddlesnaps/snapcraft-alsa.git
    override-pull: |
      echo $PWD
      cat > asound.conf <<EOF
      pcm.!default {
          type pulse
          fallback "sysdefault"
          hint {
              show on
              description "Default ALSA Output (currently PulseAudio Sound Server)"
      ctl.!default {
          type pulse
          fallback "sysdefault"
      cat > alsa-launch <<EOF
      export ALSA_CONFIG_PATH="\$SNAP/etc/asound.conf"
      export XDG_RUNTIME_DIR=/run/user/$UID

      if [ -d "\$SNAP/usr/lib/alsa-lib" ]; then
          export LD_LIBRARY_PATH="\$LD_LIBRARY_PATH:\$SNAP/usr/lib/alsa-lib"
      elif [ -d "\$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/alsa-lib" ]; then
          export LD_LIBRARY_PATH="\$LD_LIBRARY_PATH:\$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/alsa-lib"
      export LD_LIBRARY_PATH="\$LD_LIBRARY_PATH:\$SNAP/opt/qt51512/lib"

      # Make PulseAudio socket available inside the snap-specific $XDG_RUNTIME_DIR
      if [ -n "\$XDG_RUNTIME_DIR" ]; then
          if [ -S "\$pulseaudio_sockpath" ]; then
              export PULSE_SERVER="unix:\$pulseaudio_sockpath"

      exec "\$@"
      chmod +x alsa-launch
    override-build: |
      # had to change to craft from snapcraft, it was changed
      snapcraftctl build 
      install -m644 -D -t $CRAFT_PART_INSTALL/etc asound.conf
      install -m755 -D -t $CRAFT_PART_INSTALL/snap/command-chain alsa-launch
      - libasound2-dev
      - libasound2
      - libasound2-plugins

That XDG_RUNTIME_DIR section is from Audio-playback interface in snaps running in daemon mode

How do I fix this? I tried seeing what env variables are on the lxc container. XDG_RUNTIME_DIR was not in there.

See this guide for better understanding on using alsa-mixin --> https://snapcraft-alsa.readthedocs.io/en/latest/snapcraft_usage.html#snapcraft-usage

@SamAlex How to I get it to understand XDG_RUNTIME_DIR?

The post earlier i linked was for understanding on how to use alsa-mixin in snaps, I think the fix for you is to try like this ?? I believe you are missing the inverted commas etc.

export XDG_RUNTIME_DIR="/run/user/$(UID)"


export XDG_RUNTIME_DIR="/run/user/$UID

This is how firefox does it or used to do it ?

export TMPDIR="$(xdg-user-dir DOWNLOAD)/firefox.tmp"

And this is how i do it in Lutris …

export TMPDIR="$HOME/common/.cache/lutris/tmp"

Hope this helps, try building with the format i suggested ??