All Projects → tbenst → purescript-nix-example

tbenst / purescript-nix-example

Licence: MIT license
Build a simple Halogen app using Nix

Programming Languages

Nix
1067 projects
Dhall
116 projects

Purescript-nix-example

This repo builds the basic example from Halogen, a type-safe UI library for Purescript, using Nix, the purely functional package manager.

This repository follows best-practices for Purescript as discussed by the community in this discourse thread: we use Yarn for Javascript packages, Spago for Purescript packages, and Parcel for bundling.

Two helper tools are used: yarn2nix and spago2nix. Instructions have been tested on NixOS and Ubuntu.

Building this repo with Nix

> git clone https://github.com/tbenst/purescript-nix-example
> cd purescript-nix-example
> nix-build -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/d3f928282c0989232e425d26f0302120a8c7218b.tar.gz

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.

Serving http

> nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/d3f928282c0989232e425d26f0302120a8c7218b.tar.gz shell.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 (using -I flag if appropriate). Make sure you yarn install first.

yarn watch
yarn dev

Bootstrapping

If you're curious, here's how to generate yarn.lock and yarn.nix:

> 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 follow bootstrapping instructions above.

From scratch

Alternatively, if you'd prefer to start from a clean git history, all you need is shell.nix and default.nix to get started:

> nix-shell shell.nix
$ yarn add --dev parcel-bundler

Now add a "name" and "version" field to package.json or else later steps will fail.

> nix-shell shell.nix
$ yarn add http-server
$ spago init
$ spago install halogen
$ yarn2nix > yarn.nix
$ spago2nix generate

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

Thanks for reading!

Let me know if you have any questions / issues, or more importantly, ideas to make this better! Pull requests welcome.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].