Skip to content

Instantly share code, notes, and snippets.

@WolfangAukang
Last active October 5, 2022 13:06
Show Gist options
  • Save WolfangAukang/571b47534adeb8954dc91be4d16693c5 to your computer and use it in GitHub Desktop.
Save WolfangAukang/571b47534adeb8954dc91be4d16693c5 to your computer and use it in GitHub Desktop.
How to use Dhall through Flakes (Nix)
{
description = "Dhall example";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, utils }:
utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
inherit (pkgs) mkShell writeShellScript dhall;
inherit (pkgs.haskellPackages) dhall-yaml;
# Using dhall-cloudformation as an example
inherit (pkgs.dhallPackages) dhall-cloudformation Prelude;
# Script settings
setDhallEnv = ''
export DHALL_PRELUDE=${Prelude}/binary.dhall
export DHALL_CLOUDFORMATION=${dhall-cloudformation}/binary.dhall
'';
prepareDhallEnv = setDhallEnv + ''
mkdir -p $XDG_CACHE_HOME/dhall
for pkg in ${dhallPackages.Prelude} ${dhallPackages.dhall-cloudformation}; do
for cache in $pkg/.cache/dhall/*; do
ln -sf $cache $XDG_CACHE_HOME/dhall/
done
done
'';
in {
apps = {
generateYaml = {
type = "app";
program = toString (writeShellScript "generate-yaml.sh" (setDhallEnv + ''
${dhall-yaml}/bin/dhall-to-yaml-ng < ./dhall/example.dhall > ./template.yaml
''));
};
validateYaml = {
type = "app";
program = toString (writeShellScript "validate-example-template.sh" (setDhallEnv + ''
${dhall}/bin/dhall <<< ./dhall/example.dhall > /dev/null
''));
};
};
devShells.default = mkShell {
buildInputs = [ dhall-yaml dhall ];
shellHook = prepareDhallEnv;
};
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment