crate2nix icon indicating copy to clipboard operation
crate2nix copied to clipboard

Incorrect generation

Open schradert opened this issue 10 months ago • 3 comments

Currently trying to package harpoon and testing my builds on x86_64-linux. Currently just running nix run github:nix-community/crate2nix on the cloned repo, but I have also tried older versions of crate2nix with the same results. The generated Cargo.nix shows the following for atty:

"atty" = rec {
  crateName = "atty";
  version = "0.2.14";
  edition = "2015";
  sha256 = "1s7yslcs6a28c5vz7jwj63lkfgyx8mx99fdirlhi9lbhhzhrpcyr";
  authors = [
    "softprops <[email protected]>"
  ];
};

But trying to build it gives use of unstable library feature 'rustc_private': this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via 'Cargo.toml' instead?, probably because libc isn't available. atty does define its dependencies. These are seemingly detected and generated correctly for internal and sample crate2nix packages here and here. I have seen other examples around that do not have this same issue as me unfortunately, so not too sure what's going on.

schradert avatar Feb 12 '25 19:02 schradert

I also got stuck trying to override everything that is wrong with the generation (it's much more than just atty). Here was my attempt:

{inputs, ...}: {
  perSystem = {inputs', lib, pkgs, system, ...}: let
    toolchain = with inputs'.fenix.packages; combine [minimal.rustc minimal.cargo targets.wasm32-wasip1.latest.rust-std];
  in {
    packages.crate2nix = inputs'.crate2nix.packages.default.override {cargo = toolchain;};
    packages.toolchain = toolchain;
    packages.harpoon = let
      # NOTE non-standard flake outputs (i.e. tools) are not cached on inputs'
      cargoNix = inputs.crate2nix.tools.${system}.generatedCargoNix {
        name = "harpoon";
        src = pkgs.fetchFromGitHub {
          owner = "Nacho114";
          repo = "harpoon";
          rev = "d3284615ba5063c73e0c1729edf5b10b46aead5e";
          hash = "sha256-heQ81oZjREU2jMmVG02KJQ6DwnCa7zT1umb5kNENxWY=";
        };
      };
      buildRustCrateForPkgs = pkgs: pkgs.buildRustCrate.override {
        rustc = toolchain;
        cargo = toolchain;
      };
      workspace = pkgs.callPackage cargoNix {inherit buildRustCrateForPkgs;};
      buildRustCrates = workspace.internal.builtRustCratesWithFeatures {
        packageId = "harpoon";
        features = ["default"];
        buildRustCrateForPkgsFunc = buildRustCrateForPkgs;
        runTests = false;
        crateConfigs = let
          libc = {
            name = "libc";
            packageId = "libc";
            target = { target, features }: (target."unix" or false);
          };
        in lib.recursiveUpdate workspace.internal.crates {
          atty.dependencies = [(libc // {usesDefaultFeatures = false;})];
          dirs-sys.dependencies = workspace.internal.crates.dirs-sys.dependencies ++ [libc];
          dirs-sys-next.dependencies = [libc];
          getrandom.dependencies = workspace.internal.crates.getrandom.dependencies ++ [libc];
          # NOTE overridden with use-libc feature since default linux_raw backend doesn't work...
          is-terminal.dependencies = [
            {
              name = "rustix";
              packageId = "rustix 0.38.2";
              target = { target, features }: (!((target."windows" or false) || ("hermit" == target."os" or null) || ("unknown" == target."os" or null)));
              features = [ "termios" "use-libc" ];
            }
          ];
          chrono.dependencies = workspace.internal.crates.chrono.dependencies ++ [
            {
              name = "iana-time-zone";
              packageId = "iana-time-zone";
            }
          ];
          iana-time-zone = {
            crateName = "iana-time-zone";
            version = "0.1.61";
            edition = "2018";
            sha256 = "sha256-I14IHzkloGcDwtARfqi5HwQnVv1uem5dkB6MoamWsiA=";
          };
          linux-raw-sys = {
            crateName = "linux-raw-sys";
            version = "0.7.0";
            edition = "2021";
            sha256 = "sha256-Ubr8C7XL7LntOB51uAMEAwL9Z4tdn6oE8B6qKAvIwCk=";
            features = {
              bootparam = [];
              btrfs = [];
              elf_uapi = [];
              errno = [];
              general = [];
              if_arp = [];
              if_ether = [];
              if_packet = [];
              io_uring = [];
              ioctl = [];
              landlock = [];
              loop_device = [];
              mempolicy = [];
              net = [];
              netlink = [];
              prctl = [];
              ptrace = [];
              system = [];
              xdp = [];
              default = ["std" "general" "errno"];
              std = [];
              no_std = [];
              elf = [];
              rustc-dep-of-std = ["core" "compiler_builtins" "no_std"];
            };
          };
          "rustix 0.38.2".dependencies = workspace.internal.crates."rustix 0.38.2".dependencies ++ [
            {
              name = "linux-raw-sys";
              packageId = "linux-raw-sys";
              features = ["ioctl"];
            }
          ];
        };
      };
    in buildRustCrates.crates.harpoon;
  };
}

Seemed like a good time to give up and try something else because clearly something is wrong.

schradert avatar Feb 12 '25 21:02 schradert

Well I tried with another similar zellij plugin called room, and it is having the same problem. This is what I would've expected to work:

{inputs, ...}: {
  perSystem = {inputs', pkgs, system, ...}: {
    packages.room = let
      toolchain = with inputs'.fenix.packages; combine [minimal.rustc minimal.cargo targets.wasm32-wasip1.latest.rust-std];
      # NOTE non-standard flake outputs (i.e. tools) are not cached on inputs'
      cargoNix = inputs.crate2nix.tools.${system}.generatedCargoNix {
        name = "room";
        src = pkgs.fetchFromGitHub {
          owner = "rvcas";
          repo = "room";
          rev = "fd6dc54a46fb9bce21065ce816189c037aeaf24f";
          hash = "sha256-T1JNFJUDCtCjXtZQUe1OQsfL3/BI7FUw60dImlUmLhg=";
        };
      };
      workspace = pkgs.callPackage cargoNix {
        buildRustCrateForPkgs = pkgs: pkgs.buildRustCrate.override {
          rustc = toolchain;
          cargo = toolchain;
        };
      };
    in workspace.rootCrate.build;
  };
}

schradert avatar Feb 12 '25 21:02 schradert

Not strictly relevant, but I do have a working build now without crate2nix, which I should have just done 4 hours ago...

{
  perSystem = {inputs', pkgs, ...}: {
    packages.harpoon' = pkgs.callPackage ({
      lib,
      fetchFromGitHub,
      rustPlatform,
      lld,
    }: rustPlatform.buildRustPackage rec {
      pname = "harpoon";
      version = "0.1.0-git.${lib.substring 0 7 src.rev}";

      src = fetchFromGitHub {
        owner = "Nacho114";
        repo = "harpoon";
        rev = "d3284615ba5063c73e0c1729edf5b10b46aead5e";
        hash = "sha256-heQ81oZjREU2jMmVG02KJQ6DwnCa7zT1umb5kNENxWY=";
      };
      cargoHash = "sha256-5b3lvxobzNbu4i4dyMGPnXfiWCENaqX7t8lfSgHQ3Rs=";

      depsBuildBuild = [lld];
      env.RUSTFLAGS = "-C linker=wasm-ld";
    }) {
      rustPlatform = let
        # NOTE requires stable rust to avoid ahash < 0.8.7 nightly bug (https://github.com/tkaitchuck/aHash/issues/200)
        toolchain = with inputs'.fenix.packages; combine [
          stable.rustc
          stable.cargo
          targets.wasm32-wasip1.stable.rust-std
        ];
      in pkgs.pkgsCross.wasi32.makeRustPlatform {
        rustc = toolchain;
        cargo = toolchain;
      };
    };
  };
}

schradert avatar Feb 12 '25 23:02 schradert