diff --git a/build.nix b/build.nix index 3dc998e..1149f75 100644 --- a/build.nix +++ b/build.nix @@ -1,6 +1,8 @@ src: -{ cargoBuild ? "cargo build --frozen --release -j $NIX_BUILD_CORES" -, cargoTest ? "cargo test --release" +{ #| What command to run during the build phase + cargoBuild ? "cargo build --frozen --release -j $NIX_BUILD_CORES" +, #| What command to run during the test phase + cargoTest ? "cargo test --release" , doCheck ? true , name ? null , rustc ? rustPackages @@ -9,7 +11,8 @@ src: , buildInputs ? [] , nativeBuildInputs ? [] , builtDependencies ? [] -, cargolock ? null +, cargolockPath ? null +, cargotomlPath ? null , rustPackages , stdenv , lib @@ -27,10 +30,23 @@ with { libb = import ./lib.nix { inherit lib writeText runCommand remarshal; }; }; +with + { cargolock = + if isNull cargolockPath then + libb.readTOML "${src}/Cargo.lock" + else + libb.readTOML cargolockPath; + cargotoml = + if isNull cargotomlPath then + libb.readTOML "${src}/Cargo.toml" + else + libb.readTOML cargotomlPath; + }; + with rec { drv = stdenv.mkDerivation - { inherit src doCheck nativeBuildInputs; + { inherit src doCheck nativeBuildInputs cargolockPath cargotomlPath; # The list of paths to Cargo.tomls. If this is a workspace, the paths # are the members. Otherwise, there is a single path, ".". @@ -81,6 +97,26 @@ with rec '' runHook preConfigure + if [ -n "$cargolockPath" ] + then + echo "Setting Cargo.lock" + if [ -f "Cargo.lock" ] + then + echo "WARNING: replacing existing Cargo.lock" + fi + cp "$cargolockPath" Cargo.lock + fi + + if [ -n "$cargotomlPath" ] + then + echo "Setting Cargo.toml" + if [ -f "Cargo.toml" ] + then + echo "WARNING: replacing existing Cargo.toml" + fi + cp "$cargotomlPath" Cargo.toml + fi + mkdir -p target cat ${writeText "deps" (builtins.toJSON builtDependencies)} |\ @@ -173,21 +209,13 @@ with rec # pretty fast so this is not too bad. In the future we'll want to pre-build # the crates and give cargo a pre-populated ./target directory. # TODO: this should most likely take more than one packageName - mkSnapshotForest = packageName: cargolock: + mkSnapshotForest = packageName: symlinkJoin { name = "crates-io"; paths = map (v: unpackCrate v.name v.version v.sha256) (libb.mkVersions packageName cargolock); }; - cargolock = libb.readTOML "${src}/Cargo.lock"; - - #cargolockFor = name: version: - - - # The top-level Cargo.toml - cargotoml = libb.readTOML "${src}/Cargo.toml"; - # All the Cargo.tomls, including the top-level one cargotomls = with rec @@ -202,13 +230,12 @@ with rec crateNames = builtins.filter (pname: ! isNull pname) ( map (ctoml: ctoml.package.name or null) cargotomls); - cargoconfig = writeText "cargo-config" - '' - [source.crates-io] - replace-with = 'nix-sources' - - [source.nix-sources] - directory = '${mkSnapshotForest (lib.head crateNames) cargolock}' - ''; + cargoconfig = libb.writeTOML + { source = + { crates-io = { replace-with = "nix-sources"; } ; + nix-sources = + { directory = mkSnapshotForest (lib.head crateNames) ; }; + }; + }; }; if isNull override then drv else drv.overrideAttrs override diff --git a/default.nix b/default.nix index 31231be..b0ad766 100644 --- a/default.nix +++ b/default.nix @@ -60,6 +60,8 @@ with doCheck = false; }; + #cargo = buildPackage (lib.cleanSource ../cargo) {}; + ripgrep-all = buildPackage sources.ripgrep-all {}; rustfmt = buildPackage sources.rustfmt {}; @@ -92,10 +94,6 @@ with srcc = runCommand "simple-dep" {} '' - mkdir -p $out - cp ${libb.writeTOML randCargoToml} $out/Cargo.toml - cp ${libb.writeTOML rcl} $out/Cargo.lock - mkdir -p $out/src touch $out/src/main.rs ''; @@ -104,23 +102,8 @@ with rand = buildPackage srcc { cargoBuild = "cargo build --release --frozen -p rand:0.7.0-pre.1 -j $NIX_BUILD_CORES"; doCheck = false; - override = _oldAttrs: - { preConfigure = - '' - cat Cargo.toml - cp ${libb.writeTOML randCargoToml} Cargo.toml - cargo --version - exit 1 - - echo - - echo - cat Cargo.toml - cp ${libb.writeTOML rcl} Cargo.lock - ''; - preBuild = "echo $PWD"; - }; - + cargotomlPath = libb.writeTOML randCargoToml; + cargolockPath = libb.writeTOML rcl; }; };