# kunijiwa/n1-immortalwrt-build architecture

> Architecture diagram of the GitHub repository kunijiwa/n1-immortalwrt-build, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kunijiwa/n1-immortalwrt-build
- Repository: https://github.com/kunijiwa/n1-immortalwrt-build

## Overview

1. **What this repo is**
   - This is not a full OpenWrt source tree. It is a **customization layer** on top of the **ImmortalWrt ImageBuilder** framework, focused on building a **Phicomm N1 side-router firmware**.
   - The repository’s job is to turn a generic ImmortalWrt base image into a highly stripped, preconfigured N1-specific firmware.

2. **Primary build entrypoint**
   - The main build path is driven by **GitHub Actions** in `.github/workflows/build-N1.yml`.
   - That workflow delegates to `n1/build.sh`, which acts as the build coordinator for this specific device/profile.

3. **Build-time control plane**
   - `n1/build.sh` is the orchestration layer: it likely validates the target firmware, selects the right architecture/profile, and assembles the final ImageBuilder invocation.
   - `shell/check-firmware.sh` is a guardrail script used to confirm the base firmware/imagebuilder environment is compatible before building.
   - `arch/arch.conf.sh` defines architecture precedence/selection, which matters because ImageBuilder builds are target-specific.

4. **Package selection philosophy**
   - `shell/exclude-packages.sh` is the core package policy file.
   - The README describes a **“positive declaration + precise exclusion”** approach: keep only the packages needed for the N1 side-router use case and explicitly remove unneeded components.
   - This is the main mechanism behind the repo’s “minimal, clean, no redundancy” firmware positioning.

5. **Runtime features baked into the image**
   - The firmware is preloaded with several user-facing components:
     - **OpenClash**
     - **Aria2**
     - **FileBrowser**
     - **Argon theme**
     - **Amlogic/N1 box tooling**
   - These are not separate runtime services from the repo’s perspective; they are embedded into the firmware image during build.

6. **Filesystem overlay and configuration injection**
   - The `files/` directory is an overlay that gets copied into the final root filesystem.
   - `files/etc/config/aria2` contains the default Aria2 configuration.
   - `files/etc/aria2/clean.sh` and `files/etc/aria2/delete.sh` are Aria2-side helper scripts, likely used for download cleanup and deletion behavior.
   - `files/etc/uci-defaults/99-custom.sh` is especially important: it runs on **first boot** to apply device-specific initialization.

7. **First-boot initialization flow**
   - The image is designed to self-configure on first startup for side-router deployment.
   - The first-boot script is responsible for:
     - obtaining an IP address via **DHCP**
     - splitting `br-lan`
     - disabling the DHCP service
   - This makes the N1 behave like a **bypass/side router** rather than a conventional LAN gateway.

8. **Network and access model**
   - The README states the default network model is **DHCP client mode**, with DHCP server disabled.
   - Firewall defaults are adjusted to allow inbound traffic on the WAN side, matching a side-router topology.
   - The default admin password is `password`, indicating the build aims for immediate usability after flashing.

9. **OpenClash and rule-data boundary**
   - OpenClash is included as a packaged capability, but its main engine/kernel is **not prebundled** according to the README.
   - The intended flow is: flash firmware → first boot → enter LuCI/OpenClash → download the required Meta core and update GEO data.
   - This keeps the build smaller and shifts large, fast-changing artifacts to post-install setup.

10. **What is intentionally removed**
   - The image removes a wide range of features the N1 doesn’t need in this role:
     - physical device drivers
     - PPPoE dial-up
     - IPv6 client components
     - filesystem utilities
   - This is a deliberate size/performance tradeoff aligned with the “bypass router” use case.

11. **Repository boundaries**
   - The repo contains **build logic, package policy, and filesystem overlays**, but not the full firmware source.
   - External dependencies are the **ImmortalWrt ImageBuilder** framework and the package feeds/plugins it references.
   - GitHub Actions is the only visible automation/infrastructure layer in this repo.

