-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
94 lines (91 loc) · 2.48 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
{
description = "crates.nix";
inputs = {
crates-io-index = {
url = "github:rust-lang/crates.io-index";
flake = false;
};
};
outputs = {
self,
crates-io-index,
}: {
mkLib = {
pkgs,
crates-io ? crates-io-index,
}: let
inherit (pkgs) lib;
trimNewline = str:
if lib.hasSuffix "\n" str
then builtins.substring 0 (builtins.stringLength str - 1) str
else str;
getFilename = name: let
len = builtins.stringLength name;
in
if len > 3
then "${crates-io}/${builtins.substring 0 2 name}/${builtins.substring 2 2 name}/${name}"
else if len == 3
then "${crates-io}/${toString len}/${builtins.substring 0 1 name}/${name}"
else "${crates-io}/${toString len}/${name}";
getVersions = name:
builtins.map builtins.fromJSON (pkgs.lib.splitString "\n" (trimNewline (builtins.readFile (getFilename name))));
getLatest = name: version:
if version == "*"
then lib.lists.last (getVersions name)
else lib.lists.findFirst (v: v.vers == version) (throw "Unable to find a version for ${name} with ${version}") (getVersions name);
in rec {
fetchCrate = name: {
pname ? name,
version ? "*",
hash ? null,
}: let
crate = getLatest pname version;
v = crate.vers;
h =
if hash == null
then "sha256:${crate.cksum}"
else hash;
src = pkgs.fetchCrate {
inherit pname;
version = v;
hash = h;
unpack = false;
};
in
pkgs.stdenvNoCC.mkDerivation {
version = v;
name = "${pname}-${v}-source";
inherit src;
buildPhase = ''
cp -r ./ $out
'';
};
buildCrate = name: {
pname ? name,
version ? "*",
hash ? null,
...
} @ flags: let
crate = getLatest pname version;
v = crate.vers;
h =
if hash == null
then "sha256:${crate.cksum}"
else hash;
in
pkgs.rustPlatform.buildRustPackage (rec {
inherit pname;
version = v;
src = fetchCrate name {
inherit pname;
version = v;
hash = h;
};
cargoLock = {
lockFile = "${src}/Cargo.lock";
};
}
// (removeAttrs flags ["pname" "version" "hash"]));
};
};
}