Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package with nix #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
Empty file added .github/workflows/build_nix.yml
Empty file.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ resource/private
*.bs.js
lib/bs
node_modules/
result
.direnv
1 change: 1 addition & 0 deletions bin/sdl2/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(executables
(names main bench)
(public_names camlboy-sdl camlboy-sdl-bench)
(ocamlopt_flags -O3)
(libraries camlboy_lib tsdl))
11 changes: 11 additions & 0 deletions dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(data_only_dirs chain node_modules)

(env
(static
(flags
(:standard -ccopt -static))
(ocamlopt_flags
(:standard -O3 -unbox-closures)))
(release
(ocamlopt_flags
(:standard -O3 -unbox-closures))))
80 changes: 80 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";

ocaml-overlay.url = "github:anmonteiro/nix-overlays";
ocaml-overlay.inputs.flake-utils.follows = "flake-utils";

nix-filter.url = "github:numtide/nix-filter";
};

outputs = { self, nixpkgs, flake-utils, ocaml-overlay, nix-filter }:
let
supported_ocaml_versions = [ "ocamlPackages_4_12" "ocamlPackages_4_13" "ocamlPackages_5_00" ];
out = system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ ocaml-overlay.overlay ];
};
inherit (pkgs) lib;
myPkgs = pkgs.recurseIntoAttrs (import ./nix {
inherit pkgs nix-filter;
doCheck = true;
}).native;
myDrvs = lib.filterAttrs (_: value: lib.isDerivation value) myPkgs;
in
{
devShell = (pkgs.mkShell {
inputsFrom = lib.attrValues myDrvs;
buildInputs = with pkgs;
with ocamlPackages; [
ocaml-lsp
ocamlformat
odoc
ocaml
dune_3
nixfmt
utop
];
});

defaultPackage = myPkgs.camlboy;
};
in
with flake-utils.lib; eachSystem defaultSystems out // {
overlays = { default = import ./nix/overlay.nix supported_ocaml_versions nix-filter; };
};
}
1 change: 1 addition & 0 deletions lib/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(library
(name camlboy_lib)
(public_name camlboy)
(libraries bigstringaf))

(include_subdirs unqualified)
43 changes: 43 additions & 0 deletions nix/camlboy.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{ pkgs, nix-filter, stdenv, lib, ocamlPackages, static ? false, doCheck }:

with ocamlPackages;
buildDunePackage {
pname = "camlboy";
version = "0.1.0";

src = nix-filter.lib.filter {
root = ../.;
# TODO: package the brr and include the browser front end
include = [
"lib"
"bin/sdl2"
"dune"
"dune-project"
"camlboy.opam"
"resources"
];
};

# Static builds support, note that you need a static profile in your dune file
buildPhase = ''
echo "running ${if static then "static" else "release"} build"
dune build --profile=${if static then "static" else "release"}
'';

checkInputs = [ ];

propagatedBuildInputs = [
ppx_deriving
tsdl
base
bigstringaf
ppx_jane
odoc
];

inherit doCheck;

meta = {
description = "A Game Boy emulator written in OCaml that runs in your browser";
};
}
22 changes: 22 additions & 0 deletions nix/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{ pkgs ? import ./sources.nix { }, nix-filter, doCheck ? false }:

{
native = {
camlboy = pkgs.callPackage ./camlboy.nix {
inherit doCheck nix-filter;
};
};

musl64 =
let
pkgsCross = pkgs.pkgsCross.musl64.pkgsStatic;

in
{
camlboy = pkgsCross.callPackage ./camlboy.nix {
static = true;
inherit doCheck;
ocamlPackages = pkgsCross.ocamlPackages;
};
};
}
19 changes: 19 additions & 0 deletions nix/overlay.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
supported_ocaml_versions: nix-filter:

final: prev: {
ocaml-ng = builtins.mapAttrs
(name: ocamlVersion:
# If the current ocamlVersion exists in supported versions
(if (builtins.any (x: x == name) supported_ocaml_versions) then
ocamlVersion.overrideScope'
(oself: osuper: {
camlboy = (prev.callPackage ./camlboy.nix {
inherit nix-filter;
ocamlPackages = oself;
doCheck = true;
});
})
else
ocamlVersion))
prev.ocaml-ng;
}