at 23.05-pre 4.3 kB view raw
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 platform `parsed` field. 80 81 We can inject these into a pattern for the whole of a structured platform, 82 and then match that. 83 */ 84 platformMatch = platform: elem: let 85 pattern = 86 if builtins.isString elem 87 then { system = elem; } 88 else { parsed = elem; }; 89 in lib.matchAttrs pattern platform; 90 91 /* Check if a package is available on a given platform. 92 93 A package is available on a platform if both 94 95 1. One of `meta.platforms` pattern matches the given platform. 96 97 2. None of `meta.badPlatforms` pattern matches the given platform. 98 */ 99 availableOn = platform: pkg: 100 lib.any (platformMatch platform) pkg.meta.platforms && 101 lib.all (elem: !platformMatch platform elem) (pkg.meta.badPlatforms or []); 102 103 /* Get the corresponding attribute in lib.licenses 104 from the SPDX ID. 105 For SPDX IDs, see 106 https://spdx.org/licenses 107 108 Type: 109 getLicenseFromSpdxId :: str -> AttrSet 110 111 Example: 112 lib.getLicenseFromSpdxId "MIT" == lib.licenses.mit 113 => true 114 lib.getLicenseFromSpdxId "mIt" == lib.licenses.mit 115 => true 116 lib.getLicenseFromSpdxId "MY LICENSE" 117 => trace: warning: getLicenseFromSpdxId: No license matches the given SPDX ID: MY LICENSE 118 => { shortName = "MY LICENSE"; } 119 */ 120 getLicenseFromSpdxId = 121 let 122 spdxLicenses = lib.mapAttrs (id: ls: assert lib.length ls == 1; builtins.head ls) 123 (lib.groupBy (l: lib.toLower l.spdxId) (lib.filter (l: l ? spdxId) (lib.attrValues lib.licenses))); 124 in licstr: 125 spdxLicenses.${ lib.toLower licstr } or ( 126 lib.warn "getLicenseFromSpdxId: No license matches the given SPDX ID: ${licstr}" 127 { shortName = licstr; } 128 ); 129 130 /* Get the path to the main program of a derivation with either 131 meta.mainProgram or pname or name 132 133 Type: getExe :: derivation -> string 134 135 Example: 136 getExe pkgs.hello 137 => "/nix/store/g124820p9hlv4lj8qplzxw1c44dxaw1k-hello-2.12/bin/hello" 138 getExe pkgs.mustache-go 139 => "/nix/store/am9ml4f4ywvivxnkiaqwr0hyxka1xjsf-mustache-go-1.3.0/bin/mustache" 140 */ 141 getExe = x: 142 "${lib.getBin x}/bin/${x.meta.mainProgram or (lib.getName x)}"; 143}