Can Swift be used to build a OS?


My question relates to a desire I have that within a handful years, I want to write an OS and other system level software in Swift. While Im currently pursuing writing high level programs, I will want to make the transition so I need to be strategic in how I approach it with respect to Swifts evolution. Currently I spend some time learning about OS and how they work.

One common thing with C and Rust which are low level is that they dont have run times but Swift does.
Swift has automatic memory management while low level languages do not which makes it impossible to build memory management systems contextual to the type of system you are aiming to build.
Currently, Swift does not support inline assembly.
Swift does not have access to IO ports.
Swift is still slow to perform well in the OS world.

Idk if it even worth trying to spend the energy trying to forcefully create an OS in pure swift and assembly when its not suitable. Just dont know how. Perhaps I have no choice but to learn C as well but whats the point when languages like Rust provide a safer and better approach while being just as fast. What are your thoughts and advice to me? Will Swift ever be suitable for such a task in the coming years?


I recently stumbled upon this: Swift Bare Bones - OSDev Wiki
So it looks like a few people have thought about it and started. It uses minimal Assembly and C to get into Swift-land.


It's possible, albeit probably not ideal at this point. You can write a kernel in any language, if you're willing to provide the runtime support for it. People have written kernels in Javascript after all. It's not true that C and Rust don't have a runtime—every C compiler requires a library of basic functions to provide C language features that has to be linked into every C binary, such as libgcc or compiler-rt. Languages vary in the size of their runtime, and that impacts how much of a baseline runtime library you need to include in your kernel. In the same way that C doesn't provide any direct support for hardware interfaces and relies on dropping down to assembly routines to provide those primitives, you could do the same from Swift or any other language. If the language doesn't provide inline assembly support, you can write the routines you need in assembly and link them in separately. Kernels also don't necessarily rely on raw performance but predictable performance to meet the real-time guarantees needed for hardware interfaces. Swift is not great here yet, but there is a subset of Swift you can use that has C-like semantics and performance for these parts; however, the compiler currently will not help you stick to that subset.


Have a look at GitHub - spevans/swift-project1: A minimal bare metal kernel in Swift


Loving this thread. :slight_smile:

1 Like

It's also worth noting that, for example, the Linux kernel is not really written in "C": it's written in a mix of GNU C, which is ANSI C with a ton of extensions, together with assembly code.

There's really no reason that most of the code in a kernel, including drivers, file systems and the network stack, cannot be written in a higher level language than C, even if you have to glue together bits of C and assembly to implement interrupt handlers and low level memory management.


Yes, you can build the OS. I would like to suggest you go through the swift with iOS development you can make it easy!!

If anyone wants to help and work on this project, please message me (even if you want to provide mentorship because currently, Im just self learning which can take longer). I dont expect us working on it like a full time job, just here and there work on its components. Be friends for a long time. Make something unimaginably amazing.


yes, you can build the iOS with Swift because is a successor to both the C and Objective-C languages. It includes low-level primitives such as types, flow control, and operators. It also provides object-oriented features such as classes, protocols, and generics, giving Cocoa and Cocoa Touch developers the performance and power they demand.