Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1{
2 lib,
3 writeTextFile,
4 buildPackages,
5}:
6
7# See https://people.freedesktop.org/~dbn/pkg-config-guide.html#concepts
8{
9 name, # The name of the pc file
10 # keywords
11 # provide a default description for convenience. it's not important but still required by pkg-config.
12 description ? "Pkg-config file for ${name}",
13 url ? "",
14 version ? "",
15 requires ? [ ],
16 requiresPrivate ? [ ],
17 conflicts ? [ ],
18 cflags ? [ ],
19 libs ? [ ],
20 libsPrivate ? [ ],
21 variables ? { },
22}:
23
24let
25 # only 'out' has to be changed, otherwise it would be replaced by the out of the writeTextFile
26 placeholderToSubstVar = builtins.replaceStrings [ "${placeholder "out"}" ] [ "@out@" ];
27
28 replacePlaceholderAndListToString =
29 x:
30 if builtins.isList x then
31 placeholderToSubstVar (builtins.concatStringsSep " " x)
32 else
33 placeholderToSubstVar x;
34
35 keywordsSection =
36 let
37 mustBeAList = attr: attrName: lib.throwIfNot (lib.isList attr) "'${attrName}' must be a list" attr;
38 in
39 {
40 "Name" = name;
41 "Description" = description;
42 "URL" = url;
43 "Version" = version;
44 "Requires" = mustBeAList requires "requires";
45 "Requires.private" = mustBeAList requiresPrivate "requiresPrivate";
46 "Conflicts" = mustBeAList conflicts "conflicts";
47 "Cflags" = mustBeAList cflags "cflags";
48 "Libs" = mustBeAList libs "libs";
49 "Libs.private" = mustBeAList libsPrivate "libsPrivate";
50 };
51
52 renderVariable =
53 name: value:
54 lib.optionalString (
55 value != "" && value != [ ]
56 ) "${name}=${replacePlaceholderAndListToString value}";
57 renderKeyword =
58 name: value:
59 lib.optionalString (
60 value != "" && value != [ ]
61 ) "${name}: ${replacePlaceholderAndListToString value}";
62
63 renderSomething =
64 renderFunc: attrs:
65 lib.pipe attrs [
66 (lib.mapAttrsToList renderFunc)
67 (builtins.filter (v: v != ""))
68 (lib.concatLines)
69 ];
70
71 variablesSectionRendered = renderSomething renderVariable variables;
72 keywordsSectionRendered = renderSomething renderKeyword keywordsSection;
73
74 content = [
75 variablesSectionRendered
76 keywordsSectionRendered
77 ];
78in
79writeTextFile {
80 name = "${name}.pc";
81 destination = "/lib/pkgconfig/${name}.pc";
82 text = builtins.concatStringsSep "\n" content;
83 checkPhase = ''${buildPackages.pkg-config}/bin/${buildPackages.pkg-config.targetPrefix}pkg-config --validate "$target"'';
84}