ipetkov / crane

A Nix library for building cargo projects. Never build twice thanks to incremental artifact caching.
https://crane.dev
MIT License
933 stars 90 forks source link

When run on darwin, fails due to missing iconv dependency. #156

Closed mitchty closed 1 year ago

mitchty commented 1 year ago

Not a huge deal but figured before I did the work to get a pr up anyone that might want to use crane with nix on darwin might want to know what needs to be changed/fixed.

After I ran nix flake init -t github:ipetkov/crane I had to do the following to get the default flake to build:

diff --git a/flake.nix b/flake.nix
index 912df00..04758ea 100644
--- a/flake.nix
+++ b/flake.nix
@@ -24,7 +24,7 @@
           inherit system;
         };

-        inherit (pkgs) lib;
+        inherit (pkgs) lib stdenv;

         craneLib = crane.lib.${system};
         src = craneLib.cleanCargoSource ./.;
@@ -32,12 +32,14 @@
         # Build *just* the cargo dependencies, so we can reuse
         # all of that work (e.g. via cachix) when running in CI
         cargoArtifacts = craneLib.buildDepsOnly {
+          buildInputs = [] ++ lib.optionals stdenv.isDarwin [ pkgs.libiconv ];
           inherit src;
         };

         # Build the actual crate itself, reusing the dependency
         # artifacts from above.
         my-crate = craneLib.buildPackage {
+          buildInputs = [] ++ lib.optionals stdenv.isDarwin [ pkgs.libiconv ];
           inherit cargoArtifacts src;
         };
       in
@@ -100,7 +102,7 @@
           nativeBuildInputs = with pkgs; [
             cargo
             rustc
-          ];
+          ] ++ (optionals stdenv.isDarwin [ libiconv ]);
         };
       });
 }

As far as I know this is a macos nix thing where due to it depending on libiconv implicitly, you have to add that as a dependency for both cargo and your native build inputs. Otherwise you end up with this barf of output:

