@@ -6,25 +6,33 @@ let
6 # The fetchers. fetch_<type> fetches specs of type <type>.
6 # The fetchers. fetch_<type> fetches specs of type <type>.
9− fetch_file = pkgs: spec:
9+ fetch_file = pkgs: name: spec:
11+ name' = sanitizeName name + "-src";
10− if spec.builtin or true then
13+ if spec.builtin or true then
11− builtins_fetchurl { inherit (spec) url sha256; }
14+ builtins_fetchurl { inherit (spec) url sha256; name = name'; }
13− pkgs.fetchurl { inherit (spec) url sha256; };
16+ pkgs.fetchurl { inherit (spec) url sha256; name = name'; };
15 fetch_tarball = pkgs: name: spec:
18 fetch_tarball = pkgs: name: spec:
17− ok = str: ! builtins.isNull (builtins.match "[a-zA-Z0-9+-._?=]" str);
20+ name' = sanitizeName name + "-src";
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";
21 if spec.builtin or true then
22 if spec.builtin or true then
22 builtins_fetchTarball { name = name'; inherit (spec) url sha256; }
23 builtins_fetchTarball { name = name'; inherit (spec) url sha256; }
24 pkgs.fetchzip { name = name'; inherit (spec) url sha256; };
25 pkgs.fetchzip { name = name'; inherit (spec) url sha256; };
26− fetch_git = spec:
27+ fetch_git = name: spec:
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`!";
27− builtins.fetchGit { url = spec.repo; inherit (spec) rev ref; };
35+ builtins.fetchGit { url = spec.repo; inherit (spec) rev; inherit ref; };
29 fetch_local = spec: spec.path;
37 fetch_local = spec: spec.path;
@@ -40,11 +48,21 @@ let
40 # Various helpers
48 # Various helpers
51+ # https://github.com/NixOS/nixpkgs/pull/83241/files#diff-c6f540a4f3bfa4b0e8b6bafd4cd54e8bR695
54+ concatMapStrings (s: if builtins.isList s then "-" else s)
56+ builtins.split "[^[:alnum:]+._?=-]+"
57+ ((x: builtins.elemAt (builtins.match "\\.*(.*)" x) 0) name)
43 # The set of packages used when specs are fetched using non-builtins.
61 # The set of packages used when specs are fetched using non-builtins.
44− mkPkgs = sources:
62+ mkPkgs = sources: system:
46 sourcesNixpkgs =
64 sourcesNixpkgs =
47− import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) {};
65+ import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) { inherit system; };
48 hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath;
66 hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath;
49 hasThisAsNixpkgsPath = <nixpkgs> == ./.;
67 hasThisAsNixpkgsPath = <nixpkgs> == ./.;
@@ -64,9 +82,9 @@ let
65 if ! builtins.hasAttr "type" spec then
83 if ! builtins.hasAttr "type" spec then
66 abort "ERROR: niv spec ${name} does not have a 'type' attribute"
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 else if spec.type == "tarball" then fetch_tarball pkgs name spec
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 else if spec.type == "local" then fetch_local spec
88 else if spec.type == "local" then fetch_local spec
71 else if spec.type == "builtin-tarball" then fetch_builtin-tarball name
89 else if spec.type == "builtin-tarball" then fetch_builtin-tarball name
72 else if spec.type == "builtin-url" then fetch_builtin-url name
90 else if spec.type == "builtin-url" then fetch_builtin-url name
@@ -80,7 +98,10 @@ let
80 saneName = stringAsChars (c: if isNull (builtins.match "[a-zA-Z0-9]" c) then "_" else c) name;
98 saneName = stringAsChars (c: if isNull (builtins.match "[a-zA-Z0-9]" c) then "_" else c) name;
81 ersatz = builtins.getEnv "NIV_OVERRIDE_${saneName}";
99 ersatz = builtins.getEnv "NIV_OVERRIDE_${saneName}";
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
104+ if builtins.substring 0 1 ersatz == "/" then /. + ersatz else /. + builtins.getEnv "PWD" + "/${ersatz}";
85 # Ports of functions for older nix versions
106 # Ports of functions for older nix versions
@@ -98,25 +119,29 @@ let
99 # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269
120 # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269
100 stringAsChars = f: s: concatStrings (map f (stringToCharacters s));
121 stringAsChars = f: s: concatStrings (map f (stringToCharacters s));
122+ concatMapStrings = f: list: concatStrings (map f list);
101 concatStrings = builtins.concatStringsSep "";
123 concatStrings = builtins.concatStringsSep "";
125+ # https://github.com/NixOS/nixpkgs/blob/8a9f58a375c401b96da862d969f66429def1d118/lib/attrsets.nix#L331
126+ optionalAttrs = cond: as: if cond then as else {};
103 # fetchTarball version that is compatible between all the versions of Nix
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:
106 inherit (builtins) lessThan nixVersion fetchTarball;
131 inherit (builtins) lessThan nixVersion fetchTarball;
108 if lessThan nixVersion "1.12" then
133 if lessThan nixVersion "1.12" then
109− fetchTarball { inherit name url; }
134+ fetchTarball ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; }))
111 fetchTarball attrs;
136 fetchTarball attrs;
113 # fetchurl version that is compatible between all the versions of Nix
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:
116 inherit (builtins) lessThan nixVersion fetchurl;
141 inherit (builtins) lessThan nixVersion fetchurl;
118 if lessThan nixVersion "1.12" then
143 if lessThan nixVersion "1.12" then
119− fetchurl { inherit url; }
144+ fetchurl ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; }))
121 fetchurl attrs;
146 fetchurl attrs;
@@ -135,7 +160,8 @@ let
135 mkConfig =
160 mkConfig =
136 { sourcesFile ? if builtins.pathExists ./sources.json then ./sources.json else null
161 { sourcesFile ? if builtins.pathExists ./sources.json then ./sources.json else null
137 , sources ? if isNull sourcesFile then {} else builtins.fromJSON (builtins.readFile sourcesFile)
162 , sources ? if isNull sourcesFile then {} else builtins.fromJSON (builtins.readFile sourcesFile)
163+ , system ? builtins.currentSystem
138− , pkgs ? mkPkgs sources
164+ , pkgs ? mkPkgs sources system
140 # The sources, i.e. the attribute set of spec name to spec
166 # The sources, i.e. the attribute set of spec name to spec
141 inherit sources;
167 inherit sources;