default.nix (8433B)
1 /* 2 This file is provided under the MIT licence: 3 4 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 6 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 8 THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 9 */ 10 # Generated by npins. Do not modify; will be overwritten regularly 11 let 12 # Backwards-compatibly make something that previously didn't take any arguments take some 13 # The function must return an attrset, and will unfortunately be eagerly evaluated 14 # Same thing, but it catches eval errors on the default argument so that one may still call it with other arguments 15 mkFunctor = fn: let 16 e = builtins.tryEval (fn {}); 17 in 18 ( 19 if e.success 20 then e.value 21 else {error = fn {};} 22 ) 23 // {__functor = _self: fn;}; 24 25 # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295 26 range = first: last: 27 if first > last 28 then [] 29 else builtins.genList (n: first + n) (last - first + 1); 30 31 # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257 32 stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1)); 33 34 # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269 35 stringAsChars = f: s: concatStrings (map f (stringToCharacters s)); 36 concatStrings = builtins.concatStringsSep ""; 37 38 # If the environment variable NPINS_OVERRIDE_${name} is set, then use 39 # the path directly as opposed to the fetched source. 40 # (Taken from Niv for compatibility) 41 mayOverride = name: path: let 42 envVarName = "NPINS_OVERRIDE_${saneName}"; 43 saneName = stringAsChars (c: 44 if (builtins.match "[a-zA-Z0-9]" c) == null 45 then "_" 46 else c) 47 name; 48 ersatz = builtins.getEnv envVarName; 49 in 50 if ersatz == "" 51 then path 52 else 53 # this turns the string into an actual Nix path (for both absolute and 54 # relative paths) 55 builtins.trace "Overriding path of \"${name}\" with \"${ersatz}\" due to set \"${envVarName}\"" ( 56 if builtins.substring 0 1 ersatz == "/" 57 then /. + ersatz 58 else /. + builtins.getEnv "PWD" + "/${ersatz}" 59 ); 60 61 mkSource = name: spec: {pkgs ? null}: 62 assert spec ? type; let 63 # Unify across builtin and pkgs fetchers. 64 # `fetchGit` requires a wrapper because of slight API differences. 65 fetchers = 66 if pkgs == null 67 then { 68 inherit (builtins) fetchTarball fetchurl; 69 # Frustratingly, due to flakes and `fetchTree`, `fetchGit` 70 # has a different signature than the other builtin 71 # fetchers 72 fetchGit = args: (builtins.fetchGit args).outPath; 73 } 74 else { 75 fetchTarball = { 76 url, 77 sha256, 78 }: 79 pkgs.fetchzip { 80 inherit url sha256; 81 extension = "tar"; 82 }; 83 inherit (pkgs) fetchurl; 84 fetchGit = { 85 url, 86 submodules, 87 rev, 88 name, 89 narHash, 90 }: 91 pkgs.fetchgit { 92 inherit url rev name; 93 fetchSubmodules = submodules; 94 hash = narHash; 95 }; 96 }; 97 98 path = 99 if spec.type == "Git" 100 then mkGitSource fetchers spec 101 else if spec.type == "GitRelease" 102 then mkGitSource fetchers spec 103 else if spec.type == "PyPi" 104 then mkPyPiSource fetchers spec 105 else if spec.type == "Channel" 106 then mkChannelSource fetchers spec 107 else if spec.type == "Url" || spec.type == "MutableUrl" 108 then mkUrlSource fetchers spec 109 else if spec.type == "Container" 110 then mkContainerSource pkgs spec 111 else builtins.throw "Unknown source type ${spec.type}"; 112 in 113 spec // {outPath = mayOverride name path;}; 114 115 mkGitSource = { 116 fetchTarball, 117 fetchGit, 118 ... 119 }: { 120 repository, 121 revision, 122 url ? null, 123 submodules, 124 hash, 125 ... 126 }: 127 assert repository ? type; 128 # At the moment, either it is a plain git repository (which has an url), or it is a GitHub/GitLab repository 129 # In the latter case, there we will always be an url to the tarball 130 if url != null && !submodules 131 then 132 fetchTarball { 133 inherit url; 134 sha256 = hash; 135 } 136 else let 137 url = 138 if repository.type == "Git" 139 then repository.url 140 else if repository.type == "GitHub" 141 then "https://github.com/${repository.owner}/${repository.repo}.git" 142 else if repository.type == "GitLab" 143 then "${repository.server}/${repository.repo_path}.git" 144 else if repository.type == "Forgejo" 145 then "${repository.server}/${repository.owner}/${repository.repo}.git" 146 else throw "Unrecognized repository type ${repository.type}"; 147 urlToName = url: rev: let 148 matched = builtins.match "^.*/([^/]*)(\\.git)?$" url; 149 150 short = builtins.substring 0 7 rev; 151 152 appendShort = 153 if (builtins.match "[a-f0-9]*" rev) != null 154 then "-${short}" 155 else ""; 156 in "${ 157 if matched == null 158 then "source" 159 else builtins.head matched 160 }${appendShort}"; 161 name = urlToName url revision; 162 in 163 fetchGit { 164 rev = revision; 165 narHash = hash; 166 167 inherit name submodules url; 168 }; 169 170 mkPyPiSource = {fetchurl, ...}: { 171 url, 172 hash, 173 ... 174 }: 175 fetchurl { 176 inherit url; 177 sha256 = hash; 178 }; 179 180 mkChannelSource = {fetchTarball, ...}: { 181 url, 182 hash, 183 ... 184 }: 185 fetchTarball { 186 inherit url; 187 sha256 = hash; 188 }; 189 190 mkUrlSource = { 191 fetchTarball, 192 fetchurl, 193 ... 194 }: { 195 url, 196 hash, 197 unpack, 198 ... 199 }: 200 ( 201 if unpack 202 then fetchTarball 203 else fetchurl 204 ) { 205 inherit url; 206 sha256 = hash; 207 }; 208 209 mkContainerSource = pkgs: { 210 image_name, 211 image_tag, 212 image_digest, 213 hash, 214 ... 215 } @ args: 216 if pkgs == null 217 then builtins.throw "container sources require passing in a Nixpkgs value: https://github.com/andir/npins/blob/master/README.md#using-the-nixpkgs-fetchers" 218 else 219 pkgs.dockerTools.pullImage ( 220 { 221 imageName = image_name; 222 imageDigest = image_digest; 223 finalImageTag = image_tag; 224 inherit hash; 225 } 226 // ( 227 if args.arch or null != null 228 then {inherit (args) arch;} 229 else {} 230 ) 231 ); 232 in 233 mkFunctor ( 234 {input ? ./sources.json}: let 235 data = 236 if builtins.isPath input 237 then 238 # while `readFile` will throw an error anyways if the path doesn't exist, 239 # we still need to check beforehand because *our* error can be caught but not the one from the builtin 240 # See: <https://git.lix.systems/lix-project/lix/issues/1098> 241 if builtins.pathExists input 242 then builtins.fromJSON (builtins.readFile input) 243 else throw "Input path ${toString input} does not exist" 244 else if builtins.isAttrs input 245 then input 246 else throw "Unsupported input type ${builtins.typeOf input}, must be a path or an attrset"; 247 inherit (data) version; 248 in 249 if version == 8 250 then builtins.mapAttrs (name: spec: mkFunctor (mkSource name spec)) data.pins 251 else throw "Unsupported format version ${toString version} in sources.json. Try running `npins upgrade`" 252 )