Compare commits

..

10 Commits

Author SHA1 Message Date
George Thomas
c1d30b0e61 refactor 2026-04-13 15:50:03 +01:00
George Thomas
ea5f816425 basic enum example 2026-04-13 15:48:41 +01:00
George Thomas
5c3aa088f7 Add maybe/optional example 2026-04-13 13:04:06 +01:00
George Thomas
6203e7570f Add vector/slice example 2026-04-13 13:04:06 +01:00
George Thomas
9e8bccdafd Pass all non-primitive types by (immutable) reference 2026-04-13 13:04:06 +01:00
George Thomas
8cb0bf9c5e Deduplicate base dependency 2026-04-13 13:04:06 +01:00
George Thomas
2ff3ad93e5 Simplify build script
Seeing as we now no longer need to modify generated header files.
2026-04-13 13:04:06 +01:00
George Thomas
8a5bf61d6b wip file dependency reload stuff 2026-04-13 13:04:06 +01:00
George Thomas
7ca469a62d use upstream fix to avoid header patching hack...
bump - no tag for libclang and alpha1 leads to build errors

drop dep

argh, unrelated breaking change, and bad names

exts
2026-03-30 13:07:16 +01:00
George Thomas
b5fd53b5ec stuff from Yuri (maybe squash?)...
things I've changed from the PR:
- dropped legacy (i.e. non-flake) compat stuff, which turns out to account for most of the diff
- dropped `packages.garnet` since it doesn't work with `nix build .#garnet`
- back to using Rust-extended packages everywhere, which _seems_ fine
the rest is just re-inlining things and other refactors

note that changes we keep are essentially:
- not using Crane's extended packages everywhere
- bumping `nix-haskell` to avoid shell hook workaround
- various changes in how we call `nix-haskell`
- using `libCgarnet_rs` name, which Cabal expects
- adding proper non-dev-shell targets, so that e.g. `nix run` works

stuff we should still take a look at:
- seems a bit weird that `garnet-rs` arg to `project.nix` was always same (and therefore now we always pass the ) - might be a mistake, and we're supposed to be using local for local build?
- similar `libCgarnet_rs.a` logic appears in three places - see if we can deduplicate to some extent somehow
- I haven't even checked all steps are necessary
2026-03-30 12:08:05 +01:00
6 changed files with 90 additions and 75 deletions

View File

@ -13,70 +13,20 @@
}; };
}; };
outputs = outputs =
inputs: inputs@{ nix-haskell, ... }:
inputs.flake-utils.lib.eachSystem [ "x86_64-linux" ] (system: inputs.flake-utils.lib.eachSystem [ "x86_64-linux" ] (system:
let let
pkgs = (import inputs.nixpkgs { inherit system; }).extend (import inputs.rust-overlay); pkgs = (import inputs.nixpkgs { inherit system; }).extend (import inputs.rust-overlay);
nix-haskell = import inputs.nix-haskell { inherit system inputs; };
crane = (inputs.crane.mkLib pkgs).overrideToolchain (p: p.rust-bin.selectLatestNightlyWith ( crane = (inputs.crane.mkLib pkgs).overrideToolchain (p: p.rust-bin.selectLatestNightlyWith (
toolchain: toolchain.default.override { toolchain: toolchain.default.override {
extensions = [ "rust-src" "rust-analyzer" ]; extensions = [ "rust-src" ];
targets = [ "x86_64-unknown-linux-gnu" ]; targets = [ "x86_64-unknown-linux-gnu" ];
} }
)); ));
addLibcIncludeDir = project =
'' (nix-haskell (import ./project.nix { inherit (inputs) hls-src hs-bindgen-src libclang-src; inherit garnet-rs; })
export C_INCLUDE_PATH="${pkgs.stdenv.cc.libc.dev}/include''${C_INCLUDE_PATH:+:$C_INCLUDE_PATH}" ).project.haskell-nix;
'';
project = (import inputs.nix-haskell { inherit system inputs; } {
name = "garnet";
src = ./.;
compiler-nix-name = "ghc914";
source-repository-packages = {
# not on Hackage yet: https://well-typed.com/blog/2026/02/hs-bindgen-alpha
# we're using more recent versions than in that post, because we want record-dot support:
# https://github.com/well-typed/hs-bindgen/issues/1829#issuecomment-4081875451
c-expr-dsl = inputs.hs-bindgen-src + "/c-expr-dsl";
c-expr-runtime = inputs.hs-bindgen-src + "/c-expr-runtime";
hs-bindgen = inputs.hs-bindgen-src + "/hs-bindgen";
hs-bindgen-runtime = inputs.hs-bindgen-src + "/hs-bindgen-runtime";
libclang-bindings = inputs.libclang-src;
};
overrides = [
({ pkgs, ... }: {
packages.libclang-bindings.components.library = {
build-tools = [ pkgs.llvmPackages.llvm ];
libs = [ pkgs.llvmPackages.libclang ];
};
packages.garnet.components.library = {
preBuild = addLibcIncludeDir;
preConfigure = ''
mkdir -p rust/target/debug
ln -s ${garnet-rs}/include/garnet_rs.h rust/target/debug/garnet_rs.h
'';
preInstall = ''
cp ${garnet-rs}/lib/libCgarnet_rs.a dist/build/libCgarnet_rs.a
'';
configureFlags = [ "--extra-lib-dirs=${garnet-rs}/lib" ];
};
})
];
shell = {
tools = {
cabal = "latest";
haskell-language-server = {
src = inputs.hls-src;
sha256map = {
"https://github.com/snowleopard/alga" = {
"d4e43fb42db05413459fb2df493361d5a666588a" = "0s1mlnl64wj7pkg3iipv5bb4syy3bhxwqzqv93zqlvkyfn64015i";
};
};
};
};
withHoogle = false;
withHaddock = true;
shellHook = addLibcIncludeDir;
};
}).project.haskell-nix;
garnet-rs = crane.buildPackage { garnet-rs = crane.buildPackage {
src = crane.cleanCargoSource ./rust; src = crane.cleanCargoSource ./rust;
doCheck = false; doCheck = false;
@ -102,6 +52,7 @@
packages = with pkgs; [ packages = with pkgs; [
bacon bacon
ghcid ghcid
rust-analyzer
]; ];
}; };
}); });

