@@ -6,25 +6,33 @@ let
66 # The fetchers. fetch_<type> fetches specs of type <type>.
77 #
88
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'; };
1417
1518 fetch_tarball = pkgs: name: spec:
1619 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";
2021 in
2122 if spec.builtin or true then
2223 builtins_fetchTarball { name = name'; inherit (spec) url sha256; }
2324 else
2425 pkgs.fetchzip { name = name'; inherit (spec) url sha256; };
2526
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; };
2836
2937 fetch_local = spec: spec.path;
3038
@@ -40,11 +48,21 @@ let
4048 # Various helpers
4149 #
4250
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+
4361 # The set of packages used when specs are fetched using non-builtins.
44− mkPkgs = sources:
62+ mkPkgs = sources: system:
4563 let
4664 sourcesNixpkgs =
47− import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) {};
65+ import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) { inherit system; };
4866 hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath;
4967 hasThisAsNixpkgsPath = <nixpkgs> == ./.;
5068 in
@@ -64,9 +82,9 @@ let
6482
6583 if ! builtins.hasAttr "type" spec then
6684 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
6886 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
7088 else if spec.type == "local" then fetch_local spec
7189 else if spec.type == "builtin-tarball" then fetch_builtin-tarball name
7290 else if spec.type == "builtin-url" then fetch_builtin-url name
@@ -80,7 +98,10 @@ let
8098 saneName = stringAsChars (c: if isNull (builtins.match "[a-zA-Z0-9]" c) then "_" else c) name;
8199 ersatz = builtins.getEnv "NIV_OVERRIDE_${saneName}";
82100 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}";
84105
85106 # Ports of functions for older nix versions
86107
@@ -98,25 +119,29 @@ let
98119
99120 # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269
100121 stringAsChars = f: s: concatStrings (map f (stringToCharacters s));
122+ concatMapStrings = f: list: concatStrings (map f list);
101123 concatStrings = builtins.concatStringsSep "";
102124
125+ # https://github.com/NixOS/nixpkgs/blob/8a9f58a375c401b96da862d969f66429def1d118/lib/attrsets.nix#L331
126+ optionalAttrs = cond: as: if cond then as else {};
127+
103128 # 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:
105130 let
106131 inherit (builtins) lessThan nixVersion fetchTarball;
107132 in
108133 if lessThan nixVersion "1.12" then
109− fetchTarball { inherit name url; }
134+ fetchTarball ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; }))
110135 else
111136 fetchTarball attrs;
112137
113138 # 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:
115140 let
116141 inherit (builtins) lessThan nixVersion fetchurl;
117142 in
118143 if lessThan nixVersion "1.12" then
119− fetchurl { inherit url; }
144+ fetchurl ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; }))
120145 else
121146 fetchurl attrs;
122147
@@ -135,7 +160,8 @@ let
135160 mkConfig =
136161 { sourcesFile ? if builtins.pathExists ./sources.json then ./sources.json else null
137162 , sources ? if isNull sourcesFile then {} else builtins.fromJSON (builtins.readFile sourcesFile)
138− , pkgs ? mkPkgs sources
163+ , system ? builtins.currentSystem
164+ , pkgs ? mkPkgs sources system
139165 }: rec {
140166 # The sources, i.e. the attribute set of spec name to spec
141167 inherit sources;