Skip to content

Commit

Permalink
Add simple template
Browse files Browse the repository at this point in the history
  • Loading branch information
ulrikstrid committed Jun 19, 2023
1 parent c46c867 commit 52091c0
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 4 deletions.
16 changes: 12 additions & 4 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,22 @@
} // attrs);
in
/*
You might read
https://nixos.org/manual/nixpkgs/stable/#sec-overlays-argument and
want to change this but because of how we're doing overlays we will
be overriding any extraOverlays if we don't use `appendOverlays`
You might read
https://nixos.org/manual/nixpkgs/stable/#sec-overlays-argument and
want to change this but because of how we're doing overlays we will
be overriding any extraOverlays if we don't use `appendOverlays`
*/
pkgs.appendOverlays extraOverlays;

overlays.default = final: prev: overlay final prev;

templates.simple = {
description =
"Simple setup for ocaml development";
path = ./templates/simple;
};

templates.default = self.templates.simple;
}
(flake-utils.lib.eachDefaultSystem (system: {
legacyPackages = self.makePkgs { inherit system; };
Expand Down
2 changes: 2 additions & 0 deletions templates/simple/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_build
result
Empty file added templates/simple/.ocamlformat
Empty file.
1 change: 1 addition & 0 deletions templates/simple/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 3.0)
44 changes: 44 additions & 0 deletions templates/simple/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
nixConfig = {
extra-substituters = "https://ocaml.nix-cache.com";
extra-trusted-public-keys = "ocaml.nix-cache.com-1:/xI2h2+56rwFfKyyFVbkJSeGqSIYMC/Je+7XXqGKDIY=";
};

inputs = {
nixpkgs.url = "github:nix-ocaml/nix-overlays";
flake-utils.url = "github:numtide/flake-utils";
flake-utils.follows = "nixpkgs/flake-utils";
nix-filter.url = "github:numtide/nix-filter";
};

outputs = { self, nixpkgs, flake-utils, nix-filter }:
flake-utils.lib.eachDefaultSystem (system:
let pkgs = nixpkgs.legacyPackages.${system}; in
{
packages = {
inherit (pkgs.ocamlPackages.callPackage ./nix {
inherit nix-filter;
doCheck = true;
}) package;
};

devShells = {
default = pkgs.mkShell {
inputsFrom = with self.packages.${system}; [
package
];

nativeBuildInputs = with pkgs.ocamlPackages; [
ocaml
dune

ocaml-lsp

ocamlformat
dune-release
odoc
];
};
};
});
}
49 changes: 49 additions & 0 deletions templates/simple/nix/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{ pkgs
, stdenv
, lib
, nix-filter
, ocamlPackages
, static ? false
, doCheck
}:
with ocamlPackages; {
package = buildDunePackage {
pname = "package";
version = "1.0.0";

src = with nix-filter.lib;
filter {
# Root of the project relative to this file
root = ./..;
# If no include is passed, it will include all the paths.
include = [
# Include the "src" path relative to the root.
"src"
"test"
# Include this specific path. The path must be under the root.
../package.opam
../dune-project
];
};

checkInputs = [
# Put test dependencies here
alcotest
];

propagatedBuildInputs = [
# Put dependencies here if you're creating a library
];

buildInputs = [
# Put build-time dependencies here
];

inherit doCheck;

meta = {
description = "Describe your project here";
# license = stdenv.lib.licenses.bsd3;
};
};
}
Empty file added templates/simple/package.opam
Empty file.
4 changes: 4 additions & 0 deletions templates/simple/src/bin/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(executable
(name main)
(public_name package)
(libraries package))
18 changes: 18 additions & 0 deletions templates/simple/src/bin/main.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
let main () =
print_string "First number: ";
let first = read_line () |> int_of_string_opt in
print_string "Second number: ";
let second = read_line () |> int_of_string_opt in
let output =
match (first, second) with
| None, Some _ -> "First input is not a number"
| Some _, None -> "Second input is not a number"
| None, None -> "Neither of the inputs is a number"
| Some f, Some s ->
let number = Package.add f s in
Printf.sprintf "%i + %i = %i" f s number
in
print_endline output
;;

main ()
3 changes: 3 additions & 0 deletions templates/simple/src/lib/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(library
(name package)
(public_name package))
1 change: 1 addition & 0 deletions templates/simple/src/lib/package.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let add a b = a + b
3 changes: 3 additions & 0 deletions templates/simple/test/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(test
(name test)
(libraries alcotest package))
5 changes: 5 additions & 0 deletions templates/simple/test/test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let test_add () = Alcotest.(check int) "adds numbers" 5 (Package.add 2 3)

let () =
let open Alcotest in
run "Package" [ ("numbers", [ test_case "add" `Quick test_add ]) ]

0 comments on commit 52091c0

Please sign in to comment.