Add example for using an alternative cargo registry

This commit is contained in:
Ivan Petkov 2022-01-30 15:06:46 -08:00
parent 715c8731c3
commit 4adc828133
10 changed files with 102 additions and 8 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/examples/*/flake.lock
/result*
target/

View File

@ -13,7 +13,9 @@ A [Nix](https://nixos.org/) library for building [cargo](https://doc.rust-lang.o
Examples can be found [here](./examples). Detailed [API docs] are available, but
at a glance, the following are supported:
* Automatic vendoring of dependencies in a way that works with Nix
- Private registry and git dependency support coming soon!
- Alternative cargo registries are supported (with a minor configuration
change)
- Git dependency support coming soon!
* Reusing dependency artifacts after only building them once
* [clippy](https://github.com/rust-lang/rust-clippy) checks
* [rustfmt](https://github.com/rust-lang/rustfmt) checks
@ -32,6 +34,9 @@ nix flake init -t github:ipetkov/crane#quick-start-simple
# If you need a custom rust toolchain (e.g. to build WASM targets):
nix flake init -t github:ipetkov/crane#custom-toolchain
# If you need to use another crate registry besides crates.io
nix flake init -t github:ipetkov/crane#alt-registry
```
For an even more lean, no frills set up, create a `flake.nix` file with the

View File

@ -1,6 +0,0 @@
use std::any::TypeId;
fn main() {
println!("byteorder::LittleEndian: {:?}", TypeId::of::<byteorder::LittleEndian>());
println!("epitech_api::Client: {:?}", TypeId::of::<epitech_api::Client>());
}

View File

@ -103,7 +103,7 @@ onlyDrvs (lib.makeScope myLib.newScope (self:
];
in
myLibWithRegistry.buildPackage {
src = ./alt-registry;
src = ../examples/alt-registry;
nativeBuildInputs = with pkgs; [
pkgconfig
openssl

View File

@ -2,6 +2,8 @@
This directory contains several different ways of configuring a project:
* **alt-registry**: build a cargo project which uses another crate registry
besides crates.io
* **custom-toolchain**: build a cargo project with a custom toolchain (e.g. WASM
builds)
* **quick-start**: build a cargo project with a comprehensive test suite

View File

@ -0,0 +1,83 @@
{
description = "Build a cargo project with alternative crate registries";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
crane = {
url = "github:ipetkov/crane";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils = {
url = "github:numtide/flake-utils";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, crane, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
};
craneLibOrig = crane.lib.${system};
craneLib = craneLibOrig.appendCrateRegistries [
# Automatically infer the download URL from the registry's index
#
# Note that this approach requires checking out the full index at the specified revision
# and adding a copy to the Nix store.
#
# Also note that the specified git revision _does not need to track updates to the index
# itself_ as long as the pinned revision contains the most recent version of the
# registry's `config.json` file. In other words, this commit revision only needs to be
# updated if the `config.json` file changes the download endpoint for this registry.
(craneLibOrig.registryFromGitIndex {
url = "https://github.com/Hirevo/alexandrie-index";
rev = "90df25daf291d402d1ded8c32c23d5e1498c6725";
})
# As a more lightweight alternative, the `dl` endpoint of the registry's `config.json`
# file can be copied here to avoid needing to copy the index to the Nix store.
# (craneLibOrig.registryFromDownloadUrl {
# url = "https://github.com/Hirevo/alexandrie-index";
# dl = "https://crates.polomack.eu/api/v1/crates/{crate}/{version}/download";
# })
];
my-crate = craneLib.buildPackage {
src = ./.;
# Specific to our example, but not always necessary in the general case.
nativeBuildInputs = with pkgs; [
pkgconfig
openssl
];
};
in
{
checks = {
# Build the crate as part of `nix flake check` for convenience
inherit my-crate;
};
defaultPackage = my-crate;
packages.my-crate = my-crate;
apps.my-app = flake-utils.lib.mkApp {
drv = my-crate;
};
defaultApp = self.apps.${system}.my-app;
devShell = pkgs.mkShell {
inputsFrom = builtins.attrValues self.checks;
# Extra inputs can be added here
nativeBuildInputs = with pkgs; [
cargo
rustc
];
};
});
}

View File

@ -0,0 +1,9 @@
use std::any::TypeId;
fn main() {
let le = TypeId::of::<byteorder::LittleEndian>();
let client = TypeId::of::<epitech_api::Client>();
println!("TypeId of byteorder::LittleEndian (from crates.io): {:?}", le);
println!("TypeId of epitech_api::Client (from alexandrie): {:?}", client);
}