Skip to main content

wip: wifi-qr Zig Rewrite

·3 mins

A couple of years ago, I frequently needed to join WiFi networks on external devices for testing. Entering long, randomly generated passwords multiple times per day became quite tedious. To address this, I created a simple bash script that queries the macOS Keychain to retrieve the current WiFi network’s password and generates a QR code to simplify the process. The project can be found on my GitHub: aranw/wifi-qr.

The functionality of the project has always been limited due to using Bash. I’ve wanted to rewrite the project and have always thought it would be a great candidate for rewriting in various languages due to its simplicity and number of features.

Zig is a language I’ve been interested in but hadn’t found a project to use it with until recently. Last year, I explored the excellent ziglings/exercises project, which offers a delightful learning experience. You solve a series of broken programmes, using hints to fix them and gradually learn the language. Over the May bank holiday, I decided to revisit the Ziglings exercises to see what had changed in Zig, as it is an actively developed language. Ziglings is a great way to learn about these updates. After completing just over half of the exercises, I wanted to try Zig with a real project and thought my wifi-qr project would be a perfect candidate for a rewrite.

Rewriting the initial requirements for the project in Zig was fairly straightforward. Using articles, Ziglings, and code examples from projects like Ghostty, I managed to implement the logic for retrieving the password from the macOS Keychain. The QR code generation part proved to be the most challenging. Initially, I tried to integrate a C QR encoding library from GitHub ( nayuki/QR-Code-generator), but I struggled with the C to Zig calls. After numerous failed attempts, I eventually discovered a Zig implementation ( bensengupta/qr), which was straightforward to integrate and use.

Overall, the experience was really enjoyable, and the language was a joy to work with. Currently, my rewrite does not have all the features of the bash script, and I will need to add some missing features to achieve feature parity between the two versions. There are also a few additional features I always wanted to add but found difficult to implement in Bash, which should be much easier in a more feature-rich programming language like Zig.

Now that I have the basic functionality working, the next step is to implement additional features. These include:

  • Linux support: Ensuring the script can retrieve WiFi passwords and generate QR codes on Linux systems.
  • Automatic WiFi network detection: Automatically retrieving the current WiFi network if none is provided via the CLI.
  • Windows support: Extending the script to work on Windows platforms.
  • Multiple QR code formats: Enabling the generation of QR codes in various formats.

My progress on the rewrite can currently be found on a branch in my aran/wifi-qr repository.