use let .. in instead of with blocks

A personal stylistic preference
This commit is contained in:
zimbatm 2019-11-19 19:28:02 +00:00
parent 6def798b78
commit 4f1f9a0a94
No known key found for this signature in database
GPG key ID: 71BAF6D40C1D63D7
3 changed files with 55 additions and 62 deletions

View file

@ -50,14 +50,12 @@
, zstd , zstd
}: }:
with let
{ builtinz = builtinz =
builtins // builtins //
import ./builtins import ./builtins
{ inherit lib writeText remarshal runCommand ; }; { inherit lib writeText remarshal runCommand; };
};
let
drv = stdenv.mkDerivation ( drv = stdenv.mkDerivation (
{ name = "${pname}-${version}"; { name = "${pname}-${version}";
inherit inherit
@ -221,12 +219,12 @@ let
# gzipped tar; we simply unpack it and introduce a ".cargo-checksum.json" # gzipped tar; we simply unpack it and introduce a ".cargo-checksum.json"
# file that cargo itself uses to double check the sha256 # file that cargo itself uses to double check the sha256
unpackCrate = name: version: sha256: unpackCrate = name: version: sha256:
with let
{ crate = builtins.fetchurl crate = builtins.fetchurl
{ url = "https://crates.io/api/v1/crates/${name}/${version}/download"; { url = "https://crates.io/api/v1/crates/${name}/${version}/download";
inherit sha256; inherit sha256;
}; };
}; in
runCommand "unpack-${name}-${version}" {} runCommand "unpack-${name}-${version}" {}
'' ''
mkdir -p $out mkdir -p $out

45
lib.nix
View file

@ -1,10 +1,10 @@
{ lib, writeText, runCommand, remarshal }: { lib, writeText, runCommand, remarshal }:
with let
{ builtinz = builtinz =
builtins // builtins //
import ./builtins import ./builtins
{ inherit lib writeText remarshal runCommand ; }; { inherit lib writeText remarshal runCommand ; };
}; in
rec rec
{ {
# The list of _all_ crates (incl. transitive dependencies) with name, # The list of _all_ crates (incl. transitive dependencies) with name,
@ -16,7 +16,7 @@ rec
# TODO: this should nub by <pkg-name>-<pkg-version> # TODO: this should nub by <pkg-name>-<pkg-version>
(lib.concatMap (x: (lib.concatMap (x:
with { mdk = mkMetadataKey x.name x.version; }; let mdk = mkMetadataKey x.name x.version; in
( lib.optional (builtins.hasAttr mdk cargolock.metadata) ( lib.optional (builtins.hasAttr mdk cargolock.metadata)
{ inherit (x) version name; { inherit (x) version name;
sha256 = cargolock.metadata.${mkMetadataKey x.name x.version}; sha256 = cargolock.metadata.${mkMetadataKey x.name x.version};
@ -30,19 +30,19 @@ rec
# Turns "lib-name lib-ver (registry+...)" to [ { name = "lib-name", etc } ] # Turns "lib-name lib-ver (registry+...)" to [ { name = "lib-name", etc } ]
# iff the package is present in the Cargo.lock (otherwise returns []) # iff the package is present in the Cargo.lock (otherwise returns [])
parseDependency = cargolock: str: parseDependency = cargolock: str:
with rec let
{ components = lib.splitString " " str; components = lib.splitString " " str;
name = lib.elemAt components 0; name = lib.elemAt components 0;
version = lib.elemAt components 1; version = lib.elemAt components 1;
mdk = mkMetadataKey name version; mdk = mkMetadataKey name version;
}; in
( lib.optional (builtins.hasAttr mdk cargolock.metadata) lib.optional (builtins.hasAttr mdk cargolock.metadata)
( (
with let
{ sha256 = cargolock.metadata.${mkMetadataKey name version}; sha256 = cargolock.metadata.${mkMetadataKey name version};
}; in
{ inherit name version sha256; } { inherit name version sha256; }
)); );
# crafts the key used to look up the sha256 in the cargo lock; no # crafts the key used to look up the sha256 in the cargo lock; no
@ -54,15 +54,15 @@ rec
# package (and the package itself). This package is Nix-generated and thus # package (and the package itself). This package is Nix-generated and thus
# only the transitive dependencies contribute to the package's derivation. # only the transitive dependencies contribute to the package's derivation.
cargolockFor = cargolock: name: version: cargolockFor = cargolock: name: version:
with rec let
{ tdeps = transitiveDeps cargolock name version; tdeps = transitiveDeps cargolock name version;
tdepPrefix = dep: "checksum ${dep.name} ${dep.version}"; tdepPrefix = dep: "checksum ${dep.name} ${dep.version}";
isTransitiveDep = p: lib.any isTransitiveDep = p: lib.any
(d: d.package.name == p.name && d.package.version == p.version) (d: d.package.name == p.name && d.package.version == p.version)
tdeps; tdeps;
isTransitiveDepChecksumKey = k: isTransitiveDepChecksumKey = k:
lib.any (tdep: lib.hasPrefix (tdepPrefix tdep.package) k) tdeps; lib.any (tdep: lib.hasPrefix (tdepPrefix tdep.package) k) tdeps;
}; in
cargolock // cargolock //
{ package = lib.filter (p: { package = lib.filter (p:
(p.name == name && p.version == version) || (p.name == name && p.version == version) ||
@ -144,22 +144,21 @@ rec
cargolock.package); cargolock.package);
directDependencies = cargolock: name: version: directDependencies = cargolock: name: version:
with rec let
{ packages = mkPackages cargolock; packages = mkPackages cargolock;
package = packages.${name}.${version}; package = packages.${name}.${version};
} ; in
lib.optionals (builtins.hasAttr "dependencies" package) lib.optionals (builtins.hasAttr "dependencies" package)
(map parseDependency' package.dependencies); (map parseDependency' package.dependencies);
transitiveDeps = cargolock: name: version: transitiveDeps = cargolock: name: version:
with let
{ wrap = p: wrap = p:
{ key = "${p.name}-${p.version}"; { key = "${p.name}-${p.version}";
package = p; package = p;
}; };
packages = mkPackages cargolock; packages = mkPackages cargolock;
}; in
builtins.genericClosure builtins.genericClosure
{ startSet = [ (wrap packages.${name}.${version}) ]; { startSet = [ (wrap packages.${name}.${version}) ];
operator = p: map (dep: wrap (packages.${dep.name}.${dep.version})) ( operator = p: map (dep: wrap (packages.${dep.name}.${dep.version})) (
@ -169,6 +168,6 @@ rec
# turns "<package> <version> ..." into { name = <package>, version = <version>; } # turns "<package> <version> ..." into { name = <package>, version = <version>; }
parseDependency' = str: parseDependency' = str:
with { components = lib.splitString " " str; }; let components = lib.splitString " " str; in
{ name = lib.elemAt components 0; version = lib.elemAt components 1; }; { name = lib.elemAt components 0; version = lib.elemAt components 1; };
} }

View file

@ -1,15 +1,11 @@
{ system ? builtins.currentSystem }: { system ? builtins.currentSystem }:
with rec let
{ sources = import ./nix/sources.nix ; sources = import ./nix/sources.nix ;
pkgs = import sources.nixpkgs { inherit system ; }; pkgs = import sources.nixpkgs { inherit system ; };
naersk = pkgs.callPackage ./default.nix naersk = pkgs.callPackage ./default.nix
{ inherit (pkgs.rustPackages) cargo rustc; { inherit (pkgs.rustPackages) cargo rustc; };
}; builtinz = builtins // pkgs.callPackage ./builtins {};
}; in
with
{ builtinz = builtins // pkgs.callPackage ./builtins {}; };
rec rec
{ # error[E0554]: `#![feature]` may not be used on the stable release channel { # error[E0554]: `#![feature]` may not be used on the stable release channel
# rustfmt = naersk.buildPackage sources.rustfmt { doDocFail = false; }; # rustfmt = naersk.buildPackage sources.rustfmt { doDocFail = false; };