BSD-3-Clause licensed by John Goerzen
Maintained by Philippe
This version can be pinned in stack with:magic-1.1.2@sha256:d5963dee453ecf23e93362e17556f18ad089ce2649a407388d786bfacadce95c,3385

Module documentation for 1.1.2

Depends on 2 packages(full list with versions):
Used by 1 package in nightly-2026-06-10(full list with versions):

magic

It is a binding to the C libmagic library. It allows you to determine the type of a file not by looking at its name or extension, but rather by examining the contents itself.

libmagic can provide either a textual description or a MIME content type (and, occasionally, also a character set). The Haskell binding can examine files, open file descriptors, and in-memory data (as a String or, for binary content, a strict ByteString).

Requirements

You need the C libmagic library and its development headers installed before building. It is part of the file package.

Platform Install
Debian / Ubuntu apt-get install libmagic-dev
Fedora dnf install file-devel
Arch pacman -S file
macOS (Homebrew) brew install libmagic
Nix file / file.dev

Building

With cabal:

cabal build
cabal test

With stack:

stack build
stack test

libmagic is located automatically via pkg-config. If pkg-config cannot find it (for example, an unusual install location), point Cabal at the library directly:

cabal build \
  --extra-lib-dirs=/path/to/lib \
  --extra-include-dirs=/path/to/include

Usage

You can simply add magic to your build-depends to enable this library.

import Magic

main :: IO ()
main = do
  magic <- magicOpen [MagicMimeType]
  magicLoadDefault magic
  mime <- magicFile magic "some-file.png"
  putStrLn mime          -- e.g. "image/png"

Author & history

magic-haskell was written by John Goerzen [email protected], who created it in 2005 and maintained it for nearly two decades. It is now maintained by Philippe, with development at https://github.com/philippedev101/magic-haskell.

License

3-clause BSD. See the COPYING file included with the package.

Changes

Changelog for magic

All notable changes to this project are documented here.

1.1.2 (2026-06-09)

Backwards-compatible additions: existing code continues to work unchanged. This release rounds out the binding to cover (almost) all of libmagic’s public API.

  • Added the remaining MagicFlag constructors from <magic.h>: MagicApple, MagicExtension, MagicCompressTransp, MagicNoCompressFork, MagicNodesc, and the whole no-check family (MagicNoCheckCompress, …Tar, …Soft, …Apptype, …Elf, …Text, …Cdf, …Csv, …Tokens, …Encoding, …Json, …Simh) plus MagicNoCheckBuiltin.
  • Added new operations in Magic.Operations: magicDescriptor (identify an open file descriptor), magicByteString (binary-safe identification of a strict ByteString), magicGetFlags, magicGetParam/magicSetParam with the new MagicParam type, magicCheck, magicGetPath, magicVersion, and magicErrno.
  • magicGetParam/magicSetParam require libmagic >= 5.21; magicGetFlags requires >= 5.05. All current systems are well past this.
  • New dependency: bytestring (a GHC boot library) for the ByteString entry points.
  • Moved the CMagic phantom type (the tag behind the Magic handle) into Magic.Types and removed the internal Magic.TypesLL module. CMagic is now documented and linkable from the exposed Magic.Types. Documentation polish throughout: full Haddock coverage with no warnings, @since annotations on the new API, and default values listed for each MagicParam.
  • magic_load is now imported as a safe foreign call (it was unsafe). It reads and parses the magic database from disk (blocking I/O), so under the threaded runtime loading a database no longer stalls other Haskell threads or blocks garbage collection. No API change.
  • Documented the thread-safety contract of the Magic handle: a single handle is not safe for concurrent use (serialise access or use one per thread); distinct handles are independent.

1.1.1 (2026-06-09)

No API changes: this release is a drop-in replacement for 1.1.

  • New maintainer: Philippe ([email protected]); package handed over from John Goerzen.
  • Modernised the Cabal package description (cabal-version: 2.4, proper library/test-suite stanzas, source-repository, tested-with).
  • Added an automatic pkgconfig flag: libmagic is now located via pkg-config when available, falling back transparently to plain extra-libraries linking otherwise.
  • Moved library sources under src/ and tests under test/ (no code or module-name changes).
  • Revived the original HUnit test suite in place: completed the unfinished Inittest module with real tests exercising the bindings against the system magic database, and fixed the runner to exit non-zero on failure.
  • Added a GitHub Actions CI workflow building and testing across GHC 9.6 / 9.8 / 9.10 / 9.12.
  • Removed dead imports so the library builds -Wall-clean.
  • Ships the unsafe-FFI fix (use safe FFI calls for blocking I/O) that was merged upstream after the 1.1 Hackage release but never published.

1.1 (2014-10) and earlier

Releases by John Goerzen. See the git history for details.