commit 8a53607ed9a5018830720e9a009e960afd13b59b Author: Rachel Lambda Samuelsson Date: Sun Jul 23 01:17:46 2023 +0200 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9bd9b01 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.~undo-tree~ diff --git a/example/example.nix b/example/example.nix new file mode 100644 index 0000000..a83ff0b --- /dev/null +++ b/example/example.nix @@ -0,0 +1,27 @@ +{ pkgs, ... }: +import ../psDeriver.nix { + inherit pkgs; + deps = [ + { + name = "console"; + version = "v6.0.0"; + gitUrl = https://github.com/purescript/purescript-console.git; + } + { + name = "effect"; + version = "v4.0.0"; + gitUrl = https://github.com/purescript/purescript-effect.git; + } + { + name = "prelude"; + version = "v6.0.1"; + gitUrl = https://github.com/purescript/purescript-prelude.git; + } + ]; + out = { + name = "example"; + version = "0.1.0"; + src = ./src; + executable = "example"; + }; +} diff --git a/example/shell.nix b/example/shell.nix new file mode 100644 index 0000000..171312b --- /dev/null +++ b/example/shell.nix @@ -0,0 +1,7 @@ +let pkgs = import {}; + example = import ./example.nix { inherit pkgs; }; +in pkgs.mkShell { + packages = [ + example + ]; +} diff --git a/example/src/Main.purs b/example/src/Main.purs new file mode 100644 index 0000000..ff2e1db --- /dev/null +++ b/example/src/Main.purs @@ -0,0 +1,10 @@ +module Main where + +import Prelude + +import Effect (Effect) +import Effect.Console (log) + +main :: Effect Unit +main = do + log "Hej!" diff --git a/psDeriver.nix b/psDeriver.nix new file mode 100644 index 0000000..b830b26 --- /dev/null +++ b/psDeriver.nix @@ -0,0 +1,75 @@ +# pkgs: nixpkgs +# +# nodejs?: node derivation to use for running executables +# +# deps: +# { +# name +# version +# src +# } +# +# or +# +# { +# name +# version +# gitUrl +# } +# +# out: +# +# { +# name +# version +# src +# executable? +# } + + +{ pkgs +, nodejs ? pkgs.nodejs +, deps +, out }: +with pkgs; with builtins; +let inputs = + map (d: stdenv.mkDerivation { + name = d.name; + version = d.version; + src = d.src or fetchGit { + url = d.gitUrl; + ref = "refs/tags/${d.version}"; + }; + installPhase = ''cp -r "$src" "$out"''; + }) + deps; +in stdenv.mkDerivation { + pname = out.name; + version = out.version; + src = out.src; + + buildInputs = [ purescript ] ++ inputs; + + buildPhase = '' + purs compile "$src/**/*.purs" ${concatStringsSep " " + (map (x: ''"${x}/src/**/*.purs"'') inputs)} + '' + (if hasAttr "executable" out then '' + cat > output/runMain.js <<-EOF + import * as Main from "$out/lib/Main/index.js" + Main.main() + EOF + + cat > ${out.executable} <<-EOF + #!/bin/sh + exec ${nodejs}/bin/node "$out/lib/runMain.js" "$@" + EOF + '' else ""); + + installPhase = '' + mkdir -p "$out/lib" + mv output/* "$out/lib" + '' + (if hasAttr "executable" out then '' + mkdir -p "$out/bin" + install -Dm 755 ${out.executable} "$out/bin" + '' else ""); +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..89d00fe --- /dev/null +++ b/readme.md @@ -0,0 +1,32 @@ +# psDeriver + +`psDeriver` is a file which lets one define postscript derivations simply. It handles specified dependencies (no dependency tree resolution), and can optionally generate an executable which runs the main function if present. + +## Usage + +See `example/example.nix` for an example. + +Import `psDeriver.nix` and pass the following arguments: + +### `pkgs` +The usual nixpkgs + +### `nodejs` (optional) +Nodejs derivation to use for running executables. If not specified then `pkgs.nodejs` will be used. + +### `deps` +A list of dependencies in one of the following formats: + +* `{ name; version; src; }` + +* `{ name; version; gitUrl; }` + +If the second format is used then version will be used as a tag of the git repository. This is the style demonstrated in `example/example.nix`. + +### `out` +Information about the output derivation, given in the following attributes. + +* `name` - name of package +* `version` - version of package +* `src` - source directory for purescript code (usually `./src`) +* `executable` (optional) - if specified an executable with this name will be created which calls `Main.main`.