# SPDX-FileCopyrightText: © 2025 Jeffrey C. Ollie # SPDX-License-Identifier: MIT { description = "zig-streamdeck"; inputs = { nixpkgs = { url = "https://channels.nixos.org/nixpkgs-unstable/nixexprs.tar.zst"; }; }; outputs = { self, nixpkgs, ... }: let inherit (nixpkgs) lib; linuxSystems = builtins.filter ( system: (lib.systems.elaborate system).isLinux ) lib.systems.flakeExposed; makePackages = system: import nixpkgs { inherit system; }; forAllSystems = lib.genAttrs linuxSystems; # 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. That is why this # project drives its targets from `tools/fuzz.zig` as well, 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 { devShells = forAllSystems ( system: let pkgs = makePackages system; in { default = pkgs.mkShell { name = "zig-streamdeck"; nativeBuildInputs = [ (fuzzableZig pkgs) pkgs.pkg-config pkgs.reuse ]; buildInputs = [ pkgs.libjpeg_turbo pkgs.resvg ]; }; } ); packages = forAllSystems ( system: let pkgs = makePackages system; in { default = pkgs.zigStdenv.mkDerivation { pname = "zig-streamdeck"; version = "0.1.0"; nativeBuildInputs = [ pkgs.zig_0_16 pkgs.pkg-config ]; buildInputs = [ pkgs.libjpeg_turbo pkgs.resvg ]; src = ./.; }; } ); # The udev rules, which are the one thing a program using this # library cannot arrange for itself: without them it finds the # device, fails to open it, and answers `error.AccessDenied`. # # System-independent, so not wrapped in `forAllSystems`. nixosModules = { zig-streamdeck = import ./nix/module.nix; default = self.nixosModules.zig-streamdeck; }; }; }