1{
2 lib,
3 stdenv,
4 fetchurl,
5 fetchpatch2,
6 pcre,
7 pcre2,
8 jemalloc,
9 libunwind,
10 libxslt,
11 groff,
12 ncurses,
13 pkg-config,
14 readline,
15 libedit,
16 coreutils,
17 python3,
18 makeWrapper,
19 nixosTests,
20}:
21
22let
23 common =
24 {
25 version,
26 hash,
27 extraNativeBuildInputs ? [ ],
28 }:
29 stdenv.mkDerivation rec {
30 pname = "varnish";
31 inherit version;
32
33 src = fetchurl {
34 url = "https://varnish-cache.org/_downloads/${pname}-${version}.tgz";
35 inherit hash;
36 };
37
38 nativeBuildInputs = with python3.pkgs; [
39 pkg-config
40 docutils
41 sphinx
42 makeWrapper
43 ];
44 buildInputs = [
45 libxslt
46 groff
47 ncurses
48 readline
49 libedit
50 python3
51 ]
52 ++ lib.optional (lib.versionOlder version "7") pcre
53 ++ lib.optional (lib.versionAtLeast version "7") pcre2
54 ++ lib.optional stdenv.hostPlatform.isDarwin libunwind
55 ++ lib.optional stdenv.hostPlatform.isLinux jemalloc;
56
57 buildFlags = [ "localstatedir=/var/run" ];
58
59 patches =
60 lib.optionals (stdenv.isDarwin && lib.versionAtLeast version "7.7") [
61 # Fix VMOD section attribute on macOS
62 # Unreleased commit on master
63 (fetchpatch2 {
64 url = "https://github.com/varnishcache/varnish-cache/commit/a95399f5b9eda1bfdba6ee6406c30a1ed0720167.patch";
65 hash = "sha256-T7DIkmnq0O+Cr9DTJS4/rOtg3J6PloUo8jHMWoUZYYk=";
66 })
67 # Fix endian.h compatibility on macOS
68 # PR: https://github.com/varnishcache/varnish-cache/pull/4339
69 ./patches/0001-fix-endian-h-compatibility-on-macos.patch
70 ]
71 ++ lib.optionals (stdenv.isDarwin && lib.versionOlder version "7.6") [
72 # Fix duplicate OS_CODE definitions on macOS
73 # PR: https://github.com/varnishcache/varnish-cache/pull/4347
74 ./patches/0002-fix-duplicate-os-code-definitions-on-macos.patch
75 ];
76
77 postPatch = ''
78 substituteInPlace bin/varnishtest/vtc_main.c --replace /bin/rm "${coreutils}/bin/rm"
79 '';
80
81 postInstall = ''
82 wrapProgram "$out/sbin/varnishd" --prefix PATH : "${lib.makeBinPath [ stdenv.cc ]}"
83 '';
84
85 # https://github.com/varnishcache/varnish-cache/issues/1875
86 env.NIX_CFLAGS_COMPILE = lib.optionalString stdenv.hostPlatform.isi686 "-fexcess-precision=standard";
87
88 outputs = [
89 "out"
90 "dev"
91 "man"
92 ];
93
94 passthru = {
95 python = python3;
96 tests =
97 nixosTests."varnish${builtins.replaceStrings [ "." ] [ "" ] (lib.versions.majorMinor version)}";
98 };
99
100 meta = with lib; {
101 description = "Web application accelerator also known as a caching HTTP reverse proxy";
102 homepage = "https://www.varnish-cache.org";
103 license = licenses.bsd2;
104 teams = [ lib.teams.flyingcircus ];
105 platforms = platforms.unix;
106 };
107 };
108in
109{
110 # EOL (LTS) TBA
111 varnish60 = common {
112 version = "6.0.14";
113 hash = "sha256-tZlBf3ppntxxYSufEJ86ot6ujvnbfIyZOu9B3kDJ72k=";
114 };
115 # EOL 2026-03-15
116 varnish77 = common {
117 version = "7.7.1";
118 hash = "sha256-TAbFyZaApCm3KTT5/VE5Y/fhuoVTszyn7BLIWlwrdRo=";
119 };
120}