Search code examples
rustrust-cargonixosrust-diesel

How to install diesel-cli with sqlite support under NixOS and cargo?


When i try to install the diesel-cli with

cargo install diesel_cli --no-default-features --features sqlite

under NixOS, i get

error: linking with `cc` failed: exit status: 1
...
 = note: /nix/store/kmqs0wll31ylwbqkpmlgbjrn6ny3myik-binutils-2.35.1/bin/ld: cannot find -lsqlite3
          collect2: error: ld returned 1 exit status

even though the Nix packages sqlite and pkg-config are installed. To me this looks like sqlite missing its static libraries, however i don't see any flag to enable in https://github.com/NixOS/nixpkgs/blob/nixos-21.05/pkgs/development/libraries/sqlite/default.nix.

Note:

  1. Installing the postgres nix package and compiling the diesel-cli with
cargo install diesel_cli --no-default-features --features postgres

works flawlessly.

  1. A workaround install the diesel-cli is using the Nix package of the same name https://github.com/NixOS/nixpkgs/blob/nixos-21.05/pkgs/development/tools/diesel-cli/default.nix which comes with enabled sqlite support. Nevertheless, i would like know how to compile it myself, and trying to compile the Rust project will fail with the same error above.

Solution

  • The compilation seems to work using the following shell.nix:

    { pkgs ? import <nixpkgs> { } }:
    pkgs.mkShell {
      buildInputs = with pkgs; [ cargo rustc pkg-config sqlite ];
    }
    

    If you only have sqlite and/or pkg-config installed globally instead, that might be what's causing problems. The pkg-config derivation sets some environment variables:

    $ env | grep PKG_CONFIG
    NIX_PKG_CONFIG_WRAPPER_TARGET_TARGET_x86_64_unknown_linux_gnu=1
    PKG_CONFIG_FOR_TARGET=pkg-config
    

    and I do not think these are set if you use globally installed packages.