1{
2 dhall,
3 dhall-docs,
4 haskell,
5 lib,
6 lndir,
7 runCommand,
8 writeText,
9}:
10
11{
12 name,
13
14 # Expressions to add to the cache before interpreting the code
15 dependencies ? [ ],
16
17 # A Dhall expression
18 #
19 # Carefully note that the following expression must be devoid of uncached HTTP
20 # imports. This is because the expression will be evaluated using an
21 # interpreter with HTTP support disabled, so all HTTP imports have to be
22 # protected by an integrity check that can be satisfied via cached
23 # dependencies.
24 #
25 # You can add a dependency to the cache using the preceding `dependencies`
26 # option
27 code,
28
29 # `buildDhallPackage` can include both a "source distribution" in
30 # `source.dhall` and a "binary distribution" in `binary.dhall`:
31 #
32 # * `source.dhall` is a dependency-free αβ-normalized Dhall expression
33 #
34 # * `binary.dhall` is an expression of the form: `missing sha256:${HASH}`
35 #
36 # This expression requires you to install the cache product located at
37 # `.cache/dhall/1220${HASH}` to successfully resolve
38 #
39 # By default, `buildDhallPackage` only includes "binary.dhall" to conserve
40 # space within the Nix store, but if you set the following `source` option to
41 # `true` then the package will also include `source.dhall`.
42 source ? false,
43
44 # Directory to generate documentation for (i.e. as the `--input` option to the
45 # `dhall-docs` command.)
46 #
47 # If `null`, then no documentation is generated.
48 documentationRoot ? null,
49
50 # Base URL prepended to paths copied to the clipboard
51 #
52 # This is used in conjunction with `documentationRoot`, and is unused if
53 # `documentationRoot` is `null`.
54 baseImportUrl ? null,
55}:
56
57let
58 # HTTP support is disabled in order to force that HTTP dependencies are built
59 # using Nix instead of using Dhall's support for HTTP imports.
60 dhallNoHTTP = haskell.lib.compose.appendConfigureFlag "-f-with-http" dhall;
61
62 file = writeText "${name}.dhall" code;
63
64 cache = ".cache";
65
66 data = ".local/share";
67
68 cacheDhall = "${cache}/dhall";
69
70 dataDhall = "${data}/dhall";
71
72 sourceFile = "source.dhall";
73
74in
75runCommand name { inherit dependencies; } ''
76 set -eu
77
78 mkdir -p ${cacheDhall}
79
80 for dependency in $dependencies; do
81 ${lndir}/bin/lndir -silent $dependency/${cacheDhall} ${cacheDhall}
82 done
83
84 export XDG_CACHE_HOME=$PWD/${cache}
85
86 mkdir -p $out/${cacheDhall}
87
88 ${dhallNoHTTP}/bin/dhall --alpha --file '${file}' > $out/${sourceFile}
89
90 SHA_HASH=$(${dhallNoHTTP}/bin/dhall hash <<< $out/${sourceFile})
91
92 HASH_FILE="''${SHA_HASH/sha256:/1220}"
93
94 ${dhallNoHTTP}/bin/dhall encode --file $out/${sourceFile} > $out/${cacheDhall}/$HASH_FILE
95
96 echo "missing $SHA_HASH" > $out/binary.dhall
97
98 ${lib.optionalString (!source) "rm $out/${sourceFile}"}
99
100 ${lib.optionalString (documentationRoot != null) ''
101 mkdir -p $out/${dataDhall}
102
103 XDG_DATA_HOME=$out/${data} ${dhall-docs}/bin/dhall-docs --output-link $out/docs ${
104 lib.cli.toGNUCommandLineShell { } {
105 base-import-url = baseImportUrl;
106
107 input = documentationRoot;
108
109 package-name = name;
110 }
111 }
112 ''}
113''