Skip to content

Commit

Permalink
build succeeds + added documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
tbenst committed Dec 18, 2019
1 parent fce7a2d commit ca204ad
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 580 deletions.
50 changes: 28 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,49 @@
# Purescript-nix-example
This repo builds the [basic example](https://github.com/slamdata/purescript-halogen/tree/master/examples/basic) from [Halogen](https://github.com/slamdata/purescript-halogen), a type-safe UI library for Purescript, using nix. Several helper tools are used, namely [yarn2nix](https://github.com/moretea/yarn2nix) and [spago2nix](https://github.com/justinwoo/spago2nix).
This repo builds the [basic example](https://github.com/slamdata/purescript-halogen/tree/master/examples/basic) from [Halogen](https://github.com/slamdata/purescript-halogen), a type-safe UI library for Purescript, using [Nix](https://nixos.org/nix/), the purely functional package manager.

This repository follows best-practices for Purescript as discussed by the community in this [discourse thread](https://discourse.purescript.org/t/recommended-tooling-for-purescript-applications-in-2019/948): we use [Yarn](https://yarnpkg.com/lang/en/) for Javascript packages, [Spago](https://github.com/spacchetti/spago) for Purescript packages, and [Parcel](https://parceljs.org/) for bundling.

Two helper tools are used: [yarn2nix](https://github.com/moretea/yarn2nix) and [spago2nix](https://github.com/justinwoo/spago2nix).

## Building this repo with Nix
```
> git clone https://github.com/tbenst/purescript-nix-example
> cd multivac
> nix-build
> nix-shell shell.nix
$ yarn http-server result/dist/
```
Thanks to the declarative nature of nix, it's easy to build this using the same exact version of dependencies that were tested. Simply add the flag `-I nixpkgs=https://github.com/NixOS/nixpkgs/archive/d3f928282c0989232e425d26f0302120a8c7218b.tar.gz` to `nix-build` and `nix-shell`. This package should build as long as the dependencies are hosted online.


## Bootstrapping default.nix
## serving http
```
> nix-shell shell.nix
$ yarn bootstrap-nix
$ yarn install
$ yarn http-server result/dist/
```
Now open a browser, and you should see a single button that toggles on or off.
To serve a version built in `nix-shell`, replace the second line with `http-server dist` as result is a symlink to nix-store created by `nix-build`.

## Hot reloading
Run the following commands in separate terminals using `nix-shell shell.nix`.
Make sure you `yarn install` first.
```
yarn watch
yarn dev
```

For clarity, `yarn bootstrap-nix` is defined in `packages.json` and does the following:
## Bootstrapping
If you're curious, here's how to generate `yarn.lock` and `yarn.nix`:
```
yarn add --dev purescript spago parcel-bundler
yarn add http-server
yarn spago init
yarn spago install halogen behaviors drawing
yarn2nix > yarn.nix
mkdir -p dist
spago2nix generate
> nix-shell shell.nix
$ yarn full-clean
$ yarn bootstrap-nix
```

## Creating your own package

### Simple version
Fork this repo and update name & version in `package.json`, as well as adding
packages to the `bootstrap-nix` script as desired. Then simply execute
`yarn full-clean && yarn bootstrap-nix`.
packages to the `bootstrap-nix` script as desired. Then simply follow
bootstrapping instructions above.

### From scratch
Alternatively, if you'd prefer to start
Expand All @@ -55,10 +63,8 @@ $ yarn2nix > yarn.nix
$ spago2nix generate
```

These are the same commands used by `yarn bootstrap-nix`, which is defined in `packages.json`.

## dev mode:
run in separate terminals using `nix-shell shell.nix`
```
yarn spago build --watch
yarn parcel assets/*.html
```
## Thanks for reading!
Let me know if you have any questions / issues, or more importantly, ideas to
make this better! Pull requests welcome.
12 changes: 7 additions & 5 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ mkYarnPackage rec {
(builtins.map
(x: ''"${x.outPath}/src/**/*.purs"'')
(builtins.attrValues spagoPkgs.inputs))}
mkdir -p $out
cp -r output $out/output
cp -r $src/assets ./
'';

postFixup = ''
mkdir -p $out/dist
${spago}/bin/spago bundle-app --no-install \
--no-build --main Example.Main --to $out/dist/app.js
${nodejs-12_x}/bin/node node_modules/.bin/parcel build assets/*.html --out-dir $out/dist/
--no-build --main Example.Main --to dist/app.js
mkdir -p $out/dist
cp -r dist $out/
ln -s $out/libexec/${name}/node_modules .
${nodejs-12_x}/bin/node node_modules/.bin/parcel \
build assets/*.html --out-dir $out/dist/
'';


Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
"name": "purescript-nix-example",
"version": "1.0.0",
"scripts": {
"bootstrap-nix": "yarn add --dev purescript spago parcel-bundler && yarn spago init && yarn spago install halogen && yarn2nix > yarn.nix && spago2nix generate",
"bootstrap-nix": "yarn add --dev purescript spago parcel-bundler && yarn add http-server && yarn spago init && yarn spago install halogen && yarn2nix > yarn.nix && spago2nix generate",
"full-clean": "rm -rf node_modules output .spago dist/* *.lock spago-packages.nix yarn.nix .spago2nix .cache spago.dhall packages.dhall",
"clean": "rm -rf output .spago dist/*",
"build": "spago build",
"watch": "spago build --watch",
"clean": "rm -rf output .spago dist/* node_modules output .cache .spago2nix",
"watch": "spago bundle-app --watch --main Example.Main --to dist/app.js",
"dev": "yarn parcel assets/*.html",
"bundle": "spago bundle-app --main Example.Main --to dist/app.js && parcel assets/*.html",
"serve": "yarn bundle && http-server dist",
Expand Down
96 changes: 0 additions & 96 deletions spago-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,6 @@ let
installPhase = "ln -s $src $out";
};

"arraybuffer-types" = pkgs.stdenv.mkDerivation {
name = "arraybuffer-types";
version = "v2.0.0";
src = pkgs.fetchgit {
url = "https://github.com/purescript-contrib/purescript-arraybuffer-types.git";
rev = "505aa3de50a1400e5f2fce4a8e7b1a39abaa8fd9";
sha256 = "059a8n49yhl46l1b1j2qj4ichzq6dzj29ajkfvw88npzj5w2rshy";
};
phases = "installPhase";
installPhase = "ln -s $src $out";
};

"arrays" = pkgs.stdenv.mkDerivation {
name = "arrays";
version = "v5.3.1";
Expand All @@ -53,18 +41,6 @@ let
installPhase = "ln -s $src $out";
};

"behaviors" = pkgs.stdenv.mkDerivation {
name = "behaviors";
version = "v8.0.0";
src = pkgs.fetchgit {
url = "https://github.com/paf31/purescript-behaviors.git";
rev = "0fc50530f71a3026bba12dd8df435bdbd9ef076a";
sha256 = "01dxhs5nl7n0w1ygdqaxpi9hxx9bakxm3vggjdfm0hmbhrf0q8my";
};
phases = "installPhase";
installPhase = "ln -s $src $out";
};

"bifunctors" = pkgs.stdenv.mkDerivation {
name = "bifunctors";
version = "v4.0.0";
Expand All @@ -77,18 +53,6 @@ let
installPhase = "ln -s $src $out";
};

"canvas" = pkgs.stdenv.mkDerivation {
name = "canvas";
version = "v4.0.0";
src = pkgs.fetchgit {
url = "https://github.com/purescript-web/purescript-canvas.git";
rev = "bbdc02388f2cc612324aa8c1eef7877666bf1bd2";
sha256 = "0m7a523hr6jn8wg3d1jck1s3bar81gq4ar8bq658sbsysh73n1py";
};
phases = "installPhase";
installPhase = "ln -s $src $out";
};

"catenable-lists" = pkgs.stdenv.mkDerivation {
name = "catenable-lists";
version = "v5.0.1";
Expand All @@ -101,18 +65,6 @@ let
installPhase = "ln -s $src $out";
};

"colors" = pkgs.stdenv.mkDerivation {
name = "colors";
version = "v5.0.0";
src = pkgs.fetchgit {
url = "https://github.com/sharkdp/purescript-colors.git";
rev = "fb582d3626e566371d20d4ef0b8d2795ee80f024";
sha256 = "05bkfqllfpkh7nj0nzgd5p387hlpk0x35nam1i6xm3vzma9csj18";
};
phases = "installPhase";
installPhase = "ln -s $src $out";
};

"console" = pkgs.stdenv.mkDerivation {
name = "console";
version = "v4.2.0";
Expand Down Expand Up @@ -209,18 +161,6 @@ let
installPhase = "ln -s $src $out";
};

"drawing" = pkgs.stdenv.mkDerivation {
name = "drawing";
version = "v4.0.0";
src = pkgs.fetchgit {
url = "https://github.com/paf31/purescript-drawing.git";
rev = "60dafc4cf155896e307a8cc1696d95d53cff8acb";
sha256 = "1ad1f2c7rnhd25hidkbxxxm2mnc1sr3zsjzwivbynwv1fmyv54nz";
};
phases = "installPhase";
installPhase = "ln -s $src $out";
};

"effect" = pkgs.stdenv.mkDerivation {
name = "effect";
version = "v2.0.1";
Expand Down Expand Up @@ -257,18 +197,6 @@ let
installPhase = "ln -s $src $out";
};

"event" = pkgs.stdenv.mkDerivation {
name = "event";
version = "v1.3.0";
src = pkgs.fetchgit {
url = "https://github.com/paf31/purescript-event.git";
rev = "5c3cf3866c01d8faed1763bb9f738db22a5ff4bb";
sha256 = "0kqlywyzhlw108fq684zcq9bfj19jbwkajancm9w4bb3jd522ca8";
};
phases = "installPhase";
installPhase = "ln -s $src $out";
};

"exceptions" = pkgs.stdenv.mkDerivation {
name = "exceptions";
version = "v4.0.0";
Expand All @@ -293,18 +221,6 @@ let
installPhase = "ln -s $src $out";
};

"filterable" = pkgs.stdenv.mkDerivation {
name = "filterable";
version = "v3.0.2";
src = pkgs.fetchgit {
url = "https://github.com/LiamGoodacre/purescript-filterable.git";
rev = "b7bff31576261bc66bf29bfeca7679c2b107d68e";
sha256 = "0pn66mrgrpnlpd1lmywj5f0hbm9mnyjahqj276vz5v2xzlr0q63g";
};
phases = "installPhase";
installPhase = "ln -s $src $out";
};

"foldable-traversable" = pkgs.stdenv.mkDerivation {
name = "foldable-traversable";
version = "v4.1.1";
Expand Down Expand Up @@ -509,18 +425,6 @@ let
installPhase = "ln -s $src $out";
};

"js-timers" = pkgs.stdenv.mkDerivation {
name = "js-timers";
version = "v4.0.1";
src = pkgs.fetchgit {
url = "https://github.com/purescript-contrib/purescript-js-timers.git";
rev = "8206d3d8034357b4665828e52f7ff1b8cb46611e";
sha256 = "1a8092sli7zqw1wflibhjza1ww21dxl7x7r602iazzwh2g70v4dg";
};
phases = "installPhase";
installPhase = "ln -s $src $out";
};

"lazy" = pkgs.stdenv.mkDerivation {
name = "lazy";
version = "v4.0.0";
Expand Down
3 changes: 1 addition & 2 deletions spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ Welcome to a Spago project!
You can edit this file as you like.
-}
{ name = "my-project"
, dependencies =
[ "behaviors", "console", "drawing", "effect", "halogen", "psci-support" ]
, dependencies = [ "console", "effect", "halogen", "psci-support" ]
, packages = ./packages.dhall
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
}
Loading

0 comments on commit ca204ad

Please sign in to comment.