on fd39d56 help sign in
haskell.nix/lib/default.nix
93 lines 2.8 kB history raw
1{ lib, haskellLib }:
3with haskellLib;
5{
6 subComponentTypes = [
7 "sublibs"
8 "foreignlibs"
9 "exes"
10 "tests"
11 "benchmarks"
12 ];
14 mergeComponents = lib.zipAttrsWith (_: comps: lib.genAttrs [
15 "depends"
16 "libs"
17 "frameworks"
18 "pkgconfig"
19 "build-tools"
20 ] (n: {
21 ${n} = lib.concatMap (comp: comp.${n} or []) comps;
22 }));
24 unionrAttrs = lib.foldr (a: b: a // b) {};
26 mergeConfigs = configs:
27 let
28 mergeComponentType = type: mergeComponents (map (conf: (conf.components or {}).${type} or {}) configs);
29 in unionrAttrs configs // {
30 flags = unionrAttrs (map (conf: conf.flags or {}) configs);
31 package = lib.foldr (conf: lib.recursiveUpdate (conf.package or {})) {} configs;
32 components =
33 mergeComponents (map (conf: removeAttrs (conf.components or {}) subComponentTypes) configs)
34 // lib.genAttrs subComponentTypes mergeComponentType;
35 configureFlags = concatMap (conf: conf.configureFlags or []) configs;
36 };
38 foldrAttrVals = f: z: attrs:
39 lib.foldr (g: acc: g acc) z (lib.mapAttrsToList f attrs);
41 attrLength = attrs: builtins.length (builtins.attrValues attrs);
43 foldComponents = tys: f: z: conf:
44 let
45 comps = conf.components or {};
46 foldSection = foldrAttrVals f;
47 libComp = acc:
48 foldSection acc
49 (removeAttrs comps subComponentTypes);
50 subComps = acc:
51 lib.foldr
52 (ty: acc': foldSection acc' (comps.${ty} or {}))
53 acc
54 tys;
55 in libComp (subComps z);
57 componentPrefix = {
58 # Are all of these right?
59 sublibs = "sublib";
60 foreignlibs = "foreignlib";
61 exes = "exe";
62 tests = "test";
63 benchmarks = "bench";
64 };
66 applyComponents = f: config:
67 let
68 comps = config.components;
69 libComp = lib.mapAttrs (cname: f {ctype="lib"; cname=config.package.identifier.name;}) (removeAttrs comps subComponentTypes);
70 subComps = lib.mapAttrs
71 (ctype: lib.mapAttrs (cname: f {inherit cname; ctype=componentPrefix.${ctype};}))
72 (builtins.intersectAttrs (lib.genAttrs subComponentTypes (_: null)) comps);
73 in subComps // libComp;
75 isLibrary = componentId: componentId.ctype == "lib";
77 # Avoid pkgs.callPackage for now. It does a lot of nonsense with OOP
78 # style programming that we should avoid until we know we want it.
80 # weakCallPackage: call a function or (importable expression)
81 # with scope + args.
82 #
83 # weakCallPackage scope f args
84 # will call f (scope // args)
85 #
86 # weakCallpackage scope ./path args
87 # will call the expression at ./path with (scope // args)
88 #
89 weakCallPackage = scope: f: args:
90 let f' = if lib.isFunction f then f else import f;
91 args' = scope // args;
92 in f' (builtins.intersectAttrs (builtins.functionArgs f') args');