Skip to content

Commit

Permalink
Merge pull request #72 from akirak/ocaml
Browse files Browse the repository at this point in the history
Add OCaml
  • Loading branch information
akirak authored Feb 25, 2024
2 parents a71c84f + a17cf6c commit 3378cbb
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 0 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/check-ocaml.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Check ocaml

on:
push:
paths:
# Set this to the directory of the template
- ocaml/**
- .github/workflows/check-ocaml.yml
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: cachix/install-nix-action@v25
with:
extra_nix_config: |
access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
- uses: actions/checkout@v4
with:
path: ./tmp

- name: Initialize a Dune project
run: |
nix run nixpkgs#dune_3 -- init project hello work
- name: Use the template
run: |
nix flake new -t ./tmp#ocaml work
- name: Prepare the project
working-directory: work
run: |
sed -i 's/throw "Name your OCaml package"/"hello"/' flake.nix
sed -i 's/throw "Version your OCaml package"/"0.1"/' flake.nix
git init
git add .
- name: Check executables
run: |
nix develop -L --command ocamlc --version
working-directory: work
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@ after initialization, e.g. to use yarn instead of pnpm.
It uses the master branch of NixPkgs to install a recent set of node
packages.

### [ocaml](ocaml/)

This flake.nix lets you use [Dune](https://dune.build/) for your development
workflow but also allows to build your package using Nix.
It depends on [the overlay from
nix-ocaml](https://github.com/nix-ocaml/nix-overlays).

``` bash
nix flake init -t github:akirak/flake-templates#ocaml
```

You will define your OCaml dependencies in `propagatedBuildInputs` of the dune
package in `flake.nix`. With the `direnv` integration, you don't have to
manually install packages using `opam` for development.

See also [the Nixpkgs
manual](https://nixos.org/manual/nixpkgs/unstable/#sec-language-ocaml) for
concrete information.

### [rust](rust/)

This flake.nix provides a Rust toolchain.
Expand Down
4 changes: 4 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
path = ./node-typescript;
description = "Toolchain for TypeScript frontend projects";
};
ocaml = {
path = ./ocaml;
description = "A flake template for development with OPAM and Dune";
};
rust = {
path = ./rust;
description = "Rust toolchain";
Expand Down
5 changes: 5 additions & 0 deletions ocaml/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use flake

# Using _build directory may not be properly allowed for storing user contents
export ODIG_CACHE_DIR="$(pwd)/_build/default/.odig"
export SHERLODOC_DB="$(pwd)/_build/default/.sherlodoc.marshal"
1 change: 1 addition & 0 deletions ocaml/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_build
12 changes: 12 additions & 0 deletions ocaml/.ocamlformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Latest version is available from
# https://github.com/ocaml-ppx/ocamlformat/blob/main/.ocamlformat

profile = ocamlformat
break-cases = fit
margin = 77
wrap-comments = true
line-endings = lf

# To make ocaml-ci happy
version = ignore
version-check = false
96 changes: 96 additions & 0 deletions ocaml/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
inputs = {
# nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
systems.url = "github:nix-systems/default";

ocaml-overlays.url = "github:nix-ocaml/nix-overlays";
ocaml-overlays.inputs.nixpkgs.follows = "nixpkgs";
};

outputs = {
self,
systems,
nixpkgs,
ocaml-overlays,
...
} @ inputs: let
inherit (nixpkgs) lib;

eachSystem = f:
nixpkgs.lib.genAttrs (import systems) (
system: let
pkgs = import nixpkgs {
inherit system;
overlays = [ocaml-overlays.overlays.default];
};
in
f
{
inherit pkgs system;
ocamlPackages = pkgs.ocaml-ng.ocamlPackages_latest;
}
);
in {
ocamlPackages = eachSystem ({ocamlPackages, ...}: ocamlPackages);

packages = eachSystem ({
pkgs,
ocamlPackages,
...
}: {
default = ocamlPackages.buildDunePackage {
pname = throw "Name your OCaml package";
version = throw "Version your OCaml package";
duneVersion = "3";
src = self.outPath;

# Uncomment if you need the executable of dream_eml during build
# preBuild = ''
# PATH="${ocamlPackages.dream}/bin:$PATH"
# export PATH
# '';

buildInputs = with ocamlPackages; [
ocaml-syntax-shims
];

propagatedBuildInputs = with ocamlPackages; [
# Add OCaml dependencies required for your project

# Jane Street
base
core
core_unix

# Some common dependencies
# eio
# eio_main
# yojson
# ppx_yojson_conv
];
};
});

devShells = eachSystem ({
pkgs,
ocamlPackages,
...
}: {
default = pkgs.mkShell {
inputsFrom = [self.packages.${pkgs.system}.default];
buildInputs =
lib.optional pkgs.stdenv.isLinux pkgs.inotify-tools
++ (with ocamlPackages; [
ocaml-lsp
ocamlformat
ocp-indent
utop
# Needed for generating documentation
opam
odoc
odig
]);
};
});
};
}
7 changes: 7 additions & 0 deletions ocaml/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
odig-odoc:
odig odoc

sherlodoc-index: odig-odoc
find "$(odig cache path)/odoc" -name '*.odocl' \
| grep -v '__' \
| xargs sherlodoc index

0 comments on commit 3378cbb

Please sign in to comment.