Caddy is a very handy HTTP server with lots of built-in features. But every once in a while you need something more. Fortunately Caddy has a plugin API. But Caddy being written in Go, the plugins need to be added at compile-time. On NixOS, there are no plugins added by default, and there are a couple of things to note when adding them.
Update: use pkgs.caddy.withPlugins instead now that it is available
Ming Di Leom has already written about Installing Caddy plugins in NixOS. I was a bit unsatisfied with the solution using xcaddy, since it requires building the package with the nix sandbox disabled (the build process needs to be allowed access to the network).
xcaddy is a tool for building caddy with plugins. But the process isn’t hard to do by hand:
- Make a copy of main.go
- Read and follow the instructions, that is:
- Edit the imports for the plugins you want
- Run
go mod init caddy - Run
go build
We just need to package this for nix:
# In configuration.nix or another suitable location
services.caddy = {
enable = true;
package = pkgs.callPackage ./caddy.nix {};
}
And the custom caddy package:
# caddy.nix
{ pkgs ? import <nixpkgs> { } }:
pkgs.buildGoModule {
pname = "caddy";
version = "2.6.4"; # or whatever version you wish recorded
vendorHash = "sha256-..."; # start with fakeHash and replace with correct
src = ./caddy; # folder with main.go, go.sum, go.mod
}
This is not as neat as the custom package that took a list of plugins, but I find it is still nicer than the one that needs to be built without the sandbox.