Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git workspaces #1

Open
wants to merge 18 commits into
base: cross-compile-2024-09-25
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,475 changes: 2,379 additions & 96 deletions crate2nix/Cargo.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion crate2nix/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3809,4 +3809,3 @@ rec {
#
};
}

1 change: 1 addition & 0 deletions crate2nix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ resolver = "2"
[dependencies]
anyhow = "1.0.28"
cargo_metadata = "0.18"
cargo = "0.72"
cargo-platform = "0.1"
hex = "0.4"
itertools = "0.12"
Expand Down
5 changes: 5 additions & 0 deletions crate2nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ let
cssparser-macros = attrs: assert builtins.trace "cssparser" true;{
buildInputs = lib.optionals stdenv.isDarwin [ darwin.apple_sdk.frameworks.Security ];
};
libgit2-sys = old: {
nativeBuildInputs = (old.nativeBuildInputs or []) ++ pkgs.libgit2.nativeBuildInputs;
buildInputs = (old.buildInputs or []) ++ pkgs.libgit2.buildInputs ++ [pkgs.iconv.dev pkgs.apple-sdk.frameworks.CoreFoundation];
propagatedBuildInputs = (old.propagatedBuildInputs or [ ]) ++ pkgs.libgit2.propagatedBuildInputs;
};
};
};
set_templates = if release then "" else "--set TEMPLATES_DIR ${./templates}";
Expand Down
2 changes: 1 addition & 1 deletion crate2nix/flake-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@
};
};

config.packages.default = pkgs.callPackage ./default.nix { };
# config.packages.default = pkgs.callPackage ./default.nix { };
};
}
31 changes: 31 additions & 0 deletions crate2nix/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,20 @@ pub enum Opt {
)]
output: PathBuf,
},

#[structopt(
name = "resolve-manifest",
about = "Resolve fields inherited from a workspace, so that the manifest can be processed stand-alone."
)]
ResolveManifest {
#[structopt(
short = "f",
long = "cargo-toml",
parse(from_os_str),
help = "The path to the Cargo.toml of the project."
)]
cargo_toml: PathBuf,
},
}

#[derive(Debug, StructOpt, Deserialize, Serialize)]
Expand Down Expand Up @@ -463,7 +477,24 @@ fn main() -> anyhow::Result<()> {
} => {
command.execute(&crate2nix_json)?;
}
Opt::ResolveManifest { cargo_toml } => {
let manifest = resolve_manifest(&cargo_toml)?;
let toml = toml::to_string_pretty(manifest.original())?;
println!("{toml}");
}
}

Ok(())
}

