diff --git a/README.md b/README.md
index 5638036..9b621a0 100644
--- a/README.md
+++ b/README.md
@@ -267,6 +267,7 @@ Note that you shouldn't call `overrideAttrs` on a derivation built by Naersk
| `postInstall` | Optional hook to run after the compilation is done; inside this script, `$out/bin` contains compiled Rust binaries. Useful if your application needs e.g. custom environment variables, in which case you can simply run `wrapProgram $out/bin/your-app-name` in here. Default: `false` |
| `usePureFromTOML` | Whether to use the `fromTOML` built-in or not. When set to `false` the python package `remarshal` is used instead (in a derivation) and the JSON output is read with `builtins.fromJSON`. This is a workaround for old versions of Nix. May be used safely from Nix 2.3 onwards where all bugs in `builtins.fromTOML` seem to have been fixed. Default: `true` |
| `mode` | What to do when building the derivation. Either `build`, `check`, `test`, `fmt` or `clippy`.
When set to something other than `build`, no binaries are generated. Default: `"build"` |
+| `autoCrateSpecificOverrides` | Whether to automatically apply crate-specific overrides, mainly additional `buildInputs` for dependencies.
For example, if you use the `openssl` crate, `pkgs.pkg-config` and `pkgs.openssl` are automatically added as buildInputs. Default: `true` |
### Note on `overrideAttrs`
@@ -397,16 +398,3 @@ naersk.buildPackage {
```
([context](https://github.com/nix-community/naersk/pull/288))
-
-### Using OpenSSL
-
-If your application uses OpenSSL (making the build process fail), try:
-
-```nix
-naersk.buildPackage {
- # ...
-
- nativeBuildInputs = with pkgs; [ pkg-config ];
- buildInputs = with pkgs; [ openssl ];
-}
-```
diff --git a/README.tpl.md b/README.tpl.md
index 45e515d..6effdc9 100644
--- a/README.tpl.md
+++ b/README.tpl.md
@@ -359,16 +359,3 @@ naersk.buildPackage {
```
([context](https://github.com/nix-community/naersk/pull/288))
-
-### Using OpenSSL
-
-If your application uses OpenSSL (making the build process fail), try:
-
-```nix
-naersk.buildPackage {
- # ...
-
- nativeBuildInputs = with pkgs; [ pkg-config ];
- buildInputs = with pkgs; [ openssl ];
-}
-```
diff --git a/build.nix b/build.nix
index f1552c4..f05cd2c 100644
--- a/build.nix
+++ b/build.nix
@@ -68,6 +68,10 @@
, fetchurl
, lndir
, userAttrs
+ #| Some additional buildInputs/overrides individual crates require
+, crateSpecificOverrides
+, autoCrateSpecificOverrides
+, pkgs
}:
let
@@ -130,14 +134,16 @@ let
jq
rsync
] ++ nativeBuildInputs
- ++ lib.optionals (mode == "clippy") [clippy];
+ ++ lib.optionals (mode == "clippy") [clippy]
+ ++ neededCrateSpecificOverrides.nativeBuildInputs;
buildInputs = lib.optionals stdenv.isDarwin [
darwin.Security
darwin.apple_sdk.frameworks.CoreServices
darwin.cf-private
darwin.libiconv
- ] ++ buildInputs;
+ ] ++ buildInputs
+ ++ neededCrateSpecificOverrides.buildInputs;
inherit builtDependencies;
@@ -374,6 +380,33 @@ let
};
};
+ # Crate-specific overrides needed for this build.
+ # They are merged from all the overrides of `cratesIoDependencies` defined in the `crate_specific.nix` file
+ #
+ # This can contain fields: `buildInputs`, `nativeBuildInputs`
+ # When changing them (eg. adding support for new ones/removing some), also update the comment in the `crate_specific.nix` file
+ neededCrateSpecificOverrides =
+ let
+ overridesList = builtins.map
+ ( crateInfo:
+ if builtins.hasAttr crateInfo.name crateSpecificOverrides then
+ crateSpecificOverrides.${crateInfo.name} { inherit crateInfo; }
+ else {}
+ )
+ cratesIoDependencies;
+ emptyOverrides = { buildInputs = []; nativeBuildInputs = []; };
+ in if autoCrateSpecificOverrides then
+ builtins.foldl'
+ (acc: elem:
+ {
+ buildInputs = acc.buildInputs ++ elem.buildInputs or [];
+ nativeBuildInputs = acc.nativeBuildInputs ++ elem.nativeBuildInputs or [];
+ }
+ )
+ emptyOverrides
+ overridesList
+ else emptyOverrides;
+
# Crates.io dependencies required to compile user's crate.
#
# As an output, for each dependency, this derivation produces a subdirectory
diff --git a/config.nix b/config.nix
index 730018b..2d2ce05 100644
--- a/config.nix
+++ b/config.nix
@@ -1,4 +1,4 @@
-{ lib, libb, builtinz, arg }:
+{ lib, libb, builtinz, arg, pkgs }:
let
allowFun = attrs0: attrName: default:
if builtins.hasAttr attrName attrs0 then
@@ -200,6 +200,12 @@ let
# What to do when building the derivation. Either `build`, `check`, `test`, `fmt` or `clippy`.
# When set to something other than `build`, no binaries are generated.
mode = attrs0.mode or "build";
+
+ # Whether to automatically apply crate-specific overrides, mainly additional
+ # `buildInputs` for dependencies.
+ # For example, if you use the `openssl` crate, `pkgs.pkg-config` and
+ # `pkgs.openssl` are automatically added as buildInputs.
+ autoCrateSpecificOverrides = attrs0.autoCrateSpecificOverrides or true;
};
argIsAttrs =
@@ -302,6 +308,7 @@ let
cargoDocOptions
copyDocsToSeparateOutput
removeReferencesToSrcFromDocs
+ autoCrateSpecificOverrides
postInstall
;
@@ -311,6 +318,8 @@ let
# Example:
# [ { name = "wabt", version = "2.0.6", sha256 = "..." } ]
cratesIoDependencies = libb.mkVersions buildPlanConfig.cargolock;
+
+ crateSpecificOverrides = import ./crate_specific.nix { inherit pkgs; };
};
# config used when planning the builds
diff --git a/crate_specific.nix b/crate_specific.nix
new file mode 100644
index 0000000..5554625
--- /dev/null
+++ b/crate_specific.nix
@@ -0,0 +1,16 @@
+# This file contains crate-specific build inputs.
+#
+# Each of them is an attribute with name of the crate and value being a function.
+# This function gets as arguments:
+# - `crateInfo` with information about the crate, such as sha256 or version
+#
+# Currently supported fields to return are: `buildInputs`, `nativeBuildInputs`
+
+{ pkgs }:
+
+{
+ openssl-sys = { ... }: {
+ nativeBuildInputs = with pkgs; [ pkg-config ];
+ buildInputs = with pkgs; [ openssl ];
+ };
+}
diff --git a/default.nix b/default.nix
index a377938..1bc1383 100644
--- a/default.nix
+++ b/default.nix
@@ -13,6 +13,7 @@
, writeText
, zstd
, clippy
+, pkgs
}@defaultBuildAttrs:
let
@@ -22,7 +23,7 @@ let
{ inherit lib writeText remarshal runCommandLocal formats; };
mkConfig = arg:
- import ./config.nix { inherit lib arg libb builtinz; };
+ import ./config.nix { inherit lib arg libb builtinz pkgs; };
buildPackage = arg:
let
diff --git a/test/fast/default.nix b/test/fast/default.nix
index 3eb1091..6d3ec51 100644
--- a/test/fast/default.nix
+++ b/test/fast/default.nix
@@ -11,6 +11,7 @@ args: {
git-dep-dup = import ./git-dep-dup args;
git-single-repository-with-multiple-crates = import ./git-single-repository-with-multiple-crates args;
git-symlink = import ./git-symlink args;
+ openssl = import ./openssl args;
post-install-hook = import ./post-install-hook args;
readme = import ./readme args;
simple-dep = import ./simple-dep args;
diff --git a/test/fast/openssl/default.nix b/test/fast/openssl/default.nix
new file mode 100644
index 0000000..87acbf4
--- /dev/null
+++ b/test/fast/openssl/default.nix
@@ -0,0 +1,33 @@
+# OpenSSL needs `pkg-config` and `openssl` buildInputs. This tests whether we correctly supply them automatically.
+
+{ naersk, pkgs, ... }:
+
+let
+ # Whether the nixpkgs is version 23 or newer, because in older nixpkgs, rustc is too old to build openssl.
+ buildIt = with pkgs.lib;
+ (strings.toInt ((builtins.elemAt (splitString "." trivial.version) 0)) >= 23);
+in
+
+if buildIt then
+ naersk.buildPackage {
+ src = ./fixtures;
+ doCheck = true;
+ }
+else
+ builtins.trace ''
+ Not building OpenSSL test, because Rust from nixpkgs under major version 23 cannot build OpenSSL
+ Current nixpkgs version: ${pkgs.lib.trivial.version}
+ ''
+
+ pkgs.stdenv.mkDerivation {
+ name = "not-building-openssl-test";
+
+ dontUnpack = true;
+
+ buildPhase = ''
+ echo Not building OpenSSL test, because Rust from nixpkgs under major version 23 cannot build OpenSSL
+ echo Current nixpkgs version: ${pkgs.lib.trivial.version}
+ '';
+
+ installPhase = "mkdir $out";
+ }
diff --git a/test/fast/openssl/fixtures/Cargo.lock b/test/fast/openssl/fixtures/Cargo.lock
new file mode 100644
index 0000000..3edf39c
--- /dev/null
+++ b/test/fast/openssl/fixtures/Cargo.lock
@@ -0,0 +1,140 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "bitflags"
+version = "2.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1"
+
+[[package]]
+name = "cc"
+version = "1.0.97"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "099a5357d84c4c61eb35fc8eafa9a79a902c2f76911e5747ced4e032edd8d9b4"
+
+[[package]]
+name = "cfg-if"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+
+[[package]]
+name = "foreign-types"
+version = "0.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1"
+dependencies = [
+ "foreign-types-shared",
+]
+
+[[package]]
+name = "foreign-types-shared"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
+
+[[package]]
+name = "libc"
+version = "0.2.154"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346"
+
+[[package]]
+name = "once_cell"
+version = "1.19.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
+
+[[package]]
+name = "openssl"
+version = "0.10.64"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "95a0481286a310808298130d22dd1fef0fa571e05a8f44ec801801e84b216b1f"
+dependencies = [
+ "bitflags",
+ "cfg-if",
+ "foreign-types",
+ "libc",
+ "once_cell",
+ "openssl-macros",
+ "openssl-sys",
+]
+
+[[package]]
+name = "openssl-macros"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "openssl-sys"
+version = "0.9.102"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c597637d56fbc83893a35eb0dd04b2b8e7a50c91e64e9493e398b5df4fb45fa2"
+dependencies = [
+ "cc",
+ "libc",
+ "pkg-config",
+ "vcpkg",
+]
+
+[[package]]
+name = "openssl-test"
+version = "0.1.0"
+dependencies = [
+ "openssl",
+]
+
+[[package]]
+name = "pkg-config"
+version = "0.3.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec"
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.82"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8ad3d49ab951a01fbaafe34f2ec74122942fe18a3f9814c3268f1bb72042131b"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.36"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "syn"
+version = "2.0.64"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7ad3dee41f36859875573074334c200d1add8e4a87bb37113ebd31d926b7b11f"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
+[[package]]
+name = "unicode-ident"
+version = "1.0.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
+
+[[package]]
+name = "vcpkg"
+version = "0.2.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
diff --git a/test/fast/openssl/fixtures/Cargo.toml b/test/fast/openssl/fixtures/Cargo.toml
new file mode 100644
index 0000000..d69e97d
--- /dev/null
+++ b/test/fast/openssl/fixtures/Cargo.toml
@@ -0,0 +1,11 @@
+# Tests that default-run doesn't break the dependency build, see
+# https://github.com/nmattia/naersk/issues/123
+
+[package]
+name = "openssl-test"
+version = "0.1.0"
+authors = ["FireFragment <55660550+FireFragment@users.noreply.github.com>"]
+edition = "2018"
+
+[dependencies]
+openssl = "0.10.64"
diff --git a/test/fast/openssl/fixtures/src/main.rs b/test/fast/openssl/fixtures/src/main.rs
new file mode 100644
index 0000000..e7a11a9
--- /dev/null
+++ b/test/fast/openssl/fixtures/src/main.rs
@@ -0,0 +1,3 @@
+fn main() {
+ println!("Hello, world!");
+}