1 /* Some functions for manipulating meta attributes, as well as the
7 inherit (lib) matchAttrs any all;
8 inherit (builtins) isString;
14 /* Add to or override the meta attributes of the given
18 addMetaAttrs {description = "Bla blah";} somePkg
20 addMetaAttrs = newAttrs: drv:
21 drv // { meta = (drv.meta or {}) // newAttrs; };
24 /* Disable Hydra builds of given derivation.
26 dontDistribute = drv: addMetaAttrs { hydraPlatforms = []; } drv;
29 /* Change the symbolic name of a package for presentation purposes
30 (i.e., so that nix-env users can tell them apart).
32 setName = name: drv: drv // {inherit name;};
35 /* Like `setName`, but takes the previous name as an argument.
38 updateName (oldName: oldName + "-experimental") somePkg
40 updateName = updater: drv: drv // {name = updater (drv.name);};
43 /* Append a suffix to the name of a package (before the version
45 appendToName = suffix: updateName (name:
46 let x = builtins.parseDrvName name; in "${x.name}-${suffix}-${x.version}");
49 /* Apply a function to each derivation and only to derivations in an attrset.
51 mapDerivationAttrset = f: set: lib.mapAttrs (name: pkg: if lib.isDerivation pkg then (f pkg) else pkg) set;
53 /* Set the nix-env priority of the package.
55 setPrio = priority: addMetaAttrs { inherit priority; };
57 /* Decrease the nix-env priority of the package, i.e., other
58 versions/variants of the package will be preferred.
62 /* Apply lowPrio to an attrset with derivations
64 lowPrioSet = set: mapDerivationAttrset lowPrio set;
67 /* Increase the nix-env priority of the package, i.e., this
68 version/variant of the package will be preferred.
70 hiPrio = setPrio (-10);
72 /* Apply hiPrio to an attrset with derivations
74 hiPrioSet = set: mapDerivationAttrset hiPrio set;
77 /* Check to see if a platform is matched by the given `meta.platforms`
80 A `meta.platform` pattern is either
82 1. (legacy) a system string.
84 2. (modern) a pattern for the entire platform structure (see `lib.systems.inspect.platformPatterns`).
86 3. (modern) a pattern for the platform `parsed` field (see `lib.systems.inspect.patterns`).
88 We can inject these into a pattern for the whole of a structured platform,
91 platformMatch = platform: elem: (
92 # Check with simple string comparison if elem was a string.
94 # The majority of comparisons done with this function will be against meta.platforms
95 # which contains a simple platform string.
97 # Avoiding an attrset allocation results in significant performance gains (~2-30) across the board in OfBorg
98 # because this is a hot path for nixpkgs.
99 if isString elem then platform ? system && elem == platform.system
101 # Normalize platform attrset.
102 if elem ? parsed then elem
103 else { parsed = elem; }
107 /* Check if a package is available on a given platform.
109 A package is available on a platform if both
111 1. One of `meta.platforms` pattern matches the given
112 platform, or `meta.platforms` is not present.
114 2. None of `meta.badPlatforms` pattern matches the given platform.
116 availableOn = platform: pkg:
117 ((!pkg?meta.platforms) || any (platformMatch platform) pkg.meta.platforms) &&
118 all (elem: !platformMatch platform elem) (pkg.meta.badPlatforms or []);
120 /* Get the corresponding attribute in lib.licenses
123 https://spdx.org/licenses
126 getLicenseFromSpdxId :: str -> AttrSet
129 lib.getLicenseFromSpdxId "MIT" == lib.licenses.mit
131 lib.getLicenseFromSpdxId "mIt" == lib.licenses.mit
133 lib.getLicenseFromSpdxId "MY LICENSE"
134 => trace: warning: getLicenseFromSpdxId: No license matches the given SPDX ID: MY LICENSE
135 => { shortName = "MY LICENSE"; }
137 getLicenseFromSpdxId =
139 spdxLicenses = lib.mapAttrs (id: ls: assert lib.length ls == 1; builtins.head ls)
140 (lib.groupBy (l: lib.toLower l.spdxId) (lib.filter (l: l ? spdxId) (lib.attrValues lib.licenses)));
142 spdxLicenses.${ lib.toLower licstr } or (
143 lib.warn "getLicenseFromSpdxId: No license matches the given SPDX ID: ${licstr}"
144 { shortName = licstr; }
147 /* Get the path to the main program of a package based on meta.mainProgram
149 Type: getExe :: package -> string
153 => "/nix/store/g124820p9hlv4lj8qplzxw1c44dxaw1k-hello-2.12/bin/hello"
154 getExe pkgs.mustache-go
155 => "/nix/store/am9ml4f4ywvivxnkiaqwr0hyxka1xjsf-mustache-go-1.3.0/bin/mustache"
159 y = x.meta.mainProgram or (
160 # This could be turned into an error when 23.05 is at end of life
161 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\"."
168 /* Get the path of a program of a derivation.
170 Type: getExe' :: derivation -> string -> string
172 getExe' pkgs.hello "hello"
173 => "/nix/store/g124820p9hlv4lj8qplzxw1c44dxaw1k-hello-2.12/bin/hello"
174 getExe' pkgs.imagemagick "convert"
175 => "/nix/store/5rs48jamq7k6sal98ymj9l4k2bnwq515-imagemagick-7.1.1-15/bin/convert"
178 assert lib.assertMsg (lib.isDerivation x)
179 "lib.meta.getExe': The first argument is of type ${builtins.typeOf x}, but it should be a derivation instead.";
180 assert lib.assertMsg (lib.isString y)
181 "lib.meta.getExe': The second argument is of type ${builtins.typeOf y}, but it should be a string instead.";
182 assert lib.assertMsg (builtins.length (lib.splitString "/" y) == 1)
183 "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.";
184 "${lib.getBin x}/bin/${y}";