$ nix build  
warning: Git tree '/private/tmp/test' is dirty
error: builder for '/nix/store/h5ddb2gv7dqjal501p6ndjyvi7xzbiz0-quick-start-deps-0.1.0.drv' failed with exit code 101;
       last 10 log lines:
       > ++ command cargo check --profile release --all-targets
       >    Compiling quick-start v0.1.0 (/private/tmp/nix-build-quick-start-deps-0.1.0.drv-0/dummy-src)
       > error: linking with `cc` failed: exit status: 1
       >   |
       >   = note: "cc" "-m64" "-arch" "x86_64" "/private/tmp/nix-build-quick-start-deps-0.1.0.drv-0/rustcV4eRQb/symbols.o" "/private/tmp/nix-build-quick-start-deps-0.1.0.drv-0/dummy-src/target/release/build/quick-start-7b8b70e8291a5325/build_script_6jsv47p94w0w45smdskbdfc3ibbranb3_dummy-7b8b70e8291a5325.build_script_6jsv47p94w0w45smdskbdfc3ibbranb3_dummy.ed04a4f7-cgu.0.rcgu.o" "/private/tmp/nix-build-quick-start-deps-0.1.0.drv-0/dummy-src/target/release/build/quick-start-7b8b70e8291a5325/build_script_6jsv47p94w0w45smdskbdfc3ibbranb3_dummy-7b8b70e8291a5325.build_script_6jsv47p94w0w45smdskbdfc3ibbranb3_dummy.ed04a4f7-cgu.1.rcgu.o" "/private/tmp/nix-build-quick-start-deps-0.1.0.drv-0/dummy-src/target/release/build/quick-start-7b8b70e8291a5325/build_script_6jsv47p94w0w45smdskbdfc3ibbranb3_dummy-7b8b70e8291a5325.build_script_6jsv47p94w0w45smdskbdfc3ibbranb3_dummy.ed04a4f7-cgu.2.rcgu.o" "/private/tmp/nix-build-quick-start-deps-0.1.0.drv-0/dummy-src/target/release/build/quick-start-7b8b70e8291a5325/build_script_6jsv47p94w0w45smdskbdfc3ibbranb3_dummy-7b8b70e8291a5325.build_script_6jsv47p94w0w45smdskbdfc3ibbranb3_dummy.ed04a4f7-cgu.3.rcgu.o" "/private/tmp/nix-build-quick-start-deps-0.1.0.drv-0/dummy-src/target/release/build/quick-start-7b8b70e8291a5325/build_script_6jsv47p94w0w45smdskbdfc3ibbranb3_dummy-7b8b70e8291a5325.build_script_6jsv47p94w0w45smdskbdfc3ibbranb3_dummy.ed04a4f7-cgu.4.rcgu.o" "/private/tmp/nix-build-quick-start-deps-0.1.0.drv-0/dummy-src/target/release/build/quick-start-7b8b70e8291a5325/build_script_6jsv47p94w0w45smdskbdfc3ibbranb3_dummy-7b8b70e8291a5325.build_script_6jsv47p94w0w45smdskbdfc3ibbranb3_dummy.ed04a4f7-cgu.5.rcgu.o" "/private/tmp/nix-build-quick-start-deps-0.1.0.drv-0/dummy-src/target/release/build/quick-start-7b8b70e8291a5325/build_script_6jsv47p94w0w45smdskbdfc3ibbranb3_dummy-7b8b70e8291a5325.build_script_6jsv47p94w0w45smdskbdfc3ibbranb3_dummy.ed04a4f7-cgu.6.rcgu.o" "/private/tmp/nix-build-quick-start-deps-0.1.0.drv-0/dummy-src/target/release/build/quick-start-7b8b70e8291a5325/build_script_6jsv47p94w0w45smdskbdfc3ibbranb3_dummy-7b8b70e8291a5325.5cghmlnhqf13p0n9.rcgu.o" "-L" "/private/tmp/nix-build-quick-start-deps-0.1.0.drv-0/dummy-src/target/release/deps" "-L" "/nix/store/ljcn8r8fl90yzgbc9ldi7kzal6fdi3p8-rustc-1.64.0/lib/rustlib/x86_64-apple-darwin/lib" "/nix/store/ljcn8r8fl90yzgbc9ldi7kzal6fdi3p8-rustc-1.64.0/lib/rustlib/x86_64-apple-darwin/lib/libstd-69395417f3e1755f.rlib" "/nix/store/ljcn8r8fl90yzgbc9ldi7kzal6fdi3p8-rustc-1.64.0/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-4cfe30a82dc98045.rlib" "/nix/store/ljcn8r8fl90yzgbc9ldi7kzal6fdi3p8-rustc-1.64.0/lib/rustlib/x86_64-apple-darwin/lib/libobject-3c481bfd30a1d3fb.rlib" "/nix/store/ljcn8r8fl90yzgbc9ldi7kzal6fdi3p8-rustc-1.64.0/lib/rustlib/x86_64-apple-darwin/lib/libmemchr-a4d2affc87bfb9bb.rlib" "/nix/store/ljcn8r8fl90yzgbc9ldi7kzal6fdi3p8-rustc-1.64.0/lib/rustlib/x86_64-apple-darwin/lib/libaddr2line-901f26d7e550b706.rlib" "/nix/store/ljcn8r8fl90yzgbc9ldi7kzal6fdi3p8-rustc-1.64.0/lib/rustlib/x86_64-apple-darwin/lib/libgimli-4d8793761d48a3f0.rlib" "/nix/store/ljcn8r8fl90yzgbc9ldi7kzal6fdi3p8-rustc-1.64.0/lib/rustlib/x86_64-apple-darwin/lib/librustc_demangle-0992154178d1769d.rlib" "/nix/store/ljcn8r8fl90yzgbc9ldi7kzal6fdi3p8-rustc-1.64.0/lib/rustlib/x86_64-apple-darwin/lib/libstd_detect-87c8e39c3e7d5eb3.rlib" "/nix/store/ljcn8r8fl90yzgbc9ldi7kzal6fdi3p8-rustc-1.64.0/lib/rustlib/x86_64-apple-darwin/lib/libhashbrown-29dbb969809059bf.rlib" "/nix/store/ljcn8r8fl90yzgbc9ldi7kzal6fdi3p8-rustc-1.64.0/lib/rustlib/x86_64-apple-darwin/lib/libminiz_oxide-4a3798b64666fb63.rlib" "/nix/store/ljcn8r8fl90yzgbc9ldi7kzal6fdi3p8-rustc-1.64.0/lib/rustlib/x86_64-apple-darwin/lib/libadler-1f5152f067755e42.rlib" "/nix/store/ljcn8r8fl90yzgbc9ldi7kzal6fdi3p8-rustc-1.64.0/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_alloc-0d05e2eb698caa79.rlib" "/nix/store/ljcn8r8fl90yzgbc9ldi7kzal6fdi3p8-rustc-1.64.0/lib/rustlib/x86_64-apple-darwin/lib/libunwind-d026e58c5d4b1a5e.rlib" "/nix/store/ljcn8r8fl90yzgbc9ldi7kzal6fdi3p8-rustc-1.64.0/lib/rustlib/x86_64-apple-darwin/lib/libcfg_if-be61dc1e44679922.rlib" "/nix/store/ljcn8r8fl90yzgbc9ldi7kzal6fdi3p8-rustc-1.64.0/lib/rustlib/x86_64-apple-darwin/lib/liblibc-1f5115519b1955cb.rlib" "/nix/store/ljcn8r8fl90yzgbc9ldi7kzal6fdi3p8-rustc-1.64.0/lib/rustlib/x86_64-apple-darwin/lib/liballoc-df9c2c62a3eb3433.rlib" "/nix/store/ljcn8r8fl90yzgbc9ldi7kzal6fdi3p8-rustc-1.64.0/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_core-93b52ecae16c128a.rlib" "/nix/store/ljcn8r8fl90yzgbc9ldi7kzal6fdi3p8-rustc-1.64.0/lib/rustlib/x86_64-apple-darwin/lib/libcore-8edafb11271422bf.rlib" "/nix/store/ljcn8r8fl90yzgbc9ldi7kzal6fdi3p8-rustc-1.64.0/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-9442fbd2ac03574e.rlib" "-lSystem" "-lresolv" "-lc" "-lm" "-liconv" "-L" "/nix/store/ljcn8r8fl90yzgbc9ldi7kzal6fdi3p8-rustc-1.64.0/lib/rustlib/x86_64-apple-darwin/lib" "-o" "/private/tmp/nix-build-quick-start-deps-0.1.0.drv-0/dummy-src/target/release/build/quick-start-7b8b70e8291a5325/build_script_6jsv47p94w0w45smdskbdfc3ibbranb3_dummy-7b8b70e8291a5325" "-Wl,-dead_strip" "-nodefaultlibs"
       >   = note: ld: library not found for -liconv
       >           clang-11: error: linker command failed with exit code 1 (use -v to see invocation)
       >           
       >
       > error: could not compile `quick-start` due to previous error
       For full logs, run 'nix log /nix/store/h5ddb2gv7dqjal501p6ndjyvi7xzbiz0-quick-start-deps-0.1.0.drv'.
