# SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie # SPDX-License-Identifier: GPL-2.0-or-later { config, lib, pkgs, ... }: let cfg = config.services.dump1090; zon = import ./zon.nix { inherit lib; }; stateDir = "/var/lib/dump1090"; runtimeDir = "/run/dump1090"; # Where FlightAware's identifier is kept, unless the configuration says # otherwise. Defaulted rather than left out because a login carrying no # identifier does not mean "log in again", it means "make me another site": # a receiver with nowhere to keep one quietly accumulates them. withDefaults = settings: settings // lib.optionalAttrs (settings ? flightaware && settings.flightaware != null) { flightaware = { feeder_id_file = "${stateDir}/feeder_id"; } // settings.flightaware; }; settingsFile = pkgs.writeText "dump1090.zon" (zon.document (withDefaults cfg.settings)); configFile = if cfg.configFile != null then cfg.configFile else settingsFile; usesRtlSdr = (cfg.settings.sdr.device or null) == ".rtlsdr"; # Every port named under `outputs` or `inputs`, which is what a reader on # another machine would need to reach. listeningPorts = lib.filter (v: builtins.isInt v) ( lib.attrValues (cfg.settings.outputs or { }) ++ lib.attrValues (cfg.settings.inputs or { }) ); in { options.services.dump1090 = { enable = lib.mkEnableOption "the zig-dump1090 Mode S and ADS-B receiver"; package = lib.mkPackageOption pkgs "zig-dump1090" { }; settings = lib.mkOption { type = lib.types.attrsOf lib.types.anything; default = { }; description = '' The receiver's configuration, rendered to the ZON file it reads. `doc/dump1090.zon` in the source is a commented example of every setting, and the field names here are exactly those. A string that is a dot followed by an identifier becomes a ZON enum literal rather than a string, which is how the five settings that take one are written: `sdr.device`, `sdr.format`, `decode.fix`, `json.location_accuracy` and a connector's `protocol`. Everything else stays a string, so paths and host names need no special treatment. A setting left `null` is omitted, which is not the same as sending null: an absent setting keeps the program's own default. ''; example = lib.literalExpression '' { receiver = { lat = 41.5868; lon = -93.6250; max_range_nm = 300; }; sdr = { device = ".rtlsdr"; gain = { db = 25.4; }; }; outputs = { bind = "127.0.0.1"; beast = 30005; }; connectors = [ { host = "feed.airplanes.live"; port = 30004; protocol = ".beast_reduce_plus_out"; uuid = "00000000-0000-0000-0000-000000000000"; } ]; flightaware.mac = "00:11:22:33:44:55"; } ''; }; configFile = lib.mkOption { type = lib.types.nullOr lib.types.path; default = null; description = '' A configuration file to use instead of rendering `settings`. For anything the renderer cannot express, or a file kept outside the Nix store because it holds something private. ''; }; openFirewall = lib.mkOption { type = lib.types.bool; default = false; description = '' Open the ports named under `settings.outputs` and `settings.inputs`. Leave this off when those are bound to the loopback address, which is where they belong unless something on another machine reads them: every one of these protocols is plaintext and unauthenticated. ''; }; blacklistDvbDriver = lib.mkOption { type = lib.types.bool; default = usesRtlSdr; defaultText = lib.literalExpression ''config.services.dump1090.settings.sdr.device == ".rtlsdr"''; description = '' Keep the kernel's DVB-T driver off the dongle. The driver underneath can take the dongle from the kernel on open and give it back on close, and usually does. Its teardown is not reliable: it has been seen to deadlock in `i2c_del_adapter`, by way of `rtl2832_remove` and `dvb_usbv2_disconnect`, leaving the receiver's own process in uninterruptible sleep -- unkillable, still holding the device, and not released by unplugging it. Only a reboot cleared that. A driver that is never bound never has to be detached. ''; }; }; config = lib.mkIf cfg.enable { assertions = [ { assertion = cfg.settings != { } || cfg.configFile != null; message = "services.dump1090: set either settings or configFile; with neither the receiver has no radio and no outputs and will exit at once."; } ]; boot.blacklistedKernelModules = lib.mkIf cfg.blacklistDvbDriver [ "dvb_usb_rtl28xxu" ]; # The udev rules that make the dongle reachable by the plugdev group. services.udev.packages = lib.mkIf usesRtlSdr [ pkgs.rtl-sdr ]; users.users.dump1090 = { isSystemUser = true; group = "dump1090"; description = "zig-dump1090 receiver"; extraGroups = lib.optional usesRtlSdr "plugdev"; }; users.groups.dump1090 = { }; networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall listeningPorts; systemd.services.dump1090 = { description = "zig-dump1090 Mode S and ADS-B receiver"; documentation = [ "https://jeff.jcollie.page/zig-dump1090/" ]; # It dials out to whatever feeds are configured, so it wants a usable # network rather than merely a configured one. wants = [ "network-online.target" ]; after = [ "network-online.target" ]; wantedBy = [ "multi-user.target" ]; serviceConfig = { Type = "exec"; ExecStart = "${lib.getExe cfg.package} --config ${configFile} --quiet"; # No ExecReload on purpose: the receiver does not re-read its # configuration while running, so a reload would have to send a signal # it does not handle, and that signal's default action is to kill it. # Switching configuration restarts it, which is the honest way. # A receiver that stops is a receiver nobody notices has stopped. Come # back, but not so fast that a misconfiguration becomes a busy loop. Restart = "always"; RestartSec = 30; User = "dump1090"; Group = "dump1090"; # /run/dump1090 for the JSON files a map polls, and /var/lib/dump1090 # for the FlightAware identifier -- the only thing this program writes, # and the only reason it needs anywhere to write at all. RuntimeDirectory = "dump1090"; RuntimeDirectoryMode = "0755"; StateDirectory = "dump1090"; StateDirectoryMode = "0750"; # It reads one configuration file, talks to one USB device, and opens # sockets. Nothing else needs to be reachable. NoNewPrivileges = true; CapabilityBoundingSet = ""; AmbientCapabilities = ""; ProtectSystem = "strict"; ProtectHome = true; PrivateTmp = true; PrivateMounts = true; ProtectKernelTunables = true; ProtectKernelModules = true; ProtectKernelLogs = true; ProtectControlGroups = true; ProtectClock = true; ProtectHostname = true; ProtectProc = "invisible"; RestrictSUIDSGID = true; RestrictRealtime = true; RestrictNamespaces = true; LockPersonality = true; MemoryDenyWriteExecute = true; SystemCallArchitectures = "native"; SystemCallFilter = [ "@system-service" ]; SystemCallErrorNumber = "EPERM"; UMask = "0022"; # PrivateDevices would give it a /dev without /dev/bus/usb in it, # which is the one device it actually needs. Deny everything and allow # that instead. DevicePolicy = "closed"; DeviceAllow = lib.mkIf usesRtlSdr [ "char-usb_device rw" ]; # AF_UNIX reaches the journal; AF_INET and AF_INET6 are the listening # ports, the feeds, and the lookups that find them. AF_NETLINK is how # the resolver asks the kernel which addresses this host has. RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" "AF_NETLINK" ]; }; }; }; }