Merge pull request #144076 from NixOS/dhallDirectoryToNix

dhallPackages.dhallDirectoryToNix: add function to turn a directory of Dhall files into Nix
This commit is contained in:
Dennis Gosnell 2021-12-07 14:25:32 +09:00 committed by GitHub
commit bcfed07a3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 117 additions and 3 deletions

View file

@ -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

View file

@ -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

View file

@ -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 ];
}
)

View file

@ -2,4 +2,5 @@
lib.recurseIntoAttrs {
buildDhallUrl = callPackage ./buildDhallUrl { };
generateDhallDirectoryPackage = callPackage ./generateDhallDirectoryPackage { };
}

View file

@ -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";
}

View file

@ -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 { });

View file

@ -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; };