|
1 | | -{ lib, stdenv, cacert, git, cargo, python3 }: |
2 | | -let cargo-vendor-normalise = stdenv.mkDerivation { |
3 | | - name = "cargo-vendor-normalise"; |
4 | | - src = ./cargo-vendor-normalise.py; |
5 | | - nativeBuildInputs = [ python3.pkgs.wrapPython ]; |
6 | | - dontUnpack = true; |
7 | | - installPhase = "install -D $src $out/bin/cargo-vendor-normalise"; |
8 | | - pythonPath = [ python3.pkgs.toml ]; |
9 | | - postFixup = "wrapPythonPrograms"; |
10 | | - doInstallCheck = true; |
11 | | - installCheckPhase = '' |
12 | | - # check that ../fetchcargo-default-config.toml is a fix point |
13 | | - reference=${../fetchcargo-default-config.toml} |
14 | | - < $reference $out/bin/cargo-vendor-normalise > test; |
15 | | - cmp test $reference |
16 | | - ''; |
17 | | - preferLocalBuild = true; |
18 | | -}; |
| 1 | +{ |
| 2 | + lib, |
| 3 | + stdenv, |
| 4 | + cacert, |
| 5 | + git, |
| 6 | + cargo, |
| 7 | + python3, |
| 8 | +}: |
| 9 | +let |
| 10 | + cargo-vendor-normalise = stdenv.mkDerivation { |
| 11 | + name = "cargo-vendor-normalise"; |
| 12 | + src = ./cargo-vendor-normalise.py; |
| 13 | + nativeBuildInputs = [ python3.pkgs.wrapPython ]; |
| 14 | + dontUnpack = true; |
| 15 | + installPhase = "install -D $src $out/bin/cargo-vendor-normalise"; |
| 16 | + pythonPath = [ python3.pkgs.toml ]; |
| 17 | + postFixup = "wrapPythonPrograms"; |
| 18 | + doInstallCheck = true; |
| 19 | + installCheckPhase = '' |
| 20 | + # check that ../fetchcargo-default-config.toml is a fix point |
| 21 | + reference=${../fetchcargo-default-config.toml} |
| 22 | + < $reference $out/bin/cargo-vendor-normalise > test; |
| 23 | + cmp test $reference |
| 24 | + ''; |
| 25 | + preferLocalBuild = true; |
| 26 | + }; |
19 | 27 | in |
20 | | -{ name ? "cargo-deps" |
21 | | -, src ? null |
22 | | -, srcs ? [] |
23 | | -, patches ? [] |
24 | | -, sourceRoot ? "" |
25 | | -, cargoUpdateHook ? "" |
26 | | -, nativeBuildInputs ? [] |
27 | | -, ... |
28 | | -} @ args: |
29 | | - |
30 | | -let hash_ = |
31 | | - if args ? hash then |
32 | | - { |
33 | | - outputHashAlgo = if args.hash == "" then "sha256" else null; |
34 | | - outputHash = args.hash; |
35 | | - } |
36 | | - else if args ? sha256 then { outputHashAlgo = "sha256"; outputHash = args.sha256; } |
37 | | - else throw "fetchCargoTarball requires a hash for ${name}"; |
38 | | -in stdenv.mkDerivation ({ |
39 | | - name = "${name}-vendor.tar.gz"; |
40 | | - nativeBuildInputs = [ cacert git cargo-vendor-normalise cargo ] ++ nativeBuildInputs; |
41 | | - |
42 | | - buildPhase = '' |
43 | | - runHook preBuild |
44 | | -
|
45 | | - # Ensure deterministic Cargo vendor builds |
46 | | - export SOURCE_DATE_EPOCH=1 |
47 | | -
|
48 | | - if [[ ! -f Cargo.lock ]]; then |
49 | | - echo |
50 | | - echo "ERROR: The Cargo.lock file doesn't exist" |
51 | | - echo |
52 | | - echo "Cargo.lock is needed to make sure that cargoHash/cargoSha256 doesn't change" |
53 | | - echo "when the registry is updated." |
54 | | - echo |
55 | | -
|
56 | | - exit 1 |
57 | | - fi |
58 | | -
|
59 | | - # Keep the original around for copyLockfile |
60 | | - cp Cargo.lock Cargo.lock.orig |
61 | | -
|
62 | | - export CARGO_HOME=$(mktemp -d cargo-home.XXX) |
63 | | - CARGO_CONFIG=$(mktemp cargo-config.XXXX) |
64 | | -
|
65 | | - if [[ -n "$NIX_CRATES_INDEX" ]]; then |
66 | | - cat >$CARGO_HOME/config.toml <<EOF |
67 | | - [source.crates-io] |
68 | | - replace-with = 'mirror' |
69 | | - [source.mirror] |
70 | | - registry = "$NIX_CRATES_INDEX" |
71 | | - EOF |
72 | | - fi |
73 | | -
|
74 | | - ${cargoUpdateHook} |
75 | | -
|
76 | | - # Override the `http.cainfo` option usually specified in `.cargo/config`. |
77 | | - export CARGO_HTTP_CAINFO=${cacert}/etc/ssl/certs/ca-bundle.crt |
78 | | -
|
79 | | - if grep '^source = "git' Cargo.lock; then |
80 | | - echo |
81 | | - echo "ERROR: The Cargo.lock contains git dependencies" |
82 | | - echo |
83 | | - echo "This is currently not supported in the fixed-output derivation fetcher." |
84 | | - echo "Use cargoLock.lockFile / importCargoLock instead." |
85 | | - echo |
86 | | -
|
87 | | - exit 1 |
88 | | - fi |
89 | | -
|
90 | | - cargo vendor $name --respect-source-config | cargo-vendor-normalise > $CARGO_CONFIG |
91 | | -
|
92 | | - # Create an empty vendor directory when there is no dependency to vendor |
93 | | - mkdir -p $name |
94 | | - # Add the Cargo.lock to allow hash invalidation |
95 | | - cp Cargo.lock.orig $name/Cargo.lock |
96 | | -
|
97 | | - # Packages with git dependencies generate non-default cargo configs, so |
98 | | - # always install it rather than trying to write a standard default template. |
99 | | - install -D $CARGO_CONFIG $name/.cargo/config; |
100 | | -
|
101 | | - runHook postBuild |
102 | | - ''; |
103 | | - |
104 | | - # Build a reproducible tar, per instructions at https://reproducible-builds.org/docs/archives/ |
105 | | - installPhase = '' |
106 | | - tar --owner=0 --group=0 --numeric-owner --format=gnu \ |
107 | | - --sort=name --mtime="@$SOURCE_DATE_EPOCH" \ |
108 | | - -czf $out $name |
109 | | - ''; |
110 | | - |
111 | | - inherit (hash_) outputHashAlgo outputHash; |
112 | | - |
113 | | - impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [ "NIX_CRATES_INDEX" ]; |
114 | | -} // (builtins.removeAttrs args [ |
115 | | - "name" "sha256" "cargoUpdateHook" "nativeBuildInputs" |
116 | | -])) |
| 28 | +{ |
| 29 | + pname ? null, |
| 30 | + version ? null, |
| 31 | + name ? if args ? pname && args ? version then "${pname}-${version}" else "cargo-deps", |
| 32 | + src ? null, |
| 33 | + srcs ? [ ], |
| 34 | + patches ? [ ], |
| 35 | + sourceRoot ? "", |
| 36 | + cargoUpdateHook ? "", |
| 37 | + nativeBuildInputs ? [ ], |
| 38 | + ... |
| 39 | +}@args: |
| 40 | + |
| 41 | +assert lib.assertMsg ( |
| 42 | + (args ? pname || args ? version) -> !(args ? name) |
| 43 | +) "Either specify `pname` with `version`, or specify `name` only, not a mix of both."; |
| 44 | +assert lib.assertMsg ( |
| 45 | + args ? pname == args ? version |
| 46 | +) "If `pname` is specified, `version` must be also, and vice versa."; |
| 47 | +let |
| 48 | + # args to remove from the final call to stdenv.mkDerivation, as we've already handled them |
| 49 | + removedArgs = [ |
| 50 | + "name" |
| 51 | + "pname" |
| 52 | + "version" |
| 53 | + "sha256" |
| 54 | + "cargoUpdateHook" |
| 55 | + "nativeBuildInputs" |
| 56 | + ]; |
| 57 | + |
| 58 | + hash_ = |
| 59 | + if args ? hash then |
| 60 | + { |
| 61 | + outputHashAlgo = if args.hash == "" then "sha256" else null; |
| 62 | + outputHash = args.hash; |
| 63 | + } |
| 64 | + else if args ? sha256 then |
| 65 | + { |
| 66 | + outputHashAlgo = "sha256"; |
| 67 | + outputHash = args.sha256; |
| 68 | + } |
| 69 | + else |
| 70 | + throw "fetchCargoTarball requires a hash for ${name}"; |
| 71 | +in |
| 72 | +stdenv.mkDerivation ( |
| 73 | + { |
| 74 | + name = "${name}-vendor.tar.gz"; |
| 75 | + nativeBuildInputs = [ |
| 76 | + cacert |
| 77 | + git |
| 78 | + cargo-vendor-normalise |
| 79 | + cargo |
| 80 | + ] ++ nativeBuildInputs; |
| 81 | + |
| 82 | + buildPhase = '' |
| 83 | + runHook preBuild |
| 84 | +
|
| 85 | + # Ensure deterministic Cargo vendor builds |
| 86 | + export SOURCE_DATE_EPOCH=1 |
| 87 | +
|
| 88 | + if [[ ! -f Cargo.lock ]]; then |
| 89 | + echo |
| 90 | + echo "ERROR: The Cargo.lock file doesn't exist" |
| 91 | + echo |
| 92 | + echo "Cargo.lock is needed to make sure that cargoHash/cargoSha256 doesn't change" |
| 93 | + echo "when the registry is updated." |
| 94 | + echo |
| 95 | +
|
| 96 | + exit 1 |
| 97 | + fi |
| 98 | +
|
| 99 | + # Keep the original around for copyLockfile |
| 100 | + cp Cargo.lock Cargo.lock.orig |
| 101 | +
|
| 102 | + export CARGO_HOME=$(mktemp -d cargo-home.XXX) |
| 103 | + CARGO_CONFIG=$(mktemp cargo-config.XXXX) |
| 104 | +
|
| 105 | + if [[ -n "$NIX_CRATES_INDEX" ]]; then |
| 106 | + cat >$CARGO_HOME/config.toml <<EOF |
| 107 | + [source.crates-io] |
| 108 | + replace-with = 'mirror' |
| 109 | + [source.mirror] |
| 110 | + registry = "$NIX_CRATES_INDEX" |
| 111 | + EOF |
| 112 | + fi |
| 113 | +
|
| 114 | + ${cargoUpdateHook} |
| 115 | +
|
| 116 | + # Override the `http.cainfo` option usually specified in `.cargo/config`. |
| 117 | + export CARGO_HTTP_CAINFO=${cacert}/etc/ssl/certs/ca-bundle.crt |
| 118 | +
|
| 119 | + if grep '^source = "git' Cargo.lock; then |
| 120 | + echo |
| 121 | + echo "ERROR: The Cargo.lock contains git dependencies" |
| 122 | + echo |
| 123 | + echo "This is currently not supported in the fixed-output derivation fetcher." |
| 124 | + echo "Use cargoLock.lockFile / importCargoLock instead." |
| 125 | + echo |
| 126 | +
|
| 127 | + exit 1 |
| 128 | + fi |
| 129 | +
|
| 130 | + cargo vendor $name --respect-source-config | cargo-vendor-normalise > $CARGO_CONFIG |
| 131 | +
|
| 132 | + # Create an empty vendor directory when there is no dependency to vendor |
| 133 | + mkdir -p $name |
| 134 | + # Add the Cargo.lock to allow hash invalidation |
| 135 | + cp Cargo.lock.orig $name/Cargo.lock |
| 136 | +
|
| 137 | + # Packages with git dependencies generate non-default cargo configs, so |
| 138 | + # always install it rather than trying to write a standard default template. |
| 139 | + install -D $CARGO_CONFIG $name/.cargo/config; |
| 140 | +
|
| 141 | + runHook postBuild |
| 142 | + ''; |
| 143 | + |
| 144 | + # Build a reproducible tar, per instructions at https://reproducible-builds.org/docs/archives/ |
| 145 | + installPhase = '' |
| 146 | + tar --owner=0 --group=0 --numeric-owner --format=gnu \ |
| 147 | + --sort=name --mtime="@$SOURCE_DATE_EPOCH" \ |
| 148 | + -czf $out $name |
| 149 | + ''; |
| 150 | + |
| 151 | + inherit (hash_) outputHashAlgo outputHash; |
| 152 | + |
| 153 | + impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [ "NIX_CRATES_INDEX" ]; |
| 154 | + } |
| 155 | + // (removeAttrs args removedArgs) |
| 156 | +) |
0 commit comments