jeslie0 / mkSpagoDerivation

Reproducible PureScript projects with Nix
https://github.com/jeslie0/mkSpagoDerivation
MIT License
16 stars 1 forks source link
flakes nix purescript spago

+title: mkSpagoDerivation

+author: James Leslie

[[https://img.shields.io/badge/built%20for-PureScript-1d222d.svg]][[https://img.shields.io/github/license/jeslie0/mkelmderivation.svg]][[https://img.shields.io/github/actions/workflow/status/jeslie0/mkSpagoDerivation/run_tests.yaml.svg]][[https://img.shields.io/github/v/release/jeslie0/mkSpagoDerivation.svg]]

This flake provides an unopinionated approach to packaging [[https://github.com/purescript/purescript][PureScript]] projects with the [[https://github.com/NixOS/nixpkgs][Nix]] package manager. Leveraging [[https://github.com/purescript/registry][PureScript's registry]] and the new [[https://github.com/purescript/spago][spago@next]], this repository facilitates the creation of reproducible builds for PureScript projects with minimal complexity. This works by treating a PureScript project's =spago.lock= as a single source of truth for dependencies.

Importantly, the =spago.lock= file is required for ~mkSpagoDerivation~ to determine your project's dependencies. Optionally, if you provide your project's =spago.yaml= file, then your project's name will be extracted and used. You can provide a =name= or =pname= argument instead.

Here is a simple =flake.nix= that builds a PureScript project.

+begin_src nix

{ inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; flake-utils.url = "github:numtide/flake-utils"; mkSpagoDerivation.url = "github:jeslie0/mkSpagoDerivation"; ps-overlay.url = "github:thomashoneyman/purescript-overlay"; };

outputs = { self, nixpkgs, flake-utils, mkSpagoDerivation }: flake-utils.lib.eachDefaultSystem ( system: let pkgs = import nixpkgs { inherit system; overlays = [ mkSpagoDerivation.overlays.default ps-overlay.overlays.default ]; }; in { packages.default = pkgs.mkSpagoDerivation { spagoYaml = ./spago.yaml; spagoLock = ./spago.lock; src = ./.; nativeBuildInputs = [ pkgs.purs-unstable pkgs.spago-unstable pkgs.esbuild ]; version = "0.1.0"; buildPhase = "spago bundle"; installPhase = "mkdir $out; cp index.js $out"; }; } ); }

+end_src

The only attribute ~mkSpagoDerivation~ requires is ~src~. The ~spagoYaml~ attribute will default to =${src}/spago.yaml= and likewise, ~spagoLock~ defaults to =${src}/spago.lock=. Everything else is passed into ~mkDerivation~. There are no assumptions about the which version of the compiler is used - you must specify which copy of Spago and PureScript you want to use. Dependencies can be added through ~buildInputs~ or ~nativeBuildInputs~. The following example demonstrates this, by using [[https://github.com/aristanetworks/purescript-backend-optimizer][purescript-backend-optimizer]] and using the unstable PureScript compiler from the PureScript Overlay.

+begin_src nix

{ inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; flake-utils.url = "github:numtide/flake-utils"; ps-overlay.url = "github:thomashoneyman/purescript-overlay"; mkSpagoDerivation.url = "github:jeslie0/mkSpagoDerivation"; };

outputs = { self, nixpkgs, flake-utils, ps-overlay, mkSpagoDerivation }: flake-utils.lib.eachDefaultSystem ( system: let pkgs = import nixpkgs { inherit system; overlays = [ mkSpagoDerivation.overlays.default ps-overlay.overlays.default ]; }; in { packages.default = pkgs.mkSpagoDerivation { spagoYaml = ./spago.yaml; spagoLock = ./spago.lock; src = ./.; version = "0.1.0"; nativeBuildInputs = [ pkgs.esbuild pkgs.purs-backend-es pkgs.purs-unstable pkgs.spago-unstable ]; buildPhase = "spago build && purs-backend-es bundle-app --no-build --minify --to=main.min.js"; installPhase = "mkdir $out; cp -r main.min.js $out"; }; } ); }

+end_src

Some other useful functions are exposed. One is ~buildDotSpago~, which builds a project's =.spago= directory. It takes an attribute set ~{ spagoLock, src }~, which respectively are the paths to the project's ~spago.lock~ and the root of the project. Another useful function is ~buildSpagoNodeJs~, which builds the =spago-nodejs= directory, typically located in the user's =.cache= directory.