about summary refs log tree commit diff
path: root/pkgs
diff options
context:
space:
mode:
author(cdep)illabout <cdep.illabout@gmail.com>2021-10-25 14:49:51 +0900
committer(cdep)illabout <cdep.illabout@gmail.com>2021-10-25 15:04:50 +0900
commita201d9744feeb6db3db5ba41b1f391f5878937b1 (patch)
treeb197a5188ea46e2a344d9e7a08756402370f4e15 /pkgs
parentccb59f86d85dbca072c64cd5e065b7334fbbed5f (diff)
dhallPackages.buildDhallUrl: add this function
`buildDhallUrl` is a function to download a Dhall remote import.

It is introduced to `dhall-to-nixpkgs` in
https://github.com/dhall-lang/dhall-haskell/pull/2318.
Diffstat (limited to 'pkgs')
-rw-r--r--pkgs/development/interpreters/dhall/build-dhall-url.nix98
-rw-r--r--pkgs/top-level/dhall-packages.nix4
2 files changed, 102 insertions, 0 deletions
diff --git a/pkgs/development/interpreters/dhall/build-dhall-url.nix b/pkgs/development/interpreters/dhall/build-dhall-url.nix
new file mode 100644
index 0000000000000..54aa775ef1448
--- /dev/null
+++ b/pkgs/development/interpreters/dhall/build-dhall-url.nix
@@ -0,0 +1,98 @@
+{ cacert, dhall, dhall-docs, haskell, lib, runCommand }:
+
+# `buildDhallUrl` is similar to `buildDhallDirectoryPackage` or
+# `buildDhallGitHubPackage`, but instead builds a Nixpkgs Dhall package
+# based on a hashed URL.  This will generally be a URL that has an integrity
+# check in a Dhall file.
+#
+# Similar to `buildDhallDirectoryPackage` and `buildDhallGitHubPackage`, the output
+# of this function is a derivation that has a `binary.dhall` file, along with
+# a `.cache/` directory with the actual contents of the Dhall file from the
+# suppiled URL.
+#
+# This function is primarily used by `dhall-to-nixpkgs directory --fixed-output-derivations`.
+
+{ # URL of the input Dhall file.
+  # example: "https://raw.githubusercontent.com/cdepillabout/example-dhall-repo/c1b0d0327146648dcf8de997b2aa32758f2ed735/example1.dhall"
+  url
+
+  # Nix hash of the input Dhall file.
+  # example: "sha256-ZTSiQUXpPbPfPvS8OeK6dDQE6j6NbP27ho1cg9YfENI="
+, hash
+
+  # Dhall hash of the input Dhall file.
+  # example: "sha256:6534a24145e93db3df3ef4bc39e2ba743404ea3e8d6cfdbb868d5c83d61f10d2"
+, dhall-hash
+
+  # Name for this derivation.
+, name ? (baseNameOf url + "-cache")
+
+  # `buildDhallUrl` can include both a "source distribution" in
+  # `source.dhall` and a "binary distribution" in `binary.dhall`:
+  #
+  # * `source.dhall` is a dependency-free αβ-normalized Dhall expression
+  #
+  # * `binary.dhall` is an expression of the form: `missing sha256:${HASH}`
+  #
+  #   This expression requires you to install the cache product located at
+  #   `.cache/dhall/1220${HASH}` to successfully resolve
+  #
+  # By default, `buildDhallUrl` only includes "binary.dhall" to conserve
+  # space within the Nix store, but if you set the following `source` option to
+  # `true` then the package will also include `source.dhall`.
+, source ? false
+}:
+
+let
+  # HTTP support is disabled in order to force that HTTP dependencies are built
+  # using Nix instead of using Dhall's support for HTTP imports.
+  dhallNoHTTP = haskell.lib.appendConfigureFlag dhall "-f-with-http";
+
+  # This uses Dhall's remote importing capabilities for downloading a Dhall file.
+  # The output Dhall file has all imports resolved, and then is
+  # alpha-normalized and binary-encoded.
+  downloadedEncodedFile =
+    runCommand
+      (baseNameOf url)
+      {
+        outputHashAlgo = null;
+        outputHash = hash;
+        name = baseNameOf url;
+        nativeBuildInputs = [ cacert ];
+      }
+      ''
+        echo "${url} ${dhall-hash}" > in-dhall-file
+        ${dhall}/bin/dhall --alpha --plain --file in-dhall-file | ${dhallNoHTTP}/bin/dhall encode > $out
+      '';
+
+   cache = ".cache";
+
+   data = ".local/share";
+
+   cacheDhall = "${cache}/dhall";
+
+   dataDhall = "${data}/dhall";
+
+   sourceFile = "source.dhall";
+
+in
+  runCommand name { } (''
+    set -eu
+
+    mkdir -p ${cacheDhall}
+
+    export XDG_CACHE_HOME=$PWD/${cache}
+
+    mkdir -p $out/${cacheDhall}
+
+    SHA_HASH="${dhall-hash}"
+
+    HASH_FILE="''${SHA_HASH/sha256:/1220}"
+
+    cp ${downloadedEncodedFile} $out/${cacheDhall}/$HASH_FILE
+
+    echo "missing $SHA_HASH" > $out/binary.dhall
+  '' +
+  lib.optionalString source ''
+    ${dhallNoHTTP}/bin/dhall decode --file ${downloadedEncodedFile} > $out/${sourceFile}
+  '')
diff --git a/pkgs/top-level/dhall-packages.nix b/pkgs/top-level/dhall-packages.nix
index 173c37f492144..9221c2b640ad1 100644
--- a/pkgs/top-level/dhall-packages.nix
+++ b/pkgs/top-level/dhall-packages.nix
@@ -17,12 +17,16 @@ let
       buildDhallDirectoryPackage =
         callPackage ../development/interpreters/dhall/build-dhall-directory-package.nix { };
 
+      buildDhallUrl =
+        callPackage ../development/interpreters/dhall/build-dhall-url.nix { };
+
     in
       { inherit
           callPackage
           buildDhallPackage
           buildDhallGitHubPackage
           buildDhallDirectoryPackage
+          buildDhallUrl
         ;
 
         lib = import ../development/dhall-modules/lib.nix { inherit lib; };