nix-cargo-integration icon indicating copy to clipboard operation
nix-cargo-integration copied to clipboard

Library to easily and effortlessly integrate Cargo projects with Nix.

nix-cargo-integration

Library to easily and effortlessly integrate Cargo projects with Nix.

  • Uses dream2nix to build Cargo packages and devshell to provide a development shell.
  • Allows configuration from Cargo.toml file(s) via package.metadata.nix and workspace.metadata.nix attributes.
  • Has sensible defaults, and strives to be compatible with Cargo (autobins, etc.).
  • Aims to offload work from the user; comes with useful configuration options (like renameOutputs, defaultOutputs etc.)
  • Can generate nixpkgs-compatible Nix expressions that captures all your packages dependencies / env vars and so on. You don't need to maintain a seperate derivation for nixpkgs! (see Generating a nixpkgs-compatible package expression in manual)
  • A CLI tool that let's you compile and run arbitrary Rust repositories directly without messing with any files or setting up overlays (see Using the nci CLI in manual)

Check out https://github.com/yusdacra/rust-nix-templater if you want an easy way to generate nix-specific boilerplate for your projects!

NOTE: nix-cargo-integration should work with any Nix version above 2.4+, but the experience may not be smooth if you aren't using the newest version of Nix.

Documentation

Documentation for master branch is on https://yusdacra.github.io/nix-cargo-integration.

You can also serve documentation by running mdbook serve docs at the root of the repository.

Important (mostly breaking) changes can be found in CHANGELOG.md.

Usage

With flakes

Run nix flake init -t github:yusdacra/nix-cargo-integration.

Or add:

{
  inputs = {
    nci.url = "github:yusdacra/nix-cargo-integration";
  };
  outputs = inputs: inputs.nci.lib.makeOutputs { root = ./.; };
}

to your flake.nix.

Without flakes

You can use flake-compat to provide the default outputs of the flake for non-flake users.

If you aren't using flakes, you can do (in your default.nix file for example):

let
  nciSrc = fetchTarball {
    url = "https://github.com/yusdacra/nix-cargo-integration/archive/<rev>.tar.gz";
    sha256 = "<hash>";
  };
  nci = import nciSrc;
in nci.makeOutputs { root = ./.; }

You can also couple it with niv:

  • First run niv add yusdacra/nix-cargo-integration
  • Then you can write in your default.nix file:
    let
      sources = import ./sources.nix;
      nci = import sources.nix-cargo-integration;
    in nci.makeOutputs { root = ./.; }
    

Examples