1 { lib, stdenv, fetchurl
3 , runCommand, pkg-config, meson, ninja, makeWrapper
5 , knot-dns, luajitPackages, libuv, gnutls, lmdb
6 , jemalloc, systemd, libcap_ng, dns-root-data, nghttp2 # optionals, in principle
7 , fstrm, protobufc # more optionals
9 , cmocka, which, cacert
10 , extraFeatures ? false /* catch-all if defaults aren't enough */
12 let # un-indented, over the whole file
14 result = if extraFeatures then wrapped-full else unwrapped;
16 inherit (lib) optional optionals optionalString;
19 unwrapped = stdenv.mkDerivation rec {
20 pname = "knot-resolver";
24 url = "https://secure.nic.cz/files/knot-resolver/${pname}-${version}.tar.xz";
25 hash = "sha256-X2oic5D81MLQqAKKZStVqdhj7HvgEpj+A43x0nP7mg8=";
28 outputs = [ "out" "dev" ];
30 # Path fixups for the NixOS service.
32 patch meson.build <<EOF
34 -systemd_work_dir = prefix / get_option('localstatedir') / 'lib' / 'knot-resolver'
35 -systemd_cache_dir = prefix / get_option('localstatedir') / 'cache' / 'knot-resolver'
36 +systemd_work_dir = '/var/lib/knot-resolver'
37 +systemd_cache_dir = '/var/cache/knot-resolver'
40 # ExecStart can't be overwritten in overrides.
41 # We need that to use wrapped executable and correct config file.
42 sed '/^ExecStart=/d' -i systemd/kresd@.service.in
44 # On x86_64-darwin loading by soname fails to find the libs, surprisingly.
45 # Even though they should already be loaded and they're in RPATH, too.
46 for f in daemon/lua/{kres,zonefile}.lua; do
47 substituteInPlace "$f" \
48 --replace "ffi.load(" "ffi.load('${lib.getLib knot-dns}/lib/' .. "
51 # some tests have issues with network sandboxing, apparently
52 + optionalString doInstallCheck ''
53 echo 'os.exit(77)' > daemon/lua/trust_anchors.test/bootstrap.test.lua
54 sed -E '/^[[:blank:]]*test_(dstaddr|headers),?$/d' -i \
55 tests/config/doh2.test.lua modules/http/http_doh.test.lua
59 patchShebangs scripts/
62 nativeBuildInputs = [ pkg-config meson ninja ];
64 # http://knot-resolver.readthedocs.io/en/latest/build.html#requirements
65 buildInputs = [ knot-dns lua.lua libuv gnutls lmdb ]
66 ## the rest are optional dependencies
67 ++ optionals stdenv.isLinux [ /*lib*/systemd libcap_ng ]
68 ++ [ jemalloc nghttp2 ]
69 ++ [ fstrm protobufc ] # dnstap support
73 "-Dkeyfile_default=${dns-root-data}/root.ds"
74 "-Droot_hints=${dns-root-data}/root.hints"
75 "-Dinstall_kresd_conf=disabled" # not really useful; examples are inside share/doc/
77 "--default-library=static" # not used by anyone
79 ++ optional doInstallCheck "-Dunit_tests=enabled"
80 ++ optional doInstallCheck "-Dconfig_tests=enabled"
81 ++ optional stdenv.isLinux "-Dsystemd_files=enabled" # used by NixOS service
82 #"-Dextra_tests=enabled" # not suitable as in-distro tests; many deps, too.
86 rm "$out"/lib/libkres.a
87 rm "$out"/lib/knot-resolver/upgrade-4-to-5.lua # not meaningful on NixOS
88 '' + optionalString stdenv.hostPlatform.isLinux ''
89 rm -r "$out"/lib/sysusers.d/ # ATM more likely to harm than help
92 doInstallCheck = with stdenv; hostPlatform == buildPlatform;
93 nativeInstallCheckInputs = [ cmocka which cacert lua.cqueues lua.basexx lua.http ];
94 installCheckPhase = ''
95 meson test --print-errorlogs --no-suite snowflake
99 description = "Caching validating DNS resolver, from .cz domain registry";
100 homepage = "https://knot-resolver.cz";
101 license = licenses.gpl3Plus;
102 platforms = platforms.unix;
103 maintainers = [ maintainers.vcunat /* upstream developer */ ];
104 mainProgram = "kresd";
108 wrapped-full = runCommand unwrapped.name
110 nativeBuildInputs = [ makeWrapper ];
111 buildInputs = with luajitPackages; [
112 # For http module, prefill module, trust anchor bootstrap.
113 # It brings lots of deps; some are useful elsewhere (e.g. cqueues).
115 # psl isn't in nixpkgs yet, but policy.slice_randomize_psl() seems not important.
117 preferLocalBuild = true;
118 allowSubstitutes = false;
119 inherit (unwrapped) meta;
123 makeWrapper '${unwrapped}/bin/kresd' "$out"/bin/kresd \
124 --set LUA_PATH "$LUA_PATH" \
125 --set LUA_CPATH "$LUA_CPATH"
127 ln -sr '${unwrapped}/share' "$out"/
128 ln -sr '${unwrapped}/lib' "$out"/ # useful in NixOS service
129 ln -sr "$out"/{bin,sbin}
130 '' + lib.optionalString unwrapped.doInstallCheck ''
131 echo "Checking that 'http' module loads, i.e. lua search paths work:"
132 echo "modules.load('http')" > test-http.lua
133 echo -e 'quit()' | env -i "$out"/bin/kresd -a 127.0.0.1#53535 -c test-http.lua