3 min read

Rust Nix flake for Mac

I have a working Nix flake that I use to have a Rust dev environment for Linux, but doesn't work with Mac. If I run cargo install rustlings I get compile errors like:

  = note: ld: framework not found CoreServices
          clang-16: error: linker command failed with exit code 1 (use -v to see invocation)

and like:

  = note: ld: framework not found CoreServices
          clang-16: error: linker command failed with exit code 1 (use -v to see invocation)

šŸ’” There are some mac-specific (and darwin) libraries that need to be present it would seem.

My working Rust nix.flake

I'm sure it could be leaner (I'm working off my Linux flake that I used for a project):

{
  inputs = {
    flake-utils.url = "github:numtide/flake-utils";
    naersk.url = "github:nix-community/naersk";
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    fenix.url = "github:nix-community/fenix";
  };

  outputs = {
    self,
    flake-utils,
    naersk,
    nixpkgs,
    fenix,
  }:
    flake-utils.lib.eachDefaultSystem (
      system: let
        pkgs = (import nixpkgs) {
          inherit system;
          overlays = [fenix.overlays.default];
        };

        PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig";

        naersk' = pkgs.callPackage naersk {};
      in rec {
        defaultPackage = naersk'.buildPackage {
          src = ./.;
        };

        inherit (pkgs.lib) optionals;
        inherit (pkgs.stdenv) isDarwin;
        inherit (pkgs.darwin.apple_sdk.frameworks);

        devShell = pkgs.mkShell {
          buildInputs = [
            pkgs.openssl
            pkgs.pkg-config
          ] ++ optionals pkgs.stdenv.isDarwin [
            # Additional darwin specific inputs can be set here
            pkgs.libiconv
            pkgs.darwin.apple_sdk.frameworks.SystemConfiguration
          ] ++ (with pkgs.darwin.apple_sdk; [
            frameworks.CoreFoundation
            frameworks.CoreServices
            frameworks.SystemConfiguration
          ])
;
          env = {
            PKG_CONFIG_PATH = PKG_CONFIG_PATH;
          };
          nativeBuildInputs = with pkgs; [
            alejandra
            rust-analyzer
            (pkgs.fenix.stable.withComponents [
              "cargo"
              "clippy"
              "rust-src"
              "rustc"
              "rustfmt"
            ])
            openssl
          ];
        };
      }
    );
}

full flake.nix file that I'm using for mac

The Mac specific lines I had to add were:

        inherit (pkgs.lib) optionals;
        inherit (pkgs.stdenv) isDarwin;
        inherit (pkgs.darwin.apple_sdk.frameworks);
        

mac specific packages

and

++ optionals pkgs.stdenv.isDarwin [
            # Additional darwin specific inputs can be set here
            pkgs.libiconv
            pkgs.darwin.apple_sdk.frameworks.SystemConfiguration
          ] ++ (with pkgs.darwin.apple_sdk; [
            frameworks.CoreFoundation
            frameworks.CoreServices
            frameworks.SystemConfiguration
          ])
        

mac specific libraries

I use direnv so I have a .envrc file in the same folder that simply looks like:

use flake

.envrc

and then run direnv allow

Every time you cd into the folder, it runs the nix.flake and you have your Rust environment!


Some helpful references

Ld: framework not found System
Facing similar issue = note: ld: framework not found SystemConfiguration clang-16: error: linker command failed with exit code 1 (use -v to see invocation) error: could not compile `prisma-cli` (bin ā€œprisma-cliā€) due to 1 previous error
Cannot build rust binary on MacOS M1
Introduction Iā€™m not being able to compile the toml crate on a MacOS M1. The output error below is from nix-shell but I can reproduce this error on native compilation, with xcode installed. Versions XCode: 1.3.0 Rust: 1.64.0 toml: 0.7.3 Extra informations With nix I also add some apple_sdk frameworks: packages.aarch64-darwin = rec { default = pescarte-desafios; pescarte-desafios = pkgs.rustPlatform.buildRustPackage { pname = ā€œpescarte-desafiosā€; versiā€¦