Skip to content

Commit

Permalink
Merge pull request #39 from deemp/develop
Browse files Browse the repository at this point in the history
Add flake, set up nix and cabal builds, cache nix store
  • Loading branch information
fizruk committed May 18, 2023
2 parents bbe78be + a5ffaa5 commit 6f1713a
Show file tree
Hide file tree
Showing 10 changed files with 288 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .envrc
@@ -0,0 +1,2 @@
use flake
eval "$shellHook"
35 changes: 23 additions & 12 deletions .github/workflows/ghcjs.yml
Expand Up @@ -18,24 +18,35 @@ on:

workflow_dispatch: # allow triggering this workflow manually

env:
store: /home/runner/nix

permissions:
contents: write

jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: 📥 Checkout repository
uses: actions/checkout@v3

- name: 🧰 Setup nix (cache)
uses: cachix/install-nix-action@v13
- name: 🧰 Setup nix
uses: cachix/install-nix-action@v20
with:
nix_path: nixpkgs=channel:nixos-unstable
nix_path: nixpkgs=channel:nixpkgs-unstable
extra_nix_config: "store = ${{ env.store }}\nsubstituters = https://cache.nixos.org/ https://cache.iog.io https://nix-community.cachix.org https://miso-haskell.cachix.org \ntrusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs= hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= miso-haskell.cachix.org-1:6N2DooyFlZOHUfJtAx1Q09H0P5XXYzoxxQYiwn6W1e8="
# pinning Nix version
install_url: https://releases.nixos.org/nix/nix-2.13.3/install

- name: 💾 Setup cachix (miso-haskell)
uses: cachix/cachix-action@v10
install_url: https://releases.nixos.org/nix/nix-2.14.1/install
- name: Restore and cache Nix store
uses: actions/cache@v3.3.0
with:
name: miso-haskell
key: ${{ runner.os }}-nix-${{ hashfiles('./flake.nix', './flake.lock') }}
path: ${{ env.store }}
restore-keys: |
${{ runner.os }}-nix-${{ hashfiles('./flake.nix', './flake.lock') }}
${{ runner.os }}-nix-
- name: 🔨 Remove rzk.cabal, lexer and parser generator files
run: |
Expand All @@ -45,20 +56,20 @@ jobs:
- name: 🔨 Build GHCJS version with Nix
run: |
nix-build try-rzk/
nix build .#try-rzk
- name: 🔨 Collect build artifacts
run: |
mkdir -p dist/result/bin
cp -r ./result/bin/try-rzk.jsexe/ dist/result/bin/.
cp -r ${{ env.store }}$(realpath result)/bin/try-rzk.jsexe/ dist/result/bin/.
chmod -R +w dist/
cp try-rzk/playground.html dist/.
- name: "📘 Publish JS \"binaries\" (${{ github.ref_name }})"
if: ${{ github.ref_name != 'main' && github.event_name == 'push' }}
uses: JamesIves/github-pages-deploy-action@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
token: ${{ secrets.GITHUB_TOKEN }}
folder: dist
target-folder: ${{ github.ref_name }}
clean: false
Expand All @@ -67,6 +78,6 @@ jobs:
if: ${{ github.ref_name == 'main' && github.event_name == 'push' }}
uses: JamesIves/github-pages-deploy-action@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
token: ${{ secrets.GITHUB_TOKEN }}
folder: dist
clean: false
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -24,3 +24,5 @@ cabal.project.local~
.HTF/
.ghc.environment.*
docs/site
result
.direnv
68 changes: 67 additions & 1 deletion README.md
Expand Up @@ -132,7 +132,73 @@ nix-build

