utils: nix: Add and use %nix-wrapper-default-exclude-env-paths.

This commit is contained in:
Hilton Chain 2026-07-03 10:01:46 +08:00
parent 428a150814
commit c2722ed176
No known key found for this signature in database
GPG Key ID: ACC66D09CA528292

View File

@ -14,11 +14,7 @@
#: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 (%nix-shell-wrapper-default-unset-env-vars #:export (installables->nix-expressions
%nix-build-profile-paths
%nix-build-profile-extra-outputs
installables->nix-expressions
nix-expressions->profile-build-wrapper nix-expressions->profile-build-wrapper
nix-shell-wrapper nix-shell-wrapper
@ -26,36 +22,46 @@
with-nix-profile)) with-nix-profile))
;; These search paths may contain incompatible libraries crash programs ;; These search paths may contain incompatible libraries and crash programs
;; loading them. See also GCD 004: ;; loading them. See also GCD 004:
;; https://consensus.guix.gnu.org/gcd/004-set-search-paths-without-program-wrappers.html ;; https://consensus.guix.gnu.org/gcd/004-set-search-paths-without-program-wrappers.html
(define %nix-shell-wrapper-default-unset-env-vars ;;
'("XDG_DATA_DIRS" ;; Valid items below, paths are started with "/":
;; ENV_VAR
;; '(ENV_VAR . PATH)
;; '(ENV_VAR . (PATHS ...))
(define %nix-wrapper-default-exclude-env-paths
'((#f . ("/bin" "/sbin"))
"XDG_DATA_DIRS"
"XDG_CONFIG_DIRS" "XDG_CONFIG_DIRS"
;; Glib ;; Glib
"GIO_EXTRA_MODULES" ("GIO_EXTRA_MODULES" . "/lib/gio/modules")
"GSETTINGS_SCHEMA_DIR" ("GSETTINGS_SCHEMA_DIR" . "/share/glib-2.0/schemas")
;; Qt ;; Qt
"QML2_IMPORT_PATH" ("QML2_IMPORT_PATH" . "/lib/qt5/qml")
"QML_IMPORT_PATH" ("QML_IMPORT_PATH" . "/lib/qt6/qml")
"QT_PLUGIN_PATH")) ("QT_PLUGIN_PATH" . ("/lib/qt5/plugins" "/lib/qt6/plugins"))))
;; Safe-to-use paths when extending search paths. ;; Additional outputs added to the profile we build.
(define %nix-build-profile-paths (define %nix-profile-extra-outputs
'("/share/fonts"
"/share/icons"
"/share/info"
"/share/man"
;; Completions.
"/share/bash-completion/completions"
"/share/fish/vendor_completions.d"
"/share/zsh/site-functions"))
;; Additional outputs needed for %nix-build-profile-paths.
(define %nix-build-profile-extra-outputs
'("man" "info")) '("man" "info"))
(define %nix-shell-wrapper-for-profile? (define %nix-wrapper-default-unset-env-vars
(filter-map (match-lambda
((? string? env) env)
(((? string? env) . _) env)
(_ #f))
%nix-wrapper-default-exclude-env-paths))
(define %nix-wrapper-default-exclude-paths
(filter identity
(append-map (match-lambda
((_ . (? string? path)) (list path))
((_ (? string? paths) ...) paths)
(_ (list #f)))
%nix-wrapper-default-exclude-env-paths)))
(define %nix-wrapper-for-profile?
(make-parameter #f)) (make-parameter #f))
@ -130,8 +136,8 @@ optional and interpreted as attribute paths relative to the Nix expression."
(define* (nix-expressions->profile-build-wrapper (define* (nix-expressions->profile-build-wrapper
expressions expressions
#:key #:key
(paths-to-link %nix-build-profile-paths) (paths-to-exclude %nix-wrapper-default-exclude-paths)
(extra-outputs-to-install %nix-build-profile-extra-outputs) (extra-outputs-to-install %nix-profile-extra-outputs)
(nixpkgs-commit "714a5f8c4ead6b31148d829288440ed033ccc041") (nixpkgs-commit "714a5f8c4ead6b31148d829288440ed033ccc041")
(nix (file-append nix "/bin/nix"))) (nix (file-append nix "/bin/nix")))
"Return a file-like object that wraps the \"nix build\" command-line utility "Return a file-like object that wraps the \"nix build\" command-line utility
@ -144,9 +150,8 @@ EXPRESSIONS (list of strings / list of G-expressions) can be formatted from
'installables->nix-expressions' and specifies packages to be added into the 'installables->nix-expressions' and specifies packages to be added into the
profile. profile.
PATHS-TO-LINK (default: %nix-build-profile-paths, list of strings) limits PATHS-TO-EXCLUDE (default: %nix-wrapper-default-exclude-paths, list of strings)
subdirectories of packages to be included into the profile. All subdirectories excludes subdirectories from being added into the profile.
will be included if specifying '(\"/\").
EXTRA-OUTPUTS-TO-INSTALL (default: %nix-build-profile-extra-outputs, list of EXTRA-OUTPUTS-TO-INSTALL (default: %nix-build-profile-extra-outputs, list of
strings) specifies additional outputs of packages to be included into the strings) specifies additional outputs of packages to be included into the
@ -172,11 +177,6 @@ in
paths = [ paths = [
~{\ ~{\
~a ~a
~}\
];
pathsToLink = [
~{\
~s
~}\ ~}\
]; ];
extraOutputsToInstall = [ extraOutputsToInstall = [
@ -184,12 +184,17 @@ in
~s ~s
~}\ ~}\
]; ];
postBuild = ''
~{\
rm -rf $out~a
~}\
'';
} }
" "
#$nixpkgs-commit #$nixpkgs-commit
(list #$@expressions) (list #$@expressions)
'#$paths-to-link '#$extra-outputs-to-install
'#$extra-outputs-to-install)))) '#$paths-to-exclude))))
#:options '(#:substitutable? #f))) #:options '(#:substitutable? #f)))
(program-file "build-nix-profile-nix-wrapper" (program-file "build-nix-profile-nix-wrapper"
@ -213,7 +218,7 @@ in
(run-command '()) (run-command '())
(options '()) (options '())
(environment-keep #t) (environment-keep #t)
(environment-unset %nix-shell-wrapper-default-unset-env-vars) (environment-unset %nix-wrapper-default-unset-env-vars)
(environment-set '()) (environment-set '())
(nix (file-append nix "/bin/nix"))) (nix (file-append nix "/bin/nix")))
"Return a file-like object that wraps the \"nix shell\" command-line utility "Return a file-like object that wraps the \"nix shell\" command-line utility
@ -236,7 +241,7 @@ When ENVIRONMENT-KEEP (default: #t, boolean / list of strings) is set to a value
other than #t, the environment will be cleared, keeping only specified other than #t, the environment will be cleared, keeping only specified
environment variables. environment variables.
ENVIRONMENT-UNSET (default: %nix-shell-wrapper-default-unset-env-vars, list of ENVIRONMENT-UNSET (default: %nix-wrapper-default-unset-env-vars, list of
strings) unsets specified environment variables from the environment. It's only strings) unsets specified environment variables from the environment. It's only
usable when ENVIRONMENT-KEEP is #t. usable when ENVIRONMENT-KEEP is #t.
@ -307,7 +312,7 @@ Examples:
#$@(ensure-list installables) #$@(ensure-list installables)
"--command" #$@run-command args))))))) "--command" #$@run-command args)))))))
(if (%nix-shell-wrapper-for-profile?) (if (%nix-wrapper-for-profile?)
(list 'nix-shell-wrapper wrapper installables expression) (list 'nix-shell-wrapper wrapper installables expression)
wrapper)) wrapper))
@ -365,7 +370,7 @@ Example:
(partition (match-lambda (partition (match-lambda
(('nix-shell-wrapper _ _ _) #t) (('nix-shell-wrapper _ _ _) #t)
(_ #f)) (_ #f))
(parameterize ((%nix-shell-wrapper-for-profile? #t)) (parameterize ((%nix-wrapper-for-profile? #t))
packages)))) packages))))
`(,(nix-shell-wrapper->package `(,(nix-shell-wrapper->package
(nix-expressions->profile-build-wrapper (nix-expressions->profile-build-wrapper