Builds a PDF resume from a json file
at main 7.5 kB view raw
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 11let 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 = 16 fn: 17 let 18 e = (builtins.tryEval (fn { })); 19 in 20 (if e.success then e.value else { error = fn { }; }) // { __functor = _self: fn; }; 21 22 # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295 23 range = 24 first: last: if first > last then [ ] else builtins.genList (n: first + n) (last - first + 1); 25 26 # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257 27 stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1)); 28 29 # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269 30 stringAsChars = f: s: concatStrings (map f (stringToCharacters s)); 31 concatMapStrings = f: list: concatStrings (map f list); 32 concatStrings = builtins.concatStringsSep ""; 33 34 # If the environment variable NPINS_OVERRIDE_${name} is set, then use 35 # the path directly as opposed to the fetched source. 36 # (Taken from Niv for compatibility) 37 mayOverride = 38 name: path: 39 let 40 envVarName = "NPINS_OVERRIDE_${saneName}"; 41 saneName = stringAsChars (c: if (builtins.match "[a-zA-Z0-9]" c) == null then "_" else c) name; 42 ersatz = builtins.getEnv envVarName; 43 in 44 if ersatz == "" then 45 path 46 else 47 # this turns the string into an actual Nix path (for both absolute and 48 # relative paths) 49 builtins.trace "Overriding path of \"${name}\" with \"${ersatz}\" due to set \"${envVarName}\"" ( 50 if builtins.substring 0 1 ersatz == "/" then 51 /. + ersatz 52 else 53 /. + builtins.getEnv "PWD" + "/${ersatz}" 54 ); 55 56 mkSource = 57 name: spec: 58 { 59 pkgs ? null, 60 }: 61 assert spec ? type; 62 let 63 # Unify across builtin and pkgs fetchers. 64 # `fetchGit` requires a wrapper because of slight API differences. 65 fetchers = 66 if pkgs == null then 67 { 68 inherit (builtins) fetchTarball fetchurl; 69 # For some fucking reason, fetchGit has a different signature than the other builtin fetchers … 70 fetchGit = args: (builtins.fetchGit args).outPath; 71 } 72 else 73 { 74 fetchTarball = pkgs.fetchzip; 75 inherit (pkgs) fetchurl; 76 fetchGit = 77 { 78 url, 79 submodules, 80 rev, 81 name, 82 narHash, 83 }: 84 pkgs.fetchgit { 85 inherit url rev name; 86 fetchSubmodules = submodules; 87 hash = narHash; 88 }; 89 }; 90 91 # Dispatch to the correct code path based on the type 92 path = 93 if spec.type == "Git" then 94 mkGitSource fetchers spec 95 else if spec.type == "GitRelease" then 96 mkGitSource fetchers spec 97 else if spec.type == "PyPi" then 98 mkPyPiSource fetchers spec 99 else if spec.type == "Channel" then 100 mkChannelSource fetchers spec 101 else if spec.type == "Tarball" then 102 mkTarballSource fetchers spec 103 else 104 builtins.throw "Unknown source type ${spec.type}"; 105 in 106 spec // { outPath = mayOverride name path; }; 107 108 mkGitSource = 109 { fetchTarball, fetchGit, ... }: 110 { 111 repository, 112 revision, 113 url ? null, 114 submodules, 115 hash, 116 branch ? null, 117 ... 118 }: 119 assert repository ? type; 120 # At the moment, either it is a plain git repository (which has an url), or it is a GitHub/GitLab repository 121 # In the latter case, there we will always be an url to the tarball 122 if url != null && !submodules then 123 fetchTarball { 124 inherit url; 125 sha256 = hash; 126 } 127 else 128 let 129 url = 130 if repository.type == "Git" then 131 repository.url 132 else if repository.type == "GitHub" then 133 "https://github.com/${repository.owner}/${repository.repo}.git" 134 else if repository.type == "GitLab" then 135 "${repository.server}/${repository.repo_path}.git" 136 else 137 throw "Unrecognized repository type ${repository.type}"; 138 urlToName = 139 url: rev: 140 let 141 matched = builtins.match "^.*/([^/]*)(\\.git)?$" url; 142 143 short = builtins.substring 0 7 rev; 144 145 appendShort = if (builtins.match "[a-f0-9]*" rev) != null then "-${short}" else ""; 146 in 147 "${if matched == null then "source" else builtins.head matched}${appendShort}"; 148 name = urlToName url revision; 149 in 150 fetchGit { 151 rev = revision; 152 narHash = hash; 153 154 inherit name submodules url; 155 }; 156 157 mkPyPiSource = 158 { fetchurl, ... }: 159 { url, hash, ... }: 160 fetchurl { 161 inherit url; 162 sha256 = hash; 163 }; 164 165 mkChannelSource = 166 { fetchTarball, ... }: 167 { url, hash, ... }: 168 fetchTarball { 169 inherit url; 170 sha256 = hash; 171 }; 172 173 mkTarballSource = 174 { fetchTarball, ... }: 175 { 176 url, 177 locked_url ? url, 178 hash, 179 ... 180 }: 181 fetchTarball { 182 url = locked_url; 183 sha256 = hash; 184 }; 185in 186mkFunctor ( 187 { 188 input ? ./sources.json, 189 }: 190 let 191 data = 192 if builtins.isPath input then 193 # while `readFile` will throw an error anyways if the path doesn't exist, 194 # we still need to check beforehand because *our* error can be caught but not the one from the builtin 195 # *piegames sighs* 196 if builtins.pathExists input then 197 builtins.fromJSON (builtins.readFile input) 198 else 199 throw "Input path ${toString input} does not exist" 200 else if builtins.isAttrs input then 201 input 202 else 203 throw "Unsupported input type ${builtins.typeOf input}, must be a path or an attrset"; 204 version = data.version; 205 in 206 if version == 6 then 207 builtins.mapAttrs (name: spec: mkFunctor (mkSource name spec)) data.pins 208 else 209 throw "Unsupported format version ${toString version} in sources.json. Try running `npins upgrade`" 210)