How to add environment variables to a haskell flake #159
Answered
by
srid
newton-migosi
asked this question in
Q&A
-
I have a haskell project that is failing For more context, I first asked this question on the nix os discourse |
Beta Was this translation helpful? Give feedback.
Answered by
srid
May 8, 2023
Replies: 1 comment 1 reply
-
Build-time environment variable? You want to override it in Apply this on https://github.com/srid/haskell-template and do diff --git a/flake.nix b/flake.nix
index 15f87d2..ea02116 100644
--- a/flake.nix
+++ b/flake.nix
@@ -26,12 +26,17 @@
# See https://github.com/srid/haskell-flake/blob/master/example/flake.nix
haskellProjects.default = {
# packages.haskell-template.root = ./.; # Auto-discovered by haskell-flake
- overrides = self: super: { };
+ overrides = self: super: {
+ haskell-template = super.haskell-template.overrideAttrs (_: _: {
+ FOO = "bar";
+ });
+ };
devShell = {
tools = hp: {
treefmt = config.treefmt.build.wrapper;
} // config.treefmt.build.programs;
hlsCheck.enable = false;
+ mkShellArgs.shellHook = "export FOO=bar";
};
autoWire = [ "packages" "apps" "checks" ]; # Wire all but the devShell
};
diff --git a/haskell-template.cabal b/haskell-template.cabal
index 770a028..cc3a397 100644
--- a/haskell-template.cabal
+++ b/haskell-template.cabal
@@ -81,6 +81,7 @@ common shared
, directory
, filepath
, mtl
+ , th-env
, optics-core
, profunctors
, relude >=1.0
diff --git a/src/Main.hs b/src/Main.hs
index fc5ea8d..6586dcb 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -1,7 +1,13 @@
+{-# LANGUAGE TemplateHaskell #-}
+
module Main where
+import Language.Haskell.TH.Env (envQ')
import Main.Utf8 qualified as Utf8
+foo :: String
+foo = $$(envQ' "FOO")
+
{- |
Main entry point.
@@ -12,3 +18,4 @@ main = do
-- For withUtf8, see https://serokell.io/blog/haskell-with-utf8
Utf8.withUtf8 $ do
putTextLn "Hello 🌎"
+ putStrLn foo
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
newton-migosi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Build-time environment variable? You want to override it in
overrides
just like any dependency, https://haskell.flake.page/dependencyApply this on https://github.com/srid/haskell-template and do
nix run
ornix build
: