summaryrefslogtreecommitdiffstats
path: root/lib/haskell.nix
diff options
context:
space:
mode:
authorlassulus <lassulus@lassul.us>2021-03-19 09:19:54 +0100
committerlassulus <lassulus@lassul.us>2021-03-19 09:19:54 +0100
commit95ab9a7af06d56c3f02a2246b8eee8a589ed3f0c (patch)
treed6ffff9d9694f193649f08863dff06dbecb50eee /lib/haskell.nix
parent86b458d5d3aaac94d51b0a860376725c0a70b6fb (diff)
parentf956b05cf1fbafd022fe2f0199e9524cd6efbe85 (diff)
Merge remote-tracking branch 'ni/master'
Diffstat (limited to 'lib/haskell.nix')
-rw-r--r--lib/haskell.nix51
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/haskell.nix b/lib/haskell.nix
new file mode 100644
index 000000000..b1889caf0
--- /dev/null
+++ b/lib/haskell.nix
@@ -0,0 +1,51 @@
+{ 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 }:
+ pkgs.writeText name (substitutePkgs' {
+ inherit pkgs;
+ sourceDescription =
+ if callsite != null then
+ "${name} in ${toString callsite}"
+ else
+ "${name} from ${toString path}";
+ text = readFile path;
+ });
+
+ substitutePkgs' = { pkgs, sourceDescription, text }:
+ let
+ f = s:
+ 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 [
+ "lib.haskell.replacePkg:"
+ "warning:"
+ "while deriving ${sourceDescription}:"
+ "no substitute found for ${elemAt parse 1}"
+ ])
+ exename;
+ in
+ if parse == null then
+ s
+ else
+ f (prefix + toJSON substitute + suffix);
+ in
+ f text;
+}