8 ms·
Fusion – A hobby OS implemented in Nim
- michaelsbradley 2y agoDevelopment journal of Fusion’s author: https://0xc0ffee.netlify.app/osdev/01-intro.html https://0xc0ffee.netlify.app/osdev/01-intro.html
- khaledh 2y agoAuthor here. Thanks for posting this <3 Happy to answer questions.
- lagniappe 2y agoWhere are the screenshots
- alberth 2y agoCongrats, fascinating work! Q: has the GC of Nim caused any challenges? (And if not, would you attribute that to Nim unique GC that does NOT “stop-the-world”?) https://nim-lang.org/1.4.0/gc.html https://nim-lang.org/1.4.0/gc.html
- Tiberium 2y ago1.4.0 is a very outdated docs version (Nim is at 2.0 currently, and 2.0 release brought ORC as default with ARC as an option), you should refer to the updated https://nim-lang.org/docs/mm.html https://nim-lang.org/docs/mm.html and also https://nim-lang.org/docs/destructors.html https://nim-lang.org/docs/destructors.html
- pests 2y agoNim should take inspiration out of other languages doc sites where a banner is placed at the top notifying the reader it is for a previous version with a link to the current version. Could also add a rel=canonical meta tag pointing to the latest version so search engines funnel people there. "Nim gc" on Google indeed puts you at the 1.4 doc page.
- alberth 2y agoSlight OT: are is the best documentation management system these day? ReadMe, Docusaurus, Mintlify, etc?
- khaledh 2y agoThanks! As @Tiberium mentioned, Nim 2.0 defaults to using ARC (Automatic Reference Counting), so no runtime GC. The Nim compiler is quite smart about when to copy/move and when to destruct, with some hints like lent/sink for when you want to have a bit more control. Keep in mind that I also need to use ptr (as opposed to ref) types in a lot of cases when working at a low level, so there might be a need for some manual memory management as well.
- suby 2y agoI don't use Nim, but it's an interesting language. I've read someone else complaining about having to make changes to an old project every time he went to recompile it. I'm wondering how true this is, so in other words, I'm wondering about the frequency and severity of breaking changes in the language.
- khaledh 2y agoI haven't faced such issue. I think the only issue I faced was when I upgraded to 2.0, they made `--threads:on` the default, so I had to turn it off, but that's about it.
- deleted 2y ago[deleted]
- efilife 2y agowasn't this about C/C++?
- PMunch 2y agoDoesn't happen a lot, but it does happen from time to time. Of course only if you actually update your Nim version, so it's not like interpreted languages like Python where stuff stops working if a new version comes out and your package manager upgrades it.
- edu_do_cerrado 2y agoAmazing work! What where the most troublesome parts of the project? Also, any tips if anyone want to write an OS from scratch, aswell?
- deaddodo 2y agoRead. A lot. Basically, OSDev is one of the few realms you can't really take shortcuts in. It's kind of like learning Rust, in that you'll have a lot of foundational work until you get some real payoff. Unlike rust, however, there isn't just some cliff where you start getting it; it's a constant uphill trudge. Learning the boot process of your target architecture, adding core functionality (process scheduling, filesystem/VFS support, IO management, etc), adding driver support, supporting your video device (just getting a basic framebuffer, and then adding each piece after that), supporting USB, supporting CRT and POSIX (if you choose to do so), etc are all herculean tasks of their own. That being said, it's a super incremental process, so you'll get to watch it grow and expand at each step. Reading up on the FreeBSD and Linux kernels are good starts. As well as reviewing other hobby OSes such as Serenity, TouruOS, Haiku, etc. And the OSDev wiki is invaluable. Also, accepting you probably aren't going to build the next big OS or trying to compete with the big dogs is something you'll have to humble yourself with.
- khaledh 2y ago> What where the most troublesome parts of the project? Task switching. It's a very intricate process, you really have to understand how to structure everything (especially the task stack) so that you can switch everything from underneath the CPU as if nothing has happened (the CPU is obliviuous to which task is running, including the kernel itself). Add to that switching between user mode and kernel mode (during interrupts and system calls) and it becomes even more challenging. > Also, any tips if anyone want to write an OS from scratch, aswell? As @deaddod said, you need to read a lot. Two invaluable resources for me were the Intel Software Development Manuals (SDM), and the osdev wiki. The SDM can be daunting, but it's surprisingly very readable. The osdev wiki has great content, but it can be a hit or miss. I complement them with various blog posts and code on github to really understand a certain topic. That being said, the most important aspect of this process is to have tons of curiousity and to be passionate about low-level systems programming. I love to learn how things really work at the lowest level. Once you learn it, you'll discover that there's no magic, and that you can write something yourself to make it work. [0] https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html https://www.intel.com/content/www/us/en/developer/articles/t... [1] https://wiki.osdev.org https://wiki.osdev.org
- sidkshatriya 2y agoI heard that null is a valid value for objects in Nim for most situations. Is that correct ? I like languages that disallow null by default (e.g. Rust, OCaml etc) because it seems to be a huge source of errors.
- netbioserror 2y agoNo, it isn't. The type must explicitly be a nullable type or pointer to be nullable. All other values must have a valid initialization. Anything not marked as a pointer or ref is by default managed by its scope. This includes dynamic types like seqs and strings, which are pointers to heap memory but managed by the stack scope and deallocated upon leaving scope.
- jasfi 2y agoI use the Options module which has a none/some check. None is the absence of a value. You can test for this quite easily and I see it as a feature, not a bug.
- khaledh 2y agoTo be honest, I haven't found this to be an issue (yet). I try to keep most of my types value types (cannot be null), which the compiler can pass by reference under the hood if it detects that it's too big, without compromising memory safety.
- deleted 2y ago[deleted]
- alberth 2y agoQ: what made you choose Nim over Swift? (since they seem very similar at this point, yet Swift is more battle tested)
- khaledh 2y agoI did consider Swift for a brief moment, but was put off by the fact that targeting bare-metal was almost non-existent. It was more work than I would like to put into bootstrapping the dev environment.
- kosolam 2y agoWhat made you use nim in the first place vs any other lang?
- khaledh 2y agoI went over this in other comments, but basically the language appeals to me since: - it's close to Python in syntax (less noise, more readable) - has no garbage collector by default (it uses ARC) - has great C interop - can be optimized through the C backend compiler - can target bare-metal with minimal effort - supports inline assembly - has great template/macro system (I do use templates, but I haven't had the need for macros yet) There's probably other reasons, but those are the ones I could think of now. As for why not other languages, I think the only other languages suitable for this kind of work are: C, C++, Rust, and Zig. Here's my take on each: - C: The mother of all system languages, but outdated with lots of UB gotchas - C++: I don't like/need OOP, so why pay the prices of C++ complexity and manual memory management - Rust: I find the language too complicated for my taste. I know it's subjective, but it just doesn't feel right for me. Also writing a kernel involves a lot of unsafe code anyway. - Zig: I tried Zig and also found its syntax to be a bit too noisy. Also having to worry about allocators in most of the code distracts from the core logic I'm trying to focus on.
- alberth 2y agoThanks for the details. Any reasons why you would not recommend Nim? Or things Nim could improve? (Nim seems like this unicorn of languages, that's completely overlooked. And I don't understand why it's overlooked)
- brcmthrowaway 2y ago[flagged]
- deleted 2y ago[deleted]
- coiailo 2y agoWhat is Nim, and what is the overarching design goal for Fusion? Thanks. I'm hoping these questions aren't too basic, I have no context whatsoever for understanding this so hope someone can explain.
- nick__m 2y agoNim is an awesome language that feels inspired by Ada and Python!
- cap11235 2y agoI think it is better described as C with metaprogramming frendliness
- deleted 2y ago[deleted]
- netbioserror 2y agoBut it's secretly also a quirky Lisp, to quote Andreas himself.
- girvo 2y agoIt's macro system certainly makes it feel that way!
- netbioserror 2y agoI'd say it's moreso the referentially-transparent expression-based programming by default. You kind of have to opt-in to imperative programming, and it actively disincentivizes OOP.
- doctorhandshake 2y agoFrom the documentation of the project: https://0xc0ffee.netlify.app/osdev/01-intro.html https://0xc0ffee.netlify.app/osdev/01-intro.html
- ghotert 2y agoHow does this compare with TempleOS? Sounds quite similar what with the single address space.
- commercialnix 2y ago[flagged]
- latentsea 2y agoTempleOS was written by the greatest programmer who ever lived.
- deepsun 2y agoSo? Idols are bad. I'd better discuss the deeds.
- latentsea 2y agoFor anyone who didn't get the reference it's a claim the author of TempleOS used to make of himself. https://youtu.be/o48KzPa42_o?si=s8_t4ReysLuhU_Ub https://youtu.be/o48KzPa42_o?si=s8_t4ReysLuhU_Ub
- jraph 2y agoIt's fun how a comment that felt hostile suddenly feels very friendly with such a piece of information.
- tjpnz 2y agoTempleOS is not real mode.
- elcritch 2y agoNifty! Fun to pull up the module for ELF and have it be so easy to read. Some day I want to write an RTOS in Nim. I enjoy writing embedded programs in Nim and it’d be fun to make an RTOS.
- khaledh 2y agoThat would be great! I'd love to follow along if you ever decide to build an RTOS.
- cb321 2y agoTo either of you, whenever you are doing something new from scratch, it can be useful to consider the granularity of provided abstractions & services which khaledh seems to be doing. I see fusion only has like 8 syscalls presently. It's not in Nim, but along these lines ("how much" individual calls do), you might want to consider an approach like this: https://github.com/c-blake/batch https://github.com/c-blake/batch to amortize costs of crossing expensive call boundaries.
- khaledh 2y agoBatching syscalls is on my mind. The architecture of Fusion will revolve around channel-based ipc (both sync and async), including between user mode and kernel mode. The end state I'm aiming for is an async channel for syscalls, where the user task issues syscalls, which get buffered in a queue, where the kernel processes the queue asynchronously. For this to work properly, user tasks need to be able to respond to completed syscalls async as well. That's why my idea of user tasks is that they should be modeled as state machines, with channel-based events as the core mechanism by which code gets executed in a deterministic manner. The equivalent of signals in Unix (which many find one of the bad aspects of Unix design) would be receiving events on one or more channels for various purposes (e.g. IO completion, timers, abort, interrupt, GUI events, etc.).
- cb321 2y agoSounds interesting - kind of like microkernels meet io_uring (in Elevator-pitch-ese).
- ryukoposting 2y agoNice, I love to see stuff like this. I've been an on-again, off-again Nim "ecosystem guy" for several years. It's great to see this delightful little project is still chugging along.
- mikenew 2y agoYour blog/docs are excellent. Perfect balance of showing and telling. Thanks so much for taking the time to share what you're doing like this.
- khaledh 2y agoThanks! Actually writing has helped me so many times in improving the design and implementation. It forces me to question my assumptions, and ask myself: would the reader be able to understand why I made such decision? I have to justify everything I do, which helped me remove unnecessary complexity and focus on the more important aspects.
- sendfoods 2y agoMay I ask what kind of blogging engine/site generator you used for the docs?
- LisaST 2y ago[dead]
- v3ss0n 2y agoThe problem that nim have and many afraid to go nim is case and style insensitivity. Is_land == island == IsLaND == is-land It is bad in team setting, in real world projects. How it goes now ? Last time I checked the main dev refuse to do anything about against popularity vote In Github. Otherwise awesome project and documentation Fusion Os
- googh 2y agoI think this problem can be solved using either a linter or formatter-like tool that makes naming consistent before the code gets committed.
- v3ss0n 2y agoDo we have proper linter for that now?
- archargelod 2y agoNim compiler supports a style enforcing flag `--styleCheck`, that can display hints or error on compilation.
- nurettin 2y agoI've seen Delphi ERP projects worked on by dozens of people and case insensitivty of Pascal was never ever the issue. You choose something and stick to it. The concerns and fears are mostly due to inexperience.
- Varriount 2y ago
- jasfi 2y agoNim is a great systems language, and should be more popular.
- Daunk 2y agoMaybe it's just because I'm getting older, but I struggle to read Nim when I'm forced to use two spaces for indentation. I can barely make out each block of code, and I don't like to rely on my IDE to make a language readable.
- jasfi 2y agoI was also a bit taken aback by the 2 space indentation. But after a bit of practice I got used to it.
- Daunk 2y agoMaybe, it's just so hard to read for me. Also, my IDE isn't all fancy, so tab is tab, I'd have to space it all myself...
- netbioserror 2y agoI use it at work for an important CLI tool that backs a number of our systems. My style is almost exactly like Python. Four-space tabs, snake_case, my own rules for indenting parameters and keywords. You do not need to use the official Nim style, you can absolutely write it like Python, Ada, however you like.
- Daunk 2y agoThe compiler seems to enforce spaces? But I did see some code that replaces tabs with spaces, but it feels dirty having that in every single source file.
- netbioserror 2y agoMaybe I was unclear, I guess I should say my indents are 4 spaces. Never tried it with actual tabs so I can't say.
- hugs 2y agoSeeing more projects in Nim makes me happy. I'm a (mostly) Python and JavaScript programmer who is interested in the benefits of also knowing a modern, fast, statically-typed language. Among a candidate list of Go, Rust, Zig, or Nim, I like Nim the most. It feels the most "Pythonic" in the sense of very little syntax clutter when I'm reading code. I also love, love, love using a REPL to prototype new code, and INim does it well. The biggest problem with Nim currently is its small community size, which makes the universe of available and maintained software libraries smaller than in the other language communities. It's a chicken-or-egg problem, but can be solved by more devs (including me!) being "the change you want to see in the world".
- khaledh 2y agoTo be honest, I haven't found the community size to be an issue. The Nim forum[0] has a vibrant community, and is the place I go to for help, and the response is usually quick and on point. The language is also evolving in a careful manner, with Araq at the helm I think it's going to be even better in the long term. As for the ecosystem, yes, it's not as big as Python or Rust, but surprisingly the standard library has most of what people need. I rarely look for 3rd party packages to do something. That being said, I acknowledge that Nim is on the lesser known languages of the spectrum, but that doesn't take away from its merits as a very promising language that does what it's supposed to do very well. One thing I think the community should focus on more is IDE support. The VSCode extension is good, but has some rough edges. I also prefer JetBrains IDEs, and the official Nim plugin is very lacking to say the least. I have another side project to create a JetBrains plugin for Nim[1], but I haven't gone far with it yet. [0] https://forum.nim-lang.org https://forum.nim-lang.org [1] https://github.com/khaledh/nimjet https://github.com/khaledh/nimjet
- hugs 2y agoMy world: I need to use OpenCV. The existing OpenCV bindings (nim-opencv) haven't been touched in years because the author left* the Nim community. (And that really stinks! It was created by dom96, who also created Nimble, Jester, and a ton of other useful stuff in the Nim world.) ... So... I created my own OpenCV bindings and published it (https://nimble.directory/pkg/mvb https://nimble.directory/pkg/mvb). But they're minimal because I'm just one dude and haven't had the time to complete the bindings (either manually, or ideally, using an automated binding generator tool). I will.. eventually.. I hope! Meanwhile, OpenCV bindings for Rust and Go are robust and well-maintained. Now I'm playing around more with Nostr (and the Lightning Network)... Nostr libraries for Nim are not as complete or well-maintained as those in Rust, Go, or even Python, etc. I'm not letting that stop me from using Nim for my projects... I love Nim! But it does mean I have more work to do (and code to maintain). I can make that choice because I'm my own boss and run my own company. But I could see others not making the same choice for rational reasons. * And dom96 left, unfortunately, because of harassment and abuse, which is another possible reason why Nim isn't as well adopted as Go, Rust, etc. If people want to see Nim succeed more, they also need to focus on improving community safety, too. https://news.ycombinator.com/item?id=38999296 https://news.ycombinator.com/item?id=38999296