ipetkov / crane

A Nix library for building cargo projects. Never build twice thanks to incremental artifact caching.
https://crane.dev
MIT License
962 stars 92 forks source link

Cross-Compile to musl example not working #662

Closed secana closed 4 months ago

secana commented 4 months ago

I tried to build for musl but there seems to be two issue with the example given here: https://crane.dev/examples/cross-musl.html

The first issue is in

rust-overlay = {
      url = "github:oxalica/rust-overlay";
      inputs = {
        nixpkgs.follows = "nixpkgs";
        flake-utils.follows = "flake-utils"; # <-- Does not exist
      };
    };

The input flake-utils does not exist for rust-overlay and an error is thrown.

If I remove the line from the template, my flake looks like this:

{
  description = "Building static binaries with musl";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";

    crane = {
      url = "github:ipetkov/crane";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    flake-utils.url = "github:numtide/flake-utils";

    rust-overlay = {
      url = "github:oxalica/rust-overlay";
      inputs = {
        nixpkgs.follows = "nixpkgs";
      };
    };
  };

  outputs = { nixpkgs, crane, flake-utils, rust-overlay, ... }:
    flake-utils.lib.eachSystem [ "aarch64-linux" ] (system:
      let
        pkgs = import nixpkgs {
          inherit system;
          overlays = [ (import rust-overlay) ];
        };

        craneLib = (crane.mkLib pkgs).overrideToolchain (p: p.rust-bin.stable.latest.default.override {
          targets = [ "aarch64-unknown-linux-musl" ];
        });

        my-crate = craneLib.buildPackage {
          pname = "kellnr";
          src = craneLib.cleanCargoSource ./.;
          strictDeps = true;

          CARGO_BUILD_TARGET = "aarch64-unknown-linux-musl";
          CARGO_BUILD_RUSTFLAGS = "-C target-feature=+crt-static";
        };
      in
      {
        checks = {
          inherit my-crate;
        };

        packages.default = my-crate;
      });
}

A nix build throws the error:

error:
       … while calling the 'derivationStrict' builtin

         at /builtin/derivation.nix:9:12: (source not available)

       … while evaluating derivation 'kellnr-0.1.0'
         whose name attribute is located at /nix/store/z49izx4bdhyi1vd1qjlgdsg9vqirrlbd-source/pkgs/stdenv/generic/make-derivation.nix:334:7

       … while evaluating attribute 'cargoArtifacts' of derivation 'kellnr-0.1.0'

         at /nix/store/6p9yzbswxw5mqs339l3ib36pdxfazdk7-source/lib/mkCargoDerivation.nix:59:10:

           58| } // {
           59|   inherit cargoArtifacts;
             |          ^
           60|

       (stack trace truncated; use '--show-trace' to show the full trace)

       error: Dependency is not of a valid type: element 1 of nativeBuildInputs for kellnr-deps

Any ideas how to get is working?

ipetkov commented 4 months ago

Hi @secana thanks for the report!


The input flake-utils does not exist for rust-overlay and an error is thrown.

Thanks for the note, should be fixed in #663 !


A nix build throws the error:

  1. What version of Nix are you using? (On my NixOS machine I currently have nix (Nix) 2.18.4
  2. Do you have a flake or branch I can check out that reproduces the issue?

I tried replacing the flake.nix of the cross-musl example with the one in your original issue but it built fine on my machine :thinking:

secana commented 4 months ago

Thanks for the fast response. I updated the flake and the second issue disappeared.

Btw. crane is an awesome project. Makes rust on Nix great experience and it's one of the few projects with good documentation.