Unverified Commit 40b7b8c1 authored by Jörg Thalheim's avatar Jörg Thalheim Committed by GitHub
Browse files

Merge pull request #202394 from figsoda/remove-carnix

carnix,cratesIO: remove
parents 694cd662 82fe76d1
Loading
Loading
Loading
Loading
+0 −221
Original line number Diff line number Diff line
@@ -608,227 +608,6 @@ buildPythonPackage rec {
}
```

## `buildRustCrate`: Compiling Rust crates using Nix instead of Cargo {#compiling-rust-crates-using-nix-instead-of-cargo}

### Simple operation {#simple-operation}

When run, `cargo build` produces a file called `Cargo.lock`,
containing pinned versions of all dependencies. Nixpkgs contains a
tool called `carnix` (`nix-env -iA nixos.carnix`), which can be used
to turn a `Cargo.lock` into a Nix expression.

That Nix expression calls `rustc` directly (hence bypassing Cargo),
and can be used to compile a crate and all its dependencies. Here is
an example for a minimal `hello` crate:

```ShellSession
$ cargo new hello
$ cd hello
$ cargo build
     Compiling hello v0.1.0 (file:///tmp/hello)
     Finished dev [unoptimized + debuginfo] target(s) in 0.20 secs
$ carnix -o hello.nix --src ./. Cargo.lock --standalone
$ nix-build hello.nix -A hello_0_1_0
```

Now, the file produced by the call to `carnix`, called `hello.nix`, looks like:

```nix
# Generated by carnix 0.6.5: carnix -o hello.nix --src ./. Cargo.lock --standalone
{ stdenv, buildRustCrate, fetchgit }:
let kernel = stdenv.buildPlatform.parsed.kernel.name;
    # ... (content skipped)
in
rec {
  hello = f: hello_0_1_0 { features = hello_0_1_0_features { hello_0_1_0 = f; }; };
  hello_0_1_0_ = { dependencies?[], buildDependencies?[], features?[] }: buildRustCrate {
    crateName = "hello";
    version = "0.1.0";
    authors = [ "pe@pijul.org <pe@pijul.org>" ];
    src = ./.;
    inherit dependencies buildDependencies features;
  };
  hello_0_1_0 = { features?(hello_0_1_0_features {}) }: hello_0_1_0_ {};
  hello_0_1_0_features = f: updateFeatures f (rec {
        hello_0_1_0.default = (f.hello_0_1_0.default or true);
    }) [ ];
}
```

In particular, note that the argument given as `--src` is copied
verbatim to the source. If we look at a more complicated
dependencies, for instance by adding a single line `libc="*"` to our
`Cargo.toml`, we first need to run `cargo build` to update the
`Cargo.lock`. Then, `carnix` needs to be run again, and produces the
following nix file:

```nix
# Generated by carnix 0.6.5: carnix -o hello.nix --src ./. Cargo.lock --standalone
{ stdenv, buildRustCrate, fetchgit }:
let kernel = stdenv.buildPlatform.parsed.kernel.name;
    # ... (content skipped)
in
rec {
  hello = f: hello_0_1_0 { features = hello_0_1_0_features { hello_0_1_0 = f; }; };
  hello_0_1_0_ = { dependencies?[], buildDependencies?[], features?[] }: buildRustCrate {
    crateName = "hello";
    version = "0.1.0";
    authors = [ "pe@pijul.org <pe@pijul.org>" ];
    src = ./.;
    inherit dependencies buildDependencies features;
  };
  libc_0_2_36_ = { dependencies?[], buildDependencies?[], features?[] }: buildRustCrate {
    crateName = "libc";
    version = "0.2.36";
    authors = [ "The Rust Project Developers" ];
    sha256 = "01633h4yfqm0s302fm0dlba469bx8y6cs4nqc8bqrmjqxfxn515l";
    inherit dependencies buildDependencies features;
  };
  hello_0_1_0 = { features?(hello_0_1_0_features {}) }: hello_0_1_0_ {
    dependencies = mapFeatures features ([ libc_0_2_36 ]);
  };
  hello_0_1_0_features = f: updateFeatures f (rec {
    hello_0_1_0.default = (f.hello_0_1_0.default or true);
    libc_0_2_36.default = true;
  }) [ libc_0_2_36_features ];
  libc_0_2_36 = { features?(libc_0_2_36_features {}) }: libc_0_2_36_ {
    features = mkFeatures (features.libc_0_2_36 or {});
  };
  libc_0_2_36_features = f: updateFeatures f (rec {
    libc_0_2_36.default = (f.libc_0_2_36.default or true);
    libc_0_2_36.use_std =
      (f.libc_0_2_36.use_std or false) ||
      (f.libc_0_2_36.default or false) ||
      (libc_0_2_36.default or false);
  }) [];
}
```

Here, the `libc` crate has no `src` attribute, so `buildRustCrate`
will fetch it from [crates.io](https://crates.io). A `sha256`
attribute is still needed for Nix purity.

### Handling external dependencies {#handling-external-dependencies}

Some crates require external libraries. For crates from
[crates.io](https://crates.io), such libraries can be specified in
`defaultCrateOverrides` package in nixpkgs itself.

Starting from that file, one can add more overrides, to add features
or build inputs by overriding the hello crate in a separate file.

```nix
with import <nixpkgs> {};
((import ./hello.nix).hello {}).override {
  crateOverrides = defaultCrateOverrides // {
    hello = attrs: { buildInputs = [ openssl ]; };
  };
}
```

Here, `crateOverrides` is expected to be a attribute set, where the
key is the crate name without version number and the value a function.
The function gets all attributes passed to `buildRustCrate` as first
argument and returns a set that contains all attribute that should be
overwritten.

For more complicated cases, such as when parts of the crate's
derivation depend on the crate's version, the `attrs` argument of
the override above can be read, as in the following example, which
patches the derivation:

```nix
with import <nixpkgs> {};
((import ./hello.nix).hello {}).override {
  crateOverrides = defaultCrateOverrides // {
    hello = attrs: lib.optionalAttrs (lib.versionAtLeast attrs.version "1.0")  {
      postPatch = ''
        substituteInPlace lib/zoneinfo.rs \
          --replace "/usr/share/zoneinfo" "${tzdata}/share/zoneinfo"
      '';
    };
  };
}
```

Another situation is when we want to override a nested
dependency. This actually works in the exact same way, since the
`crateOverrides` parameter is forwarded to the crate's
dependencies. For instance, to override the build inputs for crate
`libc` in the example above, where `libc` is a dependency of the main
crate, we could do:

```nix
with import <nixpkgs> {};
((import hello.nix).hello {}).override {
  crateOverrides = defaultCrateOverrides // {
    libc = attrs: { buildInputs = []; };
  };
}
```

### Options and phases configuration {#options-and-phases-configuration}

Actually, the overrides introduced in the previous section are more
general. A number of other parameters can be overridden:

- The version of `rustc` used to compile the crate:

  ```nix
  (hello {}).override { rust = pkgs.rust; };
  ```

- Whether to build in release mode or debug mode (release mode by
  default):

  ```nix
  (hello {}).override { release = false; };
  ```

- Whether to print the commands sent to `rustc` when building
  (equivalent to `--verbose` in cargo:

  ```nix
  (hello {}).override { verbose = false; };
  ```

- Extra arguments to be passed to `rustc`:

  ```nix
  (hello {}).override { extraRustcOpts = "-Z debuginfo=2"; };
  ```

- Phases, just like in any other derivation, can be specified using
  the following attributes: `preUnpack`, `postUnpack`, `prePatch`,
  `patches`, `postPatch`, `preConfigure` (in the case of a Rust crate,
  this is run before calling the "build" script), `postConfigure`
  (after the "build" script),`preBuild`, `postBuild`, `preInstall` and
  `postInstall`. As an example, here is how to create a new module
  before running the build script:

  ```nix
  (hello {}).override {
    preConfigure = ''
       echo "pub const PATH=\"${hi.out}\";" >> src/path.rs"
    '';
  };
  ```

### Features {#features}

One can also supply features switches. For example, if we want to
compile `diesel_cli` only with the `postgres` feature, and no default
features, we would write:

```nix
(callPackage ./diesel.nix {}).diesel {
  default = false;
  postgres = true;
}
```

Where `diesel.nix` is the file generated by Carnix, as explained above.

## Setting Up `nix-shell` {#setting-up-nix-shell}

Oftentimes you want to develop code from within `nix-shell`. Unfortunately
+7 −1
Original line number Diff line number Diff line
@@ -33,7 +33,13 @@
    <itemizedlist spacing="compact">
      <listitem>
        <para>
          Create the first release note entry in this section!
          <literal>carnix</literal> and <literal>cratesIO</literal> has
          been removed due to being unmaintained, use alternatives such
          as
          <link xlink:href="https://github.com/nix-community/naersk">naersk</link>
          and
          <link xlink:href="https://github.com/kolloch/crate2nix">crate2nix</link>
          instead.
        </para>
      </listitem>
    </itemizedlist>
+1 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ In addition to numerous new and upgraded packages, this release has the followin

<!-- To avoid merge conflicts, consider adding your item at an arbitrary place in the list instead. -->

- Create the first release note entry in this section!
- `carnix` and `cratesIO` has been removed due to being unmaintained, use alternatives such as [naersk](https://github.com/nix-community/naersk) and [crate2nix](https://github.com/kolloch/crate2nix) instead.

## Other Notable Changes {#sec-release-23.05-notable-changes}

+0 −259
Original line number Diff line number Diff line
# Generated by carnix 0.9.10: carnix generate-nix
{ lib, buildPlatform, buildRustCrate, buildRustCrateHelpers, cratesIO, fetchgit }:
with buildRustCrateHelpers;
let inherit (lib.lists) fold;
    inherit (lib.attrsets) recursiveUpdate;
in
rec {
  crates = cratesIO;
  carnix = crates.crates.carnix."0.10.0" deps;
  __all = [ (carnix {}) ];
  deps.aho_corasick."0.6.10" = {
    memchr = "2.2.0";
  };
  deps.ansi_term."0.11.0" = {
    winapi = "0.3.6";
  };
  deps.argon2rs."0.2.5" = {
    blake2_rfc = "0.2.18";
    scoped_threadpool = "0.1.9";
  };
  deps.arrayvec."0.4.10" = {
    nodrop = "0.1.13";
  };
  deps.atty."0.2.11" = {
    termion = "1.5.1";
    libc = "0.2.50";
    winapi = "0.3.6";
  };
  deps.autocfg."0.1.2" = {};
  deps.backtrace."0.3.14" = {
    cfg_if = "0.1.7";
    rustc_demangle = "0.1.13";
    autocfg = "0.1.2";
    backtrace_sys = "0.1.28";
    libc = "0.2.50";
    winapi = "0.3.6";
  };
  deps.backtrace_sys."0.1.28" = {
    libc = "0.2.50";
    cc = "1.0.32";
  };
  deps.bitflags."1.0.4" = {};
  deps.blake2_rfc."0.2.18" = {
    arrayvec = "0.4.10";
    constant_time_eq = "0.1.3";
  };
  deps.carnix."0.10.0" = {
    clap = "2.32.0";
    dirs = "1.0.5";
    env_logger = "0.6.1";
    failure = "0.1.5";
    failure_derive = "0.1.5";
    itertools = "0.8.0";
    log = "0.4.6";
    nom = "3.2.1";
    regex = "1.1.2";
    serde = "1.0.89";
    serde_derive = "1.0.89";
    serde_json = "1.0.39";
    tempdir = "0.3.7";
    toml = "0.5.0";
    url = "1.7.2";
  };
  deps.cc."1.0.32" = {};
  deps.cfg_if."0.1.7" = {};
  deps.clap."2.32.0" = {
    atty = "0.2.11";
    bitflags = "1.0.4";
    strsim = "0.7.0";
    textwrap = "0.10.0";
    unicode_width = "0.1.5";
    vec_map = "0.8.1";
    ansi_term = "0.11.0";
  };
  deps.cloudabi."0.0.3" = {
    bitflags = "1.0.4";
  };
  deps.constant_time_eq."0.1.3" = {};
  deps.dirs."1.0.5" = {
    redox_users = "0.3.0";
    libc = "0.2.50";
    winapi = "0.3.6";
  };
  deps.either."1.5.1" = {};
  deps.env_logger."0.6.1" = {
    atty = "0.2.11";
    humantime = "1.2.0";
    log = "0.4.6";
    regex = "1.1.2";
    termcolor = "1.0.4";
  };
  deps.failure."0.1.5" = {
    backtrace = "0.3.14";
    failure_derive = "0.1.5";
  };
  deps.failure_derive."0.1.5" = {
    proc_macro2 = "0.4.27";
    quote = "0.6.11";
    syn = "0.15.29";
    synstructure = "0.10.1";
  };
  deps.fuchsia_cprng."0.1.1" = {};
  deps.humantime."1.2.0" = {
    quick_error = "1.2.2";
  };
  deps.idna."0.1.5" = {
    matches = "0.1.8";
    unicode_bidi = "0.3.4";
    unicode_normalization = "0.1.8";
  };
  deps.itertools."0.8.0" = {
    either = "1.5.1";
  };
  deps.itoa."0.4.3" = {};
  deps.lazy_static."1.3.0" = {};
  deps.libc."0.2.50" = {};
  deps.log."0.4.6" = {
    cfg_if = "0.1.7";
  };
  deps.matches."0.1.8" = {};
  deps.memchr."1.0.2" = {
    libc = "0.2.50";
  };
  deps.memchr."2.2.0" = {};
  deps.nodrop."0.1.13" = {};
  deps.nom."3.2.1" = {
    memchr = "1.0.2";
  };
  deps.percent_encoding."1.0.1" = {};
  deps.proc_macro2."0.4.27" = {
    unicode_xid = "0.1.0";
  };
  deps.quick_error."1.2.2" = {};
  deps.quote."0.6.11" = {
    proc_macro2 = "0.4.27";
  };
  deps.rand."0.4.6" = {
    rand_core = "0.3.1";
    rdrand = "0.4.0";
    fuchsia_cprng = "0.1.1";
    libc = "0.2.50";
    winapi = "0.3.6";
  };
  deps.rand_core."0.3.1" = {
    rand_core = "0.4.0";
  };
  deps.rand_core."0.4.0" = {};
  deps.rand_os."0.1.3" = {
    rand_core = "0.4.0";
    rdrand = "0.4.0";
    cloudabi = "0.0.3";
    fuchsia_cprng = "0.1.1";
    libc = "0.2.50";
    winapi = "0.3.6";
  };
  deps.rdrand."0.4.0" = {
    rand_core = "0.3.1";
  };
  deps.redox_syscall."0.1.51" = {};
  deps.redox_termios."0.1.1" = {
    redox_syscall = "0.1.51";
  };
  deps.redox_users."0.3.0" = {
    argon2rs = "0.2.5";
    failure = "0.1.5";
    rand_os = "0.1.3";
    redox_syscall = "0.1.51";
  };
  deps.regex."1.1.2" = {
    aho_corasick = "0.6.10";
    memchr = "2.2.0";
    regex_syntax = "0.6.5";
    thread_local = "0.3.6";
    utf8_ranges = "1.0.2";
  };
  deps.regex_syntax."0.6.5" = {
    ucd_util = "0.1.3";
  };
  deps.remove_dir_all."0.5.1" = {
    winapi = "0.3.6";
  };
  deps.rustc_demangle."0.1.13" = {};
  deps.ryu."0.2.7" = {};
  deps.scoped_threadpool."0.1.9" = {};
  deps.serde."1.0.89" = {};
  deps.serde_derive."1.0.89" = {
    proc_macro2 = "0.4.27";
    quote = "0.6.11";
    syn = "0.15.29";
  };
  deps.serde_json."1.0.39" = {
    itoa = "0.4.3";
    ryu = "0.2.7";
    serde = "1.0.89";
  };
  deps.smallvec."0.6.9" = {};
  deps.strsim."0.7.0" = {};
  deps.syn."0.15.29" = {
    proc_macro2 = "0.4.27";
    quote = "0.6.11";
    unicode_xid = "0.1.0";
  };
  deps.synstructure."0.10.1" = {
    proc_macro2 = "0.4.27";
    quote = "0.6.11";
    syn = "0.15.29";
    unicode_xid = "0.1.0";
  };
  deps.tempdir."0.3.7" = {
    rand = "0.4.6";
    remove_dir_all = "0.5.1";
  };
  deps.termcolor."1.0.4" = {
    wincolor = "1.0.1";
  };
  deps.termion."1.5.1" = {
    libc = "0.2.50";
    redox_syscall = "0.1.51";
    redox_termios = "0.1.1";
  };
  deps.textwrap."0.10.0" = {
    unicode_width = "0.1.5";
  };
  deps.thread_local."0.3.6" = {
    lazy_static = "1.3.0";
  };
  deps.toml."0.5.0" = {
    serde = "1.0.89";
  };
  deps.ucd_util."0.1.3" = {};
  deps.unicode_bidi."0.3.4" = {
    matches = "0.1.8";
  };
  deps.unicode_normalization."0.1.8" = {
    smallvec = "0.6.9";
  };
  deps.unicode_width."0.1.5" = {};
  deps.unicode_xid."0.1.0" = {};
  deps.url."1.7.2" = {
    idna = "0.1.5";
    matches = "0.1.8";
    percent_encoding = "1.0.1";
  };
  deps.utf8_ranges."1.0.2" = {};
  deps.vec_map."0.8.1" = {};
  deps.winapi."0.3.6" = {
    winapi_i686_pc_windows_gnu = "0.4.0";
    winapi_x86_64_pc_windows_gnu = "0.4.0";
  };
  deps.winapi_i686_pc_windows_gnu."0.4.0" = {};
  deps.winapi_util."0.1.2" = {
    winapi = "0.3.6";
  };
  deps.winapi_x86_64_pc_windows_gnu."0.4.0" = {};
  deps.wincolor."1.0.1" = {
    winapi = "0.3.6";
    winapi_util = "0.1.2";
  };
}
+0 −7756

File deleted.

Preview size limit exceeded, changes collapsed.

Loading