magic: Interface to C file/magic library

[ bsd3, library, text ] [ Propose Tags ] [ Report a vulnerability ]

This package provides a Haskell interface to the C libmagic library. With it, you can determine the type of a file by examining its contents rather than its name. The Haskell interface provides a full-featured binding.

Note: this package requires the C libmagic library (part of the file package) and its development headers to be installed at build time.


[Skip to Readme]

Flags

Manual Flags

NameDescriptionDefault
dev

Turn on developer settings, notably -Werror.

Disabled
Automatic Flags
NameDescriptionDefault
pkgconfig

Use pkg-config to locate the C libmagic library. Enabled by default; because this flag is automatic, Cabal's solver transparently falls back to plain extra-libraries linking when pkg-config or libmagic.pc is unavailable.

Enabled

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 1.0.5, 1.0.7, 1.0.8, 1.1, 1.1.1, 1.1.2, 2.0.0
Change log CHANGELOG.md
Dependencies base (>=4.9 && <5), bytestring (>=0.10 && <0.13), deepseq (>=1.4 && <1.6), filepath (>=1.4.100 && <1.6), os-string (>=2.0 && <2.1), text (>=1.2.3 && <2.2) [details]
Tested with ghc ==9.6.7 || ==9.8.4 || ==9.10.3 || ==9.12.4
License BSD-3-Clause
Copyright Copyright (c) 2005-2009 John Goerzen
Author John Goerzen
Maintainer Philippe <philippedev101@gmail.com>
Uploaded by philippedev101 at 2026-06-09T15:09:45Z
Category Text
Home page https://github.com/philippedev101/magic-haskell
Bug tracker https://github.com/philippedev101/magic-haskell/issues
Source repo head: git clone https://github.com/philippedev101/magic-haskell.git
Distributions Arch:1.1.1, Fedora:1.1, LTSHaskell:1.1, Stackage:1.1.2
Reverse Dependencies 7 direct, 0 indirect [details]
Downloads 12478 total (30 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for magic-2.0.0

[back to package description]

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 strict ByteString). Results come back as Text.

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

Add magic to your build-depends. The easiest way in is Magic.FilePath, which takes ordinary String paths:

import Magic.FilePath          -- convenience facade: ordinary String paths
import qualified Data.Text.IO as T

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

Magic vs Magic.FilePath

import Magic is the primary API. It is the same in every way except that paths are OsPath instead of String. Prefer it when filename correctness matters.

Magic.FilePath is a drop-in facade that converts paths through the locale's filesystem encoding (encodeFS / decodeFS). For ordinary ASCII/UTF-8 filenames under a UTF-8 locale that is perfectly fine. What you give up: a String path cannot faithfully represent every path the operating system can. Filenames containing bytes that are not valid in the current locale (raw non-UTF-8 bytes on Unix, names created under a different LANG/LC_*, and so on) may be corrupted or fail to round-trip, so you can fail to open a file that exists, or open the wrong one, and the outcome depends on the environment. OsPath stores the OS-native path bytes verbatim, so it represents any path losslessly and independently of the locale.

Author & history

magic-haskell was written by John Goerzen jgoerzen@complete.org, 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.