Haskellのパッケージシステム (Cabal)

(2017.1) 大幅に加筆。

HaskellにはCabalというパッケージシステムがあります。RubyでいうRubyGemsのようなものです。

追加パッケージをコマンド一つでインストールできます。

バージョン地獄 (dependency hell) が大変。

cabal-install のインストール

Fedora Linux では, haskell-platform パッケージからの依存関係で, cabal コマンドが /usr/bin にインストールされます。

Fedora 25 のは ghc-Cabal 1.22.5.0 と cabal-install 1.22.9.0 です。ghc-Cabal がライブラリ, cabal-install がコマンドライン・ツールです。

バージョンが古いので, 別に新しいバージョンを入れます。ほかのライブラリも, rpm では入れません。

2017年1月現在の最新バージョンは, cabal-install 1.24.0.2 (using Cabal library 1.24.2.0) です。

root になって, まず cabal update コマンドで, パッケージリストを更新します。

# cabal update
Config file path source is default config file.
Config file /root/.cabal/config not found.
Writing default configuration to /root/.cabal/config
Downloading the latest package list from hackage.haskell.org

新しいバージョンの cabal は, --global オプションを指定して /usr/local 以下にインストールします。

# cabal install --global cabal-install

ネイティブのライブラリは, /usr/local/lib/x86_64-linux-ghc-7.10.3/ 以下に入ります。

パッケージを探す

cabal list コマンドを使います。

パッケージ名は部分一致です。

$ cabal update
Downloading the latest package list from hackage.haskell.org
$ cabal list persistent

パッケージは, Hackage のWebサイトでも検索できます。

インストールされたパッケージ

インストールされたものだけを表示するときは cabal list --installed.

$ cabal list --installed --simple-output
Cabal 1.22.5.0
Cabal 1.24.2.0
GLURaw 1.5.0.2
GLUT 2.7.0.3
HTTP 4000.3.3
HUnit 1.3.1.1
ObjectName 1.1.0.0
OpenGL 2.13.1.0
...以下、一覧が続く

パッケージをインストールする

パッケージをインストールするときは、次のようにします。--dry-run オプションを付けると何が行われるか表示されるだけで、実際にインストールはされません。

--global を付けないと, 自分のホームディレクトリ以下にインストールされます。

$ cabal update
$ cabal install --dry-run HDBC-postgresql
Resolving dependencies...
In order, the following would be installed (use -v for more details):
convertible-1.0.11.1
utf8-string-0.3.7
HDBC-2.3.1.2
HDBC-postgresql-2.3.2.2

内容を確認してから、--dry-runオプションなしで実行します。

特定のバージョンを指定してインストールするときは、次のようにします。

 cabal install foo-1.0

そのほかのコマンド

cabal のコマンドは, 次のものがあります;

コマンド 説明
info Display detailed information about a particular package.
fetch Downloads packages for later installation.
unpack Unpacks packages for user inspection.
report Upload build reports to a remote server.
copy Copy the files into the install locations.
haddock Generate Haddock HTML documentation.
hscolour Generate HsColour colourised code, in HTML format.
register Register this package with the compiler.
help Help about commands

例えば, infoコマンドでは, 次のようにパッケージの内容を, インストールする前に確認できます。

# cabal info persistent
* persistent       (library)
    Synopsis:      Type-safe, multi-backend data serialization.
    Versions available: 0.9.0.4, 1.2.3.3, 1.3.2, 1.3.3, (2.0.5.1), (2.0.7),
                        (2.0.7.1), (2.0.8), 2.1 (and 86 others)
    Versions installed: [ Not installed ]
    Homepage:      http://www.yesodweb.com/book/persistent
    Bug reports:   https://github.com/yesodweb/persistent/issues
    Description:   Type-safe, data serialization. You must use a specific
                   backend in order to make this useful.
    Category:      Database, Yesod
    License:       MIT
    Author:        Michael Snoyman <michael@snoyman.com>
    Maintainer:    Michael Snoyman <michael@snoyman.com>, Greg Weber <greg@gregweber.info>
    Source repo:   git://github.com/yesodweb/persistent.git
    Flags:         nooverlap
    Dependencies:  base ==4.*, bytestring >=0.9, transformers >=0.2.1,
                   time >=1.1.4, text >=0.8, containers >=0.2, conduit >=1.0,
                   resourcet >=1.1, exceptions >=0.6, monad-control >=0.3,
                   lifted-base >=0.1, resource-pool -any, path-pieces >=0.1,
                   aeson >=0.5, monad-logger >=0.3, transformers-base -any,
                   base64-bytestring -any, unordered-containers -any,
                   vector -any, attoparsec -any, template-haskell -any,
                   blaze-html >=0.5, blaze-markup >=0.5.1, silently -any,
                   mtl -any, fast-logger >=2.1, scientific -any,
                   resource-pool -any, tagged -any, scientific -any,
                   blaze-html -any, monad-control -any, conduit -any,
                   monad-logger -any, resourcet -any, aeson -any,
                   path-pieces -any, transformers -any, attoparsec -any,
                   base64-bytestring -any, vector -any, bytestring -any,
                   time -any, unordered-containers -any, text -any,
                   containers -any, hspec >=1.3, base ==4.*
    Cached:        No
    Modules:
        Database.Persist
        Database.Persist.Class
        Database.Persist.Quasi
        Database.Persist.Sql
        Database.Persist.Types

さらに, 開発用に, 次のコマンドもあります;

コマンド 説明
init Interactively create a .cabal file.
check Check the package for common mistakes
sdist Generate a source distribution file (.tar.gz).
upload Uploads source packages to Hackage
configure Prepare to build the package.
build Make this package ready for installation.
clean Clean up after a build.
test Run the test suite, if any (configure with UserHooks).
bench Run the benchmark, if any (configure with UserHooks).