Swift on Balena – a Swift Language Stack for Balena

Hi all,

I wanted to share a link to a project that I’ve been working on – Swift on Balena.

It adds support for the Swift programming language to a subset of balena’s base images for Raspberry Pi and other ARM devices.

I’ve been using Swift on BalenaCloud for several months and have been very excited about the potential of running Swift on IoT devices.

There’s a hello world sample project to get started with for those who are interested. Feedback welcome and appreciated. :slight_smile:

Thanks to everyone at balena who has been so helpful and supportive on the forums!

Cheers,
Will

2 Likes

Hey, this is fantastic work - thank you so much for your effort and contribution to the Balena ecosystem :+1:

1 Like

Hey @wlisac this looks awesome! I’ll definitely have a play around with it. Have you tried your swift base images with the new --live flag on balena push <deviceIP>? I think if swift does incremental compilation, it should speed things up quiet a bit as you wouldn’t rebuild from scratch everytime.

Thanks @shaunmulligan :blush: let me know how it goes.

The --live flag sounds promising. I’ll have to give it a try.

Is there some additional documentation for how to use --live besides what’s available on the latest CLI docs?

For example: it sounds like it’ll keep the sources files in sync, but I’m not sure how it would know to trigger a swift build to perform an incremental compilation and to restart the app binary.

So far I’ve been using various other techniques to improve the local device debug loop, mostly by semi-abusing the docker layer cache to cache swift package dependencies / modules.

I’ve also experimented with cross compiling the actual ARM Swift app on macOS and then pushing that to the device via lldb remote debugging (it’s fast and you get breakpoints!). It’s conceptually similar to how iOS apps are built / debugged from macOS and pushed to iOS (ARM) devices. Other folks in the Swift on ARM community are making progress on this front and I plan to explore it more when I have some time.

The local build / test / debug loop is definitely an area of interest for me, so looking forward trying this out.

Hey @wlisac,

The livepush readme has a bit more documentation, but it’s still not that fleshed out (it’s on my TODO list!) but I’d be happy to answer any questions you have on livepush (or take feedback too :slight_smile: )

You can find the README here: https://github.com/balena-io-modules/livepush

1 Like

@CameronDiver wow! Using --live is pretty awesome, it greatly improves the build / debug loop.

I’ll have to spend more time experimenting with it, but so far :+1:

One addition that could be nice is to provide a command / API to manually trigger a “live” rebuild instead of auto-live rebuilding when a source file changes.

When I’m making changes to source files, I often save (or the editor auto-saves) in a state that won’t actually compile.

The ability to make changes to several files, saving as I go, and then manually trigger a “live” build would be great (especially if it could cancel any in-flight live builds).

Hey @wlisac yeah the situation you mention is one of the ones which is keeping livepush in beta for the time being.

Solving it is a little complex, as it involves detecting when a service is restarting constantly, and running a sleep loop while the other code has time to “take over”. The problem is we need to detect this and also detect if it’s a normal running process or in error. I’m thinking of using the return code of the main process and if a non-zero value is detected, we take over the runtime of the container. This of course needs to investigated and tested, but I’ll definitely keep you updated.

In the meantime, it’s possible to avoid this situation with:

|| while true; do sleep 10; done

added to the end of your CMD line, but it’s not ideal.

The issue is here: https://github.com/balena-io-modules/livepush/issues/40