2
votes

I am trying to build a derivation for one of my executable projects and it requires a version of Rust that is not yet in the nixpkgs channels, so I have built a nix derivation that installs that version that I need:

{ mkDerivation, stdenv, fetchurl }:
rec {
    version = "1.33.0";
    platform = mkDerivation rec {
        ver = version;
        name = "rust-${ver}";
        platform = if stdenv.system == "x86_64-linux" then "x86_64-unknown-linux-gnu"
            else if stdenv.system == "x86_64-darwin" then "x86_64-apple-darwin"
            else abort "unsupported platform";
        pkgSha = if stdenv.system == "x86_64-linux" then "6623168b9ee9de79deb0d9274c577d741ea92003768660aca184e04fe774393f"
            else if stdenv.system == "x86_64-darwin" then "864e7c074a0b88e38883c87c169513d072300bb52e1d320a067bd34cf14f66bd"
            else abort "unsupported platform";

        src = fetchurl {
            url = "https://static.rust-lang.org/dist/rust-${ver}-${platform}.tar.gz";
            sha256 = pkgSha;
        };

        phases = ["unpackPhase" "installPhase"];
        installPhase = ''
            mkdir -p $out
            ./install.sh --prefix=$out
        '';
    };

    rustc = platform;
    cargo = platform;
}

This is very easy for me to incorporate into my shell environment, as I just have to include buildInputs = [ rust.rustc rust.cargo ], and immediately I have Rust 1.33.

I used carnix-0.9.8 to set up a Cargo.nix, crates-io.nix, and crates-io.list file. Because of that, the libraries that I depend on get installed into the nix store like any other derivation, and my build process theoretically ends up not using Cargo at all.

I already require a macOS native security library, so I have already created a default.nix file:

{ pkgs ? import <nixpkgs-18.09> {}
, unstable ? import <nixpkgs> {}
, stdenv ? pkgs.stdenv
, licenses ? pkgs.lib.licenses
, maintainers ? pkgs.stdenv.maintainers }:
let
    rust = import ./nixpkgs/rust-1.33.nix {
      mkDerivation = pkgs.stdenv.mkDerivation;
      fetchurl = pkgs.fetchurl;
      stdenv = pkgs.stdenv;
    };

    cratesIO = import ./crates-io.nix {
        lib = stdenv.lib;
        buildRustCrate = unstable.buildRustCrate;
        buildRustCrateHelpers = unstable.buildRustCrateHelpers;
    };
    cargo = ...

    frameworks = ...
    security = ...
    orizentic = (cargo.orizentic {}).override { ... };

in pkgs.symlinkJoin rec {
    ...
}

And then I set up a build.rs file that errors out if the Rust version being used for building isn't at least version 1.33.0.

How do I inject my Rust derivation into buildRustCrate and buildRustCrateHelpers?

For reference, here is the entire Rust version of my project.

1

1 Answers

3
votes

I recently had a similar problem, so I can provide my approach here. My version of rustc came from the Mozilla overlay, but I guess the difference should be minimal.

{ moz_overlay ? builtins.fetchTarball https://github.com/mozilla/nixpkgs-ozilla/archive/master.tar.gz
, nixpkgs ? <nixpkgs>
}:
let
  moz_nixpkgs = import "${nixpkgs}" { overlays = [ (import "${moz_overlay}") ]; };
  rustc = (moz_nixpkgs.rustChannelOf { date = "2019-03-15"; channel = "nightly"; }).rust;
  crates = (import ./Cargo.nix {
    inherit (moz_nixpkgs) lib buildPlatform buildRustCrateHelpers fetchgit;
    buildRustCrate = moz_nixpkgs.buildRustCrate.override {
      inherit rustc; # I guess that injection happens here?
    };
    cratesIO = import ./nix/crates-io.nix {
      inherit (moz_nixpkgs) lib buildRustCrate buildRustCrateHelpers;
    };
  });
in {
  myapp = crates.myapp {};
}