Now open `playground.html` to see the result. Note that if local GHCJS build is unavailable, `playground.html` will use the [JS file from GitHub Pages](https://fizruk.github.io/rzk/v0.1.0/result/bin/try-rzk.jsexe/all.js) as a fallback.

# References
##### Flake

The flake in this repository allows to build `try-rzk` incrementally and reproducibly.

1. Install `Nix` via single-user [installation](https://nixos.org/download.html#download-nix):

1. Run script

```sh
sh <(curl -L https://nixos.org/nix/install) --no-daemon
```

1. Permanently [enable](https://nixos.wiki/wiki/Flakes#Permanent) flakes

1. Enter the `devShell` with `GHC` (not `GHCJS`). Answer `y` to `Nix` prompts to use binary caches.

```sh
nix develop
```

1. The shell provides `ghc`, `haskell-language-server`, `cabal-install`, `hpack`.

1. (Optionally) Install [direnv](https://direnv.net/) to start the `devShell` when you enter the repository directory.

1. Build `rzk`.

```sh
cabal build
```

1. Enter the `devShell` with `GHCJS`.

```sh
nix develop .#ghcjs
```

1. Build `try-rzk`. This may require ~10 GB of RAM.

```sh
cabal build --ghcjs
```

1. (Optionally) Build `rzk` via `Nix`. The resulting executable will be in `result/bin/rzk`.

```sh
nix build .#rzk
```

1. (Optionally) Run `rzk` via `Nix`.

```sh
nix run .#rzk
```

1. (Optionally) Build `try-rzk` via `Nix`. This may require ~10 GB of RAM. The resulting executable will be in `try-rzk/result/bin/try-rzk.jsexe`.

```sh
nix build .#try-rzk --out-link try-rzk/result
```

1. Open the app in a browser.

```sh
firefox try-rzk/playground.html
```

## References

1. Emily Riehl & Michael Shulman. A type theory for synthetic ∞-categories. Higher Structures 1(1), 147-224. 2017. https://arxiv.org/abs/1705.07442
2. Nikolai Kudasov. E-unification for Second-Order Abstract Syntax. 2023. https://arxiv.org/abs/2302.05815
Expand Down
1 change: 1 addition & 0 deletions cabal.project
@@ -0,0 +1 @@
packages: rzk try-rzk
79 changes: 79 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

107 changes: 107 additions & 0 deletions flake.nix
@@ -0,0 +1,107 @@
{
inputs = {
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
miso = {
url = "github:dmjio/miso/5c66ed20818ce4aff81aaefbd5789007717923eb";
flake = false;
};
};
outputs = inputs: inputs.flake-utils.lib.eachDefaultSystem (system:
let
pkgs = inputs.nixpkgs.legacyPackages.${system};

rzk = "rzk";
try-rzk = "try-rzk";
ghcVersion = "ghc927";

# select a Haskell package set for a specified GHC version
hpkgs = pkgs.haskell.packages.${ghcVersion};

inherit (pkgs.haskell.lib) overrideCabal;

# Provide overrides
# https://nixos.wiki/wiki/Haskell#Overrides
# An override should include a local package into the Haskell package set
override = {
overrides = self: super: {
${rzk} = overrideCabal (self.callCabal2nix rzk ./${rzk} { }) (x: {
librarySystemDepends = [ pkgs.alex pkgs.happy ] ++ (x.librarySystemDepends or [ ]);
});
${try-rzk} = overrideCabal (self.callCabal2nix try-rzk ./${try-rzk} { }) (x: {
executableSystemDepends = [ self.ghcjs-prim self.ghcjs-base ] ++ (x.executableSystemDepends or [ ]);
});
};
};
hpkgs_ = hpkgs.override override;

# Get all dependencies of local Haskell packages excluding these local packages
# This approach is useful for cases when a local package A depends on a local package B
# In this case, package B won't be built by Nix as a dependency of A
getHaskellPackagesDeps = someHaskellPackages: let l = pkgs.lib.lists; in (l.subtractLists someHaskellPackages (l.concatLists (map (package: l.concatLists (__attrValues package.getCabalDeps)) someHaskellPackages)));

# build a GHC with the dependencies of local Haskell packages
ghcForPackages = localHaskellPackageNames: hpkgs_.ghcWithPackages (ps: (getHaskellPackagesDeps (map (x: ps.${x}) localHaskellPackageNames) ++ [ ps.haskell-language-server ])); # Why provide HLS here - https://github.com/NixOS/nixpkgs/issues/225895#issuecomment-1509991742

# GHC with dependencies of local Haskell packages
ghc = ghcForPackages [ rzk try-rzk ];

# tools that should be available in a development shell
tools = [
pkgs.cabal-install
pkgs.hpack
# haskell-language-server is already available as a GHC package
ghc
];

misoNix = (import "${inputs.miso.outPath}/default.nix" { inherit system; });
pkgsMiso = misoNix.pkgs;

# TODO add jsaddle version of the app
# https://github.com/dmjio/miso/tree/master/sample-app-jsaddle
# try-rzk-dev =
# let
# pkgsDev = pkgsMiso.haskell.packages.ghc865;
# rzk = pkgsDev.callPackage rzk/rzk.nix { inherit (pkgs) hpack; };
# in
# pkgsDev.callCabal2nix try-rzk ./${try-rzk} { miso = misoNix.miso-jsaddle; rzk = rzk; };

try-rzk-exe =
let
pkgsRelease = pkgsMiso.haskell.packages.ghcjs;
rzk = pkgsRelease.callPackage rzk/rzk.nix { inherit (pkgs) hpack; };
in
pkgsRelease.callCabal2nix try-rzk ./${try-rzk} { rzk = rzk; };

packages = {
rzk = hpkgs_.${rzk};
try-rzk = try-rzk-exe;
};

devShells = {
default = pkgs.mkShell {
shellHook = "export LANG=C.utf8";
buildInputs = tools;
};
ghcjs = try-rzk-exe.env.overrideAttrs (old: {
buildInputs = old.buildInputs ++ [ pkgs.cabal-install pkgs.hpack ];
});
};
in
{
inherit packages devShells;
});

nixConfig = {
extra-substituters = [
"https://miso-haskell.cachix.org"
"https://nix-community.cachix.org"
"https://cache.iog.io"
];
extra-trusted-public-keys = [
"miso-haskell.cachix.org-1:6N2DooyFlZOHUfJtAx1Q09H0P5XXYzoxxQYiwn6W1e8="
"nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="
"hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ="
];
};
}
2 changes: 1 addition & 1 deletion rzk/rzk.cabal
@@ -1,6 +1,6 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.35.1.
-- This file has been generated from package.yaml by hpack version 0.35.2.
--
-- see: https://github.com/sol/hpack
--
Expand Down
4 changes: 2 additions & 2 deletions rzk/rzk.nix
@@ -1,5 +1,5 @@
{ mkDerivation, array, base, bifunctors, hpack, lib, mtl
, template-haskell
, template-haskell, alex, happy
}:
mkDerivation {
pname = "rzk";
Expand All @@ -8,7 +8,7 @@ mkDerivation {
isLibrary = true;
isExecutable = true;
libraryHaskellDepends = [
array base bifunctors mtl template-haskell
array base bifunctors mtl template-haskell alex happy
];
libraryToolDepends = [ hpack ];
executableHaskellDepends = [
Expand Down
5 changes: 4 additions & 1 deletion try-rzk/try-rzk.cabal
Expand Up @@ -9,5 +9,8 @@ executable try-rzk
main-is: Main.hs
ghcjs-options:
-dedupe
build-depends: base, miso, rzk, ghcjs-base, ghcjs-prim
build-depends: base, miso, rzk
if impl(ghcjs)
build-depends:
ghcjs-base, ghcjs-prim
default-language: Haskell2010

0 comments on commit 6f1713a

Please sign in to comment.