···11+# This file has been generated by Niv.
22+33+let
44+55+ #
66+ # The fetchers. fetch_<type> fetches specs of type <type>.
77+ #
88+99+ fetch_file = pkgs: spec:
1010+ if spec.builtin or true then
1111+ builtins_fetchurl { inherit (spec) url sha256; }
1212+ else
1313+ pkgs.fetchurl { inherit (spec) url sha256; };
1414+1515+ fetch_tarball = pkgs: spec:
1616+ if spec.builtin or true then
1717+ builtins_fetchTarball { inherit (spec) url sha256; }
1818+ else
1919+ pkgs.fetchzip { inherit (spec) url sha256; };
2020+2121+ fetch_git = spec:
2222+ builtins.fetchGit { url = spec.repo; inherit (spec) rev ref; };
2323+2424+ fetch_builtin-tarball = spec:
2525+ builtins.trace
2626+ ''
2727+ WARNING:
2828+ The niv type "builtin-tarball" will soon be deprecated. You should
2929+ instead use `builtin = true`.
3030+3131+ $ niv modify <package> -a type=tarball -a builtin=true
3232+ ''
3333+ builtins_fetchTarball { inherit (spec) url sha256; };
3434+3535+ fetch_builtin-url = spec:
3636+ builtins.trace
3737+ ''
3838+ WARNING:
3939+ The niv type "builtin-url" will soon be deprecated. You should
4040+ instead use `builtin = true`.
4141+4242+ $ niv modify <package> -a type=file -a builtin=true
4343+ ''
4444+ (builtins_fetchurl { inherit (spec) url sha256; });
4545+4646+ #
4747+ # Various helpers
4848+ #
4949+5050+ # The set of packages used when specs are fetched using non-builtins.
5151+ mkPkgs = sources:
5252+ let
5353+ sourcesNixpkgs =
5454+ import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) {};
5555+ hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath;
5656+ hasThisAsNixpkgsPath = <nixpkgs> == ./.;
5757+ in
5858+ if builtins.hasAttr "nixpkgs" sources
5959+ then sourcesNixpkgs
6060+ else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then
6161+ import <nixpkgs> {}
6262+ else
6363+ abort
6464+ ''
6565+ Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or
6666+ add a package called "nixpkgs" to your sources.json.
6767+ '';
6868+6969+ # The actual fetching function.
7070+ fetch = pkgs: name: spec:
7171+7272+ if ! builtins.hasAttr "type" spec then
7373+ abort "ERROR: niv spec ${name} does not have a 'type' attribute"
7474+ else if spec.type == "file" then fetch_file pkgs spec
7575+ else if spec.type == "tarball" then fetch_tarball pkgs spec
7676+ else if spec.type == "git" then fetch_git spec
7777+ else if spec.type == "builtin-tarball" then fetch_builtin-tarball spec
7878+ else if spec.type == "builtin-url" then fetch_builtin-url spec
7979+ else
8080+ abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}";
8181+8282+ # Ports of functions for older nix versions
8383+8484+ # a Nix version of mapAttrs if the built-in doesn't exist
8585+ mapAttrs = builtins.mapAttrs or (
8686+ f: set: with builtins;
8787+ listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set))
8888+ );
8989+9090+ # fetchTarball version that is compatible between all the versions of Nix
9191+ builtins_fetchTarball = { url, sha256 }@attrs:
9292+ let
9393+ inherit (builtins) lessThan nixVersion fetchTarball;
9494+ in
9595+ if lessThan nixVersion "1.12" then
9696+ fetchTarball { inherit url; }
9797+ else
9898+ fetchTarball attrs;
9999+100100+ # fetchurl version that is compatible between all the versions of Nix
101101+ builtins_fetchurl = { url, sha256 }@attrs:
102102+ let
103103+ inherit (builtins) lessThan nixVersion fetchurl;
104104+ in
105105+ if lessThan nixVersion "1.12" then
106106+ fetchurl { inherit url; }
107107+ else
108108+ fetchurl attrs;
109109+110110+ # Create the final "sources" from the config
111111+ mkSources = config:
112112+ mapAttrs (
113113+ name: spec:
114114+ if builtins.hasAttr "outPath" spec
115115+ then abort
116116+ "The values in sources.json should not have an 'outPath' attribute"
117117+ else
118118+ spec // { outPath = fetch config.pkgs name spec; }
119119+ ) config.sources;
120120+121121+ # The "config" used by the fetchers
122122+ mkConfig =
123123+ { sourcesFile ? ./sources.json
124124+ , sources ? builtins.fromJSON (builtins.readFile sourcesFile)
125125+ , pkgs ? mkPkgs sources
126126+ }: rec {
127127+ # The sources, i.e. the attribute set of spec name to spec
128128+ inherit sources;
129129+130130+ # The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers
131131+ inherit pkgs;
132132+ };
133133+in
134134+mkSources (mkConfig {}) // { __functor = _: settings: mkSources (mkConfig settings); }