Add builder-side utilities for phases, stream editing, & co.
[guix.git] / distro.scm
blob784e537debc24de5e877c6123438aee4ee08fd2e
1 ;;; Guix --- Nix package management from Guile.         -*- coding: utf-8 -*-
2 ;;; Copyright (C) 2012 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of Guix.
5 ;;;
6 ;;; 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 ;;; 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 Guix.  If not, see <http://www.gnu.org/licenses/>.
19 (define-module (distro)
20   #:use-module (guix packages)
21   #:use-module (ice-9 ftw)
22   #:use-module (srfi srfi-1)
23   #:use-module (srfi srfi-26)
24   #:export (find-packages-by-name))
26 ;;; Commentary:
27 ;;;
28 ;;; General utilities for the software distribution---i.e., the modules under
29 ;;; (distro ...).
30 ;;;
31 ;;; Code:
33 (define _ (cut gettext <> "guix"))
35 (define %distro-module-directory
36   ;; Absolute path of the (distro ...) module root.
37   (string-append (dirname (search-path %load-path "distro.scm"))
38                  "/distro"))
40 (define (package-files)
41   "Return the list of files that implement distro modules."
42   (define prefix-len
43     (string-length (dirname %distro-module-directory)))
45   (file-system-fold (const #t)                    ; enter?
46                     (lambda (path stat result)    ; leaf
47                       (if (string-suffix? ".scm" path)
48                           (cons (substring path prefix-len) result)
49                           result))
50                     (lambda (path stat result)    ; down
51                       result)
52                     (lambda (path stat result)    ; up
53                       result)
54                     (const #f)                    ; skip
55                     (lambda (path stat errno result)
56                       (format (current-error-port)
57                               (_ "warning: cannot access `~a': ~a")
58                               path (strerror errno))
59                       result)
60                     '()
61                     %distro-module-directory
62                     stat))
64 (define (package-modules)
65   "Return the list of modules that provide packages for the distribution."
66   (define not-slash
67     (char-set-complement (char-set #\/)))
69   (filter-map (lambda (path)
70                 (let ((name (map string->symbol
71                                  (string-tokenize (string-drop-right path 4)
72                                                   not-slash))))
73                   (false-if-exception (resolve-interface name))))
74               (package-files)))
76 (define* (find-packages-by-name name #:optional version)
77   "Return the list of packages with the given NAME.  If VERSION is not #f,
78 then only return packages whose version is equal to VERSION."
79   (define right-package?
80     (if version
81         (lambda (p)
82           (and (package? p)
83                (string=? (package-name p) name)
84                (string=? (package-version p) version)))
85         (lambda (p)
86           (and (package? p)
87                (string=? (package-name p) name)))))
89   (append-map (lambda (module)
90                 (filter right-package?
91                         (module-map (lambda (sym var)
92                                       (variable-ref var))
93                                     module)))
94               (package-modules)))