packages: Convert source derivations to monadic style.
[jackhill/guix/guix.git] / guix / git-download.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (guix git-download)
20 #:use-module (guix gexp)
21 #:use-module (guix store)
22 #:use-module (guix monads)
23 #:use-module (guix records)
24 #:use-module (guix packages)
25 #:autoload (guix build-system gnu) (standard-packages)
26 #:use-module (ice-9 match)
27 #:export (git-reference
28 git-reference?
29 git-reference-url
30 git-reference-commit
31 git-reference-recursive?
32
33 git-fetch))
34
35 ;;; Commentary:
36 ;;;
37 ;;; An <origin> method that fetches a specific commit from a Git repository.
38 ;;; The repository URL and commit hash are specified with a <git-reference>
39 ;;; object.
40 ;;;
41 ;;; Code:
42
43 (define-record-type* <git-reference>
44 git-reference make-git-reference
45 git-reference?
46 (url git-reference-url)
47 (commit git-reference-commit)
48 (recursive? git-reference-recursive? ; whether to recurse into sub-modules
49 (default #f)))
50
51 (define (git-package)
52 "Return the default Git package."
53 (let ((distro (resolve-interface '(gnu packages version-control))))
54 (module-ref distro 'git)))
55
56 (define* (git-fetch ref hash-algo hash
57 #:optional name
58 #:key (system (%current-system)) (guile (default-guile))
59 (git (git-package)))
60 "Return a fixed-output derivation that fetches REF, a <git-reference>
61 object. The output is expected to have recursive hash HASH of type
62 HASH-ALGO (a symbol). Use NAME as the file name, or a generic name if #f."
63 (define inputs
64 ;; When doing 'git clone --recursive', we need sed, grep, etc. to be
65 ;; available so that 'git submodule' works.
66 (if (git-reference-recursive? ref)
67 (standard-packages)
68 '()))
69
70 (define build
71 #~(begin
72 (use-modules (guix build git)
73 (guix build utils)
74 (ice-9 match))
75
76 ;; The 'git submodule' commands expects Coreutils, sed,
77 ;; grep, etc. to be in $PATH.
78 (set-path-environment-variable "PATH" '("bin")
79 (match '#$inputs
80 (((names dirs) ...)
81 dirs)))
82
83 (git-fetch '#$(git-reference-url ref)
84 '#$(git-reference-commit ref)
85 #$output
86 #:recursive? '#$(git-reference-recursive? ref)
87 #:git-command (string-append #$git "/bin/git"))))
88
89 (mlet %store-monad ((guile (package->derivation guile system)))
90 (gexp->derivation (or name "git-checkout") build
91 #:system system
92 ;; FIXME: See <https://bugs.gnu.org/18747>.
93 ;;#:local-build? #t
94 #:hash-algo hash-algo
95 #:hash hash
96 #:recursive? #t
97 #:modules '((guix build git)
98 (guix build utils))
99 #:guile-for-build guile
100 #:local-build? #t)))
101
102 ;;; git-download.scm ends here