snoyberg/file-embed

Use Template Haskell to embed file contents directly.

Haskell

99

116 commits

updated Feb 24, 2025

See the code

README

file-embed Hackage Hackage-Deps Tests

Embed arbitrary files like JSON or plain text into your Haskell code as if they were written inside Haskell.

Note: This library uses Template Haskell for the embedding.

Quickstart

Add the latest version of file-embed to your package description <package>.cabal or Stack package.yaml file.

Given the folder structure

$ tree
.
└── myapp
│   ├── app
│   │   └── Main.hs
│   ├── embedded.json
│   └── myapp.cabal
└── cabal.project

you can embed a file as follows:

-- file: Main.hs
{-# LANGUAGE TemplateHaskell #-}

import qualified Data.ByteString as BS
import Data.FileEmbed (embedFileRelative)

myFile :: BS.ByteString
myFile = $(embedFileRelative "embedded.json")

The path to embedFileRelative is relative to the package root; the folder where the <package>.cabal file is. Take a look at the Hackage documentation for more examples and variations.

Contributors

snoyberg

84 commits

malteneuss

5 commits

phadej

5 commits

snoyberg/file-embed

Use Template Haskell to embed file contents directly.

Haskell

99

116 commits

updated Feb 24, 2025

See the code

README

file-embed Hackage Hackage-Deps Tests

Embed arbitrary files like JSON or plain text into your Haskell code as if they were written inside Haskell.

Note: This library uses Template Haskell for the embedding.

Quickstart

Add the latest version of file-embed to your package description <package>.cabal or Stack package.yaml file.

Given the folder structure

$ tree
.
└── myapp
│   ├── app
│   │   └── Main.hs
│   ├── embedded.json
│   └── myapp.cabal
└── cabal.project

you can embed a file as follows:

-- file: Main.hs
{-# LANGUAGE TemplateHaskell #-}

import qualified Data.ByteString as BS
import Data.FileEmbed (embedFileRelative)

myFile :: BS.ByteString
myFile = $(embedFileRelative "embedded.json")

The path to embedFileRelative is relative to the package root; the folder where the <package>.cabal file is. Take a look at the Hackage documentation for more examples and variations.

Contributors

snoyberg

84 commits

malteneuss

5 commits

phadej

5 commits

Languages

Haskell

99.5%