# nileshgupta1/nilos architecture

> Architecture diagram of the GitHub repository nileshgupta1/nilos, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nileshgupta1/nilos
- Repository: https://github.com/nileshgupta1/nilos

## Overview

##### Project Type & Purpose
- NilOS is a lightweight operating system that includes a kernel, a minimal libc (often serving kernel-specific functionalities as libk), boot scripts, and ISO creation tools.
- The project is developed to boot via GRUB, load the kernel (which prints a welcome message), and run on x86 hardware (specifically the i386 architecture).

##### Main Components
a. Boot Loader & ISO Creation
- GRUB: Although the GRUB configuration is generated as part of the ISO creation process, include it as an external bootloader component that loads NilOS.
- ISO Image (nilos.iso): The product of the build process, prepared by the iso.sh script.

b. Kernel
- Kernel Source (in kernel/): Contains boot code (kernel/arch/i386/boot.S), early initialization (e.g., linker script, crti.S, crtn.S), and the core kernel logic (kernel/kernel/kernel.c).
- Architecture-specific components in kernel/arch/i386 (such as tty.c for terminal output) that highlight the low-level hardware interactions.

c. Minimal C Library (libc)
- libc Source (in libc/): Provides essential functions for stdio, stdlib, and string operations to support the kernel.
- That this minimal library is meant more as a “libk” for the kernel – possibly emphasizing tailored versions of common functions.

d. Build & Configuration Scripts
- Build Scripts (build.sh, iso.sh, clean.sh, qemu.sh, headers.sh, etc.): These scripts orchestrate the build process, header installation, ISO generation, and virtual machine booting.
- Configuration scripts (config.sh, default-host.sh) and makefiles within the kernel and libc also hint at the build pipeline and toolchain integration.

e. External Dependencies & Environment
- Cross-Compiler & Toolchain: The README describes building a cross-compiler (i686-elf-gcc), required for compiling both the kernel and the libraries.
- External tools such as GRUB, QEMU, and Xorriso that are vital for the boot process and for creating a bootable ISO.

##### Interactions and Data Flow
- Flow from the build output to ISO creation: build scripts generating the nilos.kernel and copying it into a boot directory in the isodir, followed by ISO assembly via grub-mkrescue.

##### Architectural Patterns & Design Principles
- Separation of Concerns: Clearly separate the “kernel” module (core OS logic and hardware interfacing) from the “libc” (providing standard C library functionalities).
- Modularity: The use of shell scripts to encapsulate build, test, and clean-up processes demonstrates a modular and reproducible build pipeline.
- Cross-Compilation: Emphasize that the system is developed to be compiled with an i686-elf cross-compiler, which isolates the build environment from the host system.

