aboutsummaryrefslogtreecommitdiff
path: root/zkvmLib.nix
blob: 539b54e1a29b85aee1cc4cd9a47b741bfb9b1028 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
pkgs: guest:
let
  generateCargoLocks = craneLib: args: rec {
    cargoLockDrv = pkgs.stdenv.mkDerivation {
      name = "CargoLocks-${args.pname}";
      src = pkgs.lib.fileset.toSource {
        root = ./.;
        fileset = ./.;
      };

      installPhase = let
        # Since we're concatenating Cargo.lock files, duplicate package entries
        # are inevitable and cargo crashes when it encounters them.
        # We'll manually remove all duplicates and cargo will be happy.
        # This is a disgusting hack, but it's the best I've come up with.
        removeDuplicates = ''
          BEGIN {
              unique = 1
          }

          /^\[\[package\]\]/ { unique = 0; next }

          /^name = / {
              match($0, /".*"/)
              name = substr($0, RSTART + 1, RLENGTH - 2)
              next
          }

          name && /^version = / {
              match($0, /".*"/)
              version = substr($0, RSTART + 1, RLENGTH - 2)
              next
          }

          version && /^source = / {
              match($0, /".*"/)
              source = substr($0, RSTART + 1, RLENGTH - 2)
              next
          }

          source && /^checksum = / {
              match($0, /".*"/)
              checksum = substr($0, RSTART + 1, RLENGTH - 2)
              next
          }

          name && !unique {
              unique = (index(versions[name], version) == 0) ||
                       (source && index(sources[name], source) == 0) ||
                       (checksum && index(checksums[name], checksum) == 0)

              if (unique) {
                  versions[name]  = versions[name] version
                  sources[name]   = sources[name] source
                  checksums[name] = checksums[name] checksum

                  print "[[package]]"
                  print "name = \"" name "\""
                  print "version = \"" version "\""
                  if (source)   print "source = \"" source "\""
                  if (checksum) print "checksum = \"" checksum "\""
              }
              name = ""; version = ""; source = ""; checksum = ""
          }

          unique || /^$/ { print }
        '';
      in ''
        mkdir -p "$out"
        cd zkvms/${args.pname}

      '' + (if args ? extraLockfile then ''
        cat ${args.extraLockfile} > lockfile
        tail -n +4 ./host/Cargo.lock >> lockfile
      '' else ''
        cat ./host/Cargo.lock > lockfile
      '') + ''

        tail -n +4 ./guest/Cargo.lock >> lockfile
        tail -n +4 ../../guests/${guest}/Cargo.lock >> lockfile
        echo >> lockfile

        awk '${removeDuplicates}' lockfile > "$out/Cargo.lock"
      '';
    };

    cargoVendorDir = craneLib.vendorCargoDeps ({
      src = cargoLockDrv;
    } // (if args ? overrideVendorCargoPackage then {
      inherit (args) overrideVendorCargoPackage;
    } else
      { }) // (if args ? overrideVendorGitCheckout then {
        inherit (args) overrideVendorGitCheckout;
      } else
        { }));

  };
