2

The Dhall configuration language

 1 year ago
source link: https://dhall-lang.org/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The Dhall configuration language

The Dhall configuration language

Maintainable configuration files

Dhall is a programmable configuration language that you can think of as: JSON + functions + types + imports

xxxxxxxxxx
{- This is an example Dhall configuration file
   Can you spot the mistake?
   Fix the typo, then move onto the "Definitions"
   example
-}
{ home       = "/home/bill"
, privateKey = "/home/bill/.ssh/id_ed25519"
, publicKey  = "/home/blil/.ssh/id_ed25519.pub"
}
xxxxxxxxxx
home: /home/bill
privateKey: /home/bill/.ssh/id_ed25519
publicKey: /home/blil/.ssh/id_ed25519.pub

Don't repeat yourself

Struggling with configuration drift? Create a single source of truth you can reference everywhere.


"Configuration drift occurs when a standardized group of IT resources, be they virtual servers, standard router configurations in VNF deployments, or any other deployment group that is built from a standard template, diverge in configuration over time. … The Infrastructure as Code methodology from DevOps is designed to combat Configuration Drift and other infrastructure management problems."

Kemp Technologies on Configuration Drift

Create a single authoritative configuration file:

xxxxxxxxxx
-- ./company.dhall
let Prelude =
      https://prelude.dhall-lang.org/v19.0.0/package.dhall sha256:eb693342eb769f782174157eba9b5924cf8ac6793897fc36a31ccbd6f56dafe2
let companyName = "Example Dot Com"
let User = { name : Text, account : Text, age : Natural }
let users
    : List User
    = [ { name = "John Doe", account = "john", age = 23 }
      , { name = "Jane Smith", account = "jane", age = 29 }
      , { name = "William Allen", account = "bill", age = 41 }
      ]
let toEmail = \(user : User) -> "${user.account}@example.com"
let Bio = { name : Text, age : Natural }
let toBio = \(user : User) -> user.(Bio)
let companySize = Prelude.List.length User users
let greetingPage =

… that you can read directly into several languages or convert to other file formats (including YAML or JSON).

xxxxxxxxxx
$ dhall-to-yaml <<< '(./company.dhall).bios'
- age: 23
  name: John Doe
- age: 29
  name: Jane Smith
- age: 41
  name: William Allen
$ dhall-to-bash --declare EMAILS <<< '(./company.dhall).emails'
$ dhall text <<< '(./company.dhall).greetingPage'
<html>
<title>Welcome to Example Dot Com!</title>
<body>
<p>Welcome to our humble company of 3 people!</p>
</body>
</html>
Supported integrations

Fearlessly refactor

Need to clean up a big mess? Move fast without breaking things by leaning on Dhall's tooling.


Refactoring something mission-critical? Use Dhall's support for semantic hashes to guarantee that many types of refactors are behavior-preserving

hash.gif
hash.gif

What if you intend to make a change? Use a semantic diff to verify that you changed what you expected to:

xxxxxxxxxx
$ dhall diff \
    'https://prelude.dhall-lang.org/v18.0.0/package.dhall' \
    'https://prelude.dhall-lang.org/v19.0.0/package.dhall'
{ + Operator = …
,   `Optional` = { + concatMap = …
                 , …
                 }
, …
}

Did you inherit a messy configuration? Use the type system and integrated editor support to navigate more effectively.

completion.gif
VSCode plugin

Safety first

Sick of Turing-complete configuration languages? Dhall is a total programming language that forbids arbitrary side effects.


We take language security seriously so that your Dhall programs never fail, hang, crash, leak secrets, or compromise your system.

The language aims to support safely importing and evaluating untrusted Dhall code, even code authored by malicious users. We treat the inability to do so as a specification bug.

Safety guarantees

Use programming language features

Hold your configuration files to the same standard of quality as the rest of your code.


xxxxxxxxxx
-- Import packages
let JSON =
      https://prelude.dhall-lang.org/v19.0.0/JSON/package.dhall sha256:79dfc281a05bc7b78f927e0da0c274ee5709b1c55c9e5f59499cb28e9d6f3ec0
-- Use precise types like enums instead of strings
let Zone = < us-east-1 | us-west-1 >
let InstanceType = < `m5.large` | `m5.xlarge` >
let Instance = { type : InstanceType, zone : Zone }
-- JSON rendering can optionally be implemented entirely within the language
let Zone/toJSON =
          \(zone : Zone)
      ->  merge
            { us-east-1 = JSON.string "us-east-1"
            , us-west-1 = JSON.string "us-west-1"
            }
            zone
let InstanceType/toJSON =
          \(type : InstanceType)
      ->  merge
            { `m5.large` = JSON.string "m5.large"
            , `m5.xlarge` = JSON.string "m5.xlarge"

"Configuration bugs, not code bugs, are the most common cause I've seen of really bad outages. … As with error handling, I'm often told that it's obvious that config changes are scary, but it's not so obvious that most companies test and stage config changes like they do code changes."

Dan Luu in Reading postmortems

Configs are code are configs are code https://t.co/fVBs7T7P3j

— Charity Majors (@mipsytipsy) October 17, 2019

Configuration testing is quite underrated in an industry where the majority of work is becoming configuration.

— JBD (@rakyll) September 26, 2019
Get started

This work is licensed under a Creative Commons Attribution 4.0 International License.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK