Skip to content

Commit

Permalink
Migrates Purescript prelude stack to flake-parts
Browse files Browse the repository at this point in the history
  • Loading branch information
bladyjoker committed Oct 15, 2023
1 parent 4acc0a0 commit 5e5c9d0
Show file tree
Hide file tree
Showing 20 changed files with 294 additions and 183 deletions.
17 changes: 17 additions & 0 deletions extras/build.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# TODO(bladyjoker): Using overlayAttrs here as a hack to share functions -.- Do this properly.
{ inputs, ... }: {
imports = [
inputs.flake-parts.flakeModules.easyOverlay # Adds perSystem.overlayAttrs
];
perSystem = { pkgs, config, ... }:
{

overlayAttrs = {
extras = {
purescriptFlake = import ./flake-purescript.nix pkgs;
haskellData = import ./haskell-data.nix pkgs;
};
};

};
}
10 changes: 8 additions & 2 deletions extras/flake-purescript.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
pursProjOpts:
pkgs: pursProjOpts:
let
mkFlake = projectName: purs: {
packages = {
"purescript:${projectName}:src" = pkgs.stdenv.mkDerivation {
name = "lbr-prelude-src";
inherit (pursProjOpts) src;
phases = "installPhase";
installPhase = "ln -s $src $out";
};
"purescript:${projectName}:lib" = purs.compiled;
"purescript:${projectName}:node-modules" = purs.nodeModules;
"purescript:${projectName}:bundle" = purs.bundlePursProject { main = "Test.Main"; entrypoint = "app/index.js"; bundledModuleName = "dist/output.js"; };
Expand All @@ -16,4 +22,4 @@ let
devShell = purs.devShell;
};
in
mkFlake pursProjOpts.projectName (pursProjOpts.pkgs.purescriptProject pursProjOpts)
mkFlake pursProjOpts.projectName (pkgs.purescriptProject pursProjOpts)
3 changes: 2 additions & 1 deletion extras/haskell-data.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{ pkgs, srcs, cabalDataPatterns, cabalPackageName, cabalPackageVersion ? "0.1.0.0" }:
# Makes a Cabal package with just a 'data' directory with proper 'data-filers' stanza
pkgs: { srcs, cabalDataPatterns, cabalPackageName, cabalPackageVersion ? "0.1.0.0" }:
let
cabalTemplate = pkgs.writeTextFile {
name = "haskell-data.nix-cabal-template";
Expand Down
3 changes: 3 additions & 0 deletions extras/lbf-nix/build.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
lbfHaskell = import ./lbf-haskell.nix pkgs config.packages.lbf config.packages.lbg-haskell;
lbfPreludeHaskell = import ./lbf-prelude-hs.nix pkgs config.packages.lbf config.packages.lbg-haskell;
lbfPlutusHaskell = import ./lbf-plutus-hs-plutustx.nix pkgs config.packages.lbf config.packages.lbg-haskell;
lbfPurescript = import ./lbf-purescript.nix pkgs config.packages.lbf config.packages.lbg-purescript;
lbfPreludePurescript = import ./lbf-prelude-purescript.nix pkgs config.packages.lbf config.packages.lbg-purescript;
lbfPlutusPurescript = import ./lbf-plutus-purescript.nix pkgs config.packages.lbf config.packages.lbg-purescript;
};
};

Expand Down
4 changes: 2 additions & 2 deletions extras/lbf-nix/lbf-haskell.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Base API for constructing Haskell Cabal packages given .lbf schemas
# Base API for constructing Haskell Cabal packages given .lbf schemas

# Nixpkgs
pkgs:
Expand All @@ -23,7 +23,7 @@ let
# Classes for which to generate implementations for (default lbf-prelude classes).
, classes ? [ ]
, # Dependencies to include in the Cabal's `build-depends` stanza.
# examples: dependencies = [ "lbf-prelude" "lbr-prelude" ]
# examples: dependencies = [ "lbf-prelude" ]
dependencies ? [ ]
, configs ? [ ]
, # Name of the package and also the name of the Cabal package.
Expand Down
28 changes: 28 additions & 0 deletions extras/lbf-nix/lbf-plutus-purescript.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Build .lbf schemas that use LB Plutus (and by extension LB Prelude) package and targets Purescript's CTL library.
pkgs: lbf: lbg-purescript: lbfPurescriptOpts:
let
utils = import ./utils.nix pkgs;

