diff --git a/pkgs/build-support/dhall/directory-to-nix.nix b/pkgs/build-support/dhall/directory-to-nix.nix new file mode 100644 index 000000000000..d751e19df3fc --- /dev/null +++ b/pkgs/build-support/dhall/directory-to-nix.nix @@ -0,0 +1,25 @@ +{ dhallPackages, dhallPackageToNix}: + +# `dhallDirectoryToNix is a utility function to take a directory of Dhall files +# and read them in as a Nix expression. +# +# This function is similar to `dhallToNix`, but takes a Nixpkgs Dhall package +# as input instead of raw Dhall code. +# +# Note that this uses "import from derivation" (IFD), meaning that Nix will +# perform a build during the evaluation phase if you use this +# `dhallDirectoryToNix` utility. It is not possible to use +# `dhallDirectoryToNix` in Nixpkgs, since the Nixpkgs Hydra doesn't allow IFD. + +{ src +, # The file to import, relative to the src root directory + file ? "package.dhall" +}@args: + +let + generatedPkg = dhallPackages.generateDhallDirectoryPackage args; + + builtPkg = dhallPackages.callPackage generatedPkg { }; + +in + dhallPackageToNix builtPkg diff --git a/pkgs/build-support/dhall/package-to-nix.nix b/pkgs/build-support/dhall/package-to-nix.nix new file mode 100644 index 000000000000..301501ad49df --- /dev/null +++ b/pkgs/build-support/dhall/package-to-nix.nix @@ -0,0 +1,36 @@ + +# `dhallPackageToNix` is a utility function to take a Nixpkgs Dhall package +# (created with a function like `dhallPackages.buildDhallDirectoryPackage`) +# and read it in as a Nix expression. +# +# This function is similar to `dhallToNix`, but takes a Nixpkgs Dhall package +# as input instead of raw Dhall code. +# +# Note that this uses "import from derivation" (IFD), meaning that Nix will +# perform a build during the evaluation phase if you use this +# `dhallPackageToNix` utility. It is not possible to use `dhallPackageToNix` +# in Nixpkgs, since the Nixpkgs Hydra doesn't allow IFD. + +{ stdenv, dhall-nix }: + +dhallPackage: + let + drv = stdenv.mkDerivation { + name = "dhall-compiled-package.nix"; + + buildCommand = '' + # Dhall requires that the cache is writable, even if it is never written to. + # We copy the cache from the input package to the current directory and + # set the cache as writable. + cp -r "${dhallPackage}/.cache" ./ + export XDG_CACHE_HOME=$PWD/.cache + chmod -R +w ./.cache + + dhall-to-nix <<< "${dhallPackage}/binary.dhall" > $out + ''; + + nativeBuildInputs = [ dhall-nix ]; + }; + + in + import drv diff --git a/pkgs/build-support/dhall-to-nix.nix b/pkgs/build-support/dhall/to-nix.nix similarity index 100% rename from pkgs/build-support/dhall-to-nix.nix rename to pkgs/build-support/dhall/to-nix.nix diff --git a/pkgs/development/interpreters/dhall/generate-dhall-directory-package.nix b/pkgs/development/interpreters/dhall/generate-dhall-directory-package.nix new file mode 100644 index 000000000000..03dfb72d7aba --- /dev/null +++ b/pkgs/development/interpreters/dhall/generate-dhall-directory-package.nix @@ -0,0 +1,27 @@ +{ dhall-nixpkgs, lib, stdenv }: + +# This function calls `dhall-to-nixpkgs directory --fixed-output-derivations` +# within a Nix derivation. +# +# This is possible because +# `dhall-to-nixpkgs directory --fixed-output-derivations` will turn remote +# Dhall imports protected with Dhall integrity checksinto fixed-output +# derivations (with the `buildDhallUrl` function), so no unrestricted network +# access is necessary. +lib.makePackageOverridable + ( { src + , # The file to import, relative to the root directory + file ? "package.dhall" + , # Set to `true` to generate documentation for the package + document ? false + }: + stdenv.mkDerivation { + name = "dhall-directory-package.nix"; + + buildCommand = '' + dhall-to-nixpkgs directory --fixed-output-derivations --file "${file}" "${src}" ${if document then "--document" else ""} > $out + ''; + + nativeBuildInputs = [ dhall-nixpkgs ]; + } + ) diff --git a/pkgs/test/dhall/default.nix b/pkgs/test/dhall/default.nix index bdb33acf0238..4c7eba6c9579 100644 --- a/pkgs/test/dhall/default.nix +++ b/pkgs/test/dhall/default.nix @@ -2,4 +2,5 @@ lib.recurseIntoAttrs { buildDhallUrl = callPackage ./buildDhallUrl { }; + generateDhallDirectoryPackage = callPackage ./generateDhallDirectoryPackage { }; } diff --git a/pkgs/test/dhall/generateDhallDirectoryPackage/default.nix b/pkgs/test/dhall/generateDhallDirectoryPackage/default.nix new file mode 100644 index 000000000000..4771e1629806 --- /dev/null +++ b/pkgs/test/dhall/generateDhallDirectoryPackage/default.nix @@ -0,0 +1,17 @@ +{ dhallPackages, fetchFromGitHub }: + +# This file tests that dhallPackages.generateDhallDirectoryPackage works. +# +# TODO: It would be nice to extend this test to make sure that the resulting +# Nix file has the expected contents, but it might be tough to do that easily +# without IFD. + +dhallPackages.generateDhallDirectoryPackage { + src = fetchFromGitHub { + owner = "cdepillabout"; + repo = "example-dhall-nix"; + rev = "e6a675c72ecd4dd23d254a02aea8181fe875747f"; + sha256 = "sha256-c/EZq76s/+hmLkaeJWKqgh2KrHuJRYI6kWry0E0YQ6s="; + }; + file = "mydhallfile.dhall"; +} diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 6bf57630a928..33aa4ae76338 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -301,9 +301,11 @@ with pkgs; crow-translate = libsForQt5.callPackage ../applications/misc/crow-translate { }; - dhallToNix = callPackage ../build-support/dhall-to-nix.nix { - inherit dhall-nix; - }; + dhallDirectoryToNix = callPackage ../build-support/dhall/directory-to-nix.nix { }; + + dhallPackageToNix = callPackage ../build-support/dhall/package-to-nix.nix { }; + + dhallToNix = callPackage ../build-support/dhall/to-nix.nix { }; deadcode = callPackage ../development/tools/deadcode { }; @@ -13285,6 +13287,8 @@ with pkgs; dhall-nix = haskell.lib.compose.justStaticExecutables haskellPackages.dhall-nix; + dhall-nixpkgs = haskell.lib.compose.justStaticExecutables haskellPackages.dhall-nixpkgs; + dhall-text = haskell.lib.compose.justStaticExecutables haskellPackages.dhall-text; dhallPackages = recurseIntoAttrs (callPackage ./dhall-packages.nix { }); diff --git a/pkgs/top-level/dhall-packages.nix b/pkgs/top-level/dhall-packages.nix index 1910cb372741..646f2b7e9d39 100644 --- a/pkgs/top-level/dhall-packages.nix +++ b/pkgs/top-level/dhall-packages.nix @@ -20,6 +20,9 @@ let buildDhallUrl = callPackage ../development/interpreters/dhall/build-dhall-url.nix { }; + generateDhallDirectoryPackage = + callPackage ../development/interpreters/dhall/generate-dhall-directory-package.nix { }; + in { inherit callPackage @@ -27,6 +30,7 @@ let buildDhallGitHubPackage buildDhallDirectoryPackage buildDhallUrl + generateDhallDirectoryPackage ; lib = import ../development/dhall-modules/lib.nix { inherit lib; };