Commit Graph

234 Commits

Author SHA1 Message Date
Ivan Petkov
49141d2b37
Import groupBy from lib not builtins
* lib.groupBy will default to builtins.groupBy if available, so it is
  safer to use
2022-06-07 14:51:20 -07:00
Yusuf Bera Ertan
8f0b89244f
fix(mkDummySrc): remove IFD trigger by not checking if cargo lock path exists, the build will fail if it doesnt 2022-06-05 14:04:20 +03:00
Ivan Petkov
6fd4d85dac
buildDepsOnly: remove (now unnecessary) cargoLock check
* Previously we would manually copy over any Cargo.lock file from the
  source to the dummified source
* Now, `mkDummySrc` will implicitly copy the file if it is present, or
  ignore it if missing, so this check is a bit overzealous now
* Loosening this check also allows callers to sneak in their own
  Cargo.lock file (e.g. through a patch phase) while still avoiding IFD
2022-06-02 20:51:28 -07:00
Ivan Petkov
c2acd4b443
Minor cleanup 2022-06-02 19:49:44 -07:00
adisbladis
a1e56fba69 Stop passing around fromTOML
It's a builtin that we can just use directly.
2022-06-03 02:01:02 +08:00
adisbladis
2491382d63 Remove internal dependency on toTOML 2022-06-03 02:01:02 +08:00
Ivan Petkov
4bd60ab128
Drop compatibility for Nix versions below 2.8.1 2022-06-01 17:05:52 -07:00
Ivan Petkov
4f2b1c43c9
mkDummySrc: fix cache invalidation if src is flake root 2022-05-16 20:10:38 -07:00
Ivan Petkov
c8b8597ecb
Merge pull request #26 from firestack/fix/relative-paths
fix(nix-relative-paths): quote relative paths to allow for external sources
2022-05-10 17:21:29 -07:00
Kayla Firestack
e908c1afea fix(nix-relative-paths): quote relative paths to allow for external sources
when trying to use crane with a non local src (eg, not `./.`) we get the error
```
$ nix flake show
<...>
error: access to absolute path '/Cargo.toml' is forbidden in pure eval mode (use '--impure' to override)
(use '--show-trace' to show detailed location information)
```

To fix this, we quote the relative paths so to properly append them to the base path.

https://nixos.wiki/wiki/Nix_Expression_Language#Coercing_a_relative_path_with_interpolated_variables_to_an_absolute_path_.28for_imports.29

## Reproduction flake
```
{
  description = "Build a cargo project without extra checks";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";

    crane = {
      url = "github:ipetkov/crane";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    external-crate-source = {
      url = "github:ray-kast/empress";
      flake = false;
    };

    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, crane, flake-utils, external-crate-source, ... }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = nixpkgs.legacyPackages.${system};

        test-crate = crane.lib.${system}.buildPackage {
          src = external-crate-source.outPath;
        };
      in
      {
        packages.default = test-crate;
      });
}
```
2022-05-10 10:00:00 -04:00
Ivan Petkov
b47fafa717
buildDepsOnly: add --all-targets to default cargoCheckCommand
This allows caching all artifacts (including from dev-dependencies) such
that tools like clippy don't have to generate them every time they run.
2022-05-08 19:46:58 -07:00
Ivan Petkov
762784a8c5
Add compatibility fallback for builtins.groupBy 2022-02-20 14:20:21 -08:00
Ivan Petkov
f8d1e70dfc
Fix handling of git repos whose rev is not on the main branch
* When Nix fetches a git repo it will only look for the specified
  revision only starting from the main branch (apparently fetching
  arbitrary revisions from a repository has some security implications)
* If a ref (i.e. branch or tag) is not specified, Nix will only fetch
  the repo's main branch
* To remedy this we will supply Nix with the branch or tag (if specified
  in the Cargo.lock) to help it find the specified revision
* If cargo does not specify a branch or tag for us, we'll set `allRefs =
  true` so that Nix can try fetching all possible branches and tags
  before trying to check out the locked revision
2022-02-18 18:10:53 -08:00
Ivan Petkov
5c8e7f732f
Fix callPackage scopes for build hooks so cross compiling works
* Previously all build hooks were instantiated in a single
  `callPackages` call which led to several issues:
  - changes via `lib.overrideScope'` were ignored because the build
    hooks were only ever instantiated with the nixpkgs instance of our
    flake (not whatever overlays the caller may have made)
  - the pkgs splicing was not done quite right, meaning when we pull in
    the build hooks as nativeBuildInputs the splicing wasn't picking up
    the overridden versions of `cargo` but was instead trying to
    recompile the nixpkgs version each time
2022-02-14 18:31:45 -08:00
Ivan Petkov
78359792ae
Form paths by concatenating a string suffix not a path
* Seems like Nix can get unhappy if a path fragment is evaluated too
  eagerly, giving errors like
  `error: access to absolute path '/Cargo.toml' is forbidden in pure eval mode (use '--impure' to override)`
* Changing to using string manipulation seems to resolve the issue
2022-02-13 12:50:43 -08:00
Ivan Petkov
a56b6d1c56
cargoTarpaulin: change behavior to install cargo artifacts by default
* This allows potentially chaining `cargoTarpaulin` invocations with other
  derivations
2022-02-13 09:36:31 -08:00
Ivan Petkov
3024283855
cargoClippy: change behavior to install cargo artifacts by default
* This allows potentially chaining `cargoClippy` invocations with other
  derivations
2022-02-13 09:34:26 -08:00
Ivan Petkov
472d7ffc99
Handle case where a git repo contains multiple crates 2022-02-10 20:32:31 -08:00
Ivan Petkov
e2c665091e
Add test that source prefix remapping works 2022-02-09 20:54:56 -08:00
Ivan Petkov
469f4a5fde
Fix git dependency handling of overlapping repos 2022-02-09 20:22:07 -08:00
Ivan Petkov
ca73fc5305
Add support for git dependencies 2022-02-07 20:03:12 -08:00
Ivan Petkov
1ab0cad98c
Split out registry vendoring to vendorCargoRegistries 2022-02-06 17:16:49 -08:00
Ivan Petkov
90d5ad84c4
cargoFmt: change behavior to install cargo artifacts by default
* This allows potentially chaining `cargoFmt` invocations with other
  derivations
2022-02-10 21:02:48 -08:00
Ivan Petkov
d4b5f8abe6
chore: switch to runCommandLocal to reduce network pressure 2022-01-30 16:10:41 -08:00
Ivan Petkov
492076c765 Rename registryFromGitIndex input to indexUrl for consistency 2022-01-30 23:58:56 +00:00
Ivan Petkov
47fbae6dc3 Add support for alternative cargo registries 2022-01-30 23:58:56 +00:00
Ivan Petkov
5e44613211 Make crate downloads aware of alternative registries 2022-01-30 23:58:56 +00:00
Ivan Petkov
5bae4b91c1 Optimize searching for cargo configuration files
* Rather than recursively look up all paths in a given project and then
  filtering for cargo-specific files, we now use our own customized
  traversal method to only capture the results we want.
2022-01-30 23:58:56 +00:00
Ivan Petkov
8f6b608ca1
Inline Cargo.{toml,lock} checks in buildDepsOnly 2022-01-16 18:24:13 -08:00
Ivan Petkov
aeb598b174
Add cargoTarpaulin 2022-01-16 18:16:21 -08:00
Ivan Petkov
438154f265
Add cargoFmt 2022-01-16 17:49:39 -08:00
Ivan Petkov
13c6a4cdce
crateNameFromCargoToml: use 0.0.1 as fallback version 2022-01-15 19:58:31 -08:00
Ivan Petkov
92d185d877
Fix some formatting 2022-01-15 19:56:46 -08:00
Ivan Petkov
72bb21ee86
crateNameFromCargoToml: fix parameter validation
* Previously the check was overzealous and it was impossible to only
  specify `cargoToml` (since both other parameters would be null)
* Instead, we take advantage of Nix's laziness and only throw when
  needed
2022-01-09 15:50:21 -08:00
Ivan Petkov
3f82111135
Merge vendorCargoDepsFromArgs into vendorCargoDeps 2022-01-08 17:52:10 -08:00
Ivan Petkov
bd27ed8f73
cargoClippy: remove unused parameter 2022-01-08 17:24:57 -08:00
Ivan Petkov
49ef407216
Rename doCopyTargetToOutput to doInstallCargoArtifacts
* Similarly rename `installCargoTargetDirHook` to
  `installCargoArtifactsHook`
* The intention is to highlight that "install" implies "copy to output"
  and not anywhere else
* Also avoids the potential confusion of "cargo target dir" (location of
  cargo's artifacts) with "cargo target" (which is the target
  architecture/platform we want cargo to build for)
2022-01-08 17:14:05 -08:00
Ivan Petkov
4099e352b8
cargoClippy: do not force cargo target dir to be installed
* Just having the derivation succeed is evidence enough and copying the
  artifacts isn't strictly necessary
2022-01-08 17:03:06 -08:00
Ivan Petkov
263fe7c78f
Fix operator precedence typo
* In Nix the `or` operator has higher precedence than function
  application, therefore `x or y z` parses as `(x or y) z`
2022-01-08 16:57:45 -08:00
Ivan Petkov
1c9633f4aa
mkDummySrc: allow cargoLock location to be passed in by caller
* This can potentially allow callers to build crates which may not have
  a Cargo.lock in their source but needs to be passed in from
  _elsewhere_
2022-01-07 20:54:07 -08:00
Ivan Petkov
44e9060497
Print cargo version before building
* Just for the benefit of being able to tell at which cargo version is
  used at a glance
2022-01-07 20:48:52 -08:00
Ivan Petkov
abc2c8c418
mkDummySrc: drop dummy lib.rs by default, not main.rs
* There are some edge cases where cargo will allow a lib.rs file to be
  present but not a main.rs (e.g. a binary cannot be called "examples",
  which will happen if the crate is called that and a main.rs file is
  present)
2022-01-07 20:42:24 -08:00
Ivan Petkov
1c8034c0d9
Update mkDummySrc to support cargo workspaces 2022-01-07 17:36:31 -08:00
Ivan Petkov
791f1b2241
Add tests for mkDummySrc
* Also fix a bug with copying over .cargo/config files
2022-01-05 20:46:05 -08:00
Ivan Petkov
aa58fce185
Memoize some parameters to avoid recomputing the vendorDir when possible 2022-01-04 18:25:39 -08:00
Ivan Petkov
7ec94f9653
Remove internally used parameters from derivations where possible
* By default we pass everything through to the actual derivation itself,
  but some internal parameters don't show up as environment variables
  directly (or rather do not need to)
* In an effort to keep the build environment as lean as possible, we can
  do some clean up (e.g. to avoid invalidating builds if some parameter
  changes but is completely ignored/overridden elsewhere)
2022-01-04 17:45:31 -08:00
Ivan Petkov
3da8e2e195
Enable doCheck on derivations by default 2022-01-04 14:40:24 -08:00
Ivan Petkov
ba50659d0d
Rename buildWithCargo to cargoBuild 2022-01-04 14:18:29 -08:00
Ivan Petkov
66db381bac
Add cargoClippy 2022-01-04 12:07:32 -08:00
Ivan Petkov
ab283fb5f7
Echo all build/check/install commands for easier debugging 2022-01-04 09:49:04 -08:00
Ivan Petkov
3b4e0bffe1
Rename copyCargoTargetToOutputHook to installCargoTargetDirHook
* New name should better reflect that we are _installing to the output_
  rather than copying from some location to another
2022-01-03 19:57:35 -08:00
Ivan Petkov
dc553e3853
Remove installFromCargoArtifactsHook
* This hook was never a great implementation to begin with since it
  would simply search the cargo artifacts directory for binaries and
  libraries to install
* This isn't really great since if we have multiple builds (say one with
  debug artifacts, one with release artifacts) it isn't exactly clear
  which artifacts would get installed (or which will get clobbered).
* Now that we have installFromCargoBuildLogHook we can simplify the
  options a bit and only have one main installation method. The caller
  can always provide their own if they wish
2022-01-03 14:42:59 -08:00
Ivan Petkov
f69c8a4c44
Add buildPackage for building and installing cargo packages 2022-01-03 14:32:51 -08:00
Ivan Petkov
47316bfe5b
Add vendorCargoDepsFromArgs
* This abstracts the path checking and message raised on errors into a
  reusable function
2022-01-03 10:28:56 -08:00
Ivan Petkov
4c1711399d
Split out buildWithCargo into a lower level mkCargoDerivation
* The intention here is to split up different "responsibilities" into
  smaller parts which can be composed as a DAG rather than mutually
  recursive functions. Specifically:
* `mkCargoDerivation` represents a lower-level thin wrapper around
  `stdenv.mkDerivation` which will
  - set up hooks
  - require the caller to define the variables needed by the hooks (like
    vendor dir, or artifacts to inherit)
  - ensure that build/check/install phases can be configured by the
    caller without having them remember to call pre/post hooks
* This allows `buildDepsOnly` to only focus on setting some default
  values (like good default commands to build all artifacts, setting the
  derivation name, etc.) and delegating the rest to `mkCargoDerivation`
* Lastly, the responsibility of `buildWithCargo` ends up ensuring that
  `cargoArtifacts` and `cargoVendorDir` are defined if the caller does
  not pass them in
2022-01-02 14:48:32 -08:00
Ivan Petkov
10a60cc085
Do not copy cargo artifacts into a separate output by default
* Copying the cargo artifacts to their own separate output is a good
  idea *in theory* where each derivation produces bins/libs so that
  other things can depend on them without pulling in the cargo artifacts
  as well
* In practice, it's much more likely that a derivation will produce
  cargo artifacts (to be reused in other build/test steps) XOR produce
  the final binaries
* Therefore _not_ separating the outputs will produce less friction when
  forming dependency trees in the general path (e.g. no more forgetting
  to specify `drv.target`)
* If a caller really wants to install cargo artifacts in a separate
  output, they can easily add the customization themselves
2022-01-01 11:12:13 -08:00
Ivan Petkov
55ba19ec1c
mkDummySrc: fix handling of bin targets
* First, bin targets go into `src/bin` not `bin`
* Second, the path fallback name needs a `rs` suffix
2021-12-31 19:03:06 -08:00
Ivan Petkov
b4a1445590
cleanCargoToml: retain crate-type
* Some tools may try to inspect crate types (e.g. wasm-pack) so it may
  be useful to retain the attribute
* In general, the crate-type can influence the project's "structure" so
  we should remain faithful to that
2021-12-31 18:40:01 -08:00
Ivan Petkov
e6cdd3df92
cleanCargoToml: strip required-features from target definitions
* Turns out the `required-features` attribute is only used from hiding
  targets from cargo's selection (e.g. do not build unless feature is
  enabled) and does not actually influence what features cargo will
  enable/build for dependencies
2021-12-31 18:34:06 -08:00
Ivan Petkov
64914fbc98
Remove doCompressTarget option
* Reducing the parameter surface area for good measure, zstd compression
  works pretty well and it seems redundant to support multiple ways of
  copying the target directory around
2021-12-30 20:25:33 -08:00
Ivan Petkov
2a14c2d53b
Update crateNameFromCargoToml with better fallback error messages
* If a derivation is created without a name *and* we cannot infer a name
  from a Cargo.toml file, we'll throw a descriptive error message which
  hints towards the remediation
* Otherwise nix can throw a pretty obtuse "derivation has no name" error
  with no error trace
2021-12-30 15:08:45 -08:00
Ivan Petkov
0aadea3b08
Rename buildWithDummySrc to buildDepsOnly 2021-12-30 14:49:52 -08:00
Ivan Petkov
2339d7959f
Add buildWithDummySrc
* This also implements automatically building dependencies in a separate
  derivation if `buildWithCargo` is invoked without `cargoArtifacts`
2021-12-30 14:46:35 -08:00
Ivan Petkov
3223e4860e
Add mkDummySrc 2021-12-30 13:22:05 -08:00
Ivan Petkov
4a515f43ca
cleanCargoToml: do not replace path attributes
* Keep the paths as specified in the Cargo.toml while cleaning, leaving
  it up to the caller to replace them if necessary
2021-12-30 10:04:12 -08:00
Ivan Petkov
234257c239
cleanCargoToml: do not strip edition fields
* The edition entries can affect cargo behavior, not just rustc
  behavior, so we should keep them in for consistency with what the
  project expects
2021-12-30 09:44:50 -08:00
Ivan Petkov
cf1c6eb0e8
Add checks for cleanCargoToml 2021-12-29 19:26:55 -08:00
Ivan Petkov
a286fe1ff5
Add cleanCargoToml 2021-12-29 16:03:39 -08:00
Ivan Petkov
e6a6ad68b1
Change default buildPhase to run cargo build 2021-12-28 18:54:51 -08:00
Ivan Petkov
ade4ea7137
Add some documentation around the buildWithCargo parameters 2021-12-28 18:51:44 -08:00
Ivan Petkov
6cc3ea3199
Add remapSourcePathPrefixHook 2021-12-28 18:51:06 -08:00
Ivan Petkov
bf17616560
Add installFromCargoArtifactsHook 2021-12-28 17:51:51 -08:00
Ivan Petkov
dd3fa0123b
Rename inheritCargoTargetHook to inheritCargoArtifactsHook
* Hopefully the updated name should be more clear that we are inhering
  some existing cargo artifacts and NOT inheriting something like
  CARGO_BUILD_TARGET which refers to the host target being built
2021-12-28 16:48:12 -08:00
Ivan Petkov
fab2fa93cc
Explicitly define more defaults for buildWithCargo
* This should make it a bit easier to tell at a glance what inputs are
  significant for the derivation without having to look at the entire
  source (and corresponding hooks)
2021-12-28 15:27:49 -08:00
Ivan Petkov
65b984b4aa
Rename doCopyTarget to doCopyTargetToOutput
* The intention is to make it more clear that this flag controls copying
  the target directory so that *other* derivations can use it, and DOES
  NOT control whether we use an *existing* target directory from another
  derivation
2021-12-28 15:19:10 -08:00
Ivan Petkov
b8d083cd7e
Implement automatically setting up a vendor dir from source
* If the source root contains a Cargo.lock file it will be used to
  create a vendor directory
* Otherwise a (descriptive) error will be thrown describing how to
  remediate
* The caller can, of course, manually specify their own `cargoVendorDir`
  which will skip the default vendoring
2021-12-28 15:15:10 -08:00
Ivan Petkov
5abd4f6054
Implement compressing cargo target directory 2021-12-26 20:25:36 -08:00
Ivan Petkov
edcb983805
Change doCopyTarget to always imply a separate output
* We can simplify the configuration by removing the
  `doCopyTargetToSeparateOutput` parameter
* If a caller wants to copy target artifacts into any other output, it
  is pretty trivial for them to add their own logic for it
2021-12-26 20:11:22 -08:00
Ivan Petkov
2d9788eb8b
Add copyCargoTargetToOutputHook 2021-12-26 18:06:19 -08:00
Ivan Petkov
c2d7d083d5
Add configureCargoCommonVarsHook 2021-12-26 17:17:39 -08:00
Ivan Petkov
e1b7cea23c
Add buildWithCargo as a thin wrapper around mkDerivation 2021-12-26 16:27:09 -08:00
Ivan Petkov
83632355d2
Add configureCargoVendoredDepsHook 2021-12-26 16:21:08 -08:00
Ivan Petkov
3af378cfd7
Implement vendoring cargo dependencies 2021-12-26 14:05:23 -08:00
Ivan Petkov
2cec871d53
Initial commit 2021-12-26 11:47:59 -08:00