lbfPurs = import ./lbf-prelude-purescript.nix pkgs lbf lbg-purescript;
lbfPurescriptOptsForPlutus = utils.overrideAttrs
{
imports = {
default = [ ];
override = libs: libs ++ [ ../../libs/lbf-plutus ];
};
dependencies = {
default = [ ];
override = deps: deps ++ [ "lbf-plutus" ];
};
classes = {
default = [ ];
override = cls: cls ++ [ "Plutus.V1.PlutusData" ];
};
configs = {
default = [ ];
override = cfgs: cfgs ++ [ ../../lambda-buffers-codegen/data/purescript-plutus-ctl.json ];
};
}
lbfPurescriptOpts;
in
lbfPurs lbfPurescriptOptsForPlutus
29 changes: 29 additions & 0 deletions extras/lbf-nix/lbf-prelude-purescript.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Build .lbf schemas that use LB Prelude package and targets Purescript's prelude (and friends) library.
pkgs: lbf: lbg-purescript: lbfPurescriptOpts:
let
utils = import ./utils.nix pkgs;

lbfPurs = import ./lbf-purescript.nix pkgs lbf lbg-purescript;
lbfPurescriptOptsForPrelude = utils.overrideAttrs
{
imports = {
default = [ ];
override = libs: libs ++ [ ../../libs/lbf-prelude ];
};
dependencies = {
default = [ ];
override = deps: deps ++ [ "lbf-prelude" ];
};
classes = {
default = [ ];
override = cls: cls ++ [ "Prelude.Eq" "Prelude.Json" ];
};
configs = {
default = [ ];
override = cfgs: cfgs ++ [ ../../lambda-buffers-codegen/data/purescript-prelude-base.json ];
};
}
lbfPurescriptOpts;

in
lbfPurs lbfPurescriptOptsForPrelude
22 changes: 0 additions & 22 deletions extras/lbf-nix/lbf-purescript-plutus.nix

This file was deleted.

126 changes: 76 additions & 50 deletions extras/lbf-nix/lbf-purescript.nix
Original file line number Diff line number Diff line change
@@ -1,58 +1,84 @@
# Base API for constructing Purescript packages given .lbf schemas

