aboutsummaryrefslogtreecommitdiff
path: root/guix/forge/utils.scm
blob: f23f76307d3139807c2efe7c3153a15b39352441 (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
;;; guix-forge --- Guix software forge meta-service
;;; Copyright © 2022 Arun Isaac <arunisaac@systemreboot.net>
;;;
;;; This file is part of guix-forge.
;;;
;;; guix-forge 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.
;;;
;;; guix-forge 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 guix-forge.  If not, see
;;; <https://www.gnu.org/licenses/>.

(define-module (forge utils)
  #:use-module (ice-9 match)
  #:use-module (guix derivations)
  #:use-module (guix gexp)
  #:use-module (guix modules)
  #:use-module (guix monads)
  #:use-module (guix profiles)
  #:use-module (guix search-paths)
  #:use-module (guix store)
  #:export (with-manifest
            with-packages))

(define (with-manifest manifest exp)
  "Return a gexp executing EXP, another gexp, in a profile defined by
MANIFEST."
  (with-imported-modules (source-module-closure '((guix search-paths)))
    #~(begin
        ;; Set the environment.
        ;; We pull out match-lambda using module-ref instead of using
        ;; use-modules because this gexp will be substituted into other
        ;; gexps and use-modules only works at the top-level.
        (let-syntax ((match-lambda (macro-transformer
                                    (module-ref (resolve-module '(ice-9 match))
                                                'match-lambda))))
          (let ((evaluate-search-paths (@@ (guix search-paths) evaluate-search-paths))
                (search-path-specification-variable (@@ (guix search-paths)
                                                        search-path-specification-variable))
                (sexp->search-path-specification (@@ (guix search-paths)
                                                     sexp->search-path-specification)))
            (for-each (match-lambda
                        ((specification . value)
                         (setenv (search-path-specification-variable specification)
                                 value)))
                      (evaluate-search-paths
                       (map sexp->search-path-specification
                            '#$(map search-path-specification->sexp
                                    (manifest-search-paths manifest)))
                       (list #$(profile
                                (content manifest)
                                (allow-collisions? #t)))))))
        ;; Run the provided expression.
        #$exp)))

(define (profile-with-packages packages)
  "Return profile with PACKAGES."
  (with-store store
    (run-with-store store
      (mlet* %store-monad ((prof-drv (profile-derivation
                                      (packages->manifest packages)))
                           (profile -> (derivation->output-path prof-drv)))
        (mbegin %store-monad
          (built-derivations (list prof-drv))
          (return profile))))))

(define (environment-with-packages packages)
  "Return environment of a profile with PACKAGES. Return value is an
association list mapping the name of an environment variable to its
value."
  (map (match-lambda
         ((search-path . value)
          (cons (search-path-specification-variable search-path)
                value)))
       (profile-search-paths (profile-with-packages packages))))

(define (with-packages packages exp)
  "Return a gexp executing EXP, another gexp, in an environment where
PACKAGES are available and their search path environment variables
have been set."
  #~(begin
      ;; Add a reference to the profile.
      #$(profile-with-packages packages)
      ;; Set the environment.
      ;; We pull out match-lambda using module-ref instead of using
      ;; use-modules because this gexp will be substituted into other
      ;; gexps and use-modules only works at the top-level.
      (let-syntax ((match-lambda (macro-transformer
                                  (module-ref (resolve-module '(ice-9 match))
                                              'match-lambda))))
        (for-each (match-lambda
                    ((variable . value)
                     (setenv variable value)))
                  '#$(environment-with-packages packages)))
      ;; Run the provided expression.
      #$exp))