From 4adc8281332683845f125ffb742d3e64563c3566 Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Sun, 30 Jan 2022 15:06:46 -0800 Subject: [PATCH] Add example for using an alternative cargo registry --- .gitignore | 1 + README.md | 7 +- checks/alt-registry/src/main.rs | 6 -- checks/default.nix | 2 +- examples/README.md | 2 + .../alt-registry/.cargo/config.toml | 0 {checks => examples}/alt-registry/Cargo.lock | 0 {checks => examples}/alt-registry/Cargo.toml | 0 examples/alt-registry/flake.nix | 83 +++++++++++++++++++ examples/alt-registry/src/main.rs | 9 ++ 10 files changed, 102 insertions(+), 8 deletions(-) delete mode 100644 checks/alt-registry/src/main.rs rename {checks => examples}/alt-registry/.cargo/config.toml (100%) rename {checks => examples}/alt-registry/Cargo.lock (100%) rename {checks => examples}/alt-registry/Cargo.toml (100%) create mode 100644 examples/alt-registry/flake.nix create mode 100644 examples/alt-registry/src/main.rs diff --git a/.gitignore b/.gitignore index 95c0648..5fff759 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +/examples/*/flake.lock /result* target/ diff --git a/README.md b/README.md index 1d67eb2..9885f6f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/checks/alt-registry/src/main.rs b/checks/alt-registry/src/main.rs deleted file mode 100644 index 80e085e..0000000 --- a/checks/alt-registry/src/main.rs +++ /dev/null @@ -1,6 +0,0 @@ -use std::any::TypeId; - -fn main() { - println!("byteorder::LittleEndian: {:?}", TypeId::of::()); - println!("epitech_api::Client: {:?}", TypeId::of::()); -} diff --git a/checks/default.nix b/checks/default.nix index 3f39991..e024d1f 100644 --- a/checks/default.nix +++ b/checks/default.nix @@ -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 diff --git a/examples/README.md b/examples/README.md index 48ea4d6..bb36f25 100644 --- a/examples/README.md +++ b/examples/README.md @@ -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 diff --git a/checks/alt-registry/.cargo/config.toml b/examples/alt-registry/.cargo/config.toml similarity index 100% rename from checks/alt-registry/.cargo/config.toml rename to examples/alt-registry/.cargo/config.toml diff --git a/checks/alt-registry/Cargo.lock b/examples/alt-registry/Cargo.lock similarity index 100% rename from checks/alt-registry/Cargo.lock rename to examples/alt-registry/Cargo.lock diff --git a/checks/alt-registry/Cargo.toml b/examples/alt-registry/Cargo.toml similarity index 100% rename from checks/alt-registry/Cargo.toml rename to examples/alt-registry/Cargo.toml diff --git a/examples/alt-registry/flake.nix b/examples/alt-registry/flake.nix new file mode 100644 index 0000000..e16b296 --- /dev/null +++ b/examples/alt-registry/flake.nix @@ -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 + ]; + }; + }); +} diff --git a/examples/alt-registry/src/main.rs b/examples/alt-registry/src/main.rs new file mode 100644 index 0000000..fe1b85c --- /dev/null +++ b/examples/alt-registry/src/main.rs @@ -0,0 +1,9 @@ +use std::any::TypeId; + +fn main() { + let le = TypeId::of::(); + let client = TypeId::of::(); + + println!("TypeId of byteorder::LittleEndian (from crates.io): {:?}", le); + println!("TypeId of epitech_api::Client (from alexandrie): {:?}", client); +}