2021-03-15 00:00:53 +00:00
|
|
|
{ lib }:
|
|
|
|
|
|
|
|
with builtins;
|
|
|
|
|
|
|
|
rec {
|
|
|
|
|
|
|
|
# Derive a file by substituting
|
|
|
|
# "${pkgs.foo}/bin/foo" for each {-pkg-}"foo", and
|
|
|
|
# "${pkgs.bar}/bin/foo" for each {-pkg:bar-}"foo".
|
|
|
|
# If a package doesn't exist, a warning gets printed.
|
|
|
|
substitutePkgs = name: { callsite ? null, pkgs, path }:
|
2022-03-21 23:55:53 +00:00
|
|
|
let
|
2021-03-15 00:00:53 +00:00
|
|
|
sourceDescription =
|
|
|
|
if callsite != null then
|
|
|
|
"${name} in ${toString callsite}"
|
|
|
|
else
|
|
|
|
"${name} from ${toString path}";
|
|
|
|
|
2022-03-21 23:55:53 +00:00
|
|
|
f = dependencies: s:
|
2021-03-15 00:00:53 +00:00
|
|
|
let
|
|
|
|
parse = match "(.*)([{]-pkg(:([^}]+))?-[}]\"([^\"]+)\")(.*)" s;
|
|
|
|
prefix = elemAt parse 0;
|
|
|
|
pname = if elemAt parse 3 != null then elemAt parse 3 else exename;
|
|
|
|
exename = elemAt parse 4;
|
|
|
|
suffix = elemAt parse 5;
|
|
|
|
pkg = pkgs.${pname} or null;
|
|
|
|
|
|
|
|
substitute =
|
|
|
|
if pkg != null then
|
|
|
|
"${pkg}/bin/${exename}"
|
|
|
|
else
|
|
|
|
trace (toString [
|
2022-03-21 23:57:44 +00:00
|
|
|
"lib.haskell.substitutePkgs:"
|
2021-03-15 00:00:53 +00:00
|
|
|
"warning:"
|
|
|
|
"while deriving ${sourceDescription}:"
|
|
|
|
"no substitute found for ${elemAt parse 1}"
|
|
|
|
])
|
|
|
|
exename;
|
|
|
|
in
|
|
|
|
if parse == null then
|
2022-03-21 23:55:53 +00:00
|
|
|
(pkgs.writeText name s).overrideAttrs (old: {
|
|
|
|
dependencies = old.dependencies or [] ++ dependencies;
|
|
|
|
})
|
|
|
|
|
2021-03-15 00:00:53 +00:00
|
|
|
else
|
2022-03-21 23:55:53 +00:00
|
|
|
f (dependencies ++ [pkg]) (prefix + toJSON substitute + suffix);
|
2021-03-15 00:00:53 +00:00
|
|
|
in
|
2022-03-21 23:55:53 +00:00
|
|
|
f [] (readFile path);
|
2021-03-15 00:00:53 +00:00
|
|
|
}
|