# SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie # SPDX-License-Identifier: MIT { description = "DNS message encoding and decoding for Zig"; inputs = { nixpkgs = { url = "https://channels.nixos.org/nixos-unstable/nixexprs.tar.xz"; }; zon2nix = { url = "github:jcollie/zon2nix"; inputs = { nixpkgs.follows = "nixpkgs"; }; }; }; outputs = { nixpkgs, zon2nix, ... }: let inherit (nixpkgs) lib; makePackages = system: import nixpkgs { inherit system; }; forAllSystems = lib.genAttrs lib.systems.flakeExposed; # The Python that `zig build differential` runs, with dnspython held at # 2.9.1 until nixpkgs catches up: 2.9.0 and 2.9.1 refuse a good deal on # the wire that 2.8.0 accepted, and the differences that makes are worth # having written down before anything depends on them. When # `python3Packages.dnspython` in nixpkgs reaches 2.9.1, this override # goes and the plain package comes back. differentialPython = pkgs: pkgs.python3.withPackages (python-pkgs: [ (python-pkgs.dnspython.overridePythonAttrs (old: rec { version = "2.9.1"; src = pkgs.fetchPypi { pname = "dnspython"; inherit version; hash = "sha256-mm0w5V+Nxj5gUll2ZhEr/wWGuVokdKne2gOvu/gg03o="; }; # 2.9 moved from hatchling to uv's build backend. build-system = [ python-pkgs.uv-build ]; # Three of its zone tests read IDNA 2008 names, and 2.9 will only # use idna 3.20 or newer, which nixpkgs does not have yet. Nothing # here reads a zone file. disabledTests = old.disabledTests ++ [ "testJustificationAndDefaultTTL" "testToTextForwardsStyle" "testUnicodeThereAndBack" ]; })) ]); in { # The differential check as a build rather than a devshell command, so # that `nix flake check` covers it too. It needs no network: the Zig # dependencies come from build.zig.zon.nix, which zon2nix generates from # the manifest, and `--system` makes a missing one an error naming the # package rather than a silent attempt to fetch it. checks = forAllSystems ( system: let pkgs = makePackages system; # Generated from build.zig.zon; regenerate with # nix develop -c zon2nix --17 --nix=build.zig.zon.nix build.zig.zon zigDeps = pkgs.callPackage ./build.zig.zon.nix { }; # Only the files the check actually reads, so that editing the README # does not rebuild it. source = lib.fileset.toSource { root = ./.; fileset = lib.fileset.unions [ ./build.zig ./build.zig.zon ./src ./tests ./tools ]; }; in { differential = pkgs.runCommand "zig-dns-differential" { nativeBuildInputs = [ pkgs.zig_0_17 (differentialPython pkgs) ]; } '' cp -r ${source} source chmod -R u+w source cd source export ZIG_GLOBAL_CACHE_DIR="$TMPDIR/zig-cache" zig build differential --system ${zigDeps} touch $out ''; } ); devShells = forAllSystems ( system: let pkgs = makePackages system; in { default = pkgs.mkShell { name = "zig-dns"; nativeBuildInputs = [ pkgs.zig_0_17 pkgs.git-pages-cli pkgs.pinact pkgs.reuse # `dig +qr` prints the query it sent as well as the answer, and # `dig +yaml` names every field, which is how a decoding here is # checked against an implementation that is not this one. `dnsq` # and `drill` disagree with it often enough to be worth having. pkgs.bind pkgs.ldns # The second decoder. `zig build differential` puts the same # mutated messages to this library and to dnspython and compares # the verdicts, which is the only check here that can catch the # decoder and the encoder being wrong the same way. (differentialPython pkgs) # Regenerates build.zig.zon.nix, which is how the flake check # gets the Zig dependencies without a network: # # zon2nix --17 --nix=build.zig.zon.nix build.zig.zon # # Wrapped so that the `zig env` it shells out to is this # project's Zig rather than whatever the caller happens to have. (pkgs.symlinkJoin { name = "zon2nix"; paths = [ zon2nix.packages.${pkgs.stdenv.hostPlatform.system}.zon2nix ]; nativeBuildInputs = [ pkgs.makeWrapper ]; postBuild = '' wrapProgram $out/bin/zon2nix \ --prefix PATH : ${lib.makeBinPath [ pkgs.zig_0_17 ]} ''; }) ] ++ lib.optionals pkgs.stdenv.hostPlatform.isLinux [ pkgs.kcov pkgs.perf ]; }; } ); }; }