Skip to content
vic

edolstra/import-cargo

A function for fetching the crates listed in a Cargo lock file

edolstra/import-cargo.json
{
"createdAt": "2019-07-05T14:55:00Z",
"defaultBranch": "master",
"description": "A function for fetching the crates listed in a Cargo lock file",
"fullName": "edolstra/import-cargo",
"homepage": "",
"language": "Nix",
"name": "import-cargo",
"pushedAt": "2022-04-04T20:34:27Z",
"stargazersCount": 55,
"topics": [],
"updatedAt": "2025-10-12T03:33:44Z",
"url": "https://github.com/edolstra/import-cargo"
}

Simple flake to import all dependencies from a Cargo.lock as fixed-output derivation using the checksum and URL from the lockfile.

This example demonstrates how to build a local Cargo project with a flake.nix:

{
description = "My Rust project";
inputs = {
nixpkgs.url = github:NixOS/nixpkgs/nixos-20.03;
import-cargo.url = github:edolstra/import-cargo;
};
outputs = { self, nixpkgs, import-cargo }: let
inherit (import-cargo.builders) importCargo;
in {
defaultPackage.x86_64-linux =
with import nixpkgs { system = "x86_64-linux"; };
stdenv.mkDerivation {
name = "testrust";
src = self;
nativeBuildInputs = [
# setupHook which makes sure that a CARGO_HOME with vendored dependencies
# exists
(importCargo { lockFile = ./Cargo.lock; inherit pkgs; }).cargoHome
# Build-time dependencies
rustc cargo
];
buildPhase = ''
cargo build --release --offline
'';
installPhase = ''
install -Dm775 ./target/release/testrust $out/bin/testrust
'';
};
};
}