(zilch nixpkgs): fix reproducibility of environment-for-derivation

Change-Id: I6a6a6964095f727498ed30d06a7236c9b241fdd0
This commit is contained in:
puck 2025-05-11 22:21:07 +00:00
parent 0c0c4b5d22
commit 9cd5bfb8dd

View file

@ -112,6 +112,7 @@
(define processor (define processor
(string-append "if [ -e \"$NIX_ATTRS_SH_FILE\" ]; then source \"$NIX_ATTRS_SH_FILE\"; fi (string-append "if [ -e \"$NIX_ATTRS_SH_FILE\" ]; then source \"$NIX_ATTRS_SH_FILE\"; fi
export IN_NIX_SHELL=impure; export dontAddDisableDepTrack=1 export IN_NIX_SHELL=impure; export dontAddDisableDepTrack=1
export NIX_OUTPATH_USED_AS_RANDOM_SEED=/nix/store/zilchzilchzilchzilch
if [[ -n $stdenv ]]; then if [[ -n $stdenv ]]; then
source $stdenv/setup source $stdenv/setup
fi fi
@ -123,10 +124,9 @@
str=\"${str//$'\\r'/\\\\r}\" str=\"${str//$'\\r'/\\\\r}\"
printf '\"%s\"' \"$str\" printf '\"%s\"' \"$str\"
} }
out=" (make-placeholder "out") " printf '(' > $zilch_out
printf '(' > $out
__run() { __run() {
declare -p | while read __line; do declare -p | grep -v zilch_out | while read __line; do
if ! [[ $__line =~ ^declare\\ (-[^ ])\\ ([^=]*) ]]; then continue; fi if ! [[ $__line =~ ^declare\\ (-[^ ])\\ ([^=]*) ]]; then continue; fi
local __varname=\"${BASH_REMATCH[2]}\" local __varname=\"${BASH_REMATCH[2]}\"
if [[ \"${BASH_REMATCH[1]}\" == -x ]]; then if [[ \"${BASH_REMATCH[1]}\" == -x ]]; then
@ -138,22 +138,38 @@
fi fi
done done
} }
__run >> $out __run >> $zilch_out
printf ')' >> $out\n")) printf ')' >> $zilch_out\n"))
(define output-names (map car (derivation-outputs drv)))
(define new-env (list)) (define new-env (list))
(define base-placeholder "zilchplaceholderdonotuseanywhere-")
(define name (derivation-name drv))
(define placeholders
(map
(lambda (l)
(cons (car l)
(string-append "/nix/store/" (car l) (string-copy base-placeholder (string-length (car l))) name (if (string=? (car l) "out") "" (string-append "-" (car l))))))
(derivation-outputs drv)))
(for-each (for-each
(lambda (kv) (lambda (kv)
(unless (member (car kv) '("out" "name" "builder" "system" "allowedReferences" "disallowedReferences" "allowedRequisites" "disallowedRequisites") string=?) (set! new-env (cons kv new-env)))) (cond
((member (car kv) '("name" "src" "builder" "system" "allowedReferences" "disallowedReferences" "allowedRequisites" "disallowedRequisites") string=?)
#f)
((member (car kv) output-names string=?)
(set! new-env (cons (assoc (car kv) placeholders) new-env)))
(else
(set! new-env (cons kv new-env)))))
(derivation-env drv)) (derivation-env drv))
(define is-ca-derivation (symbol? (derivation-output-hash (cdar (derivation-outputs drv))))) (define is-ca-derivation (symbol? (derivation-output-hash (cdar (derivation-outputs drv)))))
(define out-drv (define out-drv
((if is-ca-derivation make-ca-derivation make-input-addressed-derivation) "zilch-env" (make-ca-derivation "zilch-env"
(derivation-system drv) (derivation-system drv)
(derivation-input-drvs drv) (derivation-input-drvs drv)
(derivation-input-src drv) (derivation-input-src drv)
(list (derivation-builder drv) "-c" processor) (list (derivation-builder drv) "-c" processor)
new-env new-env
'("out"))) '("zilch_out")))
(define environment (call-with-port (store-path-open (make-store-path out-drv "out" #f)) (lambda (p) (read p)))) (define environment (call-with-port (store-path-open (make-store-path out-drv "zilch_out" #f)) (lambda (p) (read p))))
(make-zexp (lambda () (zexp-context-register-items (list (list out-drv "out")) '()) environment) (lambda (p) (fprintf p "<shell environment for ~A>" (derivation-path drv))))))) (make-zexp (lambda () (zexp-context-register-items (list (list out-drv "zilch_out")) '()) environment) (lambda (p) (fprintf p "<shell environment for ~A>" (derivation-path drv)))))))