pack.scm 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2017, 2018 Ricardo Wurmus <rekado@elephly.net>
  4. ;;; Copyright © 2018 Konrad Hinsen <konrad.hinsen@fastmail.net>
  5. ;;; Copyright © 2018 Chris Marusich <cmmarusich@gmail.com>
  6. ;;; Copyright © 2018 Efraim Flashner <efraim@flashner.co.il>
  7. ;;; Copyright © 2020 Tobias Geerinckx-Rice <me@tobias.gr>
  8. ;;; Copyright © 2020 Eric Bavier <bavier@posteo.net>
  9. ;;;
  10. ;;; This file is part of GNU Guix.
  11. ;;;
  12. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  13. ;;; under the terms of the GNU General Public License as published by
  14. ;;; the Free Software Foundation; either version 3 of the License, or (at
  15. ;;; your option) any later version.
  16. ;;;
  17. ;;; GNU Guix is distributed in the hope that it will be useful, but
  18. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;;; GNU General Public License for more details.
  21. ;;;
  22. ;;; You should have received a copy of the GNU General Public License
  23. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  24. (define-module (guix scripts pack)
  25. #:use-module (guix scripts)
  26. #:use-module (guix ui)
  27. #:use-module (guix gexp)
  28. #:use-module (guix utils)
  29. #:use-module (guix store)
  30. #:use-module ((guix status) #:select (with-status-verbosity))
  31. #:use-module ((guix self) #:select (make-config.scm))
  32. #:use-module (guix grafts)
  33. #:autoload (guix inferior) (inferior-package?
  34. inferior-package-name
  35. inferior-package-version)
  36. #:use-module (guix monads)
  37. #:use-module (guix modules)
  38. #:use-module (guix packages)
  39. #:use-module (guix profiles)
  40. #:use-module (guix describe)
  41. #:use-module (guix derivations)
  42. #:use-module (guix search-paths)
  43. #:use-module (guix build-system gnu)
  44. #:use-module (guix scripts build)
  45. #:use-module (guix transformations)
  46. #:use-module ((guix self) #:select (make-config.scm))
  47. #:use-module (gnu packages)
  48. #:use-module (gnu packages bootstrap)
  49. #:use-module ((gnu packages compression) #:hide (zip))
  50. #:use-module (gnu packages guile)
  51. #:use-module (gnu packages base)
  52. #:autoload (gnu packages package-management) (guix)
  53. #:autoload (gnu packages gnupg) (guile-gcrypt)
  54. #:autoload (gnu packages guile) (guile2.0-json guile-json)
  55. #:use-module (srfi srfi-1)
  56. #:use-module (srfi srfi-9)
  57. #:use-module (srfi srfi-26)
  58. #:use-module (srfi srfi-37)
  59. #:use-module (ice-9 match)
  60. #:export (compressor?
  61. compressor-name
  62. compressor-extenstion
  63. compressor-command
  64. %compressors
  65. lookup-compressor
  66. self-contained-tarball
  67. docker-image
  68. squashfs-image
  69. %formats
  70. guix-pack))
  71. ;; Type of a compression tool.
  72. (define-record-type <compressor>
  73. (compressor name extension command)
  74. compressor?
  75. (name compressor-name) ;string (e.g., "gzip")
  76. (extension compressor-extension) ;string (e.g., ".lz")
  77. (command compressor-command)) ;gexp (e.g., #~("/gnu/store/…/gzip" "-9n"))
  78. (define %compressors
  79. ;; Available compression tools.
  80. (list (compressor "gzip" ".gz"
  81. #~(#+(file-append gzip "/bin/gzip") "-9n"))
  82. (compressor "lzip" ".lz"
  83. #~(#+(file-append lzip "/bin/lzip") "-9"))
  84. (compressor "xz" ".xz"
  85. #~(#+(file-append xz "/bin/xz") "-e"))
  86. (compressor "bzip2" ".bz2"
  87. #~(#+(file-append bzip2 "/bin/bzip2") "-9"))
  88. (compressor "zstd" ".zst"
  89. ;; The default level 3 compresses better than gzip in a
  90. ;; fraction of the time, while the highest level 19
  91. ;; (de)compresses more slowly and worse than xz.
  92. #~(#+(file-append zstd "/bin/zstd") "-3"))
  93. (compressor "none" "" #f)))
  94. ;; This one is only for use in this module, so don't put it in %compressors.
  95. (define bootstrap-xz
  96. (compressor "bootstrap-xz" ".xz"
  97. #~(#+(file-append %bootstrap-coreutils&co "/bin/xz") "-e")))
  98. (define (lookup-compressor name)
  99. "Return the compressor object called NAME. Error out if it could not be
  100. found."
  101. (or (find (match-lambda
  102. (($ <compressor> name*)
  103. (string=? name* name)))
  104. %compressors)
  105. (leave (G_ "~a: compressor not found~%") name)))
  106. (define not-config?
  107. ;; Select (guix …) and (gnu …) modules, except (guix config).
  108. (match-lambda
  109. (('guix 'config) #f)
  110. (('guix _ ...) #t)
  111. (('gnu _ ...) #t)
  112. (_ #f)))
  113. (define gcrypt-sqlite3&co
  114. ;; Guile-Gcrypt, Guile-SQLite3, and their propagated inputs.
  115. (append-map (lambda (package)
  116. (cons package
  117. (match (package-transitive-propagated-inputs package)
  118. (((labels packages) ...)
  119. packages))))
  120. (list guile-gcrypt guile-sqlite3)))
  121. (define (store-database items)
  122. "Return a directory containing a store database where all of ITEMS and their
  123. dependencies are registered."
  124. (define schema
  125. (local-file (search-path %load-path
  126. "guix/store/schema.sql")))
  127. (define labels
  128. (map (lambda (n)
  129. (string-append "closure" (number->string n)))
  130. (iota (length items))))
  131. (define build
  132. (with-extensions gcrypt-sqlite3&co
  133. (with-imported-modules `(((guix config) => ,(make-config.scm))
  134. ,@(source-module-closure
  135. '((guix build store-copy)
  136. (guix store database))
  137. #:select? not-config?))
  138. #~(begin
  139. (use-modules (guix store database)
  140. (guix build store-copy)
  141. (srfi srfi-1))
  142. (define (read-closure closure)
  143. (call-with-input-file closure read-reference-graph))
  144. (define db-file
  145. (store-database-file #:state-directory #$output))
  146. ;; Make sure non-ASCII file names are properly handled.
  147. (setenv "GUIX_LOCPATH"
  148. #+(file-append glibc-utf8-locales "/lib/locale"))
  149. (setlocale LC_ALL "en_US.utf8")
  150. (sql-schema #$schema)
  151. (let ((items (append-map read-closure '#$labels)))
  152. (with-database db-file db
  153. (register-items db items
  154. #:registration-time %epoch)))))))
  155. (computed-file "store-database" build
  156. #:options `(#:references-graphs ,(zip labels items))))
  157. (define* (self-contained-tarball name profile
  158. #:key target
  159. (profile-name "guix-profile")
  160. deduplicate?
  161. entry-point
  162. (compressor (first %compressors))
  163. localstatedir?
  164. (symlinks '())
  165. (archiver tar))
  166. "Return a self-contained tarball containing a store initialized with the
  167. closure of PROFILE, a derivation. The tarball contains /gnu/store; if
  168. LOCALSTATEDIR? is true, it also contains /var/guix, including /var/guix/db
  169. with a properly initialized store database.
  170. SYMLINKS must be a list of (SOURCE -> TARGET) tuples denoting symlinks to be
  171. added to the pack."
  172. (define database
  173. (and localstatedir?
  174. (file-append (store-database (list profile))
  175. "/db/db.sqlite")))
  176. (define set-utf8-locale
  177. ;; Arrange to not depend on 'glibc-utf8-locales' when using '--bootstrap'.
  178. (and (or (not (profile? profile))
  179. (profile-locales? profile))
  180. #~(begin
  181. (setenv "GUIX_LOCPATH"
  182. #+(file-append glibc-utf8-locales "/lib/locale"))
  183. (setlocale LC_ALL "en_US.utf8"))))
  184. (define (import-module? module)
  185. ;; Since we don't use deduplication support in 'populate-store', don't
  186. ;; import (guix store deduplication) and its dependencies, which includes
  187. ;; Guile-Gcrypt. That way we can run tests with '--bootstrap'.
  188. (and (not-config? module)
  189. (not (equal? '(guix store deduplication) module))))
  190. (define build
  191. (with-imported-modules (source-module-closure
  192. `((guix build utils)
  193. (guix build union)
  194. (gnu build install))
  195. #:select? import-module?)
  196. #~(begin
  197. (use-modules (guix build utils)
  198. ((guix build union) #:select (relative-file-name))
  199. (gnu build install)
  200. (srfi srfi-1)
  201. (srfi srfi-26)
  202. (ice-9 match))
  203. (define %root "root")
  204. (define symlink->directives
  205. ;; Return "populate directives" to make the given symlink and its
  206. ;; parent directories.
  207. (match-lambda
  208. ((source '-> target)
  209. (let ((target (string-append #$profile "/" target))
  210. (parent (dirname source)))
  211. ;; Never add a 'directory' directive for "/" so as to
  212. ;; preserve its ownnership when extracting the archive (see
  213. ;; below), and also because this would lead to adding the
  214. ;; same entries twice in the tarball.
  215. `(,@(if (string=? parent "/")
  216. '()
  217. `((directory ,parent)))
  218. (,source
  219. -> ,(relative-file-name parent target)))))))
  220. (define directives
  221. ;; Fully-qualified symlinks.
  222. (append-map symlink->directives '#$symlinks))
  223. ;; The --sort option was added to GNU tar in version 1.28, released
  224. ;; 2014-07-28. For testing, we use the bootstrap tar, which is
  225. ;; older and doesn't support it.
  226. (define tar-supports-sort?
  227. (zero? (system* (string-append #+archiver "/bin/tar")
  228. "cf" "/dev/null" "--files-from=/dev/null"
  229. "--sort=name")))
  230. ;; Make sure non-ASCII file names are properly handled.
  231. #+set-utf8-locale
  232. ;; Add 'tar' to the search path.
  233. (setenv "PATH" #+(file-append archiver "/bin"))
  234. ;; Note: there is not much to gain here with deduplication and there
  235. ;; is the overhead of the '.links' directory, so turn it off.
  236. ;; Furthermore GNU tar < 1.30 sometimes fails to extract tarballs
  237. ;; with hard links:
  238. ;; <http://lists.gnu.org/archive/html/bug-tar/2017-11/msg00009.html>.
  239. (populate-single-profile-directory %root
  240. #:profile #$profile
  241. #:profile-name #$profile-name
  242. #:closure "profile"
  243. #:database #+database)
  244. ;; Create SYMLINKS.
  245. (for-each (cut evaluate-populate-directive <> %root)
  246. directives)
  247. ;; Create the tarball. Use GNU format so there's no file name
  248. ;; length limitation.
  249. (with-directory-excursion %root
  250. (exit
  251. (zero? (apply system* "tar"
  252. #+@(if (compressor-command compressor)
  253. #~("-I"
  254. (string-join
  255. '#+(compressor-command compressor)))
  256. #~())
  257. "--format=gnu"
  258. ;; Avoid non-determinism in the archive. Use
  259. ;; mtime = 1, not zero, because that is what the
  260. ;; daemon does for files in the store (see the
  261. ;; 'mtimeStore' constant in local-store.cc.)
  262. (if tar-supports-sort? "--sort=name" "--mtime=@1")
  263. "--mtime=@1" ;for files in /var/guix
  264. "--owner=root:0"
  265. "--group=root:0"
  266. "--check-links"
  267. "-cvf" #$output
  268. ;; Avoid adding / and /var to the tarball, so
  269. ;; that the ownership and permissions of those
  270. ;; directories will not be overwritten when
  271. ;; extracting the archive. Do not include /root
  272. ;; because the root account might have a
  273. ;; different home directory.
  274. #$@(if localstatedir?
  275. '("./var/guix")
  276. '())
  277. (string-append "." (%store-directory))
  278. (delete-duplicates
  279. (filter-map (match-lambda
  280. (('directory directory)
  281. (string-append "." directory))
  282. ((source '-> _)
  283. (string-append "." source))
  284. (_ #f))
  285. directives)))))))))
  286. (when entry-point
  287. (warning (G_ "entry point not supported in the '~a' format~%")
  288. 'tarball))
  289. (gexp->derivation (string-append name ".tar"
  290. (compressor-extension compressor))
  291. build
  292. #:target target
  293. #:references-graphs `(("profile" ,profile))))
  294. (define (singularity-environment-file profile)
  295. "Return a shell script that defines the environment variables corresponding
  296. to the search paths of PROFILE."
  297. (define build
  298. (with-extensions (list guile-gcrypt)
  299. (with-imported-modules `(((guix config) => ,(make-config.scm))
  300. ,@(source-module-closure
  301. `((guix profiles)
  302. (guix search-paths))
  303. #:select? not-config?))
  304. #~(begin
  305. (use-modules (guix profiles) (guix search-paths)
  306. (ice-9 match))
  307. (call-with-output-file #$output
  308. (lambda (port)
  309. (for-each (match-lambda
  310. ((spec . value)
  311. (format port "~a=~a~%export ~a~%"
  312. (search-path-specification-variable spec)
  313. value
  314. (search-path-specification-variable spec))))
  315. (profile-search-paths #$profile))))))))
  316. (computed-file "singularity-environment.sh" build))
  317. (define* (squashfs-image name profile
  318. #:key target
  319. (profile-name "guix-profile")
  320. (compressor (first %compressors))
  321. entry-point
  322. localstatedir?
  323. (symlinks '())
  324. (archiver squashfs-tools))
  325. "Return a squashfs image containing a store initialized with the closure of
  326. PROFILE, a derivation. The image contains a subset of /gnu/store, empty mount
  327. points for virtual file systems (like procfs), and optional symlinks.
  328. SYMLINKS must be a list of (SOURCE -> TARGET) tuples denoting symlinks to be
  329. added to the pack."
  330. (define database
  331. (and localstatedir?
  332. (file-append (store-database (list profile))
  333. "/db/db.sqlite")))
  334. (define environment
  335. (singularity-environment-file profile))
  336. (define symlinks*
  337. ;; Singularity requires /bin (specifically /bin/sh), so ensure that
  338. ;; symlink is created.
  339. (if (find (match-lambda
  340. (("/bin" . _) #t)
  341. (_ #f))
  342. symlinks)
  343. symlinks
  344. `(("/bin" -> "bin") ,@symlinks)))
  345. (define build
  346. (with-extensions (list guile-gcrypt)
  347. (with-imported-modules (source-module-closure
  348. '((guix build utils)
  349. (guix build store-copy)
  350. (guix build union)
  351. (gnu build install))
  352. #:select? not-config?)
  353. #~(begin
  354. (use-modules (guix build utils)
  355. (guix build store-copy)
  356. ((guix build union) #:select (relative-file-name))
  357. (gnu build install)
  358. (srfi srfi-1)
  359. (srfi srfi-26)
  360. (ice-9 match))
  361. (define database #+database)
  362. (define entry-point #$entry-point)
  363. (define (mksquashfs args)
  364. (apply invoke "mksquashfs"
  365. `(,@args
  366. ;; Do not create a "recovery file" when appending to the
  367. ;; file system since it's useless in this case.
  368. "-no-recovery"
  369. ;; Do not attempt to store extended attributes.
  370. ;; See <https://bugs.gnu.org/40043>.
  371. "-no-xattrs"
  372. ;; Set file times and the file system creation time to
  373. ;; one second after the Epoch.
  374. "-all-time" "1" "-mkfs-time" "1"
  375. ;; Reset all UIDs and GIDs.
  376. "-force-uid" "0" "-force-gid" "0")))
  377. (setenv "PATH" #+(file-append archiver "/bin"))
  378. ;; We need an empty file in order to have a valid file argument when
  379. ;; we reparent the root file system. Read on for why that's
  380. ;; necessary.
  381. (with-output-to-file ".empty" (lambda () (display "")))
  382. ;; Create the squashfs image in several steps.
  383. ;; Add all store items. Unfortunately mksquashfs throws away all
  384. ;; ancestor directories and only keeps the basename. We fix this
  385. ;; in the following invocations of mksquashfs.
  386. (mksquashfs `(,@(map store-info-item
  387. (call-with-input-file "profile"
  388. read-reference-graph))
  389. #$environment
  390. ,#$output
  391. ;; Do not perform duplicate checking because we
  392. ;; don't have any dupes.
  393. "-no-duplicates"
  394. "-comp"
  395. ,#+(compressor-name compressor)))
  396. ;; Here we reparent the store items. For each sub-directory of
  397. ;; the store prefix we need one invocation of "mksquashfs".
  398. (for-each (lambda (dir)
  399. (mksquashfs `(".empty"
  400. ,#$output
  401. "-root-becomes" ,dir)))
  402. (reverse (string-tokenize (%store-directory)
  403. (char-set-complement (char-set #\/)))))
  404. ;; Add symlinks and mount points.
  405. (mksquashfs
  406. `(".empty"
  407. ,#$output
  408. ;; Create SYMLINKS via pseudo file definitions.
  409. ,@(append-map
  410. (match-lambda
  411. ((source '-> target)
  412. ;; Create relative symlinks to work around a bug in
  413. ;; Singularity 2.x:
  414. ;; https://bugs.gnu.org/34913
  415. ;; https://github.com/sylabs/singularity/issues/1487
  416. (let ((target (string-append #$profile "/" target)))
  417. (list "-p"
  418. (string-join
  419. ;; name s mode uid gid symlink
  420. (list source
  421. "s" "777" "0" "0"
  422. (relative-file-name (dirname source)
  423. target)))))))
  424. '#$symlinks*)
  425. "-p" "/.singularity.d d 555 0 0"
  426. ;; Create the environment file.
  427. "-p" "/.singularity.d/env d 555 0 0"
  428. "-p" ,(string-append
  429. "/.singularity.d/env/90-environment.sh s 777 0 0 "
  430. (relative-file-name "/.singularity.d/env"
  431. #$environment))
  432. ;; Create /.singularity.d/actions, and optionally the 'run'
  433. ;; script, used by 'singularity run'.
  434. "-p" "/.singularity.d/actions d 555 0 0"
  435. ,@(if entry-point
  436. `( ;; This one if for Singularity 2.x.
  437. "-p"
  438. ,(string-append
  439. "/.singularity.d/actions/run s 777 0 0 "
  440. (relative-file-name "/.singularity.d/actions"
  441. (string-append #$profile "/"
  442. entry-point)))
  443. ;; This one is for Singularity 3.x.
  444. "-p"
  445. ,(string-append
  446. "/.singularity.d/runscript s 777 0 0 "
  447. (relative-file-name "/.singularity.d"
  448. (string-append #$profile "/"
  449. entry-point))))
  450. '())
  451. ;; Create empty mount points.
  452. "-p" "/proc d 555 0 0"
  453. "-p" "/sys d 555 0 0"
  454. "-p" "/dev d 555 0 0"
  455. "-p" "/home d 555 0 0"))
  456. (when database
  457. ;; Initialize /var/guix.
  458. (install-database-and-gc-roots "var-etc" database #$profile)
  459. (mksquashfs `("var-etc" ,#$output)))))))
  460. (gexp->derivation (string-append name
  461. (compressor-extension compressor)
  462. ".squashfs")
  463. build
  464. #:target target
  465. #:references-graphs `(("profile" ,profile))))
  466. (define* (docker-image name profile
  467. #:key target
  468. (profile-name "guix-profile")
  469. (compressor (first %compressors))
  470. entry-point
  471. localstatedir?
  472. (symlinks '())
  473. (archiver tar))
  474. "Return a derivation to construct a Docker image of PROFILE. The
  475. image is a tarball conforming to the Docker Image Specification, compressed
  476. with COMPRESSOR. It can be passed to 'docker load'. If TARGET is true, it
  477. must a be a GNU triplet and it is used to derive the architecture metadata in
  478. the image."
  479. (define database
  480. (and localstatedir?
  481. (file-append (store-database (list profile))
  482. "/db/db.sqlite")))
  483. (define defmod 'define-module) ;trick Geiser
  484. (define build
  485. ;; Guile-JSON and Guile-Gcrypt are required by (guix docker).
  486. (with-extensions (list guile-json-3 guile-gcrypt)
  487. (with-imported-modules `(((guix config) => ,(make-config.scm))
  488. ,@(source-module-closure
  489. `((guix docker)
  490. (guix build store-copy)
  491. (guix profiles)
  492. (guix search-paths))
  493. #:select? not-config?))
  494. #~(begin
  495. (use-modules (guix docker) (guix build store-copy)
  496. (guix profiles) (guix search-paths)
  497. (srfi srfi-1) (srfi srfi-19)
  498. (ice-9 match))
  499. (define environment
  500. (map (match-lambda
  501. ((spec . value)
  502. (cons (search-path-specification-variable spec)
  503. value)))
  504. (profile-search-paths #$profile)))
  505. (define symlink->directives
  506. ;; Return "populate directives" to make the given symlink and its
  507. ;; parent directories.
  508. (match-lambda
  509. ((source '-> target)
  510. (let ((target (string-append #$profile "/" target))
  511. (parent (dirname source)))
  512. `((directory ,parent)
  513. (,source -> ,target))))))
  514. (define directives
  515. ;; Create a /tmp directory, as some programs expect it, and
  516. ;; create SYMLINKS.
  517. `((directory "/tmp" ,(getuid) ,(getgid) #o1777)
  518. ,@(append-map symlink->directives '#$symlinks)))
  519. (define tag
  520. ;; Compute a meaningful "repository" name, which will show up in
  521. ;; the output of "docker images".
  522. (let ((manifest (profile-manifest #$profile)))
  523. (let loop ((names (map manifest-entry-name
  524. (manifest-entries manifest))))
  525. (define str (string-join names "-"))
  526. (if (< (string-length str) 40)
  527. str
  528. (match names
  529. ((_) str)
  530. ((names ... _) (loop names))))))) ;drop one entry
  531. (setenv "PATH" #+(file-append archiver "/bin"))
  532. (build-docker-image #$output
  533. (map store-info-item
  534. (call-with-input-file "profile"
  535. read-reference-graph))
  536. #$profile
  537. #:repository tag
  538. #:database #+database
  539. #:system (or #$target (utsname:machine (uname)))
  540. #:environment environment
  541. #:entry-point
  542. #$(and entry-point
  543. #~(list (string-append #$profile "/"
  544. #$entry-point)))
  545. #:extra-files directives
  546. #:compressor '#+(compressor-command compressor)
  547. #:creation-time (make-time time-utc 0 1))))))
  548. (gexp->derivation (string-append name ".tar"
  549. (compressor-extension compressor))
  550. build
  551. #:target target
  552. #:references-graphs `(("profile" ,profile))))
  553. ;;;
  554. ;;; Compiling C programs.
  555. ;;;
  556. ;; A C compiler. That lowers to a single program that can be passed typical C
  557. ;; compiler flags, and it makes sure the whole toolchain is available.
  558. (define-record-type <c-compiler>
  559. (%c-compiler toolchain guile)
  560. c-compiler?
  561. (toolchain c-compiler-toolchain)
  562. (guile c-compiler-guile))
  563. (define* (c-compiler #:optional inputs
  564. #:key (guile (default-guile)))
  565. (%c-compiler inputs guile))
  566. (define (bootstrap-c-compiler)
  567. "Return the C compiler that uses the bootstrap toolchain. This is used only
  568. by '--bootstrap', for testing purposes."
  569. (define bootstrap-toolchain
  570. (list (first (assoc-ref (%bootstrap-inputs) "gcc"))
  571. (first (assoc-ref (%bootstrap-inputs) "binutils"))
  572. (first (assoc-ref (%bootstrap-inputs) "libc"))))
  573. (c-compiler bootstrap-toolchain
  574. #:guile %bootstrap-guile))
  575. (define-gexp-compiler (c-compiler-compiler (compiler <c-compiler>) system target)
  576. "Lower COMPILER to a single script that does the right thing."
  577. (define toolchain
  578. (or (c-compiler-toolchain compiler)
  579. (list (first (assoc-ref (standard-packages) "gcc"))
  580. (first (assoc-ref (standard-packages) "ld-wrapper"))
  581. (first (assoc-ref (standard-packages) "binutils"))
  582. (first (assoc-ref (standard-packages) "libc"))
  583. (gexp-input (first (assoc-ref (standard-packages) "libc"))
  584. "static"))))
  585. (define inputs
  586. (match (append-map package-propagated-inputs
  587. (filter package? toolchain))
  588. (((labels things . _) ...)
  589. (append toolchain things))))
  590. (define search-paths
  591. (cons $PATH
  592. (append-map package-native-search-paths
  593. (filter package? inputs))))
  594. (define run
  595. (with-imported-modules (source-module-closure
  596. '((guix build utils)
  597. (guix search-paths)))
  598. #~(begin
  599. (use-modules (guix build utils) (guix search-paths)
  600. (ice-9 match))
  601. (define (output-file args)
  602. (let loop ((args args))
  603. (match args
  604. (() "a.out")
  605. (("-o" file _ ...) file)
  606. ((head rest ...) (loop rest)))))
  607. (set-search-paths (map sexp->search-path-specification
  608. '#$(map search-path-specification->sexp
  609. search-paths))
  610. '#$inputs)
  611. (let ((output (output-file (command-line))))
  612. (apply invoke "gcc" (cdr (command-line)))
  613. (invoke "strip" output)))))
  614. (when target
  615. ;; TODO: Yep, we'll have to do it someday!
  616. (leave (G_ "cross-compilation not implemented here;
  617. please email '~a'~%")
  618. (@ (guix config) %guix-bug-report-address)))
  619. (gexp->script "c-compiler" run
  620. #:guile (c-compiler-guile compiler)))
  621. ;;;
  622. ;;; Wrapped package.
  623. ;;;
  624. (define* (wrapped-package package
  625. #:optional
  626. (output* "out")
  627. (compiler (c-compiler))
  628. #:key proot?)
  629. "Return the OUTPUT of PACKAGE with its binaries wrapped such that they are
  630. relocatable. When PROOT? is true, include PRoot in the result and use it as a
  631. last resort for relocation."
  632. (define runner
  633. (local-file (search-auxiliary-file "run-in-namespace.c")))
  634. (define audit-source
  635. (local-file (search-auxiliary-file "pack-audit.c")))
  636. (define (proot)
  637. (specification->package "proot-static"))
  638. (define (fakechroot-library)
  639. (computed-file "libfakechroot.so"
  640. #~(copy-file #$(file-append
  641. (specification->package "fakechroot")
  642. "/lib/fakechroot/libfakechroot.so")
  643. #$output)))
  644. (define (audit-module)
  645. ;; Return an ld.so audit module for use by the 'fakechroot' execution
  646. ;; engine that translates file names of all the files ld.so loads.
  647. (computed-file "pack-audit.so"
  648. (with-imported-modules '((guix build utils))
  649. #~(begin
  650. (use-modules (guix build utils))
  651. (copy-file #$audit-source "audit.c")
  652. (substitute* "audit.c"
  653. (("@STORE_DIRECTORY@")
  654. (%store-directory)))
  655. (invoke #$compiler "-std=gnu99"
  656. "-shared" "-fPIC" "-Os" "-g0"
  657. "-Wall" "audit.c" "-o" #$output)))))
  658. (define build
  659. (with-imported-modules (source-module-closure
  660. '((guix build utils)
  661. (guix build union)
  662. (guix build gremlin)
  663. (guix elf)))
  664. #~(begin
  665. (use-modules (guix build utils)
  666. ((guix build union) #:select (symlink-relative))
  667. (guix elf)
  668. (guix build gremlin)
  669. (ice-9 binary-ports)
  670. (ice-9 ftw)
  671. (ice-9 match)
  672. (ice-9 receive)
  673. (srfi srfi-1)
  674. (rnrs bytevectors))
  675. (define input
  676. ;; The OUTPUT* output of PACKAGE.
  677. (ungexp package output*))
  678. (define target
  679. ;; The output we are producing.
  680. (ungexp output output*))
  681. (define (strip-store-prefix file)
  682. ;; Given a file name like "/gnu/store/…-foo-1.2/bin/foo", return
  683. ;; "/bin/foo".
  684. (let* ((len (string-length (%store-directory)))
  685. (base (string-drop file (+ 1 len))))
  686. (match (string-index base #\/)
  687. (#f base)
  688. (index (string-drop base index)))))
  689. (define (elf-interpreter elf)
  690. ;; Return the interpreter of ELF as a string, or #f if ELF has no
  691. ;; interpreter segment.
  692. (match (find (lambda (segment)
  693. (= (elf-segment-type segment) PT_INTERP))
  694. (elf-segments elf))
  695. (#f #f) ;maybe a .so
  696. (segment
  697. (let ((bv (make-bytevector (- (elf-segment-memsz segment) 1))))
  698. (bytevector-copy! (elf-bytes elf)
  699. (elf-segment-offset segment)
  700. bv 0 (bytevector-length bv))
  701. (utf8->string bv)))))
  702. (define (runpath file)
  703. ;; Return the RUNPATH of FILE as a list of directories.
  704. (let* ((bv (call-with-input-file file get-bytevector-all))
  705. (elf (parse-elf bv))
  706. (dyninfo (elf-dynamic-info elf)))
  707. (or (and=> dyninfo elf-dynamic-info-runpath)
  708. '())))
  709. (define (elf-loader-compile-flags program)
  710. ;; Return the cpp flags defining macros for the ld.so/fakechroot
  711. ;; wrapper of PROGRAM.
  712. ;; TODO: Handle scripts by wrapping their interpreter.
  713. (if (elf-file? program)
  714. (let* ((bv (call-with-input-file program
  715. get-bytevector-all))
  716. (elf (parse-elf bv))
  717. (interp (elf-interpreter elf))
  718. (gconv (and interp
  719. (string-append (dirname interp)
  720. "/gconv"))))
  721. (if interp
  722. (list (string-append "-DPROGRAM_INTERPRETER=\""
  723. interp "\"")
  724. (string-append "-DFAKECHROOT_LIBRARY=\""
  725. #$(fakechroot-library) "\"")
  726. (string-append "-DLOADER_AUDIT_MODULE=\""
  727. #$(audit-module) "\"")
  728. ;; XXX: Normally (runpath #$(audit-module)) is
  729. ;; enough. However, to work around
  730. ;; <https://sourceware.org/bugzilla/show_bug.cgi?id=26634>
  731. ;; (glibc <= 2.32), pass the whole search path of
  732. ;; PROGRAM, which presumably is a superset of that
  733. ;; of the audit module.
  734. (string-append "-DLOADER_AUDIT_RUNPATH={ "
  735. (string-join
  736. (map object->string
  737. (runpath program))
  738. ", " 'suffix)
  739. "NULL }")
  740. (if gconv
  741. (string-append "-DGCONV_DIRECTORY=\""
  742. gconv "\"")
  743. "-UGCONV_DIRECTORY"))
  744. '()))
  745. '()))
  746. (define (build-wrapper program)
  747. ;; Build a user-namespace wrapper for PROGRAM.
  748. (format #t "building wrapper for '~a'...~%" program)
  749. (copy-file #$runner "run.c")
  750. (substitute* "run.c"
  751. (("@WRAPPED_PROGRAM@") program)
  752. (("@STORE_DIRECTORY@") (%store-directory)))
  753. (let* ((base (strip-store-prefix program))
  754. (result (string-append target base))
  755. (proot #$(and proot?
  756. #~(string-drop
  757. #$(file-append (proot) "/bin/proot")
  758. (+ (string-length (%store-directory))
  759. 1)))))
  760. (mkdir-p (dirname result))
  761. (apply invoke #$compiler "-std=gnu99" "-static" "-Os" "-g0" "-Wall"
  762. "run.c" "-o" result
  763. (string-append "-DWRAPPER_PROGRAM=\""
  764. (canonicalize-path (dirname result)) "/"
  765. (basename result) "\"")
  766. (append (if proot
  767. (list (string-append "-DPROOT_PROGRAM=\""
  768. proot "\""))
  769. '())
  770. (elf-loader-compile-flags program)))
  771. (delete-file "run.c")))
  772. (setvbuf (current-output-port) 'line)
  773. ;; Link the top-level files of PACKAGE so that search paths are
  774. ;; properly defined in PROFILE/etc/profile.
  775. (mkdir target)
  776. (for-each (lambda (file)
  777. (unless (member file '("." ".." "bin" "sbin" "libexec"))
  778. (symlink-relative (string-append input "/" file)
  779. (string-append target "/" file))))
  780. (scandir input))
  781. (receive (executables others)
  782. (partition executable-file?
  783. ;; Note: Trailing slash in case these are symlinks.
  784. (append (find-files (string-append input "/bin/"))
  785. (find-files (string-append input "/sbin/"))
  786. (find-files (string-append input "/libexec/"))))
  787. ;; Wrap only executables, since the wrapper will eventually need
  788. ;; to execve them. E.g. git's "libexec" directory contains many
  789. ;; shell scripts that are source'd from elsewhere, which fails if
  790. ;; they are wrapped.
  791. (for-each build-wrapper executables)
  792. ;; Link any other non-executable files
  793. (for-each (lambda (old)
  794. (let ((new (string-append target (strip-store-prefix old))))
  795. (mkdir-p (dirname new))
  796. (symlink-relative old new)))
  797. others)))))
  798. (computed-file (string-append
  799. (cond ((package? package)
  800. (package-full-name package "-"))
  801. ((inferior-package? package)
  802. (string-append (inferior-package-name package)
  803. "-"
  804. (inferior-package-version package)))
  805. (else "wrapper"))
  806. "R")
  807. build))
  808. (define (wrapped-manifest-entry entry . args)
  809. (manifest-entry
  810. (inherit entry)
  811. (item (apply wrapped-package
  812. (manifest-entry-item entry)
  813. (manifest-entry-output entry)
  814. args))
  815. (dependencies (map (lambda (entry)
  816. (apply wrapped-manifest-entry entry args))
  817. (manifest-entry-dependencies entry)))))
  818. ;;;
  819. ;;; Command-line options.
  820. ;;;
  821. (define %default-options
  822. ;; Alist of default option values.
  823. `((format . tarball)
  824. (profile-name . "guix-profile")
  825. (system . ,(%current-system))
  826. (substitutes? . #t)
  827. (offload? . #t)
  828. (graft? . #t)
  829. (print-build-trace? . #t)
  830. (print-extended-build-trace? . #t)
  831. (multiplexed-build-output? . #t)
  832. (debug . 0)
  833. (verbosity . 1)
  834. (symlinks . ())
  835. (compressor . ,(first %compressors))))
  836. (define %formats
  837. ;; Supported pack formats.
  838. `((tarball . ,self-contained-tarball)
  839. (squashfs . ,squashfs-image)
  840. (docker . ,docker-image)))
  841. (define (show-formats)
  842. ;; Print the supported pack formats.
  843. (display (G_ "The supported formats for 'guix pack' are:"))
  844. (newline)
  845. (display (G_ "
  846. tarball Self-contained tarball, ready to run on another machine"))
  847. (display (G_ "
  848. squashfs Squashfs image suitable for Singularity"))
  849. (display (G_ "
  850. docker Tarball ready for 'docker load'"))
  851. (newline))
  852. (define %options
  853. ;; Specifications of the command-line options.
  854. (cons* (option '(#\h "help") #f #f
  855. (lambda args
  856. (show-help)
  857. (exit 0)))
  858. (option '(#\V "version") #f #f
  859. (lambda args
  860. (show-version-and-exit "guix pack")))
  861. (option '(#\n "dry-run") #f #f
  862. (lambda (opt name arg result)
  863. (alist-cons 'dry-run? #t result)))
  864. (option '(#\d "derivation") #f #f
  865. (lambda (opt name arg result)
  866. (alist-cons 'derivation-only? #t result)))
  867. (option '(#\f "format") #t #f
  868. (lambda (opt name arg result)
  869. (alist-cons 'format (string->symbol arg) result)))
  870. (option '("list-formats") #f #f
  871. (lambda args
  872. (show-formats)
  873. (exit 0)))
  874. (option '(#\R "relocatable") #f #f
  875. (lambda (opt name arg result)
  876. (match (assq-ref result 'relocatable?)
  877. (#f
  878. (alist-cons 'relocatable? #t result))
  879. (_
  880. (alist-cons 'relocatable? 'proot
  881. (alist-delete 'relocatable? result))))))
  882. (option '(#\e "expression") #t #f
  883. (lambda (opt name arg result)
  884. (alist-cons 'expression arg result)))
  885. (option '(#\m "manifest") #t #f
  886. (lambda (opt name arg result)
  887. (alist-cons 'manifest arg result)))
  888. (option '(#\s "system") #t #f
  889. (lambda (opt name arg result)
  890. (alist-cons 'system arg
  891. (alist-delete 'system result eq?))))
  892. (option '("entry-point") #t #f
  893. (lambda (opt name arg result)
  894. (alist-cons 'entry-point arg result)))
  895. (option '("target") #t #f
  896. (lambda (opt name arg result)
  897. (alist-cons 'target arg
  898. (alist-delete 'target result eq?))))
  899. (option '(#\C "compression") #t #f
  900. (lambda (opt name arg result)
  901. (alist-cons 'compressor (lookup-compressor arg)
  902. result)))
  903. (option '(#\S "symlink") #t #f
  904. (lambda (opt name arg result)
  905. ;; Note: Using 'string-split' allows us to handle empty
  906. ;; TARGET (as in "/opt/guile=", meaning that /opt/guile is
  907. ;; a symlink to the profile) correctly.
  908. (match (string-split arg (char-set #\=))
  909. ((source target)
  910. (let ((symlinks (assoc-ref result 'symlinks)))
  911. (alist-cons 'symlinks
  912. `((,source -> ,target) ,@symlinks)
  913. (alist-delete 'symlinks result eq?))))
  914. (x
  915. (leave (G_ "~a: invalid symlink specification~%")
  916. arg)))))
  917. (option '("save-provenance") #f #f
  918. (lambda (opt name arg result)
  919. (alist-cons 'save-provenance? #t result)))
  920. (option '("localstatedir") #f #f
  921. (lambda (opt name arg result)
  922. (alist-cons 'localstatedir? #t result)))
  923. (option '("profile-name") #t #f
  924. (lambda (opt name arg result)
  925. (match arg
  926. ((or "guix-profile" "current-guix")
  927. (alist-cons 'profile-name arg result))
  928. (_
  929. (leave (G_ "~a: unsupported profile name~%") arg)))))
  930. (option '(#\r "root") #t #f
  931. (lambda (opt name arg result)
  932. (alist-cons 'gc-root arg result)))
  933. (option '(#\v "verbosity") #t #f
  934. (lambda (opt name arg result)
  935. (let ((level (string->number* arg)))
  936. (alist-cons 'verbosity level
  937. (alist-delete 'verbosity result)))))
  938. (option '("bootstrap") #f #f
  939. (lambda (opt name arg result)
  940. (alist-cons 'bootstrap? #t result)))
  941. (append %transformation-options
  942. %standard-build-options)))
  943. (define (show-help)
  944. (display (G_ "Usage: guix pack [OPTION]... PACKAGE...
  945. Create a bundle of PACKAGE.\n"))
  946. (show-build-options-help)
  947. (newline)
  948. (show-transformation-options-help)
  949. (newline)
  950. (display (G_ "
  951. -f, --format=FORMAT build a pack in the given FORMAT"))
  952. (display (G_ "
  953. --list-formats list the formats available"))
  954. (display (G_ "
  955. -R, --relocatable produce relocatable executables"))
  956. (display (G_ "
  957. -e, --expression=EXPR consider the package EXPR evaluates to"))
  958. (display (G_ "
  959. -s, --system=SYSTEM attempt to build for SYSTEM--e.g., \"i686-linux\""))
  960. (display (G_ "
  961. --target=TRIPLET cross-build for TRIPLET--e.g., \"armel-linux-gnu\""))
  962. (display (G_ "
  963. -C, --compression=TOOL compress using TOOL--e.g., \"lzip\""))
  964. (display (G_ "
  965. -S, --symlink=SPEC create symlinks to the profile according to SPEC"))
  966. (display (G_ "
  967. -m, --manifest=FILE create a pack with the manifest from FILE"))
  968. (display (G_ "
  969. --entry-point=PROGRAM
  970. use PROGRAM as the entry point of the pack"))
  971. (display (G_ "
  972. --save-provenance save provenance information"))
  973. (display (G_ "
  974. --localstatedir include /var/guix in the resulting pack"))
  975. (display (G_ "
  976. --profile-name=NAME
  977. populate /var/guix/profiles/.../NAME"))
  978. (display (G_ "
  979. -r, --root=FILE make FILE a symlink to the result, and register it
  980. as a garbage collector root"))
  981. (display (G_ "
  982. -d, --derivation return the derivation of the pack"))
  983. (display (G_ "
  984. -v, --verbosity=LEVEL use the given verbosity LEVEL"))
  985. (display (G_ "
  986. --bootstrap use the bootstrap binaries to build the pack"))
  987. (newline)
  988. (display (G_ "
  989. -h, --help display this help and exit"))
  990. (display (G_ "
  991. -V, --version display version information and exit"))
  992. (newline)
  993. (show-bug-report-information))
  994. ;;;
  995. ;;; Entry point.
  996. ;;;
  997. (define-command (guix-pack . args)
  998. (category development)
  999. (synopsis "create application bundles")
  1000. (define opts
  1001. (parse-command-line args %options (list %default-options)))
  1002. (define maybe-package-argument
  1003. ;; Given an option pair, return a package, a package/output tuple, or #f.
  1004. (match-lambda
  1005. (('argument . spec)
  1006. (call-with-values
  1007. (lambda ()
  1008. (specification->package+output spec))
  1009. list))
  1010. (('expression . exp)
  1011. (read/eval-package-expression exp))
  1012. (x #f)))
  1013. (define (manifest-from-args store opts)
  1014. (let* ((transform (options->transformation opts))
  1015. (packages (map (match-lambda
  1016. (((? package? package) output)
  1017. (list (transform package) output))
  1018. ((? package? package)
  1019. (list (transform package) "out")))
  1020. (reverse
  1021. (filter-map maybe-package-argument opts))))
  1022. (manifests (filter-map (match-lambda
  1023. (('manifest . file) file)
  1024. (_ #f))
  1025. opts)))
  1026. (define with-provenance
  1027. (if (assoc-ref opts 'save-provenance?)
  1028. (lambda (manifest)
  1029. (map-manifest-entries
  1030. (lambda (entry)
  1031. (let ((entry (manifest-entry-with-provenance entry)))
  1032. (unless (assq 'provenance (manifest-entry-properties entry))
  1033. (warning (G_ "could not determine provenance of package ~a~%")
  1034. (manifest-entry-name entry)))
  1035. entry))
  1036. manifest))
  1037. identity))
  1038. (define (with-transformations manifest)
  1039. (map-manifest-entries manifest-entry-with-transformations
  1040. manifest))
  1041. (with-provenance
  1042. (with-transformations
  1043. (cond
  1044. ((and (not (null? manifests)) (not (null? packages)))
  1045. (leave (G_ "both a manifest and a package list were given~%")))
  1046. ((not (null? manifests))
  1047. (concatenate-manifests
  1048. (map (lambda (file)
  1049. (let ((user-module (make-user-module
  1050. '((guix profiles) (gnu)))))
  1051. (load* file user-module)))
  1052. manifests)))
  1053. (else
  1054. (packages->manifest packages)))))))
  1055. (with-error-handling
  1056. (with-store store
  1057. (with-status-verbosity (assoc-ref opts 'verbosity)
  1058. ;; Set the build options before we do anything else.
  1059. (set-build-options-from-command-line store opts)
  1060. (with-build-handler (build-notifier #:dry-run?
  1061. (assoc-ref opts 'dry-run?)
  1062. #:verbosity
  1063. (assoc-ref opts 'verbosity)
  1064. #:use-substitutes?
  1065. (assoc-ref opts 'substitutes?))
  1066. (parameterize ((%graft? (assoc-ref opts 'graft?))
  1067. (%guile-for-build (package-derivation
  1068. store
  1069. (if (assoc-ref opts 'bootstrap?)
  1070. %bootstrap-guile
  1071. (default-guile))
  1072. (assoc-ref opts 'system)
  1073. #:graft? (assoc-ref opts 'graft?))))
  1074. (let* ((derivation? (assoc-ref opts 'derivation-only?))
  1075. (relocatable? (assoc-ref opts 'relocatable?))
  1076. (proot? (eq? relocatable? 'proot))
  1077. (manifest (let ((manifest (manifest-from-args store opts)))
  1078. ;; Note: We cannot honor '--bootstrap' here because
  1079. ;; 'glibc-bootstrap' lacks 'libc.a'.
  1080. (if relocatable?
  1081. (map-manifest-entries
  1082. (cut wrapped-manifest-entry <> #:proot? proot?)
  1083. manifest)
  1084. manifest)))
  1085. (pack-format (assoc-ref opts 'format))
  1086. (name (string-append (symbol->string pack-format)
  1087. "-pack"))
  1088. (target (assoc-ref opts 'target))
  1089. (bootstrap? (assoc-ref opts 'bootstrap?))
  1090. (compressor (if bootstrap?
  1091. bootstrap-xz
  1092. (assoc-ref opts 'compressor)))
  1093. (archiver (if (equal? pack-format 'squashfs)
  1094. squashfs-tools
  1095. (if bootstrap?
  1096. %bootstrap-coreutils&co
  1097. tar)))
  1098. (symlinks (assoc-ref opts 'symlinks))
  1099. (build-image (match (assq-ref %formats pack-format)
  1100. ((? procedure? proc) proc)
  1101. (#f
  1102. (leave (G_ "~a: unknown pack format~%")
  1103. pack-format))))
  1104. (localstatedir? (assoc-ref opts 'localstatedir?))
  1105. (entry-point (assoc-ref opts 'entry-point))
  1106. (profile-name (assoc-ref opts 'profile-name))
  1107. (gc-root (assoc-ref opts 'gc-root))
  1108. (profile (profile
  1109. (content manifest)
  1110. ;; Always produce relative symlinks for
  1111. ;; Singularity (see
  1112. ;; <https://bugs.gnu.org/34913>).
  1113. (relative-symlinks?
  1114. (or relocatable?
  1115. (eq? 'squashfs pack-format)))
  1116. (hooks (if bootstrap?
  1117. '()
  1118. %default-profile-hooks))
  1119. (locales? (not bootstrap?)))))
  1120. (define (lookup-package package)
  1121. (manifest-lookup manifest (manifest-pattern (name package))))
  1122. (when (null? (manifest-entries manifest))
  1123. (warning (G_ "no packages specified; building an empty pack~%")))
  1124. (when (and (eq? pack-format 'squashfs)
  1125. (not (any lookup-package '("bash" "bash-minimal"))))
  1126. (warning (G_ "Singularity requires you to provide a shell~%"))
  1127. (display-hint (G_ "Add @code{bash} or @code{bash-minimal} \
  1128. to your package list.")))
  1129. (run-with-store store
  1130. (mlet* %store-monad ((drv (build-image name profile
  1131. #:target
  1132. target
  1133. #:compressor
  1134. compressor
  1135. #:symlinks
  1136. symlinks
  1137. #:localstatedir?
  1138. localstatedir?
  1139. #:entry-point
  1140. entry-point
  1141. #:profile-name
  1142. profile-name
  1143. #:archiver
  1144. archiver)))
  1145. (mbegin %store-monad
  1146. (mwhen derivation?
  1147. (return (format #t "~a~%"
  1148. (derivation-file-name drv))))
  1149. (munless derivation?
  1150. (built-derivations (list drv))
  1151. (mwhen gc-root
  1152. (register-root* (match (derivation->output-paths drv)
  1153. (((names . items) ...)
  1154. items))
  1155. gc-root))
  1156. (return (format #t "~a~%"
  1157. (derivation->output-path drv))))))
  1158. #:target target
  1159. #:system (assoc-ref opts 'system)))))))))