Generating An Unattached String In Dhall

I’m not sure if my description is good but I’ll elaborate. I want to use dhall to generate conda meta.yaml files to make Python conda packages. A typical file looks like this:

{% set name = "click" %}
{% set version = "7.0" %}

package:
  name: "{{ name|lower }}"
  version: "{{ version }}"

source:
  url: https://pypi.io/packages/source/{{ name[0] }}/{{ name }}/{{ name }}-{{ version }}.tar.gz
  sha256: 5b94b49521f6456670fdb30cd82a4eca9412788a93fa6dd6df72c94d5a8ff2d7

build:
  number: 0
  script: "{{ PYTHON }} -m pip install . --no-deps --ignore-installed -vv "

requirements:
  host:
    - pip
    - python
  run:
    - python

test:
  imports:
    - click

about:
  home: https://palletsprojects.com/p/click/
  license: BSD
  license_family: BSD
  summary: Composable command line interface toolkit

I eventually figured out how to write a dhall version of everything except the first two lines. How would I do that? Tangentially, is yaml-to-dhall supposed to be installed separately? I only have dhall-to-json, json-to-dhall, and dhall-to-yaml.

-- I wrote this but have no idea how to integrate it.
let x = "{% set name = 'click' %}" 
let y = "{% set version = '7.0' %}"

let package = { name = "{{ name|lower }}", version = "{{ version }}"}

let source = {
url = "https://pypi.io/packages/source/{{ name[0] }}/{{ name }}/{{ name }}-{{ version }}.tar.gz",
sha256 = "5b94b49521f6456670fdb30cd82a4eca9412788a93fa6dd6df72c94d5a8ff2d7"
}

let build = {
number = 0,
script = "{{ PYTHON }} -m pip install . --no-deps --ignore-installed -vv "
}

let requirements = {host = ["pip", "python"], run = ["pip"]}

let tests = {imports = ["click"]}

let about = {
home = "https://palletsprojects.com/p/click/", 
license = "BSD", 
license_family = "BSD", 
summary = "Composable command line interface toolkit"
}

in {package, source, requirements, tests, about}

What is this {% set name = "click" %} bit at the top your file? I’ve never seen anything like it. Is it something like a valid YAML comment?

If so, we could try to get it supported in dhall-to-yaml-ng

Otherwise, have you considered solving this with templating using dhall text instead of dhall-to-yaml[-ng]?

yaml-to-dhall is in the dhall-yaml package – maybe you’ve only installed dhall-json so far?!

As far as I can figure it’s some templating black magic that conda uses to build packages. It looks like I could just inject it straight into the actual yaml though. I was just wondering if dhall could handle it. I’ll look into dhall text.

I think that may be it because I followed the tutorial which only specified json. Why are there four versions (json, yaml, bash, and regular)?

I think this is mostly to simplify packaging and installation. Users who want to generate bash statements don’t need to depend on some YAML dependencies.

With dhall text you could give https://github.com/dhall-lang/dhall-lang/blob/master/Prelude/JSON/renderYAML try.

@sjakobi this looks like Jinja templating, a common syntax in Python. The {% set .. %} bit is probably documented here

Agree that this looks like jinja templating.

First, to answer the direct question, you could use Prelude.JSON.renderYAML instead of dhall-to-yaml, though see the caveats in the comments.

Then you could prepend the desired text in Dhall:

— definitions and imports omitted
''
{% set name = "${name}" %}
{% set version = "${version}" %}
${Prelude.JSON.renderYAML myconfig}
''

…and feed this Dhall into the dhall-to-text tool.

This is a direct answer to your question, but you can see that this is starting to mix two different kinds of templating - jinja and Dhall itself.

It would be worth asking if it’s possible for Dhall to entirely replace jinja here. I suspect not, or not without support from conda itself, but it’s worth asking the question.

2 Likes

Great. Thanks for the help.