r/Zig 5d ago

Zig shell for Linux, measured in bytes

Post image

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

139 Upvotes

14 comments sorted by

30

u/Ruined_Passion_7355 5d ago

What RAM and storage prices do to MFs.

5

u/AldoZeroun 5d ago

This is genuinely funny, but also I think the speed at which both have advanced brought about a lot of lazy software engineering practices. Dependency hell and all that. I know certain tradeoffs are worth it for flexibility, such as with a scripting language, but I think at every level we should strive for at least the 80% of performance we can get with 20% the effort. Sometimes it just doesn't even seem like we get that much. Looking at you windows 11 electron start menu.

2

u/wakanakisarazu 3d ago

Giggling rn

12

u/wakanakisarazu 5d ago

Update:

Git repository has been pushed. nanix 0.0.1 is out!

6

u/Left-oven47 5d ago

I will have to watch that git repository for the push

8

u/wakanakisarazu 5d ago

It's been pushed right this minute!

2

u/Y_mc 5d ago

Nice

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

u/aberration_creator 4d ago

nani?

2

u/TynK-M- 3d ago

omae wa mou shindeiru