self-extract: A Haskell library to make self-extracting executables

[ bsd3, distribution, library ] [ Propose Tags ]

A Haskell library to make self-extracting executables.

[Skip to Readme]
Change log
Dependencies base (>=4.7 && <5), binary (>=0.8.5 && <0.9), bytestring (>=0.10.8 && <0.11), Cabal (>=2.0 && <3), extra (==1.6.*), file-embed (>=0.0.10 && <0.1), path (==0.6.*), path-io (==1.3.*), process (==1.6.*), unix-compat (==0.5.*) [details]
License BSD-3-Clause
Author Brandon Chinn <>
Maintainer Brandon Chinn <>
Category Distribution
Source repo head: git clone
Uploaded by brandonchinn178 at Wed May 16 22:48:17 UTC 2018
Distributions NixOS:
Downloads 38 total (38 in the last 30 days)
Rating (no votes yet) [estimated by rule of succession]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2018-05-17 [all 1 reports]
Hackage Matrix CI




Maintainer's Corner

For package maintainers and hackage trustees

Readme for self-extract-

[back to package description]


A Haskell library that can make an executable self-extracting.


.cabal file

build-type: Custom

  setup-depends: base, Cabal, self-extract


executable name-of-executable
  build-depends: self-extract, ...


import Codec.SelfExtract.Distribution (bundle)
import Distribution.Simple

main = defaultMainWithHooks simpleUserHooks
  { postCopy = \args cf pd lbi -> do
      postCopy simpleUserHooks args cf pd lbi
      bundle "name-of-executable" "dir-to-bundle" lbi

Executable file

import Codec.SelfExtract (extractTo)

main = do
  extractTo "dir" -- will extract to $CWD/dir
  extractTo "/usr/local/lib"

Extract to a temporary directory:

import Codec.SelfExtract (withExtractToTemp)
import System.Directory (removeDirectory)

main = do
  withExtractToTemp $ \tmp -> do


The above instructions should be a black box, but here is an explanation of the implementation if you need to know the details of how it works.

When the executable containing extractTo is built, some space will be allocated to contain the size of the binary.

bundle will find the executable from LocalBuildInfo's buildDir. It will take the directory specified and run tar on it. It will also get the size of the executable and write the size into the space allocated by extractTo. Then bundle will replace the executable with the executable itself concatenated with the tar archive.

When extractTo is called, it will read the size of the executable that was written in bundle. After seeking to the size of the binary, the tar archive can be extracted to the desired directory.