error: 1 dependencies of derivation '/nix/store/qbh9p6m40s608inxpynxc5mq6c0d0kx9-quick-start-0.1.0.drv' failed to build
zsh: exit 1     nix build

Otherwise great utility just hit this snag when trying to use it on macos nix. Would this be ok to pr roughly as-is?

ipetkov commented 1 year ago

Hi @mitchty thanks for mentioning this! Yes those changes would be great as a PR and much appreciated 🙏 😄

dpc commented 1 year ago

Multiple times I had to add not only iconv, but also darwin.apple_sdk.frameworks.Security BTW.

mitchty commented 1 year ago

Perhaps this is a bit simpler/less redundancy? Would make it pretty easy to add specific inputs such as the macos frameworks too though those tend to crop up when you need them.

I'm not sure if it makes sense to always specify a blank buildInputs to each of the functions but this seems to pass nix flake check which the above didn't. Not sure if the doc/fmt/audit checks need the build inputs too, least they don't seem to when I run it but haven't tried a more complex setup yet.

modified   flake.nix
@@ -24,21 +24,25 @@
           inherit system;
         };

-        inherit (pkgs) lib;
+        inherit (pkgs) lib stdenv;

         craneLib = crane.lib.${system};
         src = craneLib.cleanCargoSource ./.;

+        # If one needs to customize the build environment here is where to add
+        # to it, mostly only needed for macos
+        buildInputs = [] ++ lib.optionals stdenv.isDarwin [ pkgs.libiconv ];
+
         # Build *just* the cargo dependencies, so we can reuse
         # all of that work (e.g. via cachix) when running in CI
         cargoArtifacts = craneLib.buildDepsOnly {
-          inherit src;
+          inherit src buildInputs;
         };

         # Build the actual crate itself, reusing the dependency
         # artifacts from above.
         my-crate = craneLib.buildPackage {
-          inherit cargoArtifacts src;
+          inherit cargoArtifacts src buildInputs;
         };
       in
       {
@@ -53,7 +57,7 @@
           # we can block the CI if there are issues here, but not
           # prevent downstream consumers from building our crate by itself.
           my-crate-clippy = craneLib.cargoClippy {
-            inherit cargoArtifacts src;
+            inherit cargoArtifacts src buildInputs;
             cargoClippyExtraArgs = "--all-targets -- --deny warnings";
           };

@@ -75,7 +79,7 @@
           # Consider setting `doCheck = false` on `my-crate` if you do not want
           # the tests to run twice
           my-crate-nextest = craneLib.cargoNextest {
-            inherit cargoArtifacts src;
+            inherit cargoArtifacts src buildInputs;
             partitions = 1;
             partitionType = "count";
           };
@@ -100,7 +104,7 @@
           nativeBuildInputs = with pkgs; [
             cargo
             rustc
-          ];
+          ] ++ buildInputs;
         };
       });
 }