nixpkgs/pkgs/build-support/dhall-to-nix.nix
Gabriella Gonzalez 9a8850aea9
dhallToNix: Permit inputs referring to derivations (#134459)
Fixes https://github.com/dhall-lang/dhall-haskell/issues/2267

`pkgs.dhallToNix` currently fails when a Dhall package is
interpolated into the input source code, like this:

```nix
let
  pkgs = import <nixpkgs> { };

  f = { buildDhallPackage }: buildDhallPackage {
    name = "not";
    code = "λ(x : Bool) → x == False";
    source = true;
  };

  not = pkgs.dhallPackages.callPackage f {};

in
  pkgs.dhallToNix "${not}/source.dhall True"
```

This is because `dhallToNix` was using `builtins.toFile`, which
does not permit inputs with interpolated derivations.  However,
`pkgs.writeText` does not have this limitation, so we can switch
to using that instead.
2021-08-21 19:25:01 -07:00

39 lines
993 B
Nix

/* `dhallToNix` is a utility function to convert expressions in the Dhall
configuration language to their corresponding Nix expressions.
Example:
dhallToNix "{ foo = 1, bar = True }"
=> { foo = 1; bar = true; }
dhallToNix "λ(x : Bool) x == False"
=> x : x == false
dhallToNix "λ(x : Bool) x == False" false
=> true
See https://hackage.haskell.org/package/dhall-nix/docs/Dhall-Nix.html for
a longer tutorial
Note that this uses "import from derivation", meaning that Nix will perform
a build during the evaluation phase if you use this `dhallToNix` utility
*/
{ stdenv, dhall-nix, writeText }:
let
dhallToNix = code :
let
file = writeText "dhall-expression" code;
drv = stdenv.mkDerivation {
name = "dhall-compiled.nix";
buildCommand = ''
dhall-to-nix <<< "${file}" > $out
'';
buildInputs = [ dhall-nix ];
};
in
import drv;
in
dhallToNix