45ca7e3fcf1ba1e85656f7ef81c4f0e88d3c61d4
[jackhill/guix/guix.git] / guix / scripts / import / nix.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2016 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2014 David Thompson <davet@gnu.org>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
11 ;;;
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
16 ;;;
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20 (define-module (guix scripts import nix)
21 #:use-module (guix ui)
22 #:use-module (guix utils)
23 #:use-module (guix scripts)
24 #:use-module (guix import snix)
25 #:use-module (guix scripts import)
26 #:use-module (srfi srfi-1)
27 #:use-module (srfi srfi-11)
28 #:use-module (srfi srfi-37)
29 #:use-module (ice-9 match)
30 #:export (guix-import-nix))
31
32 \f
33 ;;;
34 ;;; Command-line options.
35 ;;;
36
37 (define %default-options
38 '())
39
40 (define (show-help)
41 (display (G_ "Usage: guix import nix NIXPKGS ATTRIBUTE
42 Import and convert the Nix expression ATTRIBUTE of NIXPKGS.\n"))
43 (display (G_ "
44 -h, --help display this help and exit"))
45 (display (G_ "
46 -V, --version display version information and exit"))
47 (newline)
48 (show-bug-report-information))
49
50 (define %options
51 ;; Specification of the command-line options.
52 (cons* (option '(#\h "help") #f #f
53 (lambda args
54 (show-help)
55 (exit 0)))
56 (option '(#\V "version") #f #f
57 (lambda args
58 (show-version-and-exit "guix import nix")))
59 %standard-import-options))
60
61 \f
62 ;;;
63 ;;; Entry point.
64 ;;;
65
66 (define (guix-import-nix . args)
67 (define (parse-options)
68 ;; Return the alist of option values.
69 (args-fold* args %options
70 (lambda (opt name arg result)
71 (leave (G_ "~A: unrecognized option~%") name))
72 (lambda (arg result)
73 (alist-cons 'argument arg result))
74 %default-options))
75
76 (let* ((opts (parse-options))
77 (args (filter-map (match-lambda
78 (('argument . value)
79 value)
80 (_ #f))
81 (reverse opts))))
82 (match args
83 ((nixpkgs attribute)
84 (let-values (((expr loc)
85 (nixpkgs->guix-package nixpkgs attribute)))
86 (format #t ";; converted from ~a:~a~%~%"
87 (location-file loc) (location-line loc))
88 expr))
89 (x
90 (leave (G_ "wrong number of arguments~%"))))))