Skip to content

Commit

Permalink
refactor(build): onix based env
Browse files Browse the repository at this point in the history
  • Loading branch information
actionshrimp committed Oct 31, 2024
1 parent 8a6fab5 commit 35c73f4
Show file tree
Hide file tree
Showing 6 changed files with 3,659 additions and 205 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ opam-install-deps:

format:
dune build @fmt --auto-promote

onix-lock:
onix lock ./gcloud.opam ./gcloud-cli.opam --resolutions="ocaml-system=5.2.0" --lock-file ./onix-lock.json
onix lock ./gcloud.opam ./gcloud-cli.opam ./gcloud-melange.opam --resolutions="ocaml-system=5.2.0" --with-dev-setup=true --with-test=true --lock-file ./onix-lock-dev.json
git add onix-lock.json onix-lock-dev.json
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
OCaml bindings to the Google Cloud Platform APIs
================================================

## Development

The default nix devShell will have the packages needed to develop `ocaml-gcloud`:
```
nix develop '.#' # (or use nix-direnv)
dune build ...
```

### Updating opam package set

If you've updated the `.opam` files in the project and need to recalculate the `opam` deps, run `make onix-lock` and refresh the devShell:

```
# From inside the nix devShell
$ make onix-lock
$ exit # (or nix-direnv-reload if using nix-direnv)
$ nix develop '.#'
```
137 changes: 15 additions & 122 deletions flake.lock

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

104 changes: 21 additions & 83 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,100 +3,38 @@
nixpkgs.url = "nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";

opam-nix = {
url = "github:tweag/opam-nix";
inputs.opam-repository.follows = "opam-repository";
};
opam-repository = {
url = "github:ocaml/opam-repository";
flake = false;
};
onix.url = "github:rizo/onix";
onix.inputs.nixpkgs.follows = "nixpkgs";
};

outputs = { self, nixpkgs, flake-utils, opam-nix, opam-repository }@inputs:
outputs = { self, nixpkgs, flake-utils, onix }@inputs:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
fs = pkgs.lib.fileset;
on = opam-nix.lib.${system};
ocaml-version = "5.1.1";
ocaml-base-compiler = ocaml-version;

# List of opam files you which to be read by opam-nix
opamFiles = [
./gcloud.opam
./gcloud-cli.opam

# Also include the opam files to any submoduled dependencies
# ./vendor/some-lib/some-lib.opam
];

opamFilePackageNames = (map (x:
(pkgs.lib.removeSuffix ".opam" (builtins.baseNameOf (toString x))))
opamFiles);

# attrset containing all opam packages defined in the opamFiles list
# for a specific OCaml version
opamScope = (on.buildOpamProject' {
repos = [ opam-repository ];
resolveArgs.with-test = true;
recursive = true;
overlays = on.__overlays ++ [
(final: prev:
builtins.foldl' (acc: pkg:
(acc // {
# Allows us to treat the whole repo as a single unit and `dune build` it together,
# without opam-nix attempting to build each opam package individually.
# Similar to opam install --deps-only
${pkg} =
prev.${pkg}.overrideAttrs (oa: { dontBuild = true; });
})) {
# other-package-override = prev.other-package-overide.overrideAttrs (oa: {
# nativeBuildInputs = oa.nativeBuildInputs ++ (with pkgs; [ makeWrapper git ]);
# });
} opamFilePackageNames)
];
} (fs.toSource {
root = ./.;
fileset = fs.unions opamFiles;
}) { inherit ocaml-base-compiler; });

gcloud-cli = pkgs.stdenv.mkDerivation {
pname = "gcloud-cli";
version = "1.0.0";
buildInputs = (map (p: opamScope.${p}) opamFilePackageNames);
buildPhase = ''
dune build @install -p gcloud,gcloud-cli
'';

installPhase = ''
mkdir -p $out/bin
cp _build/install/default/bin/* $out/bin/
'';

src = (fs.toSource {
root = ./.;

# Prevent changes to the nix flake file from busting the build cache.
# Add any other files which the build should ignore to this list.
fileset =
fs.difference ./. (fs.unions [ ./flake.nix ./flake.lock ]);
});
onix' = onix.packages.${system}.latest;
onixEnv = onix'.env {
path = ./.;
# roots = [./gcloud.opam ./gcloud-cli.opam];
lock = ./onix-lock.json;
};
onixEnvDev = onix'.env {
path = ./.;
# roots = [./gcloud.opam ./gcloud-cli.opam];
lock = ./onix-lock-dev.json;
};

in rec {
# for use by nix fmt
formatter = pkgs.nixfmt-rfc-style;

packages.gcloud-cli = gcloud-cli;
packages.gcloud-cli = onixEnv.pkgs.gcloud-cli;

packages.default = pkgs.mkShell {
dontDetectOcamlConflicts = true;
buildInputs = (map (p: opamScope.${p}) opamFilePackageNames) ++ [
pkgs.ocaml-ng.ocamlPackages_5_1.utop
pkgs.ocaml-ng.ocamlPackages_5_1.ocaml-lsp
pkgs.ocamlformat_0_22_4
];
};
devShells.onixLock = pkgs.mkShell { buildInputs = [ onix' ]; };

devShells.default = onixEnvDev.shell.overrideAttrs (final: prev: {
buildInputs = prev.buildInputs ++ [ pkgs.ocamlformat_0_22_4 onix' ];
});

packages.dev-shell = devShells.default.inputDerivation;
});
}
Loading

0 comments on commit 35c73f4

Please sign in to comment.