|
6 | 6 | # The fetchers. fetch_<type> fetches specs of type <type>.
|
7 | 7 | #
|
8 | 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; }; |
| 9 | + fetch_file = pkgs: name: spec: |
| 10 | + let |
| 11 | + name' = sanitizeName name + "-src"; |
| 12 | + in |
| 13 | + if spec.builtin or true then |
| 14 | + builtins_fetchurl { inherit (spec) url sha256; name = name'; } |
| 15 | + else |
| 16 | + pkgs.fetchurl { inherit (spec) url sha256; name = name'; }; |
14 | 17 |
|
15 | 18 | fetch_tarball = pkgs: name: spec:
|
16 | 19 | 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 | + name' = sanitizeName name + "-src"; |
20 | 21 | in
|
21 | 22 | if spec.builtin or true then
|
22 | 23 | builtins_fetchTarball { name = name'; inherit (spec) url sha256; }
|
23 | 24 | else
|
24 | 25 | pkgs.fetchzip { name = name'; inherit (spec) url sha256; };
|
25 | 26 |
|
26 |
| - fetch_git = spec: |
27 |
| - builtins.fetchGit { url = spec.repo; inherit (spec) rev ref; }; |
| 27 | + fetch_git = name: spec: |
| 28 | + let |
| 29 | + ref = |
| 30 | + if spec ? ref then spec.ref else |
| 31 | + if spec ? branch then "refs/heads/${spec.branch}" else |
| 32 | + if spec ? tag then "refs/tags/${spec.tag}" else |
| 33 | + abort "In git source '${name}': Please specify `ref`, `tag` or `branch`!"; |
| 34 | + in |
| 35 | + builtins.fetchGit { url = spec.repo; inherit (spec) rev; inherit ref; }; |
28 | 36 |
|
29 | 37 | fetch_local = spec: spec.path;
|
30 | 38 |
|
|
40 | 48 | # Various helpers
|
41 | 49 | #
|
42 | 50 |
|
| 51 | + # https://github.com/NixOS/nixpkgs/pull/83241/files#diff-c6f540a4f3bfa4b0e8b6bafd4cd54e8bR695 |
| 52 | + sanitizeName = name: |
| 53 | + ( |
| 54 | + concatMapStrings (s: if builtins.isList s then "-" else s) |
| 55 | + ( |
| 56 | + builtins.split "[^[:alnum:]+._?=-]+" |
| 57 | + ((x: builtins.elemAt (builtins.match "\\.*(.*)" x) 0) name) |
| 58 | + ) |
| 59 | + ); |
| 60 | + |
43 | 61 | # The set of packages used when specs are fetched using non-builtins.
|
44 |
| - mkPkgs = sources: |
| 62 | + mkPkgs = sources: system: |
45 | 63 | let
|
46 | 64 | sourcesNixpkgs =
|
47 |
| - import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) {}; |
| 65 | + import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) { inherit system; }; |
48 | 66 | hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath;
|
49 | 67 | hasThisAsNixpkgsPath = <nixpkgs> == ./.;
|
50 | 68 | in
|
|
64 | 82 |
|
65 | 83 | if ! builtins.hasAttr "type" spec then
|
66 | 84 | abort "ERROR: niv spec ${name} does not have a 'type' attribute"
|
67 |
| - else if spec.type == "file" then fetch_file pkgs spec |
| 85 | + else if spec.type == "file" then fetch_file pkgs name spec |
68 | 86 | else if spec.type == "tarball" then fetch_tarball pkgs name spec
|
69 |
| - else if spec.type == "git" then fetch_git spec |
| 87 | + else if spec.type == "git" then fetch_git name spec |
70 | 88 | else if spec.type == "local" then fetch_local spec
|
71 | 89 | else if spec.type == "builtin-tarball" then fetch_builtin-tarball name
|
72 | 90 | else if spec.type == "builtin-url" then fetch_builtin-url name
|
|
80 | 98 | saneName = stringAsChars (c: if isNull (builtins.match "[a-zA-Z0-9]" c) then "_" else c) name;
|
81 | 99 | ersatz = builtins.getEnv "NIV_OVERRIDE_${saneName}";
|
82 | 100 | in
|
83 |
| - if ersatz == "" then drv else ersatz; |
| 101 | + if ersatz == "" then drv else |
| 102 | + # this turns the string into an actual Nix path (for both absolute and |
| 103 | + # relative paths) |
| 104 | + if builtins.substring 0 1 ersatz == "/" then /. + ersatz else /. + builtins.getEnv "PWD" + "/${ersatz}"; |
84 | 105 |
|
85 | 106 | # Ports of functions for older nix versions
|
86 | 107 |
|
|
98 | 119 |
|
99 | 120 | # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269
|
100 | 121 | stringAsChars = f: s: concatStrings (map f (stringToCharacters s));
|
| 122 | + concatMapStrings = f: list: concatStrings (map f list); |
101 | 123 | concatStrings = builtins.concatStringsSep "";
|
102 | 124 |
|
| 125 | + # https://github.com/NixOS/nixpkgs/blob/8a9f58a375c401b96da862d969f66429def1d118/lib/attrsets.nix#L331 |
| 126 | + optionalAttrs = cond: as: if cond then as else {}; |
| 127 | + |
103 | 128 | # fetchTarball version that is compatible between all the versions of Nix
|
104 |
| - builtins_fetchTarball = { url, name, sha256 }@attrs: |
| 129 | + builtins_fetchTarball = { url, name ? null, sha256 }@attrs: |
105 | 130 | let
|
106 | 131 | inherit (builtins) lessThan nixVersion fetchTarball;
|
107 | 132 | in
|
108 | 133 | if lessThan nixVersion "1.12" then
|
109 |
| - fetchTarball { inherit name url; } |
| 134 | + fetchTarball ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; })) |
110 | 135 | else
|
111 | 136 | fetchTarball attrs;
|
112 | 137 |
|
113 | 138 | # fetchurl version that is compatible between all the versions of Nix
|
114 |
| - builtins_fetchurl = { url, sha256 }@attrs: |
| 139 | + builtins_fetchurl = { url, name ? null, sha256 }@attrs: |
115 | 140 | let
|
116 | 141 | inherit (builtins) lessThan nixVersion fetchurl;
|
117 | 142 | in
|
118 | 143 | if lessThan nixVersion "1.12" then
|
119 |
| - fetchurl { inherit url; } |
| 144 | + fetchurl ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; })) |
120 | 145 | else
|
121 | 146 | fetchurl attrs;
|
122 | 147 |
|
|
135 | 160 | mkConfig =
|
136 | 161 | { sourcesFile ? if builtins.pathExists ./sources.json then ./sources.json else null
|
137 | 162 | , sources ? if isNull sourcesFile then {} else builtins.fromJSON (builtins.readFile sourcesFile)
|
138 |
| - , pkgs ? mkPkgs sources |
| 163 | + , system ? builtins.currentSystem |
| 164 | + , pkgs ? mkPkgs sources system |
139 | 165 | }: rec {
|
140 | 166 | # The sources, i.e. the attribute set of spec name to spec
|
141 | 167 | inherit sources;
|
|
0 commit comments