MyNixOS website logo
Description

A Haskell library to make self-extracting executables.

self-extract

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

Usage

Basic

import Codec.SelfExtract (extractTo)
import System.Environment (getArgs)

main :: IO ()
main = do
  dir <- head <$> getArgs
  extractTo dir
$ stack ghc Example.hs
$ mkdir artifacts && touch artifacts/hello.txt artifacts/world.txt
$ stack build self-extract && stack exec -- self-bundle ./Example artifacts/
$ ./Example dist
$ ls dist
hello.txt
world.txt

With Cabal hooks

  • Add self-extract to the Cabal file
custom-setup
  setup-depends: base, Cabal, self-extract

executable name-of-executable
  build-depends: self-extract
  • Call bundle in Setup.hs
import Codec.SelfExtract (bundle)
import Codec.SelfExtract.Distribution (getExe)
import Distribution.Simple

main = defaultMainWithHooks simpleUserHooks
  { postCopy = \args cf pd lbi -> do
      postCopy simpleUserHooks args cf pd lbi
      exe <- getExe lbi "name-of-executable"
      bundle exe "dir-to-bundle"
  }
  • Call extractTo in the executable
import Codec.SelfExtract

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

  -- will extract to /usr/local/lib
  extractTo "/usr/local/lib"

  -- will extract to a temporary directory
  withExtractToTemp $ \dir -> ...

Details

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 take the directory specified and run tar on it. It will also get the size of the given 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 with bundle. After seeking to the size of the binary, the tar archive can be extracted to the desired directory.

Metadata

Version

0.4.1

Executables (1)

  • bin/self-bundle

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows