Something went wrong. Try again.
A port of FlightAware's dump1090, a Mode S and ADS-B decoder for software defined radios, to Zig 0.16.
Something went wrong. Try again.
zig-dump1090 zon.nix
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119# SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us># SPDX-License-Identifier: GPL-2.0-or-later## Rendering Nix values as ZON, Zig's object notation, so that a NixOS module# can write zig-dump1090's configuration file.## ZON has one shape Nix has no equivalent for: the enum literal, written `.foo`# and distinct from the string `"foo"`. Five settings take one -- `sdr.device`,# `sdr.format`, `decode.fix`, `json.location_accuracy` and a connector's# `protocol` -- so it cannot be ignored.## The convention here is that a string which looks exactly like a leading dot# followed by a Zig identifier is an enum literal, and anything else is a# string. So `device = ".rtlsdr"` in Nix becomes `.device = .rtlsdr` in ZON,# which is what the person writing it sees in the example configuration and in# FlightAware's or an aggregator's instructions.## The identifier test is what keeps that from swallowing real strings: a path# contains a slash or a dot in the wrong place, so "./recording.bin",# "/run/dump1090" and "feed.example.org" are all left as strings. A file named# exactly ".rtlsdr" would be misread, and the answer to that is an absolute# path, which every path-valued setting here should have anyway.
{ lib }:
let inherit (builtins) isAttrs isBool isFloat isInt isList isString match ;
identifier = "\\.[A-Za-z_][A-Za-z0-9_]*";
# Zig's string escapes. Anything outside printable ASCII goes out as \xNN, # which ZON accepts and which avoids having to think about encodings. escape = s: lib.concatMapStrings ( c: if c == "\"" then "\\\"" else if c == "\\" then "\\\\" else if c == "\n" then "\\n" else if c == "\t" then "\\t" else if c == "\r" then "\\r" else c ) (lib.stringToCharacters s);
render = indent: value: let pad = lib.concatStrings (lib.genList (_: " ") indent); inner = lib.concatStrings (lib.genList (_: " ") (indent + 1)); in if value == null then "null" else if isBool value then (if value then "true" else "false") else if isInt value then toString value else if isFloat value then # `toString` renders a Nix float in a form Zig's parser accepts, but a # whole number comes out as "1." which it does not; pad it. let s = toString value; in if lib.hasSuffix "." s then "${s}0" else s else if isString value then (if match identifier value != null then value else "\"${escape value}\"") else if isList value then if value == [ ] then ".{}" else ".{\n" + lib.concatMapStrings (v: "${inner}${render (indent + 1) v},\n") value + "${pad}}" else if isAttrs value then let # A null means "leave this setting out", which is not the same as # writing null: every field in the schema is optional and absent means # "keep the program's default". present = lib.filterAttrs (_: v: v != null) value; in if present == { } then ".{}" else ".{\n" + lib.concatMapStrings ( n: "${inner}.${n} = ${render (indent + 1) present.${n}},\n" ) (lib.attrNames present) + "${pad}}" else throw "zon: cannot render a value of type ${builtins.typeOf value}";in{ # A whole configuration file, header and trailing newline included. document = settings: '' // Generated by the zig-dump1090 NixOS module. Do not edit; change // services.dump1090.settings instead. '' + render 0 settings + "\n";
# Exposed for testing. inherit render;}