···1+2+# `dhallPackageToNix` is a utility function to take a Nixpkgs Dhall package
3+# (created with a function like `dhallPackages.buildDhallDirectoryPackage`)
4+# and read it in as a Nix expression.
5+#
6+# This function is similar to `dhallToNix`, but takes a Nixpkgs Dhall package
7+# as input instead of raw Dhall code.
8+#
9+# Note that this uses "import from derivation" (IFD), meaning that Nix will
10+# perform a build during the evaluation phase if you use this
11+# `dhallPackageToNix` utility. It is not possible to use `dhallPackageToNix`
12+# in Nixpkgs, since the Nixpkgs Hydra doesn't allow IFD.
13+14+{ stdenv, dhall-nix }:
15+16+dhallPackage:
17+ let
18+ drv = stdenv.mkDerivation {
19+ name = "dhall-compiled-package.nix";
20+21+ buildCommand = ''
22+ # Dhall requires that the cache is writable, even if it is never written to.
23+ # We copy the cache from the input package to the current directory and
24+ # set the cache as writable.
25+ cp -r "${dhallPackage}/.cache" ./
26+ export XDG_CACHE_HOME=$PWD/.cache
27+ chmod -R +w ./.cache
28+29+ dhall-to-nix <<< "${dhallPackage}/binary.dhall" > $out
30+ '';
31+32+ nativeBuildInputs = [ dhall-nix ];
33+ };
34+35+ in
36+ import drv