util: nix: Add <nix-wrapper>.

* modules/rosenthal/utils/nix.scm (<nix-wrapper>): New data type.
(nix-expressions->profile-build-wrapper, nix-shell-wrapper): Return a value of
it.
(nix-shell-wrapper->package, with-nix-profile): Use it.
This commit is contained in:
Hilton Chain committed 2026-07-04 12:30:49 +08:00
1 parent 30af71b024
commit 180635cee4
1 file changed
+74 -71
+74 -71
View File
@@ -5,6 +5,7 @@
;; Guile builtins ;; Guile builtins
#:use-module (ice-9 match) #:use-module (ice-9 match)
#:use-module (srfi srfi-1) #:use-module (srfi srfi-1)
#:use-module (srfi srfi-9)
#:use-module (srfi srfi-26) #:use-module (srfi srfi-26)
#:use-module (srfi srfi-71) #:use-module (srfi srfi-71)
;; Utilities ;; Utilities
@@ -14,12 +15,8 @@
#:use-module (guix build-system trivial) #:use-module (guix build-system trivial)
;; Guix packages ;; Guix packages
#:autoload (gnu packages package-management) (nix) #:autoload (gnu packages package-management) (nix)
#:export (installables->nix-expressions #:export (nix-shell-wrapper
nix-expressions->profile-build-wrapper
nix-shell-wrapper
nix-shell-wrapper->package nix-shell-wrapper->package
with-nix-profile)) with-nix-profile))
;; These search paths may contain incompatible libraries and crash programs ;; These search paths may contain incompatible libraries and crash programs
@@ -30,6 +27,7 @@
;; ENV_VAR ;; ENV_VAR
;; '(ENV_VAR . PATH) ;; '(ENV_VAR . PATH)
;; '(ENV_VAR . (PATHS ...)) ;; '(ENV_VAR . (PATHS ...))
(define %nix-wrapper-default-exclude-env-paths (define %nix-wrapper-default-exclude-env-paths
'((#f . ("/bin" "/sbin")) '((#f . ("/bin" "/sbin"))
"XDG_DATA_DIRS" "XDG_DATA_DIRS"
@@ -61,8 +59,16 @@
(_ (list #f))) (_ (list #f)))
%nix-wrapper-default-exclude-env-paths))) %nix-wrapper-default-exclude-env-paths)))
(define %nix-wrapper-for-profile? (define-record-type <nix-wrapper>
(make-parameter #f)) (nix-wrapper name command file expressions)
nix-wrapper?
(name nix-wrapper-name) ;string
(command nix-wrapper-command) ;string
(file nix-wrapper-file) ;file-like object
(expressions nix-wrapper-expressions)) ;list of file-like objects
(define-gexp-compiler (nix-wrapper-compiler (wrapper <nix-wrapper>) system target)
(lower-object (nix-wrapper-file wrapper) system #:target target))
;;; ;;;
@@ -198,19 +204,25 @@ in
'#$paths-to-exclude)))) '#$paths-to-exclude))))
#:options '(#:substitutable? #f))) #:options '(#:substitutable? #f)))
(program-file "build-nix-profile-nix-wrapper" (define wrapper
(with-imported-modules '((guix build utils)) (program-file "build-nix-profile-nix-wrapper"
#~(begin (with-imported-modules '((guix build utils))
(use-modules (ice-9 match) #~(begin
(guix build utils)) (use-modules (ice-9 match)
(match (command-line) (guix build utils))
((_ . args) (match (command-line)
(apply invoke #$nix ((_ . args)
"build" "--print-out-paths" (apply invoke #$nix
`(,@(if (null? args) "build" "--print-out-paths"
(list "--no-link") `(,@(if (null? args)
(list "--out-link" (car args))) (list "--no-link")
"--file" #$profile.nix)))))))) (list "--out-link" (car args)))
"--file" #$profile.nix))))))))
(nix-wrapper "build-nix-profile-nix-wrapper"
"build-nix-profile"
wrapper
expressions))
;; See also https://nix.dev/manual/nix/2.34/command-ref/new-cli/nix3-env-shell.html ;; See also https://nix.dev/manual/nix/2.34/command-ref/new-cli/nix3-env-shell.html
(define* (nix-shell-wrapper name (define* (nix-shell-wrapper name
@@ -291,8 +303,11 @@ Examples:
environment-set) environment-set)
options)) options))
(define wrapper-name
(string-append name "-nix-wrapper"))
(define wrapper (define wrapper
(program-file (string-append name "-nix-wrapper") (program-file wrapper-name
(with-imported-modules '((guix build utils)) (with-imported-modules '((guix build utils))
#~(begin #~(begin
(use-modules (ice-9 match) (use-modules (ice-9 match)
@@ -313,49 +328,50 @@ Examples:
#$@(ensure-list installables) #$@(ensure-list installables)
"--command" #$@run-command args))))))) "--command" #$@run-command args)))))))
(if (%nix-wrapper-for-profile?) (nix-wrapper wrapper-name
(list 'nix-shell-wrapper wrapper installables expression) name
wrapper)) wrapper
(installables->nix-expressions
installables
#:expression expression)))
(define (nix-shell-wrapper->package wrapper) (define (nix-shell-wrapper->package wrapper)
"Return a package for WRAPPER, file-like object created by 'nix-shell-wrapper' "Return a package for WRAPPER, file-like object created by 'nix-shell-wrapper'
or 'nix-expressions->profile-build-wrapper'. The package will install a command or 'nix-expressions->profile-build-wrapper'. The package will install a command
under its /bin directory, with the same name as WRAPPER. under its /bin directory, with the command name of WRAPPER.
Note that packages created by this procedure are not supposed to be used in the Note that packages created by this procedure are not supposed to be used in the
build environment and won't be able to interoperate with other packages in build environment and won't be able to interoperate with other packages in
practice." practice."
(let* ((wrapper-name (program-file-name wrapper)) (package
(command (string-drop-right wrapper-name (string-length "-nix-wrapper")))) (name (nix-wrapper-name wrapper))
(package (version "0.0.0")
(name wrapper-name) (source #f)
(version "0.0.0") (build-system trivial-build-system)
(source #f) (arguments
(build-system trivial-build-system) (list #:builder
(arguments (with-imported-modules '((guix build utils))
(list #:builder #~(begin
(with-imported-modules '((guix build utils)) (use-modules (guix build utils))
#~(begin (let ((dest (string-append #$output "/bin/"
(use-modules (guix build utils)) #$(nix-wrapper-command wrapper))))
(let ((dest (string-append #$output "/bin/" #$command))) (mkdir-p (dirname dest))
(mkdir-p (dirname dest)) (symlink #$wrapper dest))))))
(symlink #$wrapper dest)))))) (home-page #f)
(home-page #f) (synopsis #f)
(synopsis #f) (description #f)
(description #f) (license #f)))
(license #f))))
(define-syntax-rule (with-nix-profile packages) (define (with-nix-profile packages)
"Transform PACKAGES, turning wrappers created by 'nix-shell-wrapper' into "Transform PACKAGES, turning wrappers created by 'nix-shell-wrapper' into
packages, additionally adding a package containing the build-nix-profile script packages, additionally adding a package containing a build-nix-profile script
created for them by 'nix-expressions->profile-build-wrapper'. created for them by 'nix-expressions->profile-build-wrapper'.
The build-nix-profile accepts an optional path argument and will symlink the The build-nix-profile script accepts an optional path argument and will symlink
resulted profile to the path. the resulted profile to the path.
This macro is intended for use in Guix System and Guix Home package This procedure is intended for use in Guix System and Guix Home package
declarations. Note that calls to 'nix-shell-wrapper' must be in the scope of declarations.
this macro.
Example: Example:
@@ -366,22 +382,9 @@ Example:
#:environment-set '((\"LANG\" . \"C.UTF-8\")))) #:environment-set '((\"LANG\" . \"C.UTF-8\"))))
%base-packages)) %base-packages))
" "
(let ((wrappers (let ((wrappers others (partition nix-wrapper? packages)))
others (append (list (nix-shell-wrapper->package
(partition (match-lambda (nix-expressions->profile-build-wrapper
(('nix-shell-wrapper _ _ _) #t) (append-map nix-wrapper-expressions wrappers))))
(_ #f)) (map nix-shell-wrapper->package wrappers)
(parameterize ((%nix-wrapper-for-profile? #t)) others)))
packages))))
`(,(nix-shell-wrapper->package
(nix-expressions->profile-build-wrapper
(append-map (match-lambda
((_ _ installables expression)
(installables->nix-expressions
installables #:expression expression)))
wrappers)))
,@(map (match-lambda
((_ wrapper _ _)
(nix-shell-wrapper->package wrapper)))
wrappers)
,@others)))