Skip to content

Commit 825a993

Browse files
committed
Hoist all utility functions from lib and builtins
1 parent 7d10573 commit 825a993

File tree

3 files changed

+47
-55
lines changed

3 files changed

+47
-55
lines changed

flake.nix

+12-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
};
1111

1212
outputs = { self, nixpkgs, flake-utils }: let
13-
inherit (nixpkgs) lib;
13+
inherit (nixpkgs.lib)
14+
elem filter filterAttrs head mapAttrs' mapAttrsToList optionalAttrs replaceStrings;
1415

1516
overlay = import ./.;
1617

@@ -58,15 +59,15 @@
5859
null;
5960

6061
result =
61-
lib.mapAttrs' (version: comps: {
62-
name = "rust-${lib.replaceStrings ["."] ["-"] version}";
62+
mapAttrs' (version: comps: {
63+
name = "rust-${replaceStrings ["."] ["-"] version}";
6364
value = defaultPkg comps;
6465
}) pkgs.rust-bin.stable //
65-
lib.mapAttrs' (version: comps: {
66+
mapAttrs' (version: comps: {
6667
name = "rust-nightly-${version}";
6768
value = defaultPkg comps;
6869
}) pkgs.rust-bin.nightly //
69-
lib.mapAttrs' (version: comps: {
70+
mapAttrs' (version: comps: {
7071
name = "rust-beta-${version}";
7172
value = defaultPkg comps;
7273
}) pkgs.rust-bin.beta //
@@ -75,7 +76,7 @@
7576
rust-nightly = result.rust-nightly-latest;
7677
rust-beta = result.rust-beta-latest;
7778
};
78-
in lib.filterAttrs (name: drv: drv != null) result;
79+
in filterAttrs (name: drv: drv != null) result;
7980

8081
checks = let
8182
inherit (pkgs) rust-bin rustChannelOf;
@@ -88,7 +89,7 @@
8889
message = "`${lhs}` != `${rhs}`";
8990
};
9091
assertUrl = drv: url: let
91-
srcUrl = lib.head drv.src.urls;
92+
srcUrl = head drv.src.urls;
9293
in assertEq srcUrl url;
9394

9495
assertions = {
@@ -97,7 +98,7 @@
9798
url-kind-beta = assertUrl beta."2021-01-01".rustc "https://static.rust-lang.org/dist/2021-01-01/rustc-beta-${rustTarget}.tar.xz";
9899

99100
# Check only tier 1 targets.
100-
} // lib.optionalAttrs (lib.elem system [ "aarch64-linux" "x86_64-linux" ]) {
101+
} // optionalAttrs (elem system [ "aarch64-linux" "x86_64-linux" ]) {
101102

102103
name-stable = assertEq stable."1.48.0".rustc.name "rustc-1.48.0";
103104
name-beta = assertEq beta."2021-01-01".rustc.name "rustc-1.50.0-beta.2-2021-01-01";
@@ -165,13 +166,13 @@
165166
});
166167
};
167168

168-
checkDrvs = lib.optionalAttrs (lib.elem system [ "aarch64-linux" "x86_64-linux" ]) {
169+
checkDrvs = optionalAttrs (elem system [ "aarch64-linux" "x86_64-linux" ]) {
169170
latest-nightly-default = rust-bin.selectLatestNightlyWith (toolchain: toolchain.default);
170171
};
171172

172173
failedAssertions =
173-
lib.filter (msg: msg != null) (
174-
lib.mapAttrsToList
174+
filter (msg: msg != null) (
175+
mapAttrsToList
175176
(name: { assertion, message }: if assertion
176177
then null
177178
else "Assertion `${name}` failed: ${message}\n")

manifest.nix

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
final: prev:
2-
with (prev.lib);
3-
with builtins;
42
let
3+
inherit (builtins) match;
4+
5+
inherit (final.lib)
6+
attrNames concatMap elemAt filter hasAttr mapAttrs mapAttrs' removeSuffix;
7+
58
targets = import ./manifests/targets.nix // { _ = "*"; };
69
renamesList = import ./manifests/renames.nix;
710
profilesList = import ./manifests/profiles.nix;
@@ -52,7 +55,7 @@ let
5255
}@manifest: rec {
5356

5457
# Version used for derivation.
55-
version = if builtins.match ".*(nightly|beta).*" v != null
58+
version = if match ".*(nightly|beta).*" v != null
5659
then "${v}-${d}" # 1.51.0-nightly-2021-01-01, 1.52.0-beta.2-2021-03-27
5760
else v; # 1.51.0
5861

rust-overlay.nix

+29-41
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
self: super:
66

77
let
8+
inherit (builtins) compareVersions fromTOML match readFile tryEval;
9+
10+
inherit (self.lib)
11+
any attrNames attrValues concatMapStrings concatStringsSep elem elemAt filter flatten foldl'
12+
hasPrefix head intersectLists isString length makeLibraryPath makeOverridable mapAttrs
13+
optional optionalString remove replaceStrings substring subtractLists trace unique;
814

915
# FIXME: https://github.com/NixOS/nixpkgs/pull/146274
1016
toRustTarget = platform:
@@ -16,8 +22,6 @@ let
1622
# Manifest selector.
1723
selectManifest = { channel, date ? null }: let
1824
inherit (self.rust-bin) manifests;
19-
inherit (builtins) match elemAt compareVersions attrNames foldl' filter;
20-
inherit (self.lib) hasPrefix;
2125

2226
assertWith = cond: msg: body: if cond then body else throw msg;
2327

@@ -82,7 +86,7 @@ let
8286
if toolchain._profiles != {} then
8387
toolchain._profiles.${profile'} or (throw ''
8488
Rust ${toolchain._version} doesn't have profile `${profile'}`.
85-
Available profiles are: ${self.lib.concatStringsSep ", " (builtins.attrNames toolchain._profiles)}
89+
Available profiles are: ${concatStringsSep ", " (attrNames toolchain._profiles)}
8690
'')
8791
# Fallback to package `rust` when profiles are not supported and not specified.
8892
else if profile == null then
@@ -96,7 +100,6 @@ let
96100

97101
# Same as `fromRustupToolchain` but read from a `rust-toolchain` file (legacy one-line string or in TOML).
98102
fromRustupToolchainFile = path: let
99-
inherit (builtins) readFile match fromTOML head;
100103
content = readFile path;
101104
legacy = match "([^\r\n]+)\r?\n?" content;
102105
in if legacy != null
@@ -109,26 +112,25 @@ let
109112
srcInfo = pkg.target.${toRustTarget stdenv.targetPlatform} or pkg.target."*";
110113
components = srcInfo.components or [];
111114
componentNamesList =
112-
builtins.map (pkg: pkg.pkg) (builtins.filter (pkg: (pkg.target != "*")) components);
115+
map (pkg: pkg.pkg) (filter (pkg: (pkg.target != "*")) components);
113116
in
114117
componentNamesList;
115118

116119
getExtensions = pkgs: pkgname: stdenv:
117120
let
118-
inherit (super.lib) unique;
119121
pkg = pkgs.${pkgname};
120122
rustTarget = toRustTarget stdenv.targetPlatform;
121123
srcInfo = pkg.target.${rustTarget} or pkg.target."*" or (throw "${pkgname} is no available");
122124
extensions = srcInfo.extensions or [];
123-
extensionNamesList = unique (builtins.map (pkg: pkg.pkg) extensions);
125+
extensionNamesList = unique (map (pkg: pkg.pkg) extensions);
124126
in
125127
extensionNamesList;
126128

127129
hasTarget = pkgs: pkgname: target:
128130
pkgs ? ${pkgname}.target.${target};
129131

130132
getTuples = pkgs: name: targets:
131-
builtins.map (target: { inherit name target; }) (builtins.filter (target: hasTarget pkgs name target) targets);
133+
map (target: { inherit name target; }) (filter (target: hasTarget pkgs name target) targets);
132134

133135
# In the manifest, a package might have different components which are bundled with it, as opposed as the extensions which can be added.
134136
# By default, a package will include the components for the same architecture, and offers them as extensions for other architectures.
@@ -137,12 +139,10 @@ let
137139
# The list contains the package for the pkgTargets as well as the packages for components for all compTargets
138140
getTargetPkgTuples = pkgs: pkgname: pkgTargets: compTargets: stdenv:
139141
let
140-
inherit (builtins) elem;
141-
inherit (super.lib) intersectLists;
142142
components = getComponentsWithFixedPlatform pkgs pkgname stdenv;
143143
extensions = getExtensions pkgs pkgname stdenv;
144144
compExtIntersect = intersectLists components extensions;
145-
tuples = (getTuples pkgs pkgname pkgTargets) ++ (builtins.map (name: getTuples pkgs name compTargets) compExtIntersect);
145+
tuples = (getTuples pkgs pkgname pkgTargets) ++ (map (name: getTuples pkgs name compTargets) compExtIntersect);
146146
in
147147
tuples;
148148

@@ -158,8 +158,7 @@ let
158158

159159
mkComponentSrc = { url, sha256, fetchurl }:
160160
let
161-
inherit (builtins) match elemAt;
162-
url' = builtins.replaceStrings [" "] ["%20"] url; # This is required or download will fail.
161+
url' = replaceStrings [" "] ["%20"] url; # This is required or download will fail.
163162
# Filter names like `llvm-tools-1.34.2 (6c2484dc3 2019-05-13)-aarch64-unknown-linux-gnu.tar.xz`
164163
matchParenPart = match ".*/([^ /]*) [(][^)]*[)](.*)" url;
165164
name = if matchParenPart == null then "" else (elemAt matchParenPart 0) + (elemAt matchParenPart 1);
@@ -168,8 +167,6 @@ let
168167

169168
checkMissingExtensions = pkgs: pkgname: stdenv: extensions:
170169
let
171-
inherit (builtins) head;
172-
inherit (super.lib) concatStringsSep subtractLists;
173170
availableExtensions = getExtensions pkgs pkgname stdenv;
174171
missingExtensions = subtractLists availableExtensions extensions;
175172
extensionsToInstall =
@@ -182,8 +179,6 @@ let
182179

183180
getComponents = pkgs: pkgname: targets: extensions: targetExtensions: stdenv: fetchurl:
184181
let
185-
inherit (builtins) head map;
186-
inherit (super.lib) flatten remove subtractLists unique;
187182
targetExtensionsToInstall = checkMissingExtensions pkgs pkgname stdenv targetExtensions;
188183
extensionsToInstall = checkMissingExtensions pkgs pkgname stdenv extensions;
189184
hostTargets = [ "*" (toRustTarget stdenv.hostPlatform) (toRustTarget stdenv.targetPlatform) ];
@@ -211,10 +206,10 @@ let
211206
# Ourselves have offset -1. In order to make these offset -1 dependencies of downstream derivation,
212207
# they are offset 0 propagated.
213208
propagatedBuildInputs =
214-
self.lib.optional (pname == "rustc") [ self.stdenv.cc self.buildPackages.stdenv.cc ];
209+
optional (pname == "rustc") [ self.stdenv.cc self.buildPackages.stdenv.cc ];
215210
# This goes downstream packages' buildInputs.
216211
depsTargetTargetPropagated =
217-
self.lib.optional (pname == "rustc" && self.stdenv.targetPlatform.isDarwin) self.libiconv;
212+
optional (pname == "rustc" && self.stdenv.targetPlatform.isDarwin) self.libiconv;
218213

219214
installPhase = ''
220215
runHook preInstall
@@ -234,7 +229,6 @@ let
234229
# This code is inspired by patchelf/setup-hook.sh to iterate over all binaries.
235230
preFixup =
236231
let
237-
inherit (super.lib) optionalString elem;
238232
inherit (self.stdenv) hostPlatform;
239233
in
240234
optionalString hostPlatform.isLinux ''
@@ -252,12 +246,12 @@ let
252246
# Handle executables
253247
patchelf \
254248
--set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" \
255-
--set-rpath "${super.lib.makeLibraryPath [ self.zlib ]}:$out/lib" \
249+
--set-rpath "${makeLibraryPath [ self.zlib ]}:$out/lib" \
256250
"$i" || true
257251
else
258252
# Handle libraries
259253
patchelf \
260-
--set-rpath "${super.lib.makeLibraryPath [ self.zlib ]}:$out/lib" \
254+
--set-rpath "${makeLibraryPath [ self.zlib ]}:$out/lib" \
261255
"$i" || true
262256
fi
263257
done < <(find "$dir" -type f -print0)
@@ -267,7 +261,7 @@ let
267261
for f in $out/bin/*; do
268262
${optionalString hostPlatform.isLinux ''
269263
patchelf \
270-
--set-rpath "${rustc}/lib:${super.lib.makeLibraryPath [ self.zlib ]}:$out/lib" \
264+
--set-rpath "${rustc}/lib:${makeLibraryPath [ self.zlib ]}:$out/lib" \
271265
"$f" || true
272266
''}
273267
${optionalString hostPlatform.isDarwin ''
@@ -304,7 +298,7 @@ let
304298
};
305299

306300
aggregateComponents = { pname, version, components }:
307-
self.pkgs.symlinkJoin {
301+
self.symlinkJoin {
308302
name = pname + "-" + version;
309303
inherit pname version;
310304

@@ -325,7 +319,7 @@ let
325319
cp -f ${./cargo-miri-wrapper.sh} $out/bin/cargo-miri
326320
chmod +w $out/bin/cargo-miri
327321
substituteInPlace $out/bin/cargo-miri \
328-
--replace "@bash@" "${self.pkgs.bash}/bin/bash" \
322+
--replace "@bash@" "${self.bash}/bin/bash" \
329323
--replace "@cargo_miri@" "$out/bin/.cargo-miri-wrapped" \
330324
--replace "@out@" "$out"
331325
fi
@@ -351,7 +345,6 @@ let
351345
, targetExtensions
352346
}:
353347
let
354-
inherit (self.lib) flatten elem isString filter any remove concatStringsSep concatMapStrings attrNames;
355348
rustHostPlatform = toRustTarget self.stdenv.hostPlatform;
356349

357350
collectComponentTargets = allowMissing: compName: comp:
@@ -456,10 +449,6 @@ let
456449
# *Attention* If you want to install an extension like rust-src, that has no fixed architecture (arch *),
457450
# you will need to specify this extension in the extensions options or it will not be installed!
458451
toolchainFromManifest = manifest: let
459-
inherit (builtins) elemAt;
460-
inherit (super) makeOverridable;
461-
inherit (super.lib) flip mapAttrs;
462-
463452
maybeRename = name: manifest.renames.${name}.to or name;
464453

465454
# For legacy pre-aggregated package `rust`.
@@ -517,7 +506,7 @@ let
517506
inherit componentSet profileComponents;
518507
inherit (manifest) targetComponentsList;
519508
extensions = extensions;
520-
targets = self.lib.unique ([
509+
targets = unique ([
521510
(toRustTarget self.stdenv.hostPlatform) # Build script requires host std.
522511
(toRustTarget self.stdenv.targetPlatform)
523512
] ++ targets);
@@ -542,8 +531,8 @@ let
542531
# It has more components than `default` profile but less than `complete` profile.
543532
rust =
544533
let pkg = mkPackage "rust" manifest.pkg.rust; in
545-
if builtins.match ".*[.].*[.].*" != null && profiles != {}
546-
then builtins.trace ''
534+
if match ".*[.].*[.].*" != null && profiles != {}
535+
then trace ''
547536
Rust ${manifest.version}:
548537
Pre-aggregated package `rust` is not encouraged for stable channel since it contains almost all and uncertain components.
549538
Consider use `default` profile like `rust-bin.stable.latest.default` and override it with extensions you need.
@@ -558,10 +547,10 @@ let
558547
};
559548

560549
# Same as `toolchainFromManifest` but read from a manifest file.
561-
toolchainFromManifestFile = path: toolchainFromManifest (builtins.fromTOML (builtins.readFile path));
550+
toolchainFromManifestFile = path: toolchainFromManifest (fromTOML (readFile path));
562551

563552
# Override all pkgs of a toolchain set.
564-
overrideToolchain = attrs: super.lib.mapAttrs (name: pkg: pkg.override attrs);
553+
overrideToolchain = attrs: mapAttrs (name: pkg: pkg.override attrs);
565554

566555
# From a git revision of rustc.
567556
# This does the same thing as crate `rustup-toolchain-install-master`.
@@ -576,8 +565,8 @@ let
576565
# Rust target to download.
577566
target ? toRustTarget self.stdenv.targetPlatform
578567
}: let
579-
shortRev = builtins.substring 0 7 rev;
580-
components' = super.lib.mapAttrs (compName: hash: mkComponent {
568+
shortRev = substring 0 7 rev;
569+
components' = mapAttrs (compName: hash: mkComponent {
581570
pname = compName;
582571
version = shortRev;
583572
src = self.fetchurl {
@@ -592,7 +581,7 @@ let
592581
aggregateComponents {
593582
inherit pname;
594583
version = shortRev;
595-
components = builtins.attrValues components';
584+
components = attrValues components';
596585
};
597586

598587
# Select latest nightly toolchain which makes selected profile builds.
@@ -601,7 +590,6 @@ let
601590
# `selectLatestNightlyWith (toolchain: toolchain.default.override { extensions = "llvm-tools-preview"; })`
602591
selectLatestNightlyWith = selector:
603592
let
604-
inherit (builtins) attrNames removeAttrs elemAt length trace tryEval;
605593
nightlyDates = attrNames (removeAttrs self.rust-bin.nightly [ "latest" ]);
606594
dateLength = length nightlyDates;
607595
go = idx:
@@ -634,7 +622,7 @@ in {
634622
#
635623
# For a specific date of nightly:
636624
# rust-bin.nightly."2020-01-01".default
637-
rust-bin = with builtins;
625+
rust-bin =
638626
(super.rust-bin or {}) //
639627
mapAttrs (channel: mapAttrs (version: toolchainFromManifest)) super.rust-bin.manifests //
640628
{
@@ -657,7 +645,7 @@ in {
657645
Select a toolchain from `rust-bin` or using `rustChannelOf` instead.
658646
See also README at https://github.com/oxalica/rust-overlay
659647
'';
660-
fromManifestFile = manifestFilePath: { stdenv, fetchurl, patchelf }@deps: builtins.trace ''
648+
fromManifestFile = manifestFilePath: { stdenv, fetchurl, patchelf }@deps: trace ''
661649
`fromManifestFile` is deprecated.
662650
Select a toolchain from `rust-bin` or using `rustChannelOf` instead.
663651
See also README at https://github.com/oxalica/rust-overlay

0 commit comments

Comments
 (0)