Unable to build Caddy with custom modules

TLDR: The files I’m using to attempt to build this are in this gist (I can post them directly here if that’s the preferred approach).

I’m attempting to build this with the command nix-build ./build_caddy.nix
The error message is

install: cannot stat '/nix/store/krcc44294g624jah0vf778czc4j3dxk5-source/init/caddy.service': No such file or directory
install: cannot stat '/nix/store/krcc44294g624jah0vf778czc4j3dxk5-source/init/caddy-api.service': No such file or directory
build failed in installPhase with exit code 1

Any ideas what I’m doing wrong?

I’ve been trying to build a custom package for the Caddy Web Server. I’m running on NixOS 21.11, and wanted to use the latest version of Caddy 2.5.1.

I saw that a derivation for 2.5.1 exists in nixos-unstable. So I started with copying this over to see I could build a package with it. I quickly discovered that the newer version of Caddy requires Go 1.17+, so I switched over to using buildGo117Module instead of buildGoModule.

I was also trying to add some custom plugins to Caddy, and plugins in caddy are added by compiling a version with the plugins added as imports. I found this blog post which led me to most of my derivation. It builds on work from @diamondburned and @phaer that’s seen in this comment.

However my attempt at building this seems to fail, apparently in the installation of the systemd unit files based on the error message at the top of this post.

I tried using the breakpointHook, but I wasn’t able to see the init files, and I’m not sure if that’s an artifact of breakpointHook or if it’s a problem with the build.


@NobbZ and viper@ ( from the discord channel) helped me find the solution to this problem over discord.
The crux of the issue was that I had re-used the sha-256 values in the two fetchFromGithub calls.

The reused sha256 value seems to have caused nix to believe that it didn’t need to download this again, and it had the dist derivation have content equal to the src derivation.

The best way I could have figured this out on my own would have been to simply poke around the filesystem by viewing /nix/store/krcc44294g624jah0vf778czc4j3dxk5-source. A simple ls there would have shown me that the contents weren’t aligned with what’s on the Github repo.

I have not updated the code in the gist, but added a README linking to this post and a summary of what the issue was. Hoping to keep that link so that there is no bitrot from this post.

1 Like

Awesome you find it !
You might wanna copy the gist over here so it easier to follow through for later guests.

Here is to be the gist

This text will be hidden

The original caddy.nix is below. Not the reused sha256 value.

{ lib, buildGo117Module, fetchFromGitHub, nixosTests, plugins ? [ ], pkgs }:
  version = "2.5.1";
  dist = fetchFromGitHub {
    owner = "caddyserver";
    repo = "dist";
    rev = "v${version}";
    sha256 = "sha256:1nlphjg5wh5drpwkm4cczrkxdzbv72ll7hp5x7z6ww8pzz3q10b3";
  imports = lib.flip lib.concatMapStrings plugins (pkg: "	_ \"${pkg}\"\n");
  main = ''
    package main

    import (
    	caddycmd "github.com/caddyserver/caddy/v2/cmd"
    	_ "github.com/caddyserver/caddy/v2/modules/standard"

    func main() {
in buildGo117Module {
  pname = "caddy";
  inherit version;
  runVend = true;

  subPackages = [ "cmd/caddy" ];

  src = fetchFromGitHub {
    owner = "caddyserver";
    repo = "caddy";
    rev = "v${version}";
    sha256 = "sha256:1nlphjg5wh5drpwkm4cczrkxdzbv72ll7hp5x7z6ww8pzz3q10b3";

  vendorSha256 = "sha256:082yh6rqr41xwg2xpqg2sdms9m7bw68bc44bgg3xwfbgdbzrs0ni";

  nativeBuildInputs = [ pkgs.breakpointHook ];

  overrideModAttrs = (_: {
    preBuild = "echo '${main}' > cmd/caddy/main.go";
    postInstall = "cp go.sum go.mod $out/ && ls $out/";

  postPatch = ''
    echo '${main}' > cmd/caddy/main.go
    cat cmd/caddy/main.go

  postConfigure = ''
    cp vendor/go.sum ./
    cp vendor/go.mod ./

  postInstall = ''
    install -Dm644 ${dist}/init/caddy.service ${dist}/init/caddy-api.service -t $out/lib/systemd/system

    substituteInPlace $out/lib/systemd/system/caddy.service --replace "/usr/bin/caddy" "$out/bin/caddy"
    substituteInPlace $out/lib/systemd/system/caddy-api.service --replace "/usr/bin/caddy" "$out/bin/caddy"

  passthru.tests = { inherit (nixosTests) caddy; };

  meta = with lib; {
    homepage = "https://caddyserver.com";
    description = "Fast, cross-platform HTTP/2 web server with automatic HTTPS";
    license = licenses.asl20;
    maintainers = with maintainers; [ Br1ght0ne techknowlogick ];