Skip to content

Instantly share code, notes, and snippets.

@hbjydev
Last active April 26, 2024 22:03
Show Gist options
  • Save hbjydev/4def8f143a1448d29617183e9114e3e3 to your computer and use it in GitHub Desktop.
Save hbjydev/4def8f143a1448d29617183e9114e3e3 to your computer and use it in GitHub Desktop.
Run `nix build .#caddy` against this to generate a Caddy binary using xcaddy with whatever plugins you've specified built in.
{
inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
outputs = inputs@{ flake-parts, ... }:
flake-parts.lib.mkFlake { inherit inputs; } {
systems = ["x86_64-linux"];
perSystem = { pkgs, ... }: {
packages.caddy = pkgs.callPackage ./pkg.nix {};
};
};
}
{ buildGoModule, stdenv, lib, git, go, xcaddy }:
let
plugins = [ "github.com/caddyserver/ntlm-transport@v0.1.1" ];
src = stdenv.mkDerivation {
pname = "caddy-buildenv";
version = "latest";
dontUnpack = true;
nativeBuildInputs = [ git go xcaddy ];
configurePhase = ''
export GOCACHE=$TMPDIR/go-cache
export GOPATH="$TMPDIR/go"
# Needed so we can use buildGoModule to create the binary later.
export XCADDY_SKIP_BUILD=1
'';
buildPhase =
let
pluginArgs = lib.concatMapStringsSep " " (plugin: "--with ${plugin}") plugins;
in
''
runHook preBuild
${xcaddy}/bin/xcaddy build latest ${pluginArgs}
(cd buildenv_* && go mod vendor)
runHook postBuild
'';
installPhase = ''
runHook preInstall
mkdir -p $out
cp -r buildenv_*/* $out/
runHook postInstall
'';
outputHashMode = "recursive";
outputHashAlgo = "sha256";
# Update this when you change your plugin set (including version changes)
outputHash = "sha256-3spjFss60N8fMlluTO2rlz68iP9g48n5mqUBfgvugdo=";
};
in
buildGoModule {
name = "caddy";
inherit src;
vendorHash = null; # We already vendor our dependencies in src, so we don't need to specify this.
doCheck = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment