Subject: bug#25327: windows-related dependencies in crates

From: Nicolas Graves <[email-masked]>
Date: Thu, 24 Mar 2022 18:18:59 +0100

Hi all,

I've delved into the subject the past week and now have a clearer view
on it :

- windows (and mac) dependencies are not needed when compiling a single
  package. It is possible to bypass it without changing the
  build-system, by a few means :

           - either by using the #:cargo-build-flags argument (doesn't
             work everytime, seems to work only seldomly with
             --target unix)
             In this option, one would also need to modify Cargo.toml
             file before the package phase, since `cargo package`
             vendors all possible dependencies to ensure a package on
             all environments.
             For instance (1) :
             #:phases
             (modify-phases %standard-phases
               (add-before 'package 'unix-cargo-toml
                 (lambda _
                   (delete-file "Cargo.toml")
                   (substitute* "Cargo.toml.orig"
                     (("^mac-notification-sys.*") "")
                     (("^chrono.*") "")
                     (("^winrt-notification.*") ""))
                   (rename-file "Cargo.toml.orig" "Cargo.toml")
                   #t)))

           - either by using the environment variable
             "CARGO_BUILD_RUSTFLAGS"  during the build phase, like that
             for instance (2) :
             #:phases
             (modify-phases %standard-phases
               (add-before 'build 'unix-cargo-toml
                 (setenv
                  "CARGO_BUILD_RUSTFLAGS"
                  (string-append
                   (getenv "CARGO_BUILD_RUSTFLAGS")
                   "--cfg 'target_os=\"linux\"'"))))
             In this case, the package phase would also require the same
             change as in (1). Note that this might not be backward
             compatible further down the line (seen in the cargo
             manual).

For some reason, these two means don't seem to propagate when using such
packages as inputs.

There are also the options to use a snippet or a patch on the source
itself. The snippet on the source could look like (1), or the patch as
its effects, and we would still need to use (2). Haven't experimented
much with that yet.

In any case, all this seems kinda bulky for an edit on an imported base,
and since we have lots of information with `cargo metadata`, I imagine
it would be possible to modify the build-system itself, so that it only
considers `target_os = "linux"` or possibly redox / bsd / dragonfly if
guix considers porting to these platforms.

I have 2 possible ideas :

1) Maybe something like adding a phase after unpack, which would :

- parse `cargo metadata` to isolate only needed dependencies (by maybe
generating and applying a patch ? or maybe there's a cargo command for
generating a Cargo.toml based on modified metadata ?). In any case,
modifying the Cargo.toml to only keep relevant information.

2) Maybe rethinking the package phase entirely instead of relying on
`cargo package --no-verify --no-metadata` which seems to not allow
options without interferring with the Cargo.toml file.

In both cases, we would then need to :
- modify the phase were environment variables are defined to add (2).
- modify the crate import script accordingly (not trivial).

As for the second hypothesis, according to the cargo manual, what `cargo
package` does is the following :

1. Load and check the current workspace, performing some basic checks.
Path dependencies are not allowed unless they have a version key. Cargo
will ignore the path key for dependencies in published
packages. dev-dependencies do not have this restriction.

2. Create the compressed .crate file.
a. The original Cargo.toml file is rewritten and normalized.
b. [patch], [replace], and [workspace] sections are removed from the manifest.
c. Cargo.lock is automatically included if the package contains an
  executable binary or example target. cargo-install(1) will use the
  packaged lock file if the --locked flag is used.
d. A .cargo_vcs_info.json file is included that contains information about
  the current VCS checkout hash if available (not included with
  --allow-dirty).

3. Extract the .crate file and build it to verify it can build.
- This will rebuild your package from scratch to ensure that it can be
  built from a pristine state. The --no-verify flag can be used to skip
  this step.

4. Check that build scripts did not modify any source files.

Step 3 and 4 are actually not performed, since the build-system calls
cargo package with the --no-verify flag. The checks in step 1 doesn't
seem that essential on guix, but I imagine we could replicate
them. Clearly the essential step here is 2, but since we get a crate,
steps a. and b. should actually already be done at the source level, and
step d. is not essential because of origin's field version. Step 2c is
not essential either since Cargo.lock file is actually deleted during
the build system.

I might try to implement these changes, although I do not have enough
time and experience to do that quickly, neither enough horsepower to try
to rebuild everything after that. I included Maxime Devos in the
conversation, would it be possible to fork guix to develop this and try
to rebuild every rust package after that on a guix build farm ? Sorry,
haven't got in touch with guix developers yet, still imposter's
syndrome effect, but implementing this doesn't seem too hard. What would
I need to do to get going on that ?

Cheers,

Nicolas Graves