summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2021-03-15 01:00:53 +0100
committertv <tv@krebsco.de>2021-03-15 01:46:50 +0100
commit1f14bf6e4050d904b6d6d60e68883d33dc8f04df (patch)
treeaa4533b014a1cbad1dff396bde0e48b8e9d3274e /lib
parent4bc25074b7e4efab882addd634ef24778ebe5b93 (diff)
lib.haskell.substitutePkgs: init
Diffstat (limited to 'lib')
-rw-r--r--lib/default.nix1
-rw-r--r--lib/haskell.nix51
2 files changed, 52 insertions, 0 deletions
diff --git a/lib/default.nix b/lib/default.nix
index 4190f8f..738e521 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -5,6 +5,7 @@ let
evalSource = import ./eval-source.nix;
git = import ./git.nix { inherit lib; };
+ haskell = import ./haskell.nix { inherit lib; };
krebs = import ./krebs lib;
krops = import ../submodules/krops/lib;
shell = import ./shell.nix { inherit lib; };
diff --git a/lib/haskell.nix b/lib/haskell.nix
new file mode 100644
index 0000000..b1889ca
--- /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;
+}