Using nix to replace xcaddy to build Caddy webserver

I found the Caddy webserver and I now want to play around with building a nix package that allows me to build caddy with overrides for the different caddy modules. Similar to what the custom build tool the caddy devs used named xcaddy: GitHub - caddyserver/xcaddy: Build Caddy with plugins.

When you run xcaddy with: xcaddy build --with github.com/caddy-dns/cloudflare

You get a main.go and go.mod that contain the following:

package main

import (
        _ "github.com/caddy-dns/cloudflare" # This is the caddy moudule for cloudflare dns
        caddycmd "github.com/caddyserver/caddy/v2/cmd"
        _ "github.com/caddyserver/caddy/v2/modules/standard"
)

func main() {
        caddycmd.Main()
}
module caddy
go 1.15
require (
        github.com/caddy-dns/cloudflare v0.0.0-20210227191100-728f5b67d095
        github.com/caddyserver/caddy/v2 v2.4.0-beta.1
)

it then runs:

go mod tidy
go build -o caddy -ldflags "-w -s" -trimpath

And that builds a caddy webserver with the cloudflare dns module enabled. You can additional caddy modules by just adding more --with flags that point to caddy module repositories.

My goal would be to replace the xcaddy tool with a nix program. I imagine it will start by forking the caddy form nixpkgs: https://github.com/NixOS/nixpkgs/blob/6040ffb45f78c66ecedcbcb82fed58bacec71198/pkgs/servers/caddy/default.nix and add some overrides that use main.go and go.mod templates that will be populated with the overrides that get passed to it. Any suggestions about this are very much welcome! (this is kinda what I’m fishing for)

Another reason I want to do this is because I think xcaddy is basically doing the oppsite of the “nix way” and it just feels very uncomfortable to put it in my nix workflow since i can’t use GitHub - nix-community/vgo2nix: Convert go.mod files to nixpkgs buildGoPackage compatible deps.nix files [maintainer=@adisbladis] on the generated go.mod file and go.sum that xcaddy spits outs. It would basically have a different nix hash every time.

Thanks for reading!

2 Likes

I ended up not using the xcaddy build tool with nix and I’m just using nix to build a goPackage with the imports.

So no dynamic caddy plugins yet.

Take a look at: GitHub - sbc64/nix-caddy: Caddy built with nix