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