in {
  buildDepsOnly = craneLib: args:
    if builtins.pathExists ./guests/${guest}/.no_${args.pname} then
      { }
    else
      let cargoLocks = generateCargoLocks craneLib args;
      in craneLib.buildDepsOnly (cargoLocks // (builtins.removeAttrs args [
        "overrideVendorCargoPackage"
        "overrideVendorGitCheckout"
      ]) // {
        postUnpack = ''
          ${args.postUnpack or ""}
          ln -s ../../../guests ./source/zkvms/${args.pname}/guest/
          ln -s ../../../guests_macro ./source/zkvms/${args.pname}/guest/

          cp '${cargoLocks.cargoLockDrv}/Cargo.lock' ./source/zkvms/${args.pname}/guest/Cargo.lock
          chmod +w ./source/zkvms/${args.pname}/guest/Cargo.lock
        '';

        preBuild = ''
          ${args.preBuild or ""}
          cd zkvms/${args.pname}/guest
          cargo check --release --offline --all-targets
        '';
      } // {
        pname = "${args.pname}_${guest}";
      });

  buildPackage = craneLib: args:
    if builtins.pathExists ./guests/${guest}/.no_${args.pname} then
      pkgs.writeShellApplication {
        name = "${args.pname}_${guest}";

        text =
          let contents = builtins.readFile ./guests/${guest}/.no_${args.pname};
          in ''
            echo 'Guest program "${guest}" has blacklisted ${args.pname} zkVM!'

          '' + (if builtins.stringLength contents > 1 then ''
            echo
            echo 'Reason:'
            echo '${contents}'
          '' else
            "");
      }
    else
      let
        pname = "${args.pname}_${guest}";
        cargoLocks = generateCargoLocks craneLib args;
      in craneLib.buildPackage (cargoLocks // {
        phases = [
          "unpackPhase"
          "patchPhase"
          "configurePhase" # Standard phases
          "cargoSetupGuest"
          "buildGuestPhase" # Custom phases
          "buildPhase"
          "checkPhase"
          "installPhase"
          "fixupPhase" # Standard phases
        ];

        cargoSetupGuest = let
          appended = ''
            [features]
            guest = [] # Only used in jolt
            no_std = ["zkp/no_std"]
            ${args.pname} = ["zkp/${args.pname}"]
          '';
        in ''
          pushd zkvms/${args.pname}/guest

          cp '${cargoLocks.cargoLockDrv}/Cargo.lock' Cargo.lock
          cargo add --path "../../../guests/${guest}" --rename zkp --offline --features '${args.pname}'
          echo '${appended}' >> Cargo.toml

          popd
        '';

        buildGuestPhase = ''
          export INPUTS_DIR="$PWD/guests/${guest}"
          export ZKVM="${args.pname}" GUEST="${guest}"
          OLD_PATH="$PATH"

          ${if args ? guestToolchain then
            ''export PATH="${args.guestToolchain}/bin:$PATH"''
          else
            ""}

          pushd zkvms/${args.pname}/guest
          runHook preBuildGuest

          ${
            args.buildGuestCommand or "cargo build --release --features ${args.pname}"
          } \
              ${
                if args ? guestTarget then
                  "--target " + args.guestTarget
                else
                  ""
              } \
              ${args.guestExtraArgs or ""}

          ${if args ? guestTarget then
            "ln -s ../../guest/target/${args.guestTarget}/release/guest ../host/src/guest"
          else
            ""}
          unset RUSTUP_TOOLCHAIN RUSTFLAGS CARGO_ENCODED_RUSTFLAGS
          export PATH="$OLD_PATH"

          runHook postBuildGuest
          popd
        '';

        buildPhase = ''
          export INPUTS_DIR="$PWD/guests/${guest}"
          export ZKVM="${args.pname}" GUEST="${guest}"
          OLD_PATH="$PATH"

          ${if args ? hostToolchain then
            ''export PATH="${args.hostToolchain}/bin:$PATH"''
          else
            ""}

          pushd zkvms/${args.pname}/host
          runHook preBuild

          cargo --version
          cargo build --release

          export PATH="$OLD_PATH"

          runHook postBuild
          popd
        '';

        installPhase = let
          preRunBinaries = if args ? preRunBinaries
          && builtins.length args.preRunBinaries > 0 then
            ''export PATH="\$PATH:'' + pkgs.lib.makeBinPath args.preRunBinaries
            + ''"''
          else
            "";
          preRunLibraries = if args ? preRunLibraries
          && builtins.length args.preRunLibraries > 0 then
            ''export LD_LIBRARY_PATH="\$LD_LIBRARY_PATH:''
            + pkgs.lib.makeLibraryPath args.preRunLibraries + ''"''
          else
            "";
        in ''
          runHook preInstall

          mkdir -p "$out"/bin
          for bin in $(find . -type f -regex "./zkvms/.*release/[^/]*" -executable -print)
          do
            mv "$bin" "$out"/bin/
          done

          cat <<EOF > "$out"/bin/${pname}
          #!/usr/bin/env sh
          ${preRunBinaries}
          ${preRunLibraries}
          ${args.preRun or ""}
          "$out"/bin/host-${args.pname} \$@
          EOF
          chmod +x "$out"/bin/${pname}

          runHook postInstall
        '';

        doNotPostBuildInstallCargoBinaries = true;
      } // (builtins.removeAttrs args [
        "overrideVendorCargoPackage"
        "overrideVendorGitCheckout"
      ]) // {
        inherit pname;
      });
}