|
| 1 | +# niv: no_update |
| 2 | +# This file has been generated by Niv. |
| 3 | + |
| 4 | +let |
| 5 | + |
| 6 | + # |
| 7 | + # The fetchers. fetch_<type> fetches specs of type <type>. |
| 8 | + # |
| 9 | + |
| 10 | + fetch_file = pkgs: spec: |
| 11 | + if spec.builtin or true then |
| 12 | + builtins_fetchurl { inherit (spec) url sha256; } |
| 13 | + else |
| 14 | + pkgs.fetchurl { inherit (spec) url sha256; }; |
| 15 | + |
| 16 | + fetch_tarball = pkgs: name: spec: |
| 17 | + let |
| 18 | + ok = str: ! builtins.isNull (builtins.match "[a-zA-Z0-9+-._?=]" str); |
| 19 | + # sanitize the name, though nix will still fail if name starts with period |
| 20 | + name' = stringAsChars (x: if ! ok x then "-" else x) "${name}-src"; |
| 21 | + in |
| 22 | + if spec.builtin or true then |
| 23 | + builtins_fetchTarball { name = name'; inherit (spec) url sha256; } |
| 24 | + else |
| 25 | + pkgs.fetchzip { name = name'; inherit (spec) url sha256; }; |
| 26 | + |
| 27 | + fetch_git = spec: |
| 28 | + builtins.fetchGit { url = spec.repo; inherit (spec) rev ref; }; |
| 29 | + |
| 30 | + fetch_local = spec: spec.path; |
| 31 | + |
| 32 | + fetch_builtin-tarball = name: throw |
| 33 | + ''[${name}] The niv type "builtin-tarball" is deprecated. You should instead use `builtin = true`. |
| 34 | + $ niv modify ${name} -a type=tarball -a builtin=true''; |
| 35 | + |
| 36 | + fetch_builtin-url = name: throw |
| 37 | + ''[${name}] The niv type "builtin-url" will soon be deprecated. You should instead use `builtin = true`. |
| 38 | + $ niv modify ${name} -a type=file -a builtin=true''; |
| 39 | + |
| 40 | + # |
| 41 | + # Various helpers |
| 42 | + # |
| 43 | + |
| 44 | + # The set of packages used when specs are fetched using non-builtins. |
| 45 | + mkPkgs = sources: |
| 46 | + let |
| 47 | + sourcesNixpkgs = |
| 48 | + import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) {}; |
| 49 | + hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath; |
| 50 | + hasThisAsNixpkgsPath = <nixpkgs> == ./.; |
| 51 | + in |
| 52 | + if builtins.hasAttr "nixpkgs" sources |
| 53 | + then sourcesNixpkgs |
| 54 | + else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then |
| 55 | + import <nixpkgs> {} |
| 56 | + else |
| 57 | + abort |
| 58 | + '' |
| 59 | + Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or |
| 60 | + add a package called "nixpkgs" to your sources.json. |
| 61 | + ''; |
| 62 | + |
| 63 | + # The actual fetching function. |
| 64 | + fetch = pkgs: name: spec: |
| 65 | + |
| 66 | + if ! builtins.hasAttr "type" spec then |
| 67 | + abort "ERROR: niv spec ${name} does not have a 'type' attribute" |
| 68 | + else if spec.type == "file" then fetch_file pkgs spec |
| 69 | + else if spec.type == "tarball" then fetch_tarball pkgs name spec |
| 70 | + else if spec.type == "git" then fetch_git spec |
| 71 | + else if spec.type == "local" then fetch_local spec |
| 72 | + else if spec.type == "builtin-tarball" then fetch_builtin-tarball name |
| 73 | + else if spec.type == "builtin-url" then fetch_builtin-url name |
| 74 | + else |
| 75 | + abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}"; |
| 76 | + |
| 77 | + # If the environment variable NIV_OVERRIDE_${name} is set, then use |
| 78 | + # the path directly as opposed to the fetched source. |
| 79 | + replace = name: drv: |
| 80 | + let |
| 81 | + saneName = stringAsChars (c: if isNull (builtins.match "[a-zA-Z0-9]" c) then "_" else c) name; |
| 82 | + ersatz = builtins.getEnv "NIV_OVERRIDE_${saneName}"; |
| 83 | + in |
| 84 | + if ersatz == "" then drv else ersatz; |
| 85 | + |
| 86 | + # Ports of functions for older nix versions |
| 87 | + |
| 88 | + # a Nix version of mapAttrs if the built-in doesn't exist |
| 89 | + mapAttrs = builtins.mapAttrs or ( |
| 90 | + f: set: with builtins; |
| 91 | + listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set)) |
| 92 | + ); |
| 93 | + |
| 94 | + # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295 |
| 95 | + range = first: last: if first > last then [] else builtins.genList (n: first + n) (last - first + 1); |
| 96 | + |
| 97 | + # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257 |
| 98 | + stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1)); |
| 99 | + |
| 100 | + # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269 |
| 101 | + stringAsChars = f: s: concatStrings (map f (stringToCharacters s)); |
| 102 | + concatStrings = builtins.concatStringsSep ""; |
| 103 | + |
| 104 | + # fetchTarball version that is compatible between all the versions of Nix |
| 105 | + builtins_fetchTarball = { url, name, sha256 }@attrs: |
| 106 | + let |
| 107 | + inherit (builtins) lessThan nixVersion fetchTarball; |
| 108 | + in |
| 109 | + if lessThan nixVersion "1.12" then |
| 110 | + fetchTarball { inherit name url; } |
| 111 | + else |
| 112 | + fetchTarball attrs; |
| 113 | + |
| 114 | + # fetchurl version that is compatible between all the versions of Nix |
| 115 | + builtins_fetchurl = { url, sha256 }@attrs: |
| 116 | + let |
| 117 | + inherit (builtins) lessThan nixVersion fetchurl; |
| 118 | + in |
| 119 | + if lessThan nixVersion "1.12" then |
| 120 | + fetchurl { inherit url; } |
| 121 | + else |
| 122 | + fetchurl attrs; |
| 123 | + |
| 124 | + # Create the final "sources" from the config |
| 125 | + mkSources = config: |
| 126 | + mapAttrs ( |
| 127 | + name: spec: |
| 128 | + if builtins.hasAttr "outPath" spec |
| 129 | + then abort |
| 130 | + "The values in sources.json should not have an 'outPath' attribute" |
| 131 | + else |
| 132 | + spec // { outPath = replace name (fetch config.pkgs name spec); } |
| 133 | + ) config.sources; |
| 134 | + |
| 135 | + # The "config" used by the fetchers |
| 136 | + mkConfig = |
| 137 | + { sourcesFile ? if builtins.pathExists ./sources.json then ./sources.json else null |
| 138 | + , sources ? if isNull sourcesFile then {} else builtins.fromJSON (builtins.readFile sourcesFile) |
| 139 | + , pkgs ? mkPkgs sources |
| 140 | + }: rec { |
| 141 | + # The sources, i.e. the attribute set of spec name to spec |
| 142 | + inherit sources; |
| 143 | + |
| 144 | + # The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers |
| 145 | + inherit pkgs; |
| 146 | + }; |
| 147 | + |
| 148 | +in |
| 149 | +mkSources (mkConfig {}) // { __functor = _: settings: mkSources (mkConfig settings); } |
0 commit comments