View File

@ -29,7 +29,6 @@ common common
NoMonomorphismRestriction NoMonomorphismRestriction
OverloadedRecordDot OverloadedRecordDot
OverloadedStrings OverloadedStrings
PatternSynonyms
RecordWildCards RecordWildCards
ViewPatterns ViewPatterns
ghc-options: ghc-options:

View File

@ -29,9 +29,18 @@ import HsBindgen.TH
import Language.Haskell.TH import Language.Haskell.TH
import System.Process import System.Process
withHsBindgen do
systemDirs <- -- TODO bit of a hack
map (Dir . T.unpack . T.strip)
. concatMap (takeWhile (maybe False ((== ' ') . fst) . T.uncons) . dropWhile T.null . T.lines)
. drop 1
. T.splitOn "search starts here:"
. T.pack
. thd3
<$> runIO (readProcessWithExitCode "cpp" ["-v"] "")
withHsBindgen
def def
{ clang = def{extraIncludeDirs = [Pkg "rust/target/debug"]} { clang = def{extraIncludeDirs = Pkg "rust/target/debug" : systemDirs}
, fieldNamingStrategy = OmitFieldPrefixes , fieldNamingStrategy = OmitFieldPrefixes
} }
def def

View File

@ -1,3 +1,5 @@
{-# LANGUAGE PatternSynonyms #-}
-- TODO automate this sort of high level wrapper boilerplate -- TODO automate this sort of high level wrapper boilerplate
-- or look at upstream plans: https://github.com/well-typed/hs-bindgen/issues?q=state%3Aopen%20label%3A%22highlevel%22 -- or look at upstream plans: https://github.com/well-typed/hs-bindgen/issues?q=state%3Aopen%20label%3A%22highlevel%22
module GarnetRs.Wrapped ( module GarnetRs.Wrapped (
@ -64,27 +66,27 @@ withBTree =
Raw.Fork_Body (unsafeFromPtr lPtr) (unsafeFromPtr rPtr) Raw.Fork_Body (unsafeFromPtr lPtr) (unsafeFromPtr rPtr)
hello :: ByteString -> IO () hello :: ByteString -> IO ()
hello bs = useAsCString bs \ptr -> Raw.hello $ unsafeFromPtr ptr hello = flip useAsCString $ Raw.hello . unsafeFromPtr
helloStruct :: T -> IO () helloStruct :: T -> IO ()
helloStruct t = with (convertT t) \ptr -> Raw.hello_struct $ unsafeFromPtr ptr helloStruct = flip with (Raw.hello_struct . unsafeFromPtr) . convertT
helloEnum :: Raw.E -> IO () helloEnum :: Raw.E -> IO ()
helloEnum e = with e \ptr -> Raw.hello_enum $ unsafeFromPtr ptr helloEnum = flip with (Raw.hello_enum . unsafeFromPtr)
helloShape :: Shape -> IO () helloShape :: Shape -> IO ()
helloShape s = with (convertShape s) \ptr -> Raw.hello_shape $ unsafeFromPtr ptr helloShape = flip with (Raw.hello_shape . unsafeFromPtr) . convertShape
add :: Int64 -> Int64 -> Int64 add :: Int64 -> Int64 -> Int64
add = Raw.add add = Raw.add
sumTree :: BTree Int64 -> Int64 sumTree :: BTree Int64 -> Int64
sumTree t = unsafePerformIO $ withBTree t \tc -> with tc \ptr -> Raw.sum_tree $ unsafeFromPtr ptr sumTree = unsafePerformIO . flip withBTree (flip with $ Raw.sum_tree . unsafeFromPtr)
sumSlice :: V.Vector Int64 -> Int64 sumSlice :: V.Vector Int64 -> Int64
sumSlice v = unsafePerformIO $ V.unsafeWith v \ptr -> Raw.sum_slice (unsafeFromPtr ptr) (fromIntegral $ V.length v) sumSlice v = unsafePerformIO $ V.unsafeWith v \p -> Raw.sum_slice (unsafeFromPtr p) (fromIntegral $ V.length v)
printOptional :: Maybe Int8 -> IO () printOptional :: Maybe Int8 -> IO ()
printOptional = \case printOptional = \case
Nothing -> Raw.print_optional $ unsafeFromPtr nullPtr Nothing -> Raw.print_optional (unsafeFromPtr nullPtr)
Just t -> with t \ptr -> Raw.print_optional $ unsafeFromPtr ptr Just t -> with t (Raw.print_optional . unsafeFromPtr)

54
project.nix Normal file
View File

@ -0,0 +1,54 @@
{ hls-src
, hs-bindgen-src
, libclang-src
, garnet-rs
}:
{
name = "garnet";
src = ./.;
compiler-nix-name = "ghc914";
source-repository-packages = {
# not on Hackage yet: https://well-typed.com/blog/2026/02/hs-bindgen-alpha
# we're using more recent versions than in that post, because we want record-dot support:
# https://github.com/well-typed/hs-bindgen/issues/1829#issuecomment-4081875451
c-expr-dsl = hs-bindgen-src + "/c-expr-dsl";
c-expr-runtime = hs-bindgen-src + "/c-expr-runtime";
hs-bindgen = hs-bindgen-src + "/hs-bindgen";
hs-bindgen-runtime = hs-bindgen-src + "/hs-bindgen-runtime";
libclang-bindings = libclang-src;
};
overrides = [
({ pkgs, ... }: {
packages.libclang-bindings.components.library = {
build-tools = [ pkgs.llvmPackages.llvm ];
libs = [ pkgs.llvmPackages.libclang ];
};
})
(_: {
packages.garnet.components.library = {
preConfigure = ''
mkdir -p rust/target/debug
ln -s ${garnet-rs}/include/garnet_rs.h rust/target/debug/garnet_rs.h
ln -s ${garnet-rs}/lib/libCgarnet_rs.a rust/target/debug/libCgarnet_rs.a
'';
preInstall = ''
cp ${garnet-rs}/lib/libCgarnet_rs.a dist/build/libCgarnet_rs.a
'';
configureFlags = [ "--extra-lib-dirs=${garnet-rs}/lib" ];
};
})
];
shell = {
tools = {
cabal = "latest";
haskell-language-server = {
src = hls-src;
sha256map = {
"https://github.com/snowleopard/alga"."d4e43fb42db05413459fb2df493361d5a666588a" = "0s1mlnl64wj7pkg3iipv5bb4syy3bhxwqzqv93zqlvkyfn64015i";
};
};
};
withHoogle = false;
withHaddock = true;
};
}

View File

@ -11,7 +11,7 @@ fn say_hello(name: &str) {
} }
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
extern "C" fn hello(c: *const c_char) { extern "C" fn hello(c: *const c_char) -> () {
say_hello(unsafe { CStr::from_ptr(c) }.to_str().unwrap()) say_hello(unsafe { CStr::from_ptr(c) }.to_str().unwrap())
} }
@ -23,7 +23,7 @@ struct T {
} }
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
extern "C" fn hello_struct(t: &T) { extern "C" fn hello_struct(t: &T) -> () {
say_hello(&format!("{:?}", t)) say_hello(&format!("{:?}", t))
} }
@ -48,7 +48,7 @@ enum Shape {
} }
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
extern "C" fn hello_shape(s: &Shape) { extern "C" fn hello_shape(s: &Shape) -> () {
say_hello(&format!("{:?}", s)) say_hello(&format!("{:?}", s))
} }