Skip to content

Update bindings to be compatible with Nix 2.30, and package with Nix#4

Open
cottand wants to merge 3 commits intofarcaller:masterfrom
cottand:nico.update-nix-2-30
Open

Update bindings to be compatible with Nix 2.30, and package with Nix#4
cottand wants to merge 3 commits intofarcaller:masterfrom
cottand:nico.update-nix-2-30

Conversation

@cottand
Copy link

@cottand cottand commented Aug 28, 2025

Brings lib up-to-date so it is compatible with the new C API since Nix 2.24 (now in Nix 2.30!), which now uses the new typdef nix_value.

In order to do this, I also implemented a couple things on the side

  • Replaced the dev-env with a proper nix build (you can still nix develop .)
  • Fixed freeing some CStrings (still many more to go)

Because of the new API are a few non-backwards compatible changes:

  • The Go API for registering PrimOps has changed
    • You are now able to return an error from Go, which will get correctly handled on the Nix side
    • The return value is now owned by Nix, rather than allocated during the Go callback. So instead of returning a *Value, you should now call SetInt(), SetString(), etc on a *Value that is provided to you (see C example or Go tests)
  • InitPlugins is gone - I could not get this to work, it seems Nix is not providing it anymore even though it is in the C API header files (although it is not in the docs)
  • GetSetting and SetSetting are often returning key error, even though the API is unchanged. I am not sure this is a problem in this library, but could not find much in the Nix changelog either.

I am not a CGO FFI expert, so there may be incorrect assumptions!

@cottand
Copy link
Author

cottand commented Aug 28, 2025

I do also plan to follow up and add the new Flake C API too to my fork, if this PR goes well I will also upstream that here

@cottand cottand force-pushed the nico.update-nix-2-30 branch from 2f6fbcd to 7cbc608 Compare August 28, 2025 08:35
@marcusramberg
Copy link
Collaborator

I checked out your branch and tried building, but I'm seeing this test failure:

❯ nix build .                                                                                 gonix-env
error: builder for '/nix/store/39yvgsq4szy5xbkrpy6zypxrpdfwqap6-gonix.drv' failed with exit code 1;
       last 25 log lines:
       > Running phase: checkPhase
       > --- FAIL: TestExampleRegisterGlobalPrimOp (0.00s)
       > panic: runtime error: invalid memory address or nil pointer dereference [recovered]
       >       panic: runtime error: invalid memory address or nil pointer dereference
       > [signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x51a6d0]
       >
       > goroutine 20 [running]:
       > testing.tRunner.func1.2({0x547740, 0x6a0c60})
       >       /nix/store/rn5bndqvz2y3ayc1j0fcchdf2hcbmx18-go-1.24.5/share/go/src/testing/testing.go:1734 +0x1ac
       > testing.tRunner.func1()
       >     /nix/store/rn5bndqvz2y3ayc1j0fcchdf2hcbmx18-go-1.24.5/share/go/src/testing/testing.go:1737 +0x334
       > panic({0x547740?, 0x6a0c60?})
       >       /nix/store/rn5bndqvz2y3ayc1j0fcchdf2hcbmx18-go-1.24.5/share/go/src/runtime/panic.go:792 +0x124
       > github.com/farcaller/gonix.(*Store).NewState.func2(...)
       >        /build/p1qk150r1175cv2fsmhy86zjv4cmy6r2-source/state.go:36
       > github.com/farcaller/gonix.(*Store).NewState(0x0, {0x0, 0x0, 0x4000075f28?})
       >       /build/p1qk150r1175cv2fsmhy86zjv4cmy6r2-source/state.go:36 +0x130
       > github.com/farcaller/gonix_test.TestExampleRegisterGlobalPrimOp(0x4000102e00)
       >       /build/p1qk150r1175cv2fsmhy86zjv4cmy6r2-source/primop_test.go:32 +0x100
       > testing.tRunner(0x4000102e00, 0x57df38)
       >       /nix/store/rn5bndqvz2y3ayc1j0fcchdf2hcbmx18-go-1.24.5/share/go/src/testing/testing.go:1792 +0xe4
       > created by testing.(*T).Run in goroutine 1
       >   /nix/store/rn5bndqvz2y3ayc1j0fcchdf2hcbmx18-go-1.24.5/share/go/src/testing/testing.go:1851 +0x374
       > FAIL github.com/farcaller/gonix      0.045s
       > FAIL
       For full logs, run 'nix log /nix/store/39yvgsq4szy5xbkrpy6zypxrpdfwqap6-gonix.drv'.

(this is on aarch64-linux if that matters)

@cottand
Copy link
Author

cottand commented Sep 7, 2025

thanks for checking! I had built on ARM darwin only, and on linux calling nix_store_open inside a Nix build (which is the case during tests) seems to be a problem

I have amended the test to print the error properly, and also added doCheck = false to the nix build, so that we don't try to run gonix tests during nix build

@marcusramberg
Copy link
Collaborator

Btw, seems 2.31 is going to be the next stable version, any reason we're not targetting that? NixOS/nixpkgs#440681

@marcusramberg marcusramberg self-requested a review September 7, 2025 12:18
result Outdated
@@ -0,0 +1 @@
/nix/store/la7026kj53y7dmy963ly5b1jx37sn4il-gonix No newline at end of file
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably shouldn't have been included in the commit.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

absolutely, my bad.

I also included it in the gitignore

@marcusramberg
Copy link
Collaborator

Btw, seems 2.31 is going to be the next stable version, any reason we're not targetting that? NixOS/nixpkgs#440681

Oh I see, it's not in 25.05. Maybe we could get it backported..

@cottand
Copy link
Author

cottand commented Sep 7, 2025

I can target 2.31 by using https://github.com/NixOS/nix as a flake input rather than nixpkgs.nix if you'd like, we'd be adding a new input to the flake is all.

It also doesn't seem like there are significant C API changes between .30 and .31

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants