1/* Some functions for manipulating meta attributes, as well as the
2 name attribute. */
3
4{ lib }:
5
6rec {
7
8
9 /* Add to or override the meta attributes of the given
10 derivation.
11
12 Example:
13 addMetaAttrs {description = "Bla blah";} somePkg
14 */
15 addMetaAttrs = newAttrs: drv:
16 drv // { meta = (drv.meta or {}) // newAttrs; };
17
18
19 /* Disable Hydra builds of given derivation.
20 */
21 dontDistribute = drv: addMetaAttrs { hydraPlatforms = []; } drv;
22
23
24 /* Change the symbolic name of a package for presentation purposes
25 (i.e., so that nix-env users can tell them apart).
26 */
27 setName = name: drv: drv // {inherit name;};
28
29
30 /* Like `setName`, but takes the previous name as an argument.
31
32 Example:
33 updateName (oldName: oldName + "-experimental") somePkg
34 */
35 updateName = updater: drv: drv // {name = updater (drv.name);};
36
37
38 /* Append a suffix to the name of a package (before the version
39 part). */
40 appendToName = suffix: updateName (name:
41 let x = builtins.parseDrvName name; in "${x.name}-${suffix}-${x.version}");
42
43
44 /* Apply a function to each derivation and only to derivations in an attrset.
45 */
46 mapDerivationAttrset = f: set: lib.mapAttrs (name: pkg: if lib.isDerivation pkg then (f pkg) else pkg) set;
47
48 /* Set the nix-env priority of the package.
49 */
50 setPrio = priority: addMetaAttrs { inherit priority; };
51
52 /* Decrease the nix-env priority of the package, i.e., other
53 versions/variants of the package will be preferred.
54 */
55 lowPrio = setPrio 10;
56
57 /* Apply lowPrio to an attrset with derivations
58 */
59 lowPrioSet = set: mapDerivationAttrset lowPrio set;
60
61
62 /* Increase the nix-env priority of the package, i.e., this
63 version/variant of the package will be preferred.
64 */
65 hiPrio = setPrio (-10);
66
67 /* Apply hiPrio to an attrset with derivations
68 */
69 hiPrioSet = set: mapDerivationAttrset hiPrio set;
70
71
72 /* Check to see if a platform is matched by the given `meta.platforms`
73 element.
74
75 A `meta.platform` pattern is either
76
77 1. (legacy) a system string.
78
79 2. (modern) a pattern for the entire platform structure (see `lib.systems.inspect.platformPatterns`).
80
81 3. (modern) a pattern for the platform `parsed` field (see `lib.systems.inspect.patterns`).
82
83 We can inject these into a pattern for the whole of a structured platform,
84 and then match that.
85 */
86 platformMatch = platform: elem: let
87 pattern =
88 if builtins.isString elem
89 then { system = elem; }
90 else if elem?parsed
91 then elem
92 else { parsed = elem; };
93 in lib.matchAttrs pattern platform;
94
95 /* Check if a package is available on a given platform.
96
97 A package is available on a platform if both
98
99 1. One of `meta.platforms` pattern matches the given
100 platform, or `meta.platforms` is not present.
101
102 2. None of `meta.badPlatforms` pattern matches the given platform.
103 */
104 availableOn = platform: pkg:
105 ((!pkg?meta.platforms) || lib.any (platformMatch platform) pkg.meta.platforms) &&
106 lib.all (elem: !platformMatch platform elem) (pkg.meta.badPlatforms or []);
107
108 /* Get the corresponding attribute in lib.licenses
109 from the SPDX ID.
110 For SPDX IDs, see
111 https://spdx.org/licenses
112
113 Type:
114 getLicenseFromSpdxId :: str -> AttrSet
115
116 Example:
117 lib.getLicenseFromSpdxId "MIT" == lib.licenses.mit
118 => true
119 lib.getLicenseFromSpdxId "mIt" == lib.licenses.mit
120 => true
121 lib.getLicenseFromSpdxId "MY LICENSE"
122 => trace: warning: getLicenseFromSpdxId: No license matches the given SPDX ID: MY LICENSE
123 => { shortName = "MY LICENSE"; }
124 */
125 getLicenseFromSpdxId =
126 let
127 spdxLicenses = lib.mapAttrs (id: ls: assert lib.length ls == 1; builtins.head ls)
128 (lib.groupBy (l: lib.toLower l.spdxId) (lib.filter (l: l ? spdxId) (lib.attrValues lib.licenses)));
129 in licstr:
130 spdxLicenses.${ lib.toLower licstr } or (
131 lib.warn "getLicenseFromSpdxId: No license matches the given SPDX ID: ${licstr}"
132 { shortName = licstr; }
133 );
134
135 /* Get the path to the main program of a package based on meta.mainProgram
136
137 Type: getExe :: package -> string
138
139 Example:
140 getExe pkgs.hello
141 => "/nix/store/g124820p9hlv4lj8qplzxw1c44dxaw1k-hello-2.12/bin/hello"
142 getExe pkgs.mustache-go
143 => "/nix/store/am9ml4f4ywvivxnkiaqwr0hyxka1xjsf-mustache-go-1.3.0/bin/mustache"
144 */
145 getExe = x:
146 let
147 y = x.meta.mainProgram or (
148 # This could be turned into an error when 23.05 is at end of life
149 lib.warn "getExe: Package ${lib.strings.escapeNixIdentifier x.meta.name or x.pname or x.name} does not have the meta.mainProgram attribute. We'll assume that the main program has the same name for now, but this behavior is deprecated, because it leads to surprising errors when the assumption does not hold. If the package has a main program, please set `meta.mainProgram` in its definition to make this warning go away. Otherwise, if the package does not have a main program, or if you don't control its definition, use getExe' to specify the name to the program, such as lib.getExe' foo \"bar\"."
150 lib.getName
151 x
152 );
153 in
154 getExe' x y;
155
156 /* Get the path of a program of a derivation.
157
158 Type: getExe' :: derivation -> string -> string
159 Example:
160 getExe' pkgs.hello "hello"
161 => "/nix/store/g124820p9hlv4lj8qplzxw1c44dxaw1k-hello-2.12/bin/hello"
162 getExe' pkgs.imagemagick "convert"
163 => "/nix/store/5rs48jamq7k6sal98ymj9l4k2bnwq515-imagemagick-7.1.1-15/bin/convert"
164 */
165 getExe' = x: y:
166 assert lib.assertMsg (lib.isDerivation x)
167 "lib.meta.getExe': The first argument is of type ${builtins.typeOf x}, but it should be a derivation instead.";
168 assert lib.assertMsg (lib.isString y)
169 "lib.meta.getExe': The second argument is of type ${builtins.typeOf y}, but it should be a string instead.";
170 assert lib.assertMsg (builtins.length (lib.splitString "/" y) == 1)
171 "lib.meta.getExe': The second argument \"${y}\" is a nested path with a \"/\" character, but it should just be the name of the executable instead.";
172 "${lib.getBin x}/bin/${y}";
173}