# Cardputer ADV–only constants and app source policy (replaces build_firmware.py staging/config). { lib, squirrel, port ? "cardputer_adv", }: let app = "${squirrel}/Squirrel!"; inherit (lib) attrNames filter hasAttr concatMap concatStringsSep mapAttrsToList; portToml = builtins.fromTOML (builtins.readFile "${app}/ports/${port}/port.toml"); featuresToml = builtins.fromTOML (builtins.readFile "${app}/features.toml"); excludeDirs = [ "tools" "archive" "firmware_patches" "fonts" "device" "tests" "test" "dist" "firmware" "docs" "vendor" "__pycache__" ".build" ]; excludeRootFiles = [ "build_firmware.py" "verify_device.py" "main.py" "main_flash.py" "setup.py" "conftest.py" "vlw.py" "make_vlw.py" "check_vlw.py" "hw/ports.py" ]; constName = parts: lib.replaceStrings [ "." "-" ] [ "_" "_" ] (lib.toUpper (lib.concatStringsSep "_" parts)); isPresent = table: table != null && (table.installed or true) != false && (table.enabled or true) != false; # Hardware paths like "input.keyboard", "radio.ble" hardware = let walk = attrs: prefix: concatMap (name: let v = attrs.${name}; sub = if prefix == "" then name else "${prefix}.${name}"; in if builtins.isAttrs v then (if isPresent v then [ sub ] else [ ]) ++ walk v sub else if v == true && prefix != "" then [ sub ] else [ ] ) (filter (n: n != "features") (attrNames attrs)); in lib.unique (walk (portToml // { }) ""); hasHardware = req: lib.any (h: lib.hasPrefix req h || h == req) hardware; featureNames = attrNames featuresToml; featureState = mapAttrsToList (name: feat: let requires = feat.requires or [ ]; missing = filter (r: !hasHardware r) requires; want = (portToml.features or { }).${name} or null; in { inherit name; enabled = want != false && missing == [ ]; inherit missing; } ) featuresToml; enabledFeatures = map (x: x.name) (filter (x: x.enabled) featureState); disabledModules = lib.unique ( concatMap (name: if elem name enabledFeatures then [ ] else (featuresToml.${name}.modules or [ ]) ) featureNames ); otherPorts = filter (p: p != port) ( attrNames (builtins.readDir "${app}/ports") ); rsyncExcludes = excludeDirs ++ map (f: f) excludeRootFiles ++ map (p: "ports/${p}") otherPorts ++ disabledModules ++ [ "port_config.py" ]; # --- port_config.py (device-side constants) --- hiddenFor = kind: lib.unique (concatMap (name: if elem name enabledFeatures then [ ] else (featuresToml.${name}.${kind} or [ ]) ) featureNames); frozenset = xs: "frozenset((" + concatStringsSep ", " (map (x: builtins.toJSON x) (lib.sort builtins.lessThan xs)) + "))"; flattenToml = let go = attrs: prefix: concatMap (key: let v = attrs.${key}; path = prefix ++ [ key ]; in if builtins.isAttrs v then go v path else [ { path = path; value = v; } ] ) (filter (n: n != "features") (attrNames attrs)); in go portToml [ ]; portConfigText = let header = '' # port_config.py - the port this firmware is built for: ${portToml.port.title or port} # # GENERATED by tools/squirrel/cardputer-adv.nix - do not edit PORT = ${builtins.toJSON port} BOARD_MODULE = ${builtins.toJSON "ports.${port}.board"} HARDWARE = ${frozenset hardware} FEATURES = ${frozenset enabledFeatures} HIDDEN_MENUS = ${frozenset (hiddenFor "menus")} HIDDEN_SCREENS = ${frozenset (hiddenFor "screens")} HIDDEN_ACTIONS = ${frozenset (hiddenFor "actions")} HIDDEN_GROUPS = ${frozenset (hiddenFor "settings")} HIDDEN_KEYS = ${frozenset (hiddenFor "keys")} ''; body = let bySection = lib.foldl' (acc: item: let sec = builtins.head item.path; in acc // { ${sec} = (acc.${sec} or [ ]) ++ [ item ]; } ) { } (flattenToml); sections = lib.sort builtins.lessThan (attrNames bySection); in concatStringsSep "\n" (map (sec: "\n" + concatStringsSep "\n" (map (item: let c = constName item.path; in "${c} = ${builtins.toJSON item.value}" ) (bySection.${sec})) ) sections); in header + body + "\n"; elem = x: xs: builtins.any (y: y == x) xs; # Firmware tree boardBase = portToml.port.board; boardSquirrel = boardBase + "_Squirrel"; boardType = "cardputeradv"; manifestMarker = "# --- Squirrel! (generated by build_firmware.py - do not edit below) ---"; in { inherit app port boardBase boardSquirrel boardType manifestMarker rsyncExcludes portConfigText ; firmwarePatches = map (n: "${app}/firmware_patches/${n}") ( filter (n: lib.hasSuffix ".patch" n) (attrNames (builtins.readDir "${app}/firmware_patches")) ); fontCandidates = [ "${app}/fonts/squirrel.vlw" "${app}/squirrel.vlw" ]; }