* nongnu/system/install.scm

Refactored %channels-file creation to make build-image cleaner.

* nongnu/system/build-image.scm
Refactored make-tmp-channel to use mkstemp! and improved %channels-file.
Improved readability of build-image function.
This commit is contained in:
AAaronson 2021-05-04 15:09:56 +00:00
parent 12d6628ba3
commit f9227ab655
2 changed files with 41 additions and 43 deletions

View File

@ -1,7 +1,9 @@
#!/usr/bin/env -S guile \\
-e build-image -s
!# ;; Guile uses a meta switch '\' which env requires escaping to passthru.
;; See: https://pingus.seul.org/~grumbel/tmp/guile-1.6.0/guile_10.html#SEC27
!#
; Guile uses a meta switch '\' which env requires escaping to passthru.
; See: https://pingus.seul.org/~grumbel/tmp/guile-1.6.0/guile_10.html#SEC27
;;; Copyright © 2021 Kyle Cassidy <123_kbc@pm.me>
;;;
@ -21,6 +23,8 @@
;; Generate a bootable image with:
;; $ chmod +x build-image.scm && ./build-image.scm
(use-modules (ice-9 pretty-print))
; Searches through Scheme file for 'define and 'define* expressions
; such as (define <name> ... ) or (define (<name> ...) ...)
(define (search-for-define scheme-file define-name)
@ -37,47 +41,41 @@
exp)
(#t (next (read port))))))))
; Writes channel-list to a tmpfile
; 'use-module is assumed to be needed, does not do anything if not used. TODO: remove assumption.
; TODO: get mkstemp! to work (result of using it was a blank file)
(define (make-tmp-channel channel-list)
(let loop ((tmpath (string-append "/tmp/guix-channel"
(number->string (random 1000000)))))
(cond ((file-exists? tmpath)
(loop (string-append "/tmp/guix-channel"
(number->string (random 1000000)))))
(#t (call-with-output-file tmpath
(lambda (port)
(write '(use-modules (guix ci)) port)
(write channel-list port)))
tmpath))))
; Writes channel-list to a temp file
(define (make-temp-channel channel-list)
(let ((port (mkstemp! (string-copy "/tmp/guix-channel-XXXXXX") "a")))
(map (lambda (_) (pretty-print _ port))
channel-list)
(force-output port)
(port-filename port)))
; Main function called when run at command line.
; Parses "nongnu/system/install.scm" for channel definition
; (current-filename) used to locate in same dir as this file
; Creates tmpfile containing channel definition
; Performs a guix pull using tmpfile as channel file
; Deletes tmpfile
; Creates a temp file containing channel definition
; Performs a guix pull using temp file as channel file
; Deletes temp file
; Builds image (currently iso only)
; supports "--uncompressed" command line argument
; If "--roll-back" is used as an argument, reverts guix pull
; Returns #t if exit-code is 0, else returns #f
(define (build-image args)
(let* ((image-type 'iso) ; todo: parse args for supported image types
(let* ((image-type 'iso) ; TODO: parse args for supported image types
(image-label "nonguix-install")
(channel-define-name '%channels)
(config-dir (dirname (current-filename)))
(config-file-name "install.scm")
(install-config (string-append config-dir
file-name-separator-string
config-file-name))
(name-to-search 'nonguix-channels) ; lookup in install-config
(channels (caddr (search-for-define
install-config
channel-define-name)))
(tmp-channel-file-path (make-tmp-channel (cadr channels)))
name-to-search)))
(temp-channel-file-path (make-temp-channel (cadr channels)))
(exit-code #f))
(system* "guix" "pull" (string-append "--channels=" tmp-channel-file-path))
(delete-file tmp-channel-file-path)
(system* "guix" "pull" (string-append "--channels=" temp-channel-file-path))
(delete-file temp-channel-file-path)
(cond ((equal? image-type 'iso)
(let ((image-link (string-append config-dir
file-name-separator-string
@ -93,6 +91,8 @@
(string-append "--label=" image-label)
(string-append "--root=" image-link)
install-config)))))
(and (member "--roll-back" args)
(system* "guix" "pull" "--roll-back"))
(equal? exit-code 0)))

View File

@ -27,30 +27,28 @@
#:use-module (ice-9 pretty-print)
#:export (installation-os-nonfree))
; List of guix channels for the "/etc/skel/.config/guix/channels.scm" file.
; Guix channel for the "/etc/skel/.config/guix/channels.scm" file.
; %default-guix-channel is configured to pull package definitions from
; the Guix substitute server, to save compiling during a guix pull.
(define %channels
'(list (channel
(name 'nonguix)
(url "https://gitlab.com/nonguix/nonguix")
(introduction
(make-channel-introduction
"897c1a470da759236cc11798f4e0a5f7d4d59fbc"
(openpgp-fingerprint
"2A39 3FFF 68F4 EF7A 3D29 12AF 6F51 20A0 22FB B2D5"))))
(channel-with-substitutes-available
%default-guix-channel
"https://ci.guix.gnu.org")))
(define nonguix-channels
'((use-modules (guix ci)) ; required for channel-with-substitutes-available
(list (channel
(name 'nonguix)
(url "https://gitlab.com/nonguix/nonguix")
(introduction
(make-channel-introduction
"897c1a470da759236cc11798f4e0a5f7d4d59fbc"
(openpgp-fingerprint
"2A39 3FFF 68F4 EF7A 3D29 12AF 6F51 20A0 22FB B2D5"))))
(channel-with-substitutes-available
%default-guix-channel
"https://ci.guix.gnu.org"))))
; Create the plain-file definition of "channels.scm" to add to the install.
(define %channels-file
(let ((port (open-output-string)))
(pretty-print '(use-modules
(guix ci)) ; for channel-with-substitutes-availabe
port)
(pretty-print %channels
port)
(map (lambda (_) (pretty-print _ port))
nonguix-channels)
(plain-file "channels.scm"
(get-output-string port))))