##### Technologies & Frameworks
- Programming Languages: Primarily C with some Assembly in the boot and architecture-specific components.
- Build Tools: GNU Make via makefiles located in kernel/ and libc/.
- Shell Scripting: Extensive use of bash scripts for configuration, building, and deployment.
- External Tools: GRUB for boot loading, QEMU for emulation, and other Unix-based build tools.

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% Build & Configuration Scripts
    subgraph "Build & Configuration Scripts"
        bs1["build.sh"]:::build
        bs2["clean.sh"]:::build
        bs3["headers.sh"]:::build
        bs4["config.sh"]:::build
        bs5["default-host.sh"]:::build
        bs6["Makefile (kernel)"]:::build
        bs7["Makefile (libc)"]:::build
    end

    %% Kernel Source Components
    subgraph "Kernel Source"
        kDir["Kernel Directory"]:::kernel
        k2["Boot Code (boot.S)"]:::kernel
        kLink["Linker Script (linker.ld)"]:::kernel
        kCrti["Early Init (crti.S)"]:::kernel
        kCrtn["Early Init (crtn.S)"]:::kernel
        kCore["Core Kernel Logic (kernel.c)"]:::kernel
        kTty["Terminal Output (tty.c)"]:::kernel
    end

    %% Minimal C Library (libk)
    subgraph "Minimal C Library (libk)"
        libDir["libc Directory"]:::libc
        libHeaders["Standard Header Files (stdio.h,stdlib.h,string.h)"]:::libc
        libStdio["Implementation: libc/stdio"]:::libc
        libStdlib["Implementation: libc/stdlib"]:::libc
        libString["Implementation: libc/string"]:::libc
    end

    %% External Toolchain & Tools
    subgraph "External Toolchain & Tools"
        cross["Cross-Compilation Toolchain (i686-elf-gcc)"]:::external
        target["Toolchain Script (target-triplet-to-arch.sh)"]:::external
        qemu["QEMU"]:::external
    end

    %% Build Output
    subgraph "Build Output"
        sysroot["Sysroot (usr/include & usr/lib)"]:::build
        kernelBin["Kernel Binary (nilos.kernel)"]:::kernel
    end

    %% Boot Loader & ISO Creation
    subgraph "Boot Loader & ISO Creation"
        grub["GRUB Boot Loader"]:::boot
        iso["nilos.iso"]:::boot
    end

    %% Build Process Relationships
    target -->|"integrates"| bs1
    cross -->|"supports"| bs1

    %% Kernel Source assembly
    k2 --> kDir
    kLink --> kDir
    kCrti --> kDir
    kCrtn --> kDir
    kCore --> kDir
    kTty --> kDir
    kDir --> kernelBin

    %% Minimal C Library assembly
    libHeaders --> libDir
    libStdio --> libDir
    libStdlib --> libDir
    libString --> libDir
    libDir --> sysroot

    %% Headers installation from build scripts
    bs3 --> sysroot

    %% ISO Creation from build process
    bs1 -->|"runs_iso.sh"| iso

    %% Boot Sequence Flow
    qemu -->|"boots_from_iso"| iso
    iso -->|"provides_files_to"| grub
    grub -->|"loads_kernel"| kernelBin

    %% Styling Classes
    classDef build fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef kernel fill:#F9E79F,stroke:#B9770E,stroke-width:2px;
    classDef libc fill:#A9DFBF,stroke:#1D8348,stroke-width:2px;
    classDef external fill:#E8DAEF,stroke:#8E44AD,stroke-width:2px;
    classDef boot fill:#F5CBA7,stroke:#D35400,stroke-width:2px;

    %% Click Events for Build & Config Scripts
    click bs1 "https://github.com/nileshgupta1/nilos/blob/main/build.sh"
    click bs2 "https://github.com/nileshgupta1/nilos/blob/main/clean.sh"
    click bs3 "https://github.com/nileshgupta1/nilos/blob/main/headers.sh"
    click bs4 "https://github.com/nileshgupta1/nilos/blob/main/config.sh"
    click bs5 "https://github.com/nileshgupta1/nilos/blob/main/default-host.sh"
    click bs6 "https://github.com/nileshgupta1/nilos/tree/main/kernel/Makefile"
    click bs7 "https://github.com/nileshgupta1/nilos/tree/main/libc/Makefile"

    %% Click Events for Kernel Source
    click kDir "https://github.com/nileshgupta1/nilos/tree/main/kernel/"
    click k2 "https://github.com/nileshgupta1/nilos/blob/main/kernel/arch/i386/boot.S"
    click kLink "https://github.com/nileshgupta1/nilos/blob/main/kernel/arch/i386/linker.ld"
    click kCrti "https://github.com/nileshgupta1/nilos/blob/main/kernel/arch/i386/crti.S"
    click kCrtn "https://github.com/nileshgupta1/nilos/blob/main/kernel/arch/i386/crtn.S"
    click kCore "https://github.com/nileshgupta1/nilos/blob/main/kernel/kernel/kernel.c"
    click kTty "https://github.com/nileshgupta1/nilos/blob/main/kernel/arch/i386/tty.c"

    %% Click Events for Minimal C Library
    click libDir "https://github.com/nileshgupta1/nilos/tree/main/libc/"
    click libHeaders "https://github.com/nileshgupta1/nilos/tree/main/libc/include"
    click libStdio "https://github.com/nileshgupta1/nilos/tree/main/libc/stdio"
    click libStdlib "https://github.com/nileshgupta1/nilos/tree/main/libc/stdlib"
    click libString "https://github.com/nileshgupta1/nilos/tree/main/libc/string"

    %% Click Events for External Toolchain & Tools
    click target "https://github.com/nileshgupta1/nilos/blob/main/target-triplet-to-arch.sh"
    click qemu "https://github.com/nileshgupta1/nilos/blob/main/qemu.sh"

    %% Click Event for Boot Loader & ISO Creation
    click iso "https://github.com/nileshgupta1/nilos/blob/main/iso.sh"
```
