garnet/flake.nix
George Thomas 7bb41f6825 Call Rust from Haskell with cargo-cabal and hs-bindgen
See https://sraka.xyz/posts/hs-bindgen-introduction.html.

For now, this is a shell-based workflow, rather than using Nix to build everything, i.e. `nix develop` works but `nix build` doesn't. And `cargo build` has to be called manually to create the C library, rather than `cabal` being clever enough to invoke it itself.

We ran `cargo cabal init` from the `rust` directory (`nix shell github:yvan-sraka/cargo-cabal`), which generated `hsbindgen.toml` (which we use), and `Setup.lhs` (which just added `extra-lib-dirs`, and with the wrong paths, so we dspecify those statically instead in `garnet.cabal`). We also follow its advice to use `staticlib`.

Also, after we ran the first `cargo build` (requiring a `mkdir rust/src` before it would run), we took the generated the Haskell file, and moved the main contents in to `Main.hs` manually.
2026-02-19 13:57:14 +00:00

69 lines
2.0 KiB
Nix

{
inputs = {
haskellNix.url = "github:input-output-hk/haskell.nix";
hls-2-13 = { url = "github:haskell/haskell-language-server/2.13.0.0"; flake = false; };
nixpkgs.follows = "haskellNix/nixpkgs-2511";
flake-utils.url = "github:numtide/flake-utils";
crane.url = "github:ipetkov/crane";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
{ self
, nixpkgs
, flake-utils
, haskellNix
, hls-2-13
, crane
, rust-overlay
}:
flake-utils.lib.eachSystem [ "x86_64-linux" ] (system:
let
overlays = [
haskellNix.overlay
(final: _prev: {
hixProject =
final.haskell-nix.hix.project {
src = ./.;
evalSystem = "x86_64-linux";
name = "garnet";
compiler-nix-name = "ghc9141";
shell.tools.cabal = "latest";
shell.withHoogle = false;
shell.tools.haskell-language-server = {
src = hls-2-13;
sha256map = {
"https://github.com/snowleopard/alga"."d4e43fb42db05413459fb2df493361d5a666588a" = "0s1mlnl64wj7pkg3iipv5bb4syy3bhxwqzqv93zqlvkyfn64015i";
};
};
};
})
(import rust-overlay)
];
pkgs = import nixpkgs { inherit system overlays; inherit (haskellNix) config; };
haskell = pkgs.hixProject.flake { };
rust = (crane.mkLib pkgs).overrideToolchain (p: p.rust-bin.selectLatestNightlyWith (
toolchain: toolchain.default.override {
extensions = [ "rust-src" ];
targets = [ "x86_64-unknown-linux-gnu" ];
}
));
in
{
devShells.default = pkgs.mkShell {
inputsFrom = [
haskell.devShells.default
(rust.devShell { })
];
packages = with pkgs; [
bacon
ghcid
rust-analyzer
];
};
}
);
}