12. **How to diagram it**
   - A clear architecture diagram should show:
     - **GitHub Actions** triggering the build
     - **build.sh** orchestrating build steps
     - **arch/config + firmware check + package exclusion** shaping the image
     - **files/** overlay injecting runtime defaults and scripts
     - the resulting **N1 firmware image**
     - **first boot uci-defaults** applying network/DHCP changes
     - the final runtime services: **LuCI/OpenClash/Aria2/FileBrowser** on the device

Main entry points: [`n1/build.sh`](https://github.com/kunijiwa/n1-immortalwrt-build/blob/main/n1/build.sh) (Build Script), [`files`](https://github.com/kunijiwa/n1-immortalwrt-build/tree/main/files) (Files Overlay), [`shell/exclude-packages.sh`](https://github.com/kunijiwa/n1-immortalwrt-build/blob/main/shell/exclude-packages.sh) (Package Policy), [`files/etc/uci-defaults/99-custom.sh`](https://github.com/kunijiwa/n1-immortalwrt-build/blob/main/files/etc/uci-defaults/99-custom.sh) (First Boot Init)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_automation["Automation"]
  node_gh_actions["GitHub Actions<br/>ci workflow<br/>[build-N1.yml]"]
end

subgraph group_build["Build Control"]
  node_build_sh["Build Script<br/>orchestrator<br/>[build.sh]"]
  node_arch_conf["Arch Config<br/>target selection<br/>[arch.conf.sh]"]
  node_check_fw["Firmware Check<br/>guard script<br/>[check-firmware.sh]"]
  node_exclude_pkgs["Package Policy<br/>package filter"]
  node_imagebuilder["ImageBuilder<br/>external framework"]
end

subgraph group_image["Image Overlay"]
  node_files_overlay[("Files Overlay<br/>rootfs overlay")]
  node_aria2_cfg["Aria2 Config<br/>default config"]
  node_aria2_scripts["Aria2 Hooks<br/>helper scripts"]
  node_uci_defaults["First Boot Init<br/>uci-defaults script<br/>[99-custom.sh]"]
  node_network_boot(("Side-router Setup<br/>network init"))
  node_firmware_img{{"N1 Firmware<br/>custom image"}}
  node_runtime_apps["Baked-in Apps<br/>bundled services"]
  node_openclash_core["OpenClash Core<br/>post-install dependency"]
  node_n1_device["Phicomm N1<br/>target device"]
end

node_gh_actions -->|"runs"| node_build_sh
node_build_sh -->|"invokes"| node_imagebuilder
node_build_sh -->|"selects target"| node_arch_conf
node_build_sh -->|"validates"| node_check_fw
node_build_sh -->|"filters packages"| node_exclude_pkgs
node_exclude_pkgs -->|"feeds policy"| node_imagebuilder
node_files_overlay -->|"overlays"| node_imagebuilder
node_files_overlay -->|"contains"| node_aria2_cfg
node_files_overlay -->|"contains"| node_aria2_scripts
node_files_overlay -->|"contains"| node_uci_defaults
node_imagebuilder -->|"produces"| node_firmware_img
node_uci_defaults -->|"configures"| node_network_boot
node_network_boot -->|"applies on"| node_n1_device
node_firmware_img -->|"includes"| node_runtime_apps
node_runtime_apps -->|"runs on"| node_n1_device
node_runtime_apps -.->|"defers core"| node_openclash_core

click node_gh_actions "https://github.com/kunijiwa/n1-immortalwrt-build/blob/main/.github/workflows/build-N1.yml"
click node_build_sh "https://github.com/kunijiwa/n1-immortalwrt-build/blob/main/n1/build.sh"
click node_arch_conf "https://github.com/kunijiwa/n1-immortalwrt-build/blob/main/arch/arch.conf.sh"
click node_check_fw "https://github.com/kunijiwa/n1-immortalwrt-build/blob/main/shell/check-firmware.sh"
click node_exclude_pkgs "https://github.com/kunijiwa/n1-immortalwrt-build/blob/main/shell/exclude-packages.sh"
click node_files_overlay "https://github.com/kunijiwa/n1-immortalwrt-build/tree/main/files"
click node_aria2_cfg "https://github.com/kunijiwa/n1-immortalwrt-build/tree/main/files/etc/config/aria2"
click node_aria2_scripts "https://github.com/kunijiwa/n1-immortalwrt-build/tree/main/files/etc/aria2"
click node_uci_defaults "https://github.com/kunijiwa/n1-immortalwrt-build/blob/main/files/etc/uci-defaults/99-custom.sh"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_gh_actions toneBlue
class node_build_sh,node_arch_conf,node_check_fw,node_exclude_pkgs,node_imagebuilder toneAmber
class node_files_overlay,node_aria2_cfg,node_aria2_scripts,node_uci_defaults,node_network_boot,node_firmware_img,node_runtime_apps,node_openclash_core,node_n1_device toneMint
```

## Components

### Automation

CI entrypoint and build trigger

- [GitHub Actions](https://github.com/kunijiwa/n1-immortalwrt-build/blob/main/.github/workflows/build-N1.yml): ci workflow · Triggers the N1 build pipeline · `.github/workflows/build-N1.yml`

### Build Control

Scripts that select target, validate environment, and shape the image

- [Build Script](https://github.com/kunijiwa/n1-immortalwrt-build/blob/main/n1/build.sh): orchestrator · Coordinates the N1 ImageBuilder build · `n1/build.sh`
- [Arch Config](https://github.com/kunijiwa/n1-immortalwrt-build/blob/main/arch/arch.conf.sh): target selection · Defines architecture precedence and profile selection · `arch/arch.conf.sh`
- [Firmware Check](https://github.com/kunijiwa/n1-immortalwrt-build/blob/main/shell/check-firmware.sh): guard script · Verifies the base ImageBuilder/firmware environment · `shell/check-firmware.sh`
- [Package Policy](https://github.com/kunijiwa/n1-immortalwrt-build/blob/main/shell/exclude-packages.sh): package filter · Explicitly removes unneeded packages for the N1 image · `shell/exclude-packages.sh`
- **ImageBuilder**: external framework · ImmortalWrt ImageBuilder used to assemble the firmware

### Image Overlay

Filesystem additions and first-boot customization inside the firmware

- [Files Overlay](https://github.com/kunijiwa/n1-immortalwrt-build/tree/main/files): rootfs overlay · Content copied into the final root filesystem · `files`
- [Aria2 Config](https://github.com/kunijiwa/n1-immortalwrt-build/tree/main/files/etc/config/aria2): default config · Preseeds Aria2 runtime settings · `files/etc/config/aria2`
- [Aria2 Hooks](https://github.com/kunijiwa/n1-immortalwrt-build/tree/main/files/etc/aria2): helper scripts · Cleanup and delete helpers for Aria2 · `files/etc/aria2`
- [First Boot Init](https://github.com/kunijiwa/n1-immortalwrt-build/blob/main/files/etc/uci-defaults/99-custom.sh): uci-defaults script · Applies device-specific configuration on first boot · `files/etc/uci-defaults/99-custom.sh`
- **Side-router Setup**: network init · Switches to DHCP client mode, splits br-lan, disables DHCP server
- **N1 Firmware**: custom image · Stripped, preconfigured ImmortalWrt image for the Phicomm N1
- **Baked-in Apps**: bundled services · LuCI, OpenClash, Aria2, FileBrowser, Argon theme, and N1 tooling
- **OpenClash Core**: post-install dependency · Downloaded after flashing rather than prebundled
- **Phicomm N1**: target device · Side-router hardware running the customized firmware

## Connections

- GitHub Actions → Build Script: runs · Workflow delegates to the device-specific build script
- Build Script → ImageBuilder: invokes · Build script assembles the final ImageBuilder command
- Build Script → Arch Config: selects target · Chooses architecture/profile settings for the N1
- Build Script → Firmware Check: validates · Ensures the base build environment is compatible
- Build Script → Package Policy: filters packages · Applies the repo's minimal package policy
- Package Policy → ImageBuilder: feeds policy · Package exclusions shape the generated image
- Files Overlay → ImageBuilder: overlays · Filesystem content is copied into the rootfs during image creation
- Files Overlay → Aria2 Config: contains · Overlay provides default Aria2 configuration
- Files Overlay → Aria2 Hooks: contains · Overlay provides Aria2 cleanup and delete hooks
- Files Overlay → First Boot Init: contains · Overlay provides first-boot initialization logic
- ImageBuilder → N1 Firmware: produces · ImageBuilder outputs the customized N1 firmware image
- First Boot Init → Side-router Setup: configures · First-boot script changes WAN/LAN behavior for side-router use
- Side-router Setup → Phicomm N1: applies on · Device boots into DHCP-client bypass-router mode
- N1 Firmware → Baked-in Apps: includes · The firmware ships with preloaded user-facing components
- Baked-in Apps → Phicomm N1: runs on · Installed apps are available after flashing and boot
- Baked-in Apps → OpenClash Core: defers core · Main OpenClash core is downloaded after first boot