fn resolve_manifest(cargo_toml: &Path) -> cargo::CargoResult<cargo::core::Manifest> {
use cargo::core::SourceId;

let full_path = cargo_toml.canonicalize()?;
let source_id = SourceId::for_path(&full_path)?;

let config = cargo::Config::default()?;
let (pkg, _paths) = cargo::ops::read_package(&full_path, source_id, &config)?;

Ok(pkg.manifest().clone())
}
2 changes: 1 addition & 1 deletion docs/flake-module.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
perSystem = { config, self', inputs', pkgs, lib, system, ... }:
let nodejs = pkgs.nodejs_21; in {
let nodejs = pkgs.nodejs_20; in {
devshells.default = {
commands = [
{ package = nodejs; category = "docs"; }
Expand Down
64 changes: 64 additions & 0 deletions docs/src/content/docs/00_guides/42_custom_toolchains.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
title: Custom Toolchains
---

One way to use a custom rust toolchain with `crate2nix` is to
import nixpkgs with an overlay for `rustc`.

Here is an example flake using [fenix]:

```nix

{
description = "containix";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/24.05";
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils.url = "github:numtide/flake-utils";
crate2nix.url = "github:nix-community/crate2nix";
};

outputs =
{
self,
nixpkgs,
flake-utils,
fenix,
crate2nix,
}:
flake-utils.lib.eachDefaultSystem (
system:
let
toolchain = fenix.packages.${system}.stable.defaultToolchain;

pkgs = import nixpkgs {
inherit system;
overlays = [
(final: prev: {
rustc = toolchain;
cargo = toolchain;
})
];
};

crate2nix' = pkgs.callPackage (import "${crate2nix}/tools.nix") {};
cargoNix = crate2nix.appliedCargoNix {
name = "my-crate";
src = ./.;
};
in
{
packages = {
default = cargoNix.rootCrate.build;
};
}
);
}

```

[fenix]: https://github.com/nix-community/fenix
78 changes: 78 additions & 0 deletions docs/src/content/docs/00_guides/80_using_a_rust_overlay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
title: Using a Rust overlay
---

It's possible to use a Rust overlay such as `rust-overlay` with `crate2nix`.
This can be used for pinning the Rust toolchain version, or to use a newer toolchain version than is available in Nixpkgs.

In a flake with flake-parts:

```nix
# flake.nix
{
# ...

inputs = {
flake-parts = {
url = "github:hercules-ci/flake-parts";
inputs.nixpkgs-lib.follows = "nixpkgs";
};
crate2nix.url = "github:nix-community/crate2nix";
# ...
};

outputs =
inputs @ { self
, nixpkgs
, flake-parts
, rust-overlay
, crate2nix
, ...
}:
flake-parts.lib.mkFlake { inherit inputs; } {
systems = [
"x86_64-linux"
"aarch64-linux"
"x86_64-linux"
"aarch64-darwin"
];

perSystem = { system, lib, inputs', ... }:
let
pkgs = import nixpkgs {
inherit system;

overlays = [ rust-overlay.overlays.default ];
};

buildRustCrateForPkgs =
crate:
pkgs.buildRustCrate.override {
rustc = pkgs.rust-bin.stable.latest.default;
cargo = pkgs.rust-bin.stable.latest.default;
};

generatedCargoNix = inputs.crate2nix.tools.${system}.appliedCargoNix {
name = "rustnix";
src = ./.;
};

cargoNix = import generatedCargoNix {
inherit pkgs buildRustCrateForPkgs;
};
in
rec {
checks = {
rustnix = cargoNix.rootCrate.build.override {
runTests = true;
};
};

packages = {
rustnix = cargoNix.rootCrate.build;
default = packages.rustnix;
};
};
};
}
```
2 changes: 1 addition & 1 deletion docs/src/content/docs/90_reference/90_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: A list of all major changes per version.

## 0.14.x - 0.15.0 (unreleased)

TODO
* [#359](https://github.com/nix-community/crate2nix/issues/359): Document using `rust-overlay`.

## 0.14.x - 0.14.1 (2024-06-30)

Expand Down
2 changes: 1 addition & 1 deletion docs/src/content/docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ You can
dependency tree from cargo. Therefore, it will use the exact same library versions as cargo and respect any locked down
version in `Cargo.lock`.

**Smart caching**: It caches the builds of individual creates so that nix rebuilds exactly the crates that need to be rebuilt.
**Smart caching**: It caches the builds of individual crates so that nix rebuilds exactly the crates that need to be rebuilt.
Compare that to docker layers...

**Nix ecosystem goodness**: You can use all things that make the nix/NixOS ecosystem great, e.g. distributed/remote builds,
Expand Down
10 changes: 4 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
description = ''
crate2nix generates [nix](https://nixos.org/nix/) build files for [rust](https://www.rust-lang.org/)
crate2nix generates [nix](https://nixos.org/nix/) build files for [rust](https://www.rust-lang.org/)
crates using [cargo](https://crates.io/).
'';

Expand Down
16 changes: 10 additions & 6 deletions nix/devshell/flake-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
gnugrep
utillinux
cacert
iconv.dev
darwin.apple_sdk.frameworks.CoreFoundation
darwin.apple_sdk.frameworks.Security
curl.out
];

commands = with pkgs; [
Expand All @@ -30,12 +34,12 @@
{ package = nixpkgs-fmt; category = "nix"; }
{ package = nix; category = "nix"; }
{ package = nix-prefetch-git; category = "nix"; }
{
name = "nix-test";
package = (import ../nix-test-runner.nix { inherit pkgs; });
category = "nix";
help = "nix test runner for unit tests.";
}
# {
# name = "nix-test";
# package = (import ../nix-test-runner.nix { inherit pkgs; });
# category = "nix";
# help = "nix test runner for unit tests.";
# }
{ package = inputs'.cachix.packages.default; category = "nix"; }
];

Expand Down
2 changes: 1 addition & 1 deletion nix/nix-test-runner.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ in
{ pkgs ? import ./nixpkgs.nix { }
# Use last pinned crate2nix packages to build the test runner
# so that it works even if we have broken stuff!
, tools ? pkgs.callPackage "${builtins.fetchTree flakeLock.nodes.crate2nix_stable.locked}/tools.nix" { }
, tools ? pkgs.callPackage ../tools.nix { }
}:
let
nixTestRunner = tools.appliedCargoNix {
Expand Down
20 changes: 16 additions & 4 deletions tools.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@
, strictDeprecation ? true
}:
let
cargoNix = pkgs.callPackage ./crate2nix/Cargo.nix { inherit strictDeprecation; };
defaultCrateOverrides = pkgs.defaultCrateOverrides // {
libgit2-sys = old: {
nativeBuildInputs = (old.nativeBuildInputs or []) ++ pkgs.libgit2.nativeBuildInputs;
buildInputs = (old.buildInputs or []) ++ pkgs.libgit2.buildInputs ++ [pkgs.iconv.dev pkgs.apple_sdk.frameworks.CoreFoundation];
propagatedBuildInputs = (old.propagatedBuildInputs or [ ]) ++ pkgs.libgit2.propagatedBuildInputs;
};
};

cargoNix = pkgs.callPackage ./crate2nix/Cargo.nix { inherit strictDeprecation defaultCrateOverrides; workspaceSrc = ./.; };
crate2nix = cargoNix.rootCrate.build;
in
rec {

/* Returns a derivation containing the whole top-level function generated
/* Returns a derivation containing the whole top-level function generated
by crate2nix (`Cargo.nix`) which is typically called with `pkgs.callPackage`.

name: will be part of the derivation name
Expand Down Expand Up @@ -44,7 +52,7 @@ rec {
stdenv.mkDerivation {
name = "${name}-crate2nix";

buildInputs = [ pkgs.cargo pkgs.jq crate2nix ];
buildInputs = [ pkgs.cargo pkgs.jq crate2nix pkgs.nix ];
preferLocalBuild = true;

inherit src;
Expand Down Expand Up @@ -429,9 +437,13 @@ rec {
''
mkdir -p $out
cp -apR ${src}/${pathToExtract}/* $out
cd $out

mv ./Cargo.toml ./Cargo.toml.orig
${crate2nix}/bin/crate2nix resolve-manifest --cargo-toml ${src}/${pathToExtract}/Cargo.toml > ./Cargo.toml

echo '{"package":null,"files":{}}' > $out/.cargo-checksum.json
'';

};
};
};
Expand Down