Something went wrong. Try again.
An IMAP client library for Zig 0.17, covering IMAP4rev2 and rev1 with STARTTLS, implicit TLS, SASL authentication and streamed literals. jeff.jcollie.page/zig-imap
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186# SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us># SPDX-License-Identifier: MIT
{ description = "zig-imap — a IMAP client library for Zig";
inputs = { nixpkgs = { url = "https://channels.nixos.org/nixos-unstable/nixexprs.tar.xz"; }; };
outputs = { self, nixpkgs, ... }:
let inherit (nixpkgs) lib; makePackages = system: import nixpkgs { inherit system; }; forAllSystems = lib.genAttrs lib.systems.flakeExposed;
# The devshell's Zig, with one line of its own standard library put # right, because without it `zig build test --fuzz` cannot compile. # # Zig 0.16.0's `compiler/test_runner.zig` reports a failing fuzz input by # asking `std.debug.writeStackTrace` to print what `@errorReturnTrace()` # gave it. Those are two different types: an error return trace is a # `builtin.StackTrace`, a ring buffer with a write index, and that # function takes a `debug.StackTrace`, which is a plain slice and a count # of what was skipped. It is a type error, it is on the path taken only # under `-ffuzz`, and it stops *any* project with a fuzz test in it from # building one. The fix is the function next door: `writeErrorReturnTrace` # takes exactly the type in hand and is what the other three places in # the same file use. # # `--replace-fail` is the whole safety of this: the day Zig ships the fix # the pattern will not be found, the build will fail here rather than # patch something else, and this can go. # # It buys the fuzzer and not its coverage. Nothing in this release # populates the table of program counters, so a bounded run ends with # "corrupted coverage file: pcs_len was zero" and an unbounded one # panics in the build runner's coverage thread; neither is a finding, # and a finding says "input saved to" above the report. `zig build fuzz` # runs the loop in `tools/fuzz.zig` instead, which needs none of it. fuzzableZig = pkgs: let # A farm of symlinks rather than a copy: the library is 217 MB, and # exactly one file of it is being changed. library = pkgs.runCommand "zig-0.16.0-lib-fuzz-fix" { } '' cp -rs --no-preserve=mode ${pkgs.zig_0_16}/lib/zig $out chmod -R u+w $out rm $out/compiler/test_runner.zig cp --no-preserve=mode \ ${pkgs.zig_0_16}/lib/zig/compiler/test_runner.zig \ $out/compiler/test_runner.zig substituteInPlace $out/compiler/test_runner.zig \ --replace-fail \ 'std.debug.writeStackTrace(trace, stderr)' \ 'std.debug.writeErrorReturnTrace(trace, stderr)' ''; in pkgs.symlinkJoin { name = "zig-0.16.0-fuzzable"; paths = [ pkgs.zig_0_16 ]; nativeBuildInputs = [ pkgs.makeWrapper ]; postBuild = '' wrapProgram $out/bin/zig --set ZIG_LIB_DIR ${library} ''; }; in { packages = forAllSystems ( system: let pkgs = makePackages system; # Generated from build.zig.zon by zon2nix; regenerate with # nix run github:jcollie/zon2nix#zon2nix -- --nix=build.zig.zon.nix --16 build.zig.zon # A Nix build has no network, and zig-sasl is a dependency now, so # this farm is where it comes from instead. zig-deps = pkgs.callPackage ./build.zig.zon.nix { }; in { # Exposed so a CI job running `zig build` for something other than # this package can hand the same farm to it. inherit zig-deps;
# The driver the NixOS test runs inside the guest. It is a package of # its own rather than part of a library build, because the thing it # needs -- a Dovecot on another machine -- only exists in that test. integration = pkgs.stdenv.mkDerivation (finalAttrs: { pname = "zig-imap-integration"; version = "0.1.0";
src = lib.fileset.toSource { root = ./.; # Named file by file, so that editing the NixOS test or the # README does not rebuild this and every virtual machine with it. fileset = lib.fileset.unions [ ./build.zig ./build.zig.zon ./build.zig.zon.nix ./src ./tests/integration.zig ./tests/e2e.zig ./tests/fuzz.zig ./tools ]; };
# `zig_0_16` is the setup hook as well as the compiler in current # nixpkgs, and it builds ReleaseSafe for a baseline CPU. nativeBuildInputs = [ pkgs.zig_0_16 ];
# `--system` does not merely offer the farm, it forbids fetching: # a dependency missing from it is a build error naming the package # rather than a network attempt that fails less legibly. zigBuildFlags = [ "-Dintegration=true" "--system" "${zig-deps}" ]; # The check phase assembles its own flags rather than reusing the # build's, so without this the tests would be built without the # option above. zigCheckFlags = finalAttrs.zigBuildFlags; # The unit tests and the loopback end-to-end tests run here: the # build sandbox has a loopback interface, and a driver built from a # tree whose own tests fail is not worth carrying into a guest. doCheck = true; }); } );
# Linux only: without this `nix flake check` tries to evaluate a NixOS # test for Darwin and fails before running anything. checks = lib.genAttrs (lib.filter (system: lib.hasSuffix "-linux" system) lib.systems.flakeExposed) ( system: let pkgs = makePackages system; in { # `nix build .#checks.x86_64-linux.dovecot -L` runs it by hand; it # needs /dev/kvm, so in CI it goes on an ephemeral runner. dovecot = pkgs.testers.runNixOSTest ( import ./tests/nixos/dovecot.nix { inherit pkgs; integration = self.packages.${system}.integration; } ); } );
devShells = forAllSystems ( system: let pkgs = makePackages system; in { default = pkgs.mkShell { name = "zig-imap"; nativeBuildInputs = [ (fuzzableZig pkgs) pkgs.git-pages-cli pkgs.reuse ] ++ lib.optionals pkgs.stdenv.hostPlatform.isLinux [ pkgs.kcov pkgs.perf ]; }; } ); };}