# Nixpkgs
pkgs:
# LambdaBuffers Frontend
lbf:
# LambdaBuffers Purescript Codegen
lbg-purescript:
{
# Nixpkgs
pkgs
, # Source that is passed to `lbf` as the `--import-path` flag and used to find `files`.
# Examples: src = ./api
src
, # Additional sources that are passed to `lbf` as the `--import-path` flag.
# Examples: imports = [ lbf-prelude ]
imports ? [ ]
, # .lbf files in `src` to compile and codegen.
# Examples: files = [ "Foo.lbf" "Foo/Bar.lbf" ]
files
# Classes for which to generate implementations for.
, classes ? [ "Prelude.Eq" "Prelude.Json" ]
, # TODO(bladyjoker): Dependencies to include in the `build` output
# examples: dependencies = [ "lbf-prelude" "lbr-prelude" ]
dependencies ? [ ]
, # Package name.
# Examples: name = "lbf-myproject"
name
, # Package version.
# Examples: version = "0.1.0.0"
version ? "0.1.0.0"
}:
let
utils = import ./utils.nix pkgs;
in
pkgs.stdenv.mkDerivation {
inherit src version;
pname = name;
outputs = [ "out" "build" ];
buildInputs = [
pkgs.cabal-install
lbf
pkgs.jq
];
buildPhase = ''
mkdir autogen
mkdir .work
lbf build ${utils.mkFlags "import-path" imports} ${utils.mkFlags "gen-class" classes} \
--work-dir .work \
--gen ${lbg-purescript}/bin/lbg-purescript \
--gen-dir autogen \
${builtins.concatStringsSep " " files}

cat autogen/build.json
'';
lbfPurescriptOpts =
{
# Source that is passed to `lbf` as the `--import-path` flag and used to find `files`.
# Examples: src = ./api
src
, # Additional sources that are passed to `lbf` as the `--import-path` flag.
# Examples: imports = [ lbf-prelude ]
imports ? [ ]
, # .lbf files in `src` to compile and codegen.
# Examples: files = [ "Foo.lbf" "Foo/Bar.lbf" ]
files
# Classes for which to generate implementations for (default lbf-prelude classes).
, classes ? [ ]
, # Dependencies to include in the Cabal's `build-depends` stanza.
# examples: dependencies = [ "lbf-prelude" ]
dependencies ? [ ]
, configs ? [ ]
, # Name of the package and also the name of the Cabal package.
# Examples: name = "lbf-myproject"
name
, # Version of the package and also the version of the Cabal package.
# Examples: version = "0.1.0.0"
version ? "0.1.0.0"
}: { inherit src imports files classes dependencies configs name version; };

lbf-build = import ./lbf-build.nix pkgs lbf;

lbfBuild = opts: with (lbfPurescriptOpts opts);
lbf-build.build
{
inherit src;
opts = {
inherit files;
import-paths = imports;
gen = "${lbg-purescript}/bin/lbg-purescript";
gen-classes = classes;
gen-dir = "autogen";
gen-opts = builtins.map (c: "--config=${c}") configs; # WARN(bladyjoker): If I put quotes here everything breaks.
work-dir = ".work";
};
};

installPhase = ''
mkdir -p $out/src;
cp -r autogen/* $out/src
mv autogen/build.json $build;
'';
}
build = opts: with (lbfPurescriptOpts opts);
let
lbfBuilt = lbfBuild opts;
in
pkgs.stdenv.mkDerivation {
inherit src version;
pname = name;
outputs = [ "out" "buildjson" ];
buildInputs = [
pkgs.cabal-install
pkgs.jq
];
buildPhase = ''
ln -s ${lbfBuilt} autogen;
ln -s ${lbfBuilt.workdir} .work-dir;
ln -s ${lbfBuilt.buildjson} build.json;
'';

installPhase = ''
cp build.json $buildjson;
echo "Dependencies collected"
cat $buildjson;
mkdir -p $out/src;
cp -r autogen/* $out/src
echo "Files generated"
find $out/;
'';
};
in
build
41 changes: 3 additions & 38 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
(import ./hercules-ci.nix)
(import ./pre-commit.nix)
(import ./docs/build.nix)
(import ./extras/build.nix)
(import ./extras/lbf-nix/build.nix)
(import ./libs/build.nix)
(import ./runtimes/haskell/lbr-prelude/build.nix)
(import ./runtimes/haskell/lbr-plutus/build.nix)
(import ./runtimes/purescript/lbr-prelude/build.nix)
(import ./testsuites/lbt-prelude/api/build.nix)
(import ./testsuites/lbt-prelude/golden/build.nix)
(import ./testsuites/lbt-prelude/lbt-prelude-haskell/build.nix)
(import ./testsuites/lbt-prelude/lbt-prelude-purescript/build.nix)
(import ./testsuites/lbt-plutus/api/build.nix)
(import ./testsuites/lbt-plutus/golden/build.nix)
(import ./testsuites/lbt-plutus/lbt-plutus-haskell/build.nix)
Expand Down Expand Up @@ -174,17 +177,6 @@

# Runtimes

## Prelude runtime - lbr-prelude

### Purescript

lbrPreludePurs = pursFlake (
import ./runtimes/purescript/lbr-prelude/build.nix {
inherit pkgs commonTools;
shellHook = config.pre-commit.installationScript;
}
);

## Plutus runtime - lbr-plutus

### Purescript
Expand All @@ -200,14 +192,6 @@

lbfLibs = {

lbf-prelude-purs = lbfPurescript {
inherit pkgs;
name = "lbf-prelude";
src = ./libs/lbf-prelude;
files = [ "Prelude.lbf" ];
dependencies = [ "lbr-prelude" ];
};

lbf-plutus-purs = lbfPurescript {
inherit pkgs;
name = "lbf-plutus";
Expand Down Expand Up @@ -239,13 +223,6 @@
};

};
lbtPreludePursFlake = pursFlake (
import ./testsuites/lbt-prelude/lbt-prelude-purescript/build.nix {
inherit pkgs commonTools shellHook lbfPurescript;
inherit (lbrPurs) lbr-prelude-purs;
inherit (lbfLibs) lbf-prelude-purs;
}
);

## Plutus test suite - lbt-plutus

Expand All @@ -269,10 +246,6 @@
// compilerFlake.packages
// frontendFlake.packages
// codegenFlake.packages
// lbrPreludePurs.packages
// lbrPlutusPurs.packages
// lbtPreludePursFlake.packages
// lbtPlutusPursFlake.packages
// clis
// lbfLibs;

Expand All @@ -282,20 +255,12 @@
dev-compiler = compilerFlake.devShell;
dev-frontend = frontendFlake.devShell;
dev-codegen = codegenFlake.devShell;
dev-lbr-prelude-purescript = lbrPreludePurs.devShell;
dev-lbr-plutus-purescript = lbrPlutusPurs.devShell;
dev-lbt-prelude-purescript = lbtPreludePursFlake.devShell;
dev-lbt-plutus-purescript = lbtPlutusPursFlake.devShell;
lb = lbEnv;
};

# nix flake check
checks = devShells //
packages //
lbrPreludePurs.checks //
lbrPlutusPurs.checks //
lbtPreludePursFlake.checks //
lbtPlutusPursFlake.checks //
renameAttrs (n: "check-${n}") (
compilerFlake.checks //
frontendFlake.checks //
Expand Down
2 changes: 1 addition & 1 deletion lambda-buffers-codegen/app/Main.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Main (main) where

import Control.Applicative (Alternative (many), optional, (<**>))
import Control.Applicative (Alternative (many), (<**>))
import GHC.IO.Encoding (setLocaleEncoding, utf8)
import LambdaBuffers.Codegen.Cli.Gen (GenOpts (GenOpts))
import LambdaBuffers.Codegen.Cli.GenHaskell qualified as Haskell
Expand Down
Loading

0 comments on commit 5e5c9d0

Please sign in to comment.