Skip to content

Commit 3d30b8c

Browse files
committed
merge quality PR's
NixOS#18 NixOS#44
1 parent 35bb57c commit 3d30b8c

File tree

1 file changed

+62
-5
lines changed

1 file changed

+62
-5
lines changed

default.nix

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,42 @@ let
1313

1414
lockFile = builtins.fromJSON (builtins.readFile lockFilePath);
1515

16+
# Using custom fetchurl function here so that we can specify outputHashMode.
17+
# The hash we get from the lock file is using recursive ingestion even though
18+
# it’s not unpacked. So builtins.fetchurl and import <nix/fetchurl.nix> are
19+
# insufficient.
20+
# Note that this will be a derivation and not a path as fetchTarball is,
21+
# causing the hash of this input to be different on flake and non-flake evaluation.
22+
fetchurl = { url, sha256 }:
23+
derivation {
24+
builder = "builtin:fetchurl";
25+
26+
name = "source";
27+
inherit url;
28+
29+
outputHash = sha256;
30+
outputHashAlgo = "sha256";
31+
outputHashMode = "recursive";
32+
executable = false;
33+
unpack = false;
34+
35+
system = "builtin";
36+
37+
# No need to double the amount of network traffic
38+
preferLocalBuild = true;
39+
40+
impureEnvVars = [
41+
# We borrow these environment variables from the caller to allow
42+
# easy proxy configuration. This is impure, but a fixed-output
43+
# derivation like fetchurl is allowed to do so since its result is
44+
# by definition pure.
45+
"http_proxy" "https_proxy" "ftp_proxy" "all_proxy" "no_proxy"
46+
];
47+
48+
# To make "nix-prefetch-url" work.
49+
urls = [ url ];
50+
};
51+
1652
fetchTree =
1753
info:
1854
if info.type == "github" then
@@ -44,7 +80,13 @@ let
4480
} else {
4581
})
4682
else if info.type == "path" then
47-
{ outPath = builtins.path { path = info.path; };
83+
84+
{ outPath = (builtins.path {
85+
path = if builtins.substring 0 1 info.path != "/"
86+
then src + ("/" + info.path)
87+
else info.path; }
88+
// (if info ? narHash then { sha256 = info.narHash; } else {})
89+
);
4890
narHash = info.narHash;
4991
}
5092
else if info.type == "tarball" then
@@ -53,6 +95,7 @@ let
5395
({ inherit (info) url; }
5496
// (if info ? narHash then { sha256 = info.narHash; } else {})
5597
);
98+
narHash = info.narHash;
5699
}
57100
else if info.type == "gitlab" then
58101
{ inherit (info) rev narHash lastModified;
@@ -63,8 +106,23 @@ let
63106
);
64107
shortRev = builtins.substring 0 7 info.rev;
65108
}
109+
else if info.type == "file" then
110+
{ outPath =
111+
if builtins.substring 0 7 info.url == "http://" || builtins.substring 0 8 info.url == "https://" then
112+
fetchurl
113+
({ inherit (info) url; }
114+
// (if info ? narHash then { sha256 = info.narHash; } else {})
115+
)
116+
else if builtins.substring 0 7 info.url == "file://" then
117+
builtins.path
118+
({ path = builtins.substring 7 (-1) info.url; }
119+
// (if info ? narHash then { sha256 = info.narHash; } else {})
120+
)
121+
else throw "can't support url scheme of flake input with url '${info.url}'";
122+
narHash = info.narHash;
123+
}
66124
else
67-
# FIXME: add Mercurial, tarball inputs.
125+
# FIXME: add Mercurial input
68126
throw "flake input has unsupported input type '${info.type}'";
69127

70128
callFlake4 = flakeSrc: locks:
@@ -167,8 +225,7 @@ let
167225

168226
outputs = flake.outputs (inputs // { self = result; });
169227

170-
result = outputs // sourceInfo // { inherit inputs; inherit outputs; inherit sourceInfo; _type = "flake"; };
171-
228+
result = outputs // sourceInfo // { inherit inputs; inherit outputs; inherit sourceInfo; };
172229
in
173230
if node.flake or true then
174231
assert builtins.isFunction flake.outputs;
@@ -190,7 +247,7 @@ let
190247
in
191248
rec {
192249
defaultNix =
193-
(builtins.removeAttrs result ["__functor"])
250+
result
194251
// (if result ? defaultPackage.${system} then { default = result.defaultPackage.${system}; } else {})
195252
// (if result ? packages.${system}.default then { default = result.packages.${system}.default; } else {});
196253

0 commit comments

Comments
 (0)