From 4f1f9a0a945f72ebc30dab74520182ac2c00f6ce Mon Sep 17 00:00:00 2001 From: zimbatm Date: Tue, 19 Nov 2019 19:28:02 +0000 Subject: [PATCH] use let .. in instead of with blocks A personal stylistic preference --- build.nix | 18 ++++++------- lib.nix | 81 +++++++++++++++++++++++++++---------------------------- test.nix | 18 +++++-------- 3 files changed, 55 insertions(+), 62 deletions(-) diff --git a/build.nix b/build.nix index 8427e51..ca8d988 100644 --- a/build.nix +++ b/build.nix @@ -50,14 +50,12 @@ , zstd }: -with - { builtinz = - builtins // - import ./builtins - { inherit lib writeText remarshal runCommand ; }; - }; - let + builtinz = + builtins // + import ./builtins + { inherit lib writeText remarshal runCommand; }; + drv = stdenv.mkDerivation ( { name = "${pname}-${version}"; inherit @@ -221,12 +219,12 @@ let # gzipped tar; we simply unpack it and introduce a ".cargo-checksum.json" # file that cargo itself uses to double check the sha256 unpackCrate = name: version: sha256: - with - { crate = builtins.fetchurl + let + crate = builtins.fetchurl { url = "https://crates.io/api/v1/crates/${name}/${version}/download"; inherit sha256; }; - }; + in runCommand "unpack-${name}-${version}" {} '' mkdir -p $out diff --git a/lib.nix b/lib.nix index f6ff234..3eb9caa 100644 --- a/lib.nix +++ b/lib.nix @@ -1,10 +1,10 @@ { lib, writeText, runCommand, remarshal }: -with - { builtinz = - builtins // - import ./builtins - { inherit lib writeText remarshal runCommand ; }; - }; +let + builtinz = + builtins // + import ./builtins + { inherit lib writeText remarshal runCommand ; }; +in rec { # The list of _all_ crates (incl. transitive dependencies) with name, @@ -16,7 +16,7 @@ rec # TODO: this should nub by - (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) { inherit (x) version name; sha256 = cargolock.metadata.${mkMetadataKey x.name x.version}; @@ -30,19 +30,19 @@ rec # Turns "lib-name lib-ver (registry+...)" to [ { name = "lib-name", etc } ] # iff the package is present in the Cargo.lock (otherwise returns []) parseDependency = cargolock: str: - with rec - { components = lib.splitString " " str; - name = lib.elemAt components 0; - version = lib.elemAt components 1; - mdk = mkMetadataKey name version; - }; - ( lib.optional (builtins.hasAttr mdk cargolock.metadata) + let + components = lib.splitString " " str; + name = lib.elemAt components 0; + version = lib.elemAt components 1; + mdk = mkMetadataKey name version; + in + lib.optional (builtins.hasAttr mdk cargolock.metadata) ( - with - { sha256 = cargolock.metadata.${mkMetadataKey name version}; - }; - { inherit name version sha256; } - )); + let + sha256 = cargolock.metadata.${mkMetadataKey name version}; + in + { inherit name version sha256; } + ); # 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 # only the transitive dependencies contribute to the package's derivation. cargolockFor = cargolock: name: version: - with rec - { tdeps = transitiveDeps cargolock name version; - tdepPrefix = dep: "checksum ${dep.name} ${dep.version}"; - isTransitiveDep = p: lib.any - (d: d.package.name == p.name && d.package.version == p.version) - tdeps; - isTransitiveDepChecksumKey = k: - lib.any (tdep: lib.hasPrefix (tdepPrefix tdep.package) k) tdeps; - }; + let + tdeps = transitiveDeps cargolock name version; + tdepPrefix = dep: "checksum ${dep.name} ${dep.version}"; + isTransitiveDep = p: lib.any + (d: d.package.name == p.name && d.package.version == p.version) + tdeps; + isTransitiveDepChecksumKey = k: + lib.any (tdep: lib.hasPrefix (tdepPrefix tdep.package) k) tdeps; + in cargolock // { package = lib.filter (p: (p.name == name && p.version == version) || @@ -144,22 +144,21 @@ rec cargolock.package); directDependencies = cargolock: name: version: - with rec - { packages = mkPackages cargolock; - package = packages.${name}.${version}; - } ; - + let + packages = mkPackages cargolock; + package = packages.${name}.${version}; + in lib.optionals (builtins.hasAttr "dependencies" package) (map parseDependency' package.dependencies); transitiveDeps = cargolock: name: version: - with - { wrap = p: - { key = "${p.name}-${p.version}"; - package = p; - }; - packages = mkPackages cargolock; - }; + let + wrap = p: + { key = "${p.name}-${p.version}"; + package = p; + }; + packages = mkPackages cargolock; + in builtins.genericClosure { startSet = [ (wrap packages.${name}.${version}) ]; operator = p: map (dep: wrap (packages.${dep.name}.${dep.version})) ( @@ -169,6 +168,6 @@ rec # turns " ..." into { name = , version = ; } parseDependency' = str: - with { components = lib.splitString " " str; }; + let components = lib.splitString " " str; in { name = lib.elemAt components 0; version = lib.elemAt components 1; }; } diff --git a/test.nix b/test.nix index 267e27c..5daddd0 100644 --- a/test.nix +++ b/test.nix @@ -1,15 +1,11 @@ { system ? builtins.currentSystem }: -with rec - { sources = import ./nix/sources.nix ; - pkgs = import sources.nixpkgs { inherit system ; }; - naersk = pkgs.callPackage ./default.nix - { inherit (pkgs.rustPackages) cargo rustc; - }; - }; - -with - { builtinz = builtins // pkgs.callPackage ./builtins {}; }; - +let + sources = import ./nix/sources.nix ; + pkgs = import sources.nixpkgs { inherit system ; }; + naersk = pkgs.callPackage ./default.nix + { inherit (pkgs.rustPackages) cargo rustc; }; + builtinz = builtins // pkgs.callPackage ./builtins {}; +in rec { # error[E0554]: `#![feature]` may not be used on the stable release channel # rustfmt = naersk.buildPackage sources.rustfmt { doDocFail = false; };