aboutsummaryrefslogtreecommitdiff
path: root/guix/import/hackage.scm
blob: b5574a8d9fe68199a76569e28f04520d267ac1ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2015 Federico Beffa <beffa@fbengineering.ch>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (guix import hackage)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-11)
  #:use-module (srfi srfi-1)
  #:use-module ((guix download) #:select (download-to-store))
  #:use-module ((guix utils) #:select (package-name->name+version))
  #:use-module (guix import utils)
  #:use-module (guix import cabal)
  #:use-module (guix store)
  #:use-module (guix hash)
  #:use-module (guix base32)
  #:use-module ((guix utils) #:select (call-with-temporary-output-file))
  #:export (hackage->guix-package))

(define ghc-standard-libraries
  ;; List of libraries distributed with ghc (7.8.4). We include GHC itself as
  ;; some packages list it.
  '("ghc"
    "haskell98"
    "hoopl"
    "base"
    "transformers"
    "deepseq"
    "array"
    "binary"
    "bytestring"
    "containers"
    "time"
    "cabal"
    "bin-package-db"
    "ghc-prim"
    "integer-gmp"
    "integer-simple"
    "win32"
    "template-haskell"
    "process"
    "haskeline"
    "terminfo"
    "directory"
    "filepath"
    "old-locale"
    "unix"
    "old-time"
    "pretty"
    "xhtml"
    "hpc"))

(define package-name-prefix "ghc-")

(define (hackage-name->package-name name)
  "Given the NAME of a Cabal package, return the corresponding Guix name."
  (if (string-prefix? package-name-prefix name)
      (string-downcase name)
      (string-append package-name-prefix (string-downcase name))))

(define (hackage-fetch name-version)
  "Return the Cabal file for the package NAME-VERSION, or #f on failure.  If
the version part is omitted from the package name, then return the latest
version."
  (let*-values (((name version) (package-name->name+version name-version))
                ((url)
                 (if version
                     (string-append "http://hackage.haskell.org/package/"
                                    name "-" version "/" name ".cabal")
                     (string-append "http://hackage.haskell.org/package/"
                                    name "/" name ".cabal"))))
    (call-with-temporary-output-file
     (lambda (temp port)
       (and (url-fetch url temp)
            (call-with-input-file temp read-cabal))))))

(define string->license
  ;; List of valid values from
  ;; https://www.haskell.org
  ;; /cabal/release/cabal-latest/doc/API/Cabal/Distribution-License.html.
  (match-lambda
   ("GPL-2" 'gpl2)
   ("GPL-3" 'gpl3)
   ("GPL" "'gpl??")
   ("AGPL-3" 'agpl3)
   ("AGPL" "'agpl??")
   ("LGPL-2.1" 'lgpl2.1)
   ("LGPL-3" 'lgpl3)
   ("LGPL" "'lgpl??")
   ("BSD2" 'bsd-2)
   ("BSD3" 'bsd-3)
   ("MIT" 'expat)
   ("ISC" 'isc)
   ("MPL" 'mpl2.0)
   ("Apache-2.0" 'asl2.0)
   ((x) (string->license x))
   ((lst ...) `(list ,@(map string->license lst)))
   (_ #f)))


(define (cabal-dependencies->names cabal include-test-dependencies?)
  "Return the list of dependencies names from the CABAL package object.  If
INCLUDE-TEST-DEPENDENCIES? is #f, do not include dependencies required by test
suites."
  (let* ((lib (cabal-package-library cabal))
         (lib-deps (if (pair? lib)
                       (map cabal-dependency-name
                            (append-map cabal-library-dependencies lib))
                       '()))
         (exe (cabal-package-executables cabal))
         (exe-deps (if (pair? exe)
                       (map cabal-dependency-name
                            (append-map cabal-executable-dependencies exe))
                       '()))
         (ts (cabal-package-test-suites cabal))
         (ts-deps (if (pair? ts)
                       (map cabal-dependency-name
                            (append-map cabal-test-suite-dependencies ts))
                       '())))
    (if include-test-dependencies?
        (delete-duplicates (append lib-deps exe-deps ts-deps))
        (delete-duplicates (append lib-deps exe-deps)))))

(define (filter-dependencies dependencies own-name)
  "Filter the dependencies included with the GHC compiler from DEPENDENCIES, a
list with the names of dependencies.  OWN-NAME is the name of the Cabal
package being processed and is used to filter references to itself."
  (filter (lambda (d) (not (member (string-downcase d)
                                   (cons own-name ghc-standard-libraries))))
          dependencies))

(define* (hackage-module->sexp cabal #:key (include-test-dependencies? #t))
  "Return the `package' S-expression for a Cabal package.  CABAL is the
representation of a Cabal file as produced by 'read-cabal'."

  (define name
    (cabal-package-name cabal))

  (define version
    (cabal-package-version cabal))
  
  (define source-url
    (string-append "http://hackage.haskell.org/package/" name
                   "/" name "-" version ".tar.gz"))

  (define dependencies
    (let ((names
           (map hackage-name->package-name
                ((compose (cut filter-dependencies <>
                               (cabal-package-name cabal))
                          (cut cabal-dependencies->names <>
                               include-test-dependencies?))
                 cabal))))
      (map (lambda (name)
             (list name (list 'unquote (string->symbol name))))
           names)))
  
  (define (maybe-inputs input-type inputs)
    (match inputs
      (()
       '())
      ((inputs ...)
       (list (list input-type
                   (list 'quasiquote inputs))))))
  
  (define (maybe-arguments)
    (if (not include-test-dependencies?)
        '((arguments `(#:tests? #f)))
        '()))

  (let ((tarball (with-store store
                   (download-to-store store source-url))))
    `(package
       (name ,(hackage-name->package-name name))
       (version ,version)
       (source (origin
                 (method url-fetch)
                 (uri (string-append ,@(factorize-uri source-url version)))
                 (sha256
                  (base32
                   ,(if tarball
                        (bytevector->nix-base32-string (file-sha256 tarball))
                        "failed to download tar archive")))))
       (build-system haskell-build-system)
       ,@(maybe-inputs 'inputs dependencies)
       ,@(maybe-arguments)
       (home-page ,(cabal-package-home-page cabal))
       (synopsis ,(cabal-package-synopsis cabal))
       (description ,(cabal-package-description cabal))
       (license ,(string->license (cabal-package-license cabal))))))

(define* (hackage->guix-package package-name #:key
                                (include-test-dependencies? #t)
                                (port #f)
                                (cabal-environment '()))
  "Fetch the Cabal file for PACKAGE-NAME from hackage.haskell.org, or, if the
called with keyword parameter PORT, from PORT.  Return the `package'
S-expression corresponding to that package, or #f on failure.
CABAL-ENVIRONMENT is an alist defining the environment in which the Cabal
conditionals are evaluated.  The accepted keys are: \"os\", \"arch\", \"impl\"
and the name of a flag.  The value associated with a flag has to be either the
symbol 'true' or 'false'.  The value associated with other keys has to conform
to the Cabal file format definition.  The default value associated with the
keys \"os\", \"arch\" and \"impl\" is \"linux\", \"x86_64\" and \"ghc\"
respectively."
  (let ((cabal-meta (if port
                        (read-cabal port)
                        (hackage-fetch package-name))))
    (and=> cabal-meta (compose (cut hackage-module->sexp <>
                                    #:include-test-dependencies? 
                                    include-test-dependencies?)
                               (cut eval-cabal <> cabal-environment)))))

;;; cabal.scm ends here