ityonemo

ityonemo

Currently just starting out on a new mini-project - getting zig NIFs to run in elixir.

https://github.com/ityonemo/zigler

The idea here is to make the zig NIFs be “embedded” in the elixir code, much like an “asm” call in C. It also makes the bridge between the zig function call and the elixir “basically transparent”. Several adapters are provided: i64, f64, string (as a c string or a zig slice), i64 arrays, f64 arrays. Of course it’s not terribly hard to do the unmarshalling of the erlang terms yourself inside the NIF.

Haven’t started documenting yet, but if you want to see how it works, the test/ directory has quite a few instructive examples which are all passing as of zig 0.4.0

Comments and criticism appreciated!

Showing Posts 1 to 10

OvermindDL1

OvermindDL1

Lol, that’s cool. ^.^

ityonemo

ityonemo OP

Thanks! That means a lot to me. I think zig is a great fit for elixir. Incidentally, do you know of any good references on refrences? I’m a little bit blocked on my understanding of them.

Edit:. Oops, resources, not references

webuhu

webuhu

Very cool project…

OvermindDL1

OvermindDL1

Do you mean OTP resources? Where you can store native code specific data pointers and so forth? Or something in Zig?

ityonemo

ityonemo OP

Yep, otp resources. I’m trying to get my head around what the right way is to square them with zig’s opinionated (but optional) allocator semantics. One thing I’m unsure about is if otp resources can be realloc’d, since there is no resource realloc function. Probably not hard to try though, once I figure out how to even make one.

OvermindDL1

OvermindDL1

I would just have you allocate a resource with enough space to hold a zig pointer and store that pointer in it. Have the resource deallocation function deallocate the zig object (if you are done with it of course). That’s generally what I do regardless, I rarely have resource be ‘big’, I treat them as basically just a pointer to my internal object or pointer.

So no, you can’t realloc them, treat them as handles and you are good to go.

ChaseGilliam

ChaseGilliam

When I try this out locally I don’t seem to have the mix task.

  defp deps do
    [
      {:zigler, git: "https://github.com/ityonemo/zigler.git"}
      # {:dep_from_hexpm, "~> 0.3.0"},
      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
    ]
  end

Then I ran mix do deps.get, deps.compile

Then

> mix zigler.get_zig latest
Compiling 1 file (.ex)
Generated zig_test app
** (Mix) The task "zigler.get_zig" could not be found

Any clue what I’m doing wrong here?

Edit it seems like only Linux is supported atm and I’m running macOS

ityonemo

ityonemo OP

that’s super strange, it should work though (just tested the mix task myself on a completely fresh system).

ityonemo@hadamard:~/code$ git clone https://github.com/ityonemo/zigler
Cloning into 'zigler'...
remote: Enumerating objects: 176, done.
remote: Counting objects: 100% (176/176), done.
remote: Compressing objects: 100% (108/108), done.
remote: Total 176 (delta 80), reused 148 (delta 52), pack-reused 0
Receiving objects: 100% (176/176), 33.32 KiB | 897.00 KiB/s, done.
Resolving deltas: 100% (80/80), done.
ityonemo@hadamard:~/code$ cd zigler
ityonemo@hadamard:~/code/zigler$ mix zigler.get_zig latest
Unchecked dependencies for environment dev:
* mojito (Hex package)
  the dependency is not available, run "mix deps.get"
** (Mix) Can't continue due to errors on dependencies
ityonemo@hadamard:~/code/zigler$ mix deps.get
Resolving Hex dependencies...
Dependency resolution completed:
Unchanged:
  castore 0.1.3
  mint 0.4.0
  mojito 0.5.0
  poolboy 1.5.2
* Getting mojito (Hex package)
* Getting castore (Hex package)
* Getting mint (Hex package)
* Getting poolboy (Hex package)
ityonemo@hadamard:~/code/zigler$ mix zigler.get_zig latest
===> Compiling poolboy
==> castore
Compiling 1 file (.ex)
Generated castore app
==> mint
Compiling 1 file (.erl)
Compiling 22 files (.ex)
Generated mint app
==> mojito
Compiling 14 files (.ex)
Generated mojito app
==> zigler
Compiling 5 files (.ex)
Generated zigler app

09:13:22.758 [info]  downloading zig version 0.5.0 and caching in /home/ityonemo/code/zigler/zig.
ityonemo@hadamard:~/code/zigler$ mix test
Compiling 5 files (.ex)
Generated zigler app
...................................

Finished in 4.9 seconds
35 tests, 0 failures

In theory there is no reason why most of this stuff shouldn’t work on OSX, but I need to get a few paths correct, but i have no way to test it. If you would like to try getting it to work on OSX, any help would be appreciated! In the meantime I’m going to put a warning about OS at build time.

ChaseGilliam

ChaseGilliam

Yeah, I got it almost working int this test PR WIP/discussion experiment with macOS support by Ch4s3 · Pull Request #16 · E-xyza/zigler · GitHub. It seems like the mix task had a hardcoded check for :os.system. But you’re right, it seems the only real blocker is a path issue.

ityonemo

ityonemo OP

ok, I will try to generalize the operating system when i get a chance to work on this tonight, will let you know how it goes.

Where Next? Top

Trending in Announcing Top

restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 12366 136
New
type1fool
WebAuthnLiveComponent WebAuthnComponents See this post about renaming the package. Passwordless authentication for Phoenix LiveView app...
New
GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. https://github.com/woylie/doggo Features Unstyled Phoenix components....
New
ahamez
Hi everyone, I’ve been working on this protobuf library for 3 years. We use it in the company I work for, EasyMile, to communicate with ...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
kip
I’ll shortly be launching Text, a nascent text analysis library. Current functionality In this early version (not ready for prime time) ...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews