-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathcreateNixpkgsDrv.nix
More file actions
176 lines (159 loc) · 5.41 KB
/
createNixpkgsDrv.nix
File metadata and controls
176 lines (159 loc) · 5.41 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Creates a nixpkgs-compatible nix expression that uses `buildRustPackage`.
common:
common.pkgs.writeTextFile {
name = "${common.cargoPkg.name}.nix";
text = let
inherit (common) root cargoPkg pkgs buildInputs nativeBuildInputs desktopFileMetadata;
inherit
(common.internal.lib)
any
map
hasAttr
baseNameOf
concatStringsSep
filter
length
attrNames
attrValues
split
isList
isString
stringLength
elemAt
optional
optionalString
cargoLicenseToNixpkgs
concatMapStringsSep
mapAttrsToList
getName
init
filterAttrs
unique
splitString
drop
hasPrefix
removePrefix
strings
;
inherit (strings) sanitizeDerivationName;
has = i: any (oi: i == oi);
clang = ["clang-wrapper" "clang"];
gcc = ["gcc-wrapper" "gcc"];
filterUnwanted = filter (n: !(has n (clang ++ gcc ++ ["pkg-config-wrapper" "binutils-wrapper"])));
mapToName = map getName;
concatForInput = i: concatStringsSep "" (map (p: "\n ${p},") i);
bi = filterUnwanted ((mapToName buildInputs) ++ (mapToName common.runtimeLibs));
nbi =
(filterUnwanted (mapToName nativeBuildInputs))
++ (optional common.internal.mkRuntimeLibsOv "makeWrapper")
++ (optional common.internal.mkDesktopFile "copyDesktopItems");
runtimeLibs = "\${lib.makeLibraryPath ([ ${concatStringsSep " " (mapToName common.runtimeLibs)} ])}";
stdenv =
if any (n: has n clang) (mapToName nativeBuildInputs)
then "clangStdenv"
else null;
putIfStdenv = optionalString (stdenv != null);
runtimeLibsScript = concatStringsSep "\n" (
map
(line: " ${line}")
(init (
filter
(list:
if isList list
then (length list) > 0
else true)
(split "\n" (common.internal.mkRuntimeLibsScript runtimeLibs))
))
);
desktopItemAttrs = let
desktopItem = common.internal.mkDesktopItemConfig cargoPkg.name;
filtered =
filterAttrs
(_: v: !(hasPrefix "/nix/store" v) && (toString v) != "")
desktopItem;
attrsWithIcon =
if !(hasAttr "icon" filtered) && (hasAttr "icon" common.desktopFileMetadata)
then filtered // {icon = "\${src}/${removePrefix "./" common.desktopFileMetadata.icon}";}
else filtered;
attrs = mapAttrsToList (n: v: " ${n} = \"${v}\";") attrsWithIcon;
in
concatStringsSep "\n" attrs;
desktopItems = "\n desktopItems = [ (makeDesktopItem {\n${desktopItemAttrs}\n }) ];";
desktopLink = "\n desktopItems = [ (pkgs.runCommand \"${cargoPkg.name}-desktopFileLink\" { } ''\n mkdir -p $out/share/applications\n ln -sf \${src}/${desktopFileMetadata} $out/share/applications\n '') ];";
isGitHub = builtins.pathExists (root + "/.github");
isGitLab = builtins.pathExists (root + "/.gitlab");
mkForgeFetch = name: rec {
fetcher = "fetchFrom${name}";
fetchCode = let
version = "v\${version}";
in ''
src = ${fetcher} {
owner = "<enter owner>";
repo = "${cargoPkg.name}";
rev = "${version}";
sha256 = lib.fakeHash;
};'';
};
githubFetcher = mkForgeFetch "GitHub";
gitlabFetcher = mkForgeFetch "GitLab";
fetcher =
if isGitLab
then gitlabFetcher
else if isGitHub
then githubFetcher
else githubFetcher;
envToString = value: let
val = toString value;
in
if hasPrefix "/nix/store" value
then let
pathSegments = filter (v: (stringLength v) > 0) (splitString "/" val);
hashName = elemAt pathSegments 2;
nameSegments = drop 1 (splitString "-" hashName);
name = concatStringsSep "-" nameSegments;
drvName = getName (sanitizeDerivationName name);
relPathSegments = drop 3 pathSegments;
relPath = concatStringsSep "/" relPathSegments;
in
"\${${drvName}}/" + relPath
else val;
in ''
{ lib,
rustPlatform,${putIfStdenv "\n ${stdenv},"}
${fetcher.fetcher},${concatForInput (unique (bi ++ nbi))}
}:
rustPlatform.buildRustPackage rec {
pname = "${cargoPkg.name}";
version = "${cargoPkg.version}";${putIfStdenv "\n\n stdenv = ${stdenv};"}
# Change to use whatever source you want
${concatMapStringsSep "\n" (line: " ${line}") (splitString "\n" fetcher.fetchCode)}
cargoSha256 = lib.fakeHash;${
optionalString
((length (attrNames common.env)) > 0)
"\n\n${concatStringsSep "\n" (mapAttrsToList (n: v: " ${n} = \"${envToString v}\";") common.env)}"
}
buildInputs = [ ${concatStringsSep " " bi} ];
nativeBuildInputs = [ ${concatStringsSep " " nbi} ];${
optionalString
common.internal.mkRuntimeLibsOv
"\n\n postFixup = ''\n${runtimeLibsScript}\n '';"
}${
optionalString
common.internal.mkDesktopFile
(
if isString desktopFileMetadata
then desktopLink
else desktopItems
)
}
cargoBuildFlags = [ "--package" "${cargoPkg.name}" ];
cargoTestFlags = cargoBuildFlags;
meta = with lib; {
description = "${common.meta.description or "<enter description>"}";
homepage = "${common.meta.homepage or "<enter homepage>"}";
license = licenses.${cargoLicenseToNixpkgs (cargoPkg.license or "unfree")};
maintainers = with maintainers; [ ];
};
}
'';
}