Guix: creating a package from a binary - Aloys' cornerGuix: creating a package from a binary
I'm currently migrating my entire homelab to Guix.<br>While Guix offers a Nginx service out of the box, I prefer using Caddy.<br>The built-in automatic TLS is a nice quality of life feature. In my experience, it is seamless and just works™.
As part of this ongoing migration, I created a dedicated Caddy service which I'll go over in detail in a future post.<br>Everything went well until I realised that Caddy isn't packaged in Guix yet.
I looked at packaging it myself, however packaging Caddy from source requires first packaging a dozen go sub-dependencies.<br>Thankfully, a group of volunteers is currently working on it - it isn't ready yet, but hopefully, soon.
In the meantime, I decided to circumvent the issue by simply packaging the binary.<br>This is quite simple in Guix, and a nice first foray into 'packaging' for me.
Most of what I am about to explain below is paraphrasing the documentation; I highly encourage you to give it a read.
Defining a simple binary package
A package is defined by creating `` record, as defined in `guix/packages.scm`.
A `` record contains the information required by Guix to create a reproducible package.
It contains the metadata of the package, such as its name and description, as well as where to get the source assets and how to build it.
Package information: what are we packaging?
Let's start with the simple metadata fields: name, version, synopsis, description, license, home-page.<br>Those are mostly informational and some of those optional.
In our case, these aren't very important as we will not distribute the package.<br>Some of those fields are used to generate the documentation when searching for package.
If you intend to distribute a package, I would encourage you to be a bit more descriptive than me!
(define caddy<br>(package<br>(name "caddy")<br>(version "2.11.4")<br>(home-page "https://caddyserver.com")<br>(synopsis "Web server with automatic HTTPS")<br>(description "Caddy is an extensible web server with automatic TLS.")<br>(license asl2)))
With this easy task out the way, we only need to address two more parts: where to get the binary from, and how to "build" it.
Source: where to get the source artifacts?
In this case, for simplicity's sake, we are packaging the binary instead of building Caddy from source.
To do so, we use the `source` field which contains an `` record.<br>The `` record is described in detail in this section of the documentation.
Our case is very simple: we fetch the binary.
I am using the GitHub release page to get the binary, and the `url-fetch` method.
(source (origin<br>(method url-fetch)<br>(uri (string-append<br>"https://github.com/caddyserver/caddy/releases/download/v"<br>version "/caddy_" version "_linux_amd64.tar.gz"))<br>(sha256<br>(base32 "1fbvxj6mifdqhwm5s1f8snr805k0anllzlri7cg9l61rgj8vyzsj")))
Two things to note here:
I'm constructing the URL based on the version. This is optional.
I hard coded the target platform, `amd64`. You might need to adapt it, or better, use a "system" variable like I have done for the version.
SHA 256 hash
The hash in the `sha256` field is here to verify the integrity of the content.<br>The idea is simple: the guix daemon create a hash based on the content of the fetched resource.<br>If the content changes, the daemon will flag the hash as not matching, and refuse to build the package until we validate the new hash.
This protects us from content changing without us knowing it.
To obtain the initial hash, you can use `guix download`:
guix download "https://github.com/caddyserver/caddy/releases/download/v2.11.4/caddy_2.11.4_linux_amd64.tar.gz"
This will print:
Starting download of /tmp/guix-file.zQdNRJ<br>From https://github.com/caddyserver/caddy/releases/download/v2.11.4/caddy_2.11.4_linux_amd64.tar.gz...<br>following redirection to `https://release-assets.githubusercontent.com/github-production-release-asset/29207621/c72e489c-84b8-406e-831f-51de46756914?sp=r&sv=2018-11-09&sr=b&spr=https&se=2026-07-13T01%3A29%3A05Z&rscd=attachment%3B+filename%3Dcaddy_2.11.4_linux_amd64.tar.gz&rsct=application%2Foctet-stream&skoid=96c2d410-5711-43a1-aedd-ab1947aa7ab0&sktid=398a6654-997b-47e9-b12b-9515b896b4de&skt=2026-07-13T00%3A28%3A22Z&ske=2026-07-13T01%3A29%3A05Z&sks=b&skv=2018-11-09&sig=dW0nPJYRzfIGAMH8c7Knsbx3qSpePvvszb1%2BbqMi9KI%3D&jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmVsZWFzZS1hc3NldHMuZ2l0aHVidXNlcmNvbnRlbnQuY29tIiwia2V5Ijoia2V5MSIsImV4cCI6MTc4MzkwNTA5MywibmJmIjoxNzgzOTAzMjkzLCJwYXRoIjoicmVsZWFzZWFzc2V0cHJvZHVjdGlvbi5ibG9iLmNvcmUud2luZG93cy5uZXQifQ.rIGwFUu9wYkziiMvOrw2ane1zlC-CnLgu1jrdC6HEZ8&response-content-disposition=attachment%3B%20filename%3Dcaddy_2.11.4_linux_amd64.tar.gz&response-content-type=application%2Foctet-stream'...<br>…nux_amd64.tar.gz 16.4MiB 23.2MiB/s 00:01 ▕██████████████████▏ 100.0%<br>/gnu/store/fydib5lca6gbvrx57n8hj67gy0kh6ngy-caddy_2.11.4_linux_amd64.tar.gz<br>1fbvxj6mifdqhwm5s1f8snr805k0anllzlri7cg9l61rgj8vyzsj
The...