Logo

Using envvault with Haskell

Note: Please ensure that you have completed the previous steps

Prerequisites

  • GHC (Glasgow Haskell Compiler) installed
  • Stack (Haskell build tool) installed
  • envvault CLI tool installed
  • An existing Haskell project

Usage

Running Your Application

To run your Haskell application with environment variables from envvault:

$ envvault run --env=dev stack run

Caching Environment Variables

For better performance, you can cache your environment variables:

$ envvault run --env=dev -c -- stack run

Example Implementation

Here is how to set up a basic Haskell web server with envvault:

-- app/Main.hs
{-# LANGUAGE OverloadedStrings #-}

module Main where

import Web.Scotty
import System.Environment (lookupEnv)
import Data.Maybe (fromMaybe)
import Data.Text.Lazy (pack)

main :: IO ()
main = do
  -- Your environment variables are automatically loaded
  port <- fmap (read . fromMaybe "3000") (lookupEnv "PORT")
  dbUrl <- fromMaybe "" <$> lookupEnv "DB_URL"
  
  putStrLn $ "Server running at http://localhost:" ++ show port ++ "/"
  
  scotty port $ do
    get "/" $ do
      text "Hello from envvault!"

-- package.yaml
name: my-app
version: 0.1.0.0

dependencies:
- base >= 4.7 && < 5
- scotty
- text

executables:
  my-app-exe:
    main: Main.hs
    source-dirs: app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N