Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
at flake-libs 139 lines 3.6 kB view raw
1{ _cuda, lib }: 2{ 3 /** 4 Evaluate assertions and add error context to return value. 5 6 NOTE: No guarantees are made about this function's stability. You may use it at your own risk. 7 8 # Type 9 10 ``` 11 _evaluateAssertions 12 :: (assertions :: List { assertion :: Bool, message :: String }) 13 -> Bool 14 ``` 15 */ 16 _evaluateAssertions = 17 assertions: 18 let 19 failedAssertionsString = _cuda.lib._mkFailedAssertionsString assertions; 20 in 21 if failedAssertionsString == "" then 22 true 23 else 24 lib.addErrorContext "with failed assertions:${failedAssertionsString}" false; 25 26 /** 27 Function to generate a string of failed assertions. 28 29 NOTE: No guarantees are made about this function's stability. You may use it at your own risk. 30 31 # Type 32 33 ``` 34 _mkFailedAssertionsString 35 :: (assertions :: List { assertion :: Bool, message :: String }) 36 -> String 37 ``` 38 39 # Inputs 40 41 `assertions` 42 43 : A list of assertions to evaluate 44 45 # Examples 46 47 :::{.example} 48 ## `_cuda.lib._mkFailedAssertionsString` usage examples 49 50 ```nix 51 _mkFailedAssertionsString [ 52 { assertion = false; message = "Assertion 1 failed"; } 53 { assertion = true; message = "Assertion 2 failed"; } 54 ] 55 => "\n- Assertion 1 failed" 56 ``` 57 58 ```nix 59 _mkFailedAssertionsString [ 60 { assertion = false; message = "Assertion 1 failed"; } 61 { assertion = false; message = "Assertion 2 failed"; } 62 ] 63 => "\n- Assertion 1 failed\n- Assertion 2 failed" 64 ``` 65 ::: 66 */ 67 _mkFailedAssertionsString = lib.foldl' ( 68 failedAssertionsString: 69 { assertion, message }: 70 failedAssertionsString + lib.optionalString (!assertion) ("\n- " + message) 71 ) ""; 72 73 /** 74 Utility function to generate assertions for missing packages. 75 76 Used to mark a package as unsupported if any of its required packages are missing (null). 77 78 Expects a set of attributes. 79 80 Most commonly used in overrides files on a callPackage-provided attribute set of packages. 81 82 NOTE: We typically use platfromAssertions instead of brokenAssertions because the presence of packages set to null 83 means evaluation will fail if package attributes are accessed without checking for null first. OfBorg evaluation 84 sets allowBroken to true, which means we can't rely on brokenAssertions to prevent evaluation of a package with 85 missing dependencies. 86 87 NOTE: No guarantees are made about this function's stability. You may use it at your own risk. 88 89 # Type 90 91 ``` 92 _mkMissingPackagesAssertions 93 :: (attrs :: AttrSet) 94 -> (assertions :: List { assertion :: Bool, message :: String }) 95 ``` 96 97 # Inputs 98 99 `attrs` 100 101 : The attributes to check for null 102 103 # Examples 104 105 :::{.example} 106 ## `_cuda.lib._mkMissingPackagesAssertions` usage examples 107 108 ```nix 109 { 110 lib, 111 libcal ? null, 112 libcublas, 113 utils, 114 }: 115 let 116 inherit (lib.attrsets) recursiveUpdate; 117 inherit (_cuda.lib) _mkMissingPackagesAssertions; 118 in 119 prevAttrs: { 120 passthru = prevAttrs.passthru or { } // { 121 platformAssertions = 122 prevAttrs.passthru.platformAssertions or [ ] 123 ++ _mkMissingPackagesAssertions { inherit libcal; }; 124 }; 125 } 126 ``` 127 ::: 128 */ 129 _mkMissingPackagesAssertions = lib.flip lib.pipe [ 130 # Take the attributes that are null. 131 (lib.filterAttrs (_: value: value == null)) 132 lib.attrNames 133 # Map them to assertions. 134 (lib.map (name: { 135 message = "${name} is available"; 136 assertion = false; 137 })) 138 ]; 139}