racket-nix/racket-install-hook.sh

154 lines
5.0 KiB
Bash

echo "Sourcing racket-install-hook"
addRacketPath() {
if [ -f "$1/nix-support/racket-pkg" ]; then
addToSearchPathWithCustomDelimiter : NIX_RACKET_PKG_PATH $1
fi
}
racketInstallPhase() {
echo "Executing racketInstallPhase"
runHook preInstall
mkdir -p $out/{include,etc/racket,lib/racket,share/racket/pkgs,share/racket/collects,bin,share/applications,share/doc/racket,share/man}
mkdir -p $out/nix-support
touch $out/nix-support/racket-pkg
out="$out" tethered="$racketTetheredInstallation" \
racket --no-user-path -nl racket/base -f - <<EOF
(require racket/function racket/hash racket/list racket/pretty racket/string racket/match)
(define out (getenv "out"))
(define pkgs (path-list-string->path-list (or (getenv "NIX_RACKET_PKG_PATH") "") '()))
(define tethered? (equal? (getenv "tethered") "1"))
(define base-config (read-installation-configuration-table))
(define (add-to-search added-list search-list)
(match search-list
['() (error "no #f found in search list!")]
[(cons #f rst) (cons #f (append added-list rst))]
[(cons fst rst) (cons fst (add-to-search added-list rst))]))
(define (make-search-path* key list-key [pkgs-search '()])
(define old-search-list (hash-ref base-config list-key '(#f)))
(define old-value
(cond
[(hash-has-key? base-config key)
(list (hash-ref base-config key))]
[(eq? key 'links-file)
(list
(path->string
(build-path (hash-ref base-config 'share-dir) "links.rktd")))]
[else (error "no key" key)]))
(define added-list (append pkgs-search old-value))
(add-to-search added-list old-search-list))
(define (default-location pkg key)
(path->string
(match key
['include-dir (build-path pkg "include")]
['lib-dir (build-path pkg "lib/racket")]
['share-dir (build-path pkg "share/racket")]
['pkgs-dir (build-path pkg "share/racket/pkgs")]
['links-file (build-path pkg "share/racket/links.rktd")]
['bin-dir (build-path pkg "bin")]
['doc-dir (build-path pkg "share/doc/racket")]
['man-dir (build-path pkg "share/man")]
[_ (error "unexpected key:" key)])))
(define (make-search-path key list-key)
(define pkgs-search
(for/list ([pkg (in-list pkgs)])
(default-location pkg key)))
(make-search-path* key list-key pkgs-search))
(define (add-libs lib-path)
(define ldflags (string-split (getenv "NIX_LDFLAGS")))
(define libs
(for/list ([lib (in-list ldflags)] #:when (string-prefix? "-L" lib))
(string-trim "-L" #:right? #f)))
(remove-duplicates (append libs lib-path)))
(define config*
(hash
'absolute-installation? #t
'build-stamp ""
'catalogs (hash-ref base-config 'catalogs)
'compiled-file-roots (hash-ref base-config 'compiled-file-roots)
'apps-dir (path->string (build-path out "share/applications"))
'bin-dir (default-location out 'bin-dir)
'bin-search-dirs (make-search-path 'bin-dir 'bin-search-dirs)
'doc-dir (default-location out 'doc-dir)
'doc-search-dirs (make-search-path 'doc-dir 'doc-search-dirs)
'doc-search-url (hash-ref base-config 'doc-search-url)
'include-dir (default-location out 'include-dir)
'include-search-dirs (make-search-path 'include-dir 'include-search-dirs)
'lib-dir (default-location out 'lib-dir)
'lib-search-dirs (add-libs (make-search-path 'lib-dir 'lib-search-dirs))
'links-file (default-location out 'links-file)
'links-search-files (make-search-path 'links-file 'links-search-files)
'man-dir (default-location out 'man-dir)
'man-search-dirs (make-search-path 'man-dir 'man-search-dirs)
'pkgs-dir (default-location out 'pkgs-dir)
'pkgs-search-dirs (make-search-path 'pkgs-dir 'pkgs-search-dirs)
'share-dir (default-location out 'share-dir)
'share-search-dirs (make-search-path 'share-dir 'share-search-dirs)))
(define config
(if tethered?
(hash-union
config*
(hash
'config-tethered-console-bin-dir (hash-ref config* 'bin-dir)
'config-tethered-gui-bin-dir (hash-ref config* 'bin-dir)
'config-tethered-apps-dir (hash-ref config* 'apps-dir)))
config*))
(with-output-to-file (build-path out "etc/racket/config.rktd")
(curry pretty-write config))
EOF
echo Initializing installation layer
if [ "$racketTetheredInstallation" == "true" ]; then
racket --config $out/etc/racket/ --no-user-path -l- \
raco setup
else
racket --config $out/etc/racket/ --no-user-path -l- \
raco setup --no-launcher
rm $out/bin/mzscheme # ????
fi
if [ -f "nix-racket-env-only" ]; then
echo Skipping raco pkg install
else
echo Running raco pkg install
racket --config $out/etc/racket/ --no-user-path -l- \
raco pkg install --installation --deps fail --copy --name "$pname" "$(readlink -e .)"
fi
runHook postInstall
echo "Finished executing racketInstallPhase"
}
if [ -z "${dontUseRacketInstall-}" ] && [ -z "${installPhase-}" ]; then
echo "Adding racket env hook"
addEnvHooks "$targetOffset" addRacketPath
echo "Using racketInstallPhase"
installPhase=racketInstallPhase
fi