Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get stack build/install to include resource files (configs, images etc.)

After having picked up stack as a step in starting to write non-trivial haskell programs (larger than a single file) I've run across the problem of not knowing how to get stack to recognize .ini files and such. It doesn't seem to fit anywhere in the .cabal or stack.yaml files.

For clarification: after running stack build/install, the folder with the generated .exe doesn't have the resources, and so of course the program crashes with a bunch of IO errors (file not found).

like image 603
sara Avatar asked Jul 29 '17 14:07

sara


Video Answer


1 Answers

There are two options. The one I assume you're looking for is data files. To do this, you'll essentially:

  1. Add the relevant files to the data-files field in your .cabal file so that they are installed when building your package
  2. Add the Paths_package_name module to your other-modules in your .cabal file (replace package_name with your package name)
  3. Import that Paths_* module where needed
  4. Use the generated getDataFileName :: FilePath -> IO FilePath function to get the absolute path to the data file you need

The alternative is to embed the data file contents inside the executable itself using Template Haskell, such as with the file-embed package.

like image 95
Michael Snoyman Avatar answered Oct 16 '22 23:10

Michael Snoyman