Initial commit

Setup a nix flake for an ocaml setup with mruby dependency
This commit is contained in:
2026-07-25 22:00:25 +01:00
commit 4cbacf7172
12 changed files with 211 additions and 0 deletions
+4
View File
@@ -0,0 +1,4 @@
_build
result
p.vim
+16
View File
@@ -0,0 +1,16 @@
[submodule "libs/mruby"]
path = libs/mruby
url = https://github.com/mruby/mruby.git
ignore = dirty
[submodule "libs/mgems/mruby-marshal-c"]
path = libs/mgems/mruby-marshal-c
url = https://github.com/LanzaSchneider/mruby-marshal-c.git
ignore = dirty
[submodule "libs/mgems/mruby-process"]
path = libs/mgems/mruby-process
url = https://github.com/iij/mruby-process.git
ignore = dirty
[submodule "libs/mgems/mruby-env"]
path = libs/mgems/mruby-env
url = https://github.com/iij/mruby-env.git
ignore = dirty
+2
View File
@@ -0,0 +1,2 @@
(lang dune 3.23)
(using ctypes 0.3)
Generated
+26
View File
@@ -0,0 +1,26 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1784887798,
"narHash": "sha256-F/NHXIk7pOzyY505xKeMrRZ8A/jS29ERharPoQjFR00=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "10061cf0f71d935e6b26195985cc9eeb04c68b63",
"type": "github"
},
"original": {
"owner": "NixOS",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}
+97
View File
@@ -0,0 +1,97 @@
{
inputs.nixpkgs.url = "github:NixOS/nixpkgs";
inputs.self.submodules = true;
outputs =
{ self, nixpkgs, ... }:
let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
mruby = pkgs.stdenv.mkDerivation {
pname = "mruby";
version = "0.0.1";
src = pkgs.lib.cleanSourceWith {
src = "${self}/libs";
name = "mruby-src";
};
patches = [ ./libs/changes.patch ];
nativeBuildInputs = with pkgs; [
ruby
gnumake
gcc
];
buildPhase = ''
(cd mruby && rake)
'';
installPhase = ''
mkdir -p $out
cp -r mruby/build/host/lib $out/
'';
};
kutu = pkgs.stdenv.mkDerivation {
pname = "kutu";
version = "0.1.0";
src = self;
nativeBuildInputs = with pkgs; [
dune_3
ocaml
ocamlPackages.findlib
];
buildInputs = with pkgs; [
libxcb
xcbutilwm
xrandr
xprop
ocamlPackages.ctypes
ocamlPackages.ctypes-foreign
mruby
];
buildPhase = ''
export MRUBY_LIB=${mruby}/lib
dune build src/main.exe --release
'';
installPhase = ''
mkdir -p $out/bin
cp _build/default/src/main.exe $out/bin/kutu
'';
};
in
{
packages.${system} = {
default = kutu;
kutu = kutu;
mruby = mruby;
};
devShells.${system}.default = pkgs.mkShell {
inputsFrom = [ kutu ];
packages = with pkgs; [
ocamlPackages.utop
ocamlPackages.ocaml-lsp
ocamlPackages.ocamlformat
];
shellHook = ''
export CC=clang
export CXX=clang++
export LD=clang
export MRUBY_LIB=${mruby}/lib
'';
};
};
}
+33
View File
@@ -0,0 +1,33 @@
--- ./mruby/build_config/default.rb
+++ ./mruby/build_config/default.rb
@@ -1,6 +1,18 @@
MRuby::Build.new do |conf|
# load specific toolchain settings
- conf.toolchain
+ conf.toolchain :gcc
+
+ conf.cc do |cc|
+ cc.command = ENV['CC']
+ end
+
+ conf.cxx do |cxx|
+ cxx.command = ENV['CXX']
+ end
+
+ conf.linker do |linker|
+ linker.command = ENV['CXX']
+ end
# Use mrbgems
# conf.gem 'examples/mrbgems/ruby_extension_example'
@@ -16,6 +28,10 @@
# include the GEM box
conf.gembox 'default'
+ conf.gem '../mgems/mruby-env'
+ conf.gem '../mgems/mruby-process'
+ conf.gem '../mgems/mruby-marshal-c'
+
# C compiler settings
# conf.cc do |cc|
# cc.command = ENV['CC'] || 'gcc'
Submodule
+1
Submodule libs/mruby added at c8f6c6c10c
+9
View File
@@ -0,0 +1,9 @@
(executable
(name main)
(libraries ctypes-foreign)
(flags :standard
-cclib -Wl,--export-dynamic
-cclib -L%{env:MRUBY_LIB=../libs/mruby/build/host/lib}
-cclib -Wl,--whole-archive
-cclib -lmruby
-cclib -Wl,--no-whole-archive))
+20
View File
@@ -0,0 +1,20 @@
open Ctypes
open Foreign
type mrb = unit ptr
let mrb : mrb typ = ptr void
let mrb_open = foreign "mrb_open" (void @-> returning mrb)
let mrb_close = foreign "mrb_close" (mrb @-> returning void)
let mrb_load_string =
foreign "mrb_load_string" (mrb @-> string @-> returning void)
let () =
let mrb_instance = mrb_open () in
print_endline "Executing Ruby code from OCaml:";
let _ = mrb_load_string mrb_instance "puts 'hello1234'.match?(/hello\\d+/)" in
mrb_close mrb_instance;
print_endline "mruby instance closed successfully."