Giving input argument to multipass / snapcraft.yaml

Hello snapcrafters,

We would like to give input arguments to our snapcraft.yaml, or to the multipass, and don’t know what would be the best way of doing it.

Example :

Let’s say we want to do a specific build that will use a specific git commit, and/or give a specific option to some scripts being triggered during the snap build process that we will push to a specific edge/beta/candidate channel. (all of this from a script)

What would be the best and possible way to achieve this ? Is it possible to :

  • Use an Environment variable from to communicate with the building process in the multipass environment ?
  • Give an input argument/option to the snapcraft.yaml (did not find any doc for this)
  • Other option ?

We want to avoid having several snapcraft.yaml files because it will be hard to maintain since they will have most of the code in common

Best

I made a proposal for how to handle this from inside the snapcraft.yaml the same way that spread does here

In the meantime what I’ve done as a workaround is to add code to the snapcraft.yaml which checks if a file exists in the project tree called “developer-options” or something and if that file exists then override some things, but it’s a bit hacky and doesn’t work as nicely as it would if snapcraft supported all the same options as spread in this way with the HOST keyword

Thanks for your answer ! Since we are in the multipass VM once we issue the snapcraft command, when you mean a file in the “project tree” you mean a file in some git repo that you will pull at some point during the snapcraft build process ?

yes if you have a snapcraft.yaml with a part that uses source: . then just putting the file in the same project directory you run snapcraft from, that will work and you can apply changes from that part. If you don’t have such a part, then you can just add one like this:

parts:
  local-opts:
    source: .
    plugin: nil
    override-build:
      if [ -f developer-options ]; then
        # do all the custom things you want
      fi

but the important part is that you have a source: . part as then the file will be picked up into your multipass / lxd container as part of the build

1 Like

thanks for the details. I guess that includes git commit git push everytime you want to add an option to your build.
We will use this workaround as well then :slight_smile:

Well it depends on how you are using the file and what kind of settings, I use it just for tweaking of the build to include some testing utilities, or debug symbols, etc. so I only end up using it locally and not in CI, etc. so my gitignore includes that file so I don’t commit it and it isn’t used in CI for production builds, etc.

However if you really are using the feature to customize the build for CI, etc. then yes you will need to check in that file somehow

There is something I did not get then.

Everytime we launch our snapcraft command, an instance of the mutipass VM is created.
Is there anyway to access files outside of the VM during the build ?

My understanding was that source . could get local files in the VM folders but I did not understand it would be possible to get acess to files outside of the VM, is that what you do ? That would be a much better workaround for us. (We are not in a CI workflow at that point)

Please enlighten me :slight_smile:

No, only files that are in the “snapcraft project directory” are accessible to the build environment when building with lxd/multipass. These files are copied and accessible primarily only if you use source: . for a part (though maybe there’s another way to get them without a source: . part, not sure), so this workaround is really only to inject build-options or something like this into the snapcraft build, if you actually need to add additional files they should be their own part properly configured with a source: somewhere-real

No, I only use it to pass a single file (but could be multiple files) from the snapcraft project directory into the build, and these set things like compile options to use when building the snap, etc. I don’t use it to inject general files or other things into the snap, for that I would recommend adding a proper part to the snap for those files.

1 Like

Ok great ! Thank you so much for your time.
I had no idea the files in the folder containing the snapcraft.yaml were copied to the VM.
Going to do some tests !
Thanks for the tip !

1 Like