-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
402 lines (367 loc) · 15.3 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
{
description = "Mlabs Plutus Template";
nixConfig = {
allow-import-from-derivation = "true";
};
inputs = {
cardano-transaction-lib.url = "github:Plutonomicon/cardano-transaction-lib/v5.0.0";
mlabs-tooling.url = "github:mlabs-haskell/mlabs-tooling.nix";
flake-parts.url = "github:hercules-ci/flake-parts";
pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix";
ply.url = "github:mlabs-haskell/ply?ref=0.5.0";
plutarch.url = "github:Plutonomicon/plutarch-plutus?ref=95e40b42a1190191d0a07e3e4e938b72e6f75268";
psm.url = "github:mlabs-haskell/plutus-simple-model";
nixpkgs-oldctl.follows = "cardano-transaction-lib/nixpkgs";
dream2nix.url = "github:nix-community/dream2nix";
hercules-ci-effects.url = "github:hercules-ci/hercules-ci-effects";
};
outputs = inputs@{ self, nixpkgs, cardano-transaction-lib, mlabs-tooling, flake-parts, nixpkgs-oldctl, dream2nix, hercules-ci-effects, ... }:
let
# We leave it to just linux to be able to run `nix flake check` on linux,
# see bug https://github.com/NixOS/nix/issues/4265
systems = [ "x86_64-linux" ];
# systems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
project-name = "plutus-scaffold";
# ONCHAIN / Plutarch
onchain = mlabs-tooling.lib.mkFlake { inherit self; }
{
imports = [
(mlabs-tooling.lib.mkHaskellFlakeModule1 {
project.src = (builtins.path {
path = ./onchain;
name = "${project-name}-src";
});
project.extraHackage = [
"${inputs.ply}/ply-core"
"${inputs.ply}/ply-plutarch"
"${inputs.plutarch}"
"${inputs.plutarch}/plutarch-extra"
"${inputs.psm}/psm"
"${inputs.psm}/cardano-simple"
];
})
];
inherit systems;
perSystem = { pkgs, config, ... }:
let
# TODO: rename mlabs-plutus-template to plutus-scaffold
exporter = config.packages."mlabs-plutus-template-onchain:exe:exporter";
script-exporter =
pkgs.runCommandLocal "script-exporter" { }
''
ln -s ${exporter}/bin/exporter $out
'';
exported-scripts =
pkgs.runCommand "exported-scripts" { }
''
${exporter}/bin/exporter $out
'';
in
{
packages = {
inherit script-exporter exported-scripts;
};
};
};
in
flake-parts.lib.mkFlake { inherit inputs; } {
imports = [
(import ./pre-commit.nix)
hercules-ci-effects.flakeModule
];
hercules-ci.github-pages.branch = "main";
inherit systems;
perSystem = { system, config, ... }:
let
# ctl overlays
pkgs = import nixpkgs {
inherit system;
overlays = [
cardano-transaction-lib.overlays.purescript
cardano-transaction-lib.overlays.runtime
cardano-transaction-lib.overlays.spago
];
};
# Derviation producing directory like that:
# mkDir {"offchain/src" : [some_derivation, other_derivation];
# "offchain": [./offchain]}
# =
# /
# - offchain/
# - src/
# - some_derivation
# - other_derivation
# - ...
# used instead of symlinkJoin, because https://github.com/nix-community/dream2nix/issues/520
mkDir = name: dir: pkgs.runCommand name { } (
''
mkdir res
'' +
(pkgs.lib.concatMapStrings
(path:
let
copyCmd =
if path == "" then
(drv: ''
cp -r ${drv}/* res
'')
else
(drv: ''
mkdir -p res/${path}
chmod +w res/${path}
cp -r ${drv}/* res/${path}
'');
in
pkgs.lib.concatMapStrings copyCmd (dir.${path})
)
(builtins.attrNames dir))
+ ''
mkdir $out
cp -r res/* $out
''
);
offchain =
let
# TAG: compiled-scripts
projectName = "${project-name}-offchain";
# Derivation producing:
# /
# - offchain
# - compiled-scripts
# This should match your local development source tree
offchain-src-w-scripts = mkDir "${projectName}-src-w-scripts" {
"offchain" = [
(builtins.path {
path = ./offchain;
name = "${projectName}-src";
})
];
"compiled-scripts" = [ onchain.packages.${system}.exported-scripts ];
};
in
pkgs.purescriptProject rec {
inherit pkgs;
inherit projectName;
# If warnings generated from project source files will trigger a build error
strictComp = false;
# We extend the offchain source with compiled-scripts to run tests with offchain.runPlutipTests
src = offchain-src-w-scripts;
packageJson = "${src}/offchain/package.json";
packageLock = "${src}/offchain/package-lock.json";
spagoPackages = "${src}/offchain/spago-packages.nix";
shell = {
withRuntime = true;
packageLockOnly = true;
packages = with pkgs; [
fd
nodePackages.eslint
nodePackages.prettier
];
};
};
# Bundles with `spago bundle-module`, sharing the built project with the offchain purescriptProject
bundlePsModule = main:
let
name = "${project-name}-bundle-${main}";
# project's source + spago output/
project = offchain.compiled;
in
pkgs.runCommand name
{
nativeBuildInputs = [
project
offchain.purs
pkgs.easy-ps.spago
];
}
# TAG: OFFCHAIN.JS
''
export HOME="$TMP"
cp -r ${project}/* .
chmod -R +rwx .
spago bundle-module --no-install --no-build -m "${main}" \
--to Offchain.js
mkdir $out
cp Offchain.js $out
'';
# TAG: SwitchContractParams
# The different bundles used to differentiate by ContractParams
offchain-api-bundles = {
OffchainApiLocal = (bundlePsModule "OffchainApiLocal");
OffchainApiDeployment = (bundlePsModule "OffchainApiDeployment");
};
# Derivation producing frontend source together with the offchain bundle in src:
# This should match your local development source tree
frontend-full-src = offchain-api-bundle: mkDir "frontend-full-src" {
"" = [ ./frontend ];
"src" = [ offchain-api-bundle ];
};
# frontend flake outputs, with offchain-api-bundle included into src
frontend = offchain-api-bundle: dream2nix.lib.makeFlakeOutputs {
systems = [ system ];
config.projectRoot = ./frontend;
# avoid symlinks in the source
source = frontend-full-src offchain-api-bundle;
projects.${project-name} = {
name = "${project-name}";
relPath = "";
subsystem = "nodejs";
translator = "package-lock";
builder = "granular-nodejs";
};
};
# Derivation producing frontend static website bundle.
# Parametrized by the offchain bundle to include (to differentiate between ContractParams).
frontend-bundle = offchain-api-bundle:
let
frontend' = frontend offchain-api-bundle;
built-frontend = "${frontend'.packages.${system}.${project-name}}/lib/node_modules";
# To produce the webpack bundle we need to include compiled-scripts in source
frontend-full-src-w-scripts = mkDir "frontend-full-src-w-scripts" {
# this below is almost like ./frontend, but with offchain bundle and node_modules
"frontend" = [ "${built-frontend}/${project-name}" ];
# TAG: compiled-scripts
"compiled-scripts" = [ onchain.packages.${system}.exported-scripts ];
};
frontend-shell = frontend'.devShells.${system}.default;
in
pkgs.runCommand "frontend-bundle"
{
buildInputs = frontend-shell.buildInputs; # just nodejs
}
# We use the binaries and node_modules (included in the package) provided by frontend's nix
''
export HOME="$TMP"
export PATH="${built-frontend}/.bin:$PATH"
cp -r ${frontend-full-src-w-scripts}/* .
chmod -R +w frontend
cd frontend
npm run build-bundle
ls -a build
mkdir $out
cp -r build/* $out
'';
# App that serves a static website - call with the static bundle like frontend-bundle-local.
run-frontend-app = webapp:
let
port = 8080;
name = "run-${project-name}-app";
run = pkgs.writeShellApplication {
inherit name;
runtimeInputs = [
pkgs.nodePackages.http-server
];
text = ''
http-server ${webapp} --port ${builtins.toString port}
# --cors='*'
'';
};
in
{
type = "app";
program = "${run}/bin/${name}";
};
# Used to add pre-commit packages and shell hook to the other project shells
mergeShells = devshell-1: devshell-2: pkgs.mkShell {
packages = [ ];
inputsFrom = [ devshell-1 devshell-2 ];
shellHook = devshell-1.shellHook + devshell-2.shellHook;
};
# haskell development shell, with pre-commit shellhook
onchain-devshell = mergeShells onchain.devShells.${system}.default config.pre-commit.devShell;
# TAG: NIX_NODE_PATH
# purescript development shell, with pre-commit shellhook
offchain-devshell = mergeShells offchain.devShell config.pre-commit.devShell;
# node development shell, with pre-commit shellhook.
# Optional, alternatively install the same node+npm version from somewhere else
frontend-devshell =
mergeShells
# For shell it doesn't which offchain bundle gets included in src
(frontend offchain-api-bundles.OffchainApiLocal).devShells.${system}.default
config.pre-commit.devShell;
# older ctl's nixpkgs, quick fix of ctl-runtime,
# should be unneeded after https://github.com/Plutonomicon/cardano-transaction-lib/pull/1496
pkgs-oldctl = import nixpkgs-oldctl {
inherit system;
overlays = [
cardano-transaction-lib.overlays.purescript
cardano-transaction-lib.overlays.runtime
cardano-transaction-lib.overlays.spago
];
};
# purescriptProject provides app that serves documentation.
# Because we don't pass just ./offchain as src the doc app breaks, this is a workaround.
# https://github.com/Plutonomicon/cardano-transaction-lib/issues/1500
docs = (pkgs.purescriptProject rec {
inherit pkgs;
projectName = "plutus-scaffold-offchain";
strictComp = false;
src = ./offchain;
packageJson = "${src}/package.json";
packageLock = "${src}/package-lock.json";
spagoPackages = "${src}/spago-packages.nix";
}).launchSearchablePursDocs { port = 9090; };
in
{
packages =
onchain.packages.${system}
//
rec {
# TAG: SwitchContractParams
frontend-bundle-local = frontend-bundle offchain-api-bundles.OffchainApiLocal;
frontend-bundle-deployment = frontend-bundle offchain-api-bundles.OffchainApiDeployment;
default = frontend-bundle-local;
}
// offchain-api-bundles
;
checks =
{
psm-tests = onchain.checks.${system}."mlabs-plutus-template-onchain:test:psm-test";
plutip-tests = offchain.runPlutipTest { testMain = "Test.Scaffold.Main"; };
};
devShells = {
frontend = frontend-devshell;
onchain = onchain-devshell;
offchain = offchain-devshell;
};
apps =
{
# Run `nix run .#docs` and open `localhost:9090` to browse this projects documentation
inherit docs;
# Ctl's docs, at `localhost:8080`.
ctl-docs = cardano-transaction-lib.apps.${system}.docs;
# nix run .#script-exporter -- compiled-scripts
script-exporter = {
type = "app";
program = self.packages.${system}.script-exporter.outPath;
};
# TAG: SwitchContractParams
# The two below are buggy. For development use `npm run start` from frontend.
run-frontend-app-local = run-frontend-app config.packages.frontend-bundle-local;
run-frontend-app-deployment = run-frontend-app config.packages.frontend-bundle-deployment;
ctl-runtime = pkgs-oldctl.launchCtlRuntime { };
ctl-blockfrost-runtime = pkgs-oldctl.launchCtlRuntime { blockfrost.enable = true; };
};
hercules-ci.github-pages.settings.contents = config.packages.frontend-bundle-deployment;
};
flake = {
# On CI, build only on available systems, to avoid errors about systems without agents.
herculesCi.ciSystems = [ "x86_64-linux" ];
# Used by `nix flake init -t <flake>`
# templates.default = {
# path = (builtins.path {
# path = ./.;
# name = "${project-name}";
# });
# description = "Mlabs Plutus project template";
# welcomeText = ''
# Welcome in the Plutus scaffold!
# Check out the "Getting Started" section of README.
# To enter the Nix environment, run `nix develop .#onchain` or `nix develop .#offchain` respectively.
# Frontend is managed by npm, see the `frontend/package.json` scripts field.
# For offchain consult [ctl docs](https://github.com/Plutonomicon/cardano-transaction-lib/tree/develop/doc).
# '';
# };
};
};
}