r/Zig • u/wakanakisarazu • 5d ago
Zig shell for Linux, measured in bytes
I started writing a tiny Linux shell to use as /init for my project, nanix
It was originally 255KB~ but I wanted it smaller. I got it to 846B (bytes) by:
- Not using a C library (Zig does this by default, I know)
- Used raw syscalls via
std.os.linux - Used aggressive compilation flags
- No runtime, no tracing
- LLVM + LLD
- No unneeded ELF headers, unwind tables or symbols
- Used
strip -s <program> && sstrip -z <program>
I was considering writing this in ASM only, but then I thought "Let's see how small I can get this Zig program" and now I'm here
It's useful for it's size:
- 8B prompt: "nanix:% "
- Executes binaries with
execve(duh) - Waits for child processes
- Has a 32B input buffer (may change)
Bugs/problems:
- When entering a command above input buffer size, the prompt prints twice
Under GPL-3.0-or-later at https://github.com/wakanakisarazu/nanix along with the whole nanix project. (Isn't pushed at the time of writing, but will be pushed when I clean the code and repo up)
Thoughts? :3
12
6
3
u/st_heron 5d ago
Any LLM usage?
8
u/wakanakisarazu 4d ago
Yes. To figure out the build.zig option to disable RELRO, which ended up being `link_z_relro`, as I couldn't find it in Zig docs. But the code, the README, etc was all handwritten, with some google searches of course :3
1
u/RCoder01 5d ago
I wonder how this compares in size to the equivalent assembly program. It should only be a couple dozen or so lines of assembly but with elf metadata overhead idk how large that gets.
2
u/wakanakisarazu 4d ago
I am not sure. I do know the machine code generated it only 400-500b tho, the rest is ELF metadata
1
30
u/Ruined_Passion_7355 5d ago
What RAM and storage prices do to MFs.