utils: nix-shell-wrapper: Add environment options and improve expression handling.

This commit is contained in:
Hilton Chain committed 2026-07-01 16:54:24 +08:00
1 parent 17c2fdf76e
commit 7a2f31556f
1 file changed
+95 -34
+95 -34
View File
@@ -2,68 +2,118 @@
;;; Copyright © 2026 Hilton Chain <hako@ultrarare.space> ;;; Copyright © 2026 Hilton Chain <hako@ultrarare.space>
(define-module (rosenthal utils nix) (define-module (rosenthal utils nix)
;; Guile builtins
#:use-module (ice-9 match)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-26)
;; Utilities ;; Utilities
#:use-module (guix gexp) #:use-module (guix gexp)
#:use-module (guix packages) #:use-module (guix packages)
;; Guix build systems ;; Guix build systems
#:use-module (guix build-system trivial) #:use-module (guix build-system trivial)
#:export (nix-shell-wrapper ;; Guix packages
#:autoload (gnu packages package-management) (nix)
#:export (%nix-shell-wrapper-default-unset-env-vars
nix-shell-wrapper
nix-shell-wrapper->package)) nix-shell-wrapper->package))
;; These search paths may contain incompatible libraries and may crash the
;; program loading them. See also GCD 004:
;; 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"
"XDG_CONFIG_DIRS"
;; Glib
"GIO_EXTRA_MODULES"
"GSETTINGS_SCHEMA_DIR"
;; Qt
"QML2_IMPORT_PATH"
"QML_IMPORT_PATH"
"QT_PLUGIN_PATH"))
;;; ;;;
;;; Helper utilities to use packages from Nix. ;;; Helper utilities to use packages from Nix.
;;; ;;;
(define* (nix-shell-wrapper name installables ;; See also https://nix.dev/manual/nix/2.34/command-ref/new-cli/nix3-env-shell.html
#:key (define* (nix-shell-wrapper name
expression? #:optional (installables ".")
#:key expression
(run-command '()) (run-command '())
(options '()) (options '())
(nix "/run/current-system/profile/bin/nix")) (environment-keep #t)
(environment-unset %nix-shell-wrapper-default-unset-env-vars)
(environment-set '())
(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
and spawns an one-off software environment when run. Nix daemon is required to and spawns an one-off software environment if run. Nix daemon is required to
use the wrapper. use the wrapper.
INSTALLABLES (string / list of strings) specifies packages to be added into the INSTALLABLES (string / list of strings) is specified as Flake output attribute
environment. If EXPRESSION? is set to #t (default: #f, boolean), it must and will be added into the environment. When EXPRESSION (string / file-like
contain exactly one Nix expression. object) is set, INSTALLABLES will be optional and interpreted as attribute paths
relative to the Nix expression.
RUN-COMMAND (default: '(), list of strings) specifies command to run in the RUN-COMMAND (default: '(), list of strings) specifies command and arguments to
environment. Arguments passed to the wrapper will be appended to it. be executed in the environment. Command-line arguments passed to the wrapper
will be appended.
OPTIONS (default '(), list of strings) specifies extra options to pass to the OPTIONS (default: '(), list of strings) specifies extra options to pass to the
\"nix shell\" command-line utility. \"nix shell\" command-line utility.
NIX (default: \"/run/current-system/profile/bin/nix\", string / file-like When ENVIRONMENT-KEEP (default: #t, boolean / list of strings) is set to a value
object) specifies the Nix binary to use. other than #t, the environment will be cleared, keeping only specified
environment variables.
ENVIRONMENT-UNSET (default: %nix-shell-wrapper-default-unset-env-vars, list of
strings) unsets specified environment variables from the environment. It's only
usable when ENVIRONMENT-KEEP is #t.
For each key-value pair in ENVIRONMENT-SET (default: '(), association list), the
environment sets its corresponding environment variable.
NIX (default: (file-append nix \"/bin/nix\"), string / file-like object)
specifies the Nix binary to use.
Examples: Examples:
(nix-shell-wrapper \"cowsay\" (nix-shell-wrapper \"cowsay-hello-coreutils-env\"
'(\"github:NixOS/nixpkgs/nixos-26.05#coreutils\" '(\"github:NixOS/nixpkgs/nixos-26.05#coreutils\"
\"github:NixOS/nixpkgs/nixos-26.05#cowsay\") \"github:NixOS/nixpkgs/nixos-26.05#cowsay\")
#:run-command '(\"env\" \"LANG=C.UTF-8\" \"cowsay\" \"hello\")) #:run-command '(\"env\" \"LANG=C.UTF-8\" \"cowsay\" \"hello\"))
(nix-shell-wrapper \"python-with-numpy\" (nix-shell-wrapper \"cowsay-hello\"
\"with (import (builtins.getFlake \\\"github:NixOS/nixpkgs/nixos-26.05\\\") {}); python3.withPackages (ps: [ps.numpy])\" \"github:NixOS/nixpkgs/nixos-26.05#cowsay\"
#:expression? #t #:run-command '(\"cowsay\" \"hello\")
#:run-command '(\"python\")) #:environment-set '((\"LANG\" . \"C.UTF-8\")))
"
(define %installables
(if (list? installables)
(begin
(when (null? installables)
(error "no installables provided"))
(when (and (not (= (length installables) 1))
expression?)
(error "more than one Nix expression provided"))
installables)
(list installables)))
(nix-shell-wrapper \"python-with-numpy\"
#:expression \"with (import (builtins.getFlake \\\"github:NixOS/nixpkgs/nixos-26.05\\\") {}); python3.withPackages (ps: [ps.numpy])\"
#:run-command '(\"python\"))
(nix-shell-wrapper \"coreutils-env\"
\"coreutils\"
#:expression (local-file \"/tmp/nixpkgs.nix\")
#:run-command '(\"env\")
#:environment-keep #f)
"
(define %options (define %options
(if expression? (append (match environment-keep
(cons* "--impure" "--expr" options) (#t
options)) (append-map (cut list "--unset-env-var" <>)
environment-unset))
(#f
'("--ignore-env"))
(else
(cons "--ignore-env"
(map (cut list "--keep-env-var" <>)
environment-keep))))
(append-map (match-lambda
((name . value)
(list "--set-env-var" name value)))
environment-set)
options))
(program-file (string-append name "-nix-shell") (program-file (string-append name "-nix-shell")
(with-imported-modules '((guix build utils)) (with-imported-modules '((guix build utils))
@@ -74,7 +124,18 @@ Examples:
((_ . args) ((_ . args)
(apply invoke #$nix (apply invoke #$nix
"--extra-experimental-features" "nix-command flakes" "--extra-experimental-features" "nix-command flakes"
"shell" #$@%options #$@%installables "shell"
#$@%options
#$@(match expression
((? file-like?)
(list "--impure" "--file" expression))
((? string?)
(list "--impure" "--expr" expression))
(else
'()))
#$@(if (list? installables)
installables
(list installables))
"--command" #$@run-command args))))))) "--command" #$@run-command args)))))))
(define (nix-shell-wrapper->package wrapper) (define (nix-shell-wrapper->package wrapper)