diff --git a/.envrc b/.envrc index daafe18d9a..1d953f4bd7 100644 --- a/.envrc +++ b/.envrc @@ -1,154 +1 @@ -# Load environment variables from `nix-shell` and export it out. -# -# Usage: use_nix [-s ] [-w ] [-w ] ... -# -s nix-expression: The nix expression to use for building the shell environment. -# -w path: watch a file for changes. It can be specified multiple times. The -# shell specified with -s is automatically watched. -# -# If no nix-expression were given with -s, it will attempt to find and load -# the shell using the following files in order: shell.nix and default.nix. -# -# Example: -# - use_nix -# - use_nix -s shell.nix -w .nixpkgs-version.json -# -# The dependencies pulled by nix-shell are added to Nix's garbage collector -# roots, such that the environment remains persistent. -# -# Nix-shell is invoked only once per environment, and the output is cached for -# better performance. If any of the watched files change, then the environment -# is rebuilt. -# -# To remove old environments, and allow the GC to collect their dependencies: -# rm -f .direnv -# -use_nix() { - if ! validate_version; then - echo "This .envrc requires direnv version 2.18.2 or above." - exit 1 - fi - - # define all local variables - local shell - local files_to_watch=() - - local opt OPTARG OPTIND # define vars used by getopts locally - while getopts ":n:s:w:" opt; do - case "${opt}" in - s) - shell="${OPTARG}" - files_to_watch=("${files_to_watch[@]}" "${shell}") - ;; - w) - files_to_watch=("${files_to_watch[@]}" "${OPTARG}") - ;; - :) - fail "Invalid option: $OPTARG requires an argument" - ;; - \?) - fail "Invalid option: $OPTARG" - ;; - esac - done - shift $((OPTIND -1)) - - if [[ -z "${shell}" ]]; then - if [[ -f shell.nix ]]; then - shell=shell.nix - files_to_watch=("${files_to_watch[@]}" shell.nix) - elif [[ -f default.nix ]]; then - shell=default.nix - files_to_watch=("${files_to_watch[@]}" default.nix) - else - fail "ERR: no shell was given" - fi - fi - - local f - for f in "${files_to_watch[@]}"; do - if ! [[ -f "${f}" ]]; then - fail "cannot watch file ${f} because it does not exist" - fi - done - - # compute the hash of all the files that makes up the development environment - local env_hash="$(hash_contents "${files_to_watch[@]}")" - - # define the paths - local dir="$(direnv_layout_dir)" - local wd="${dir}/wd-${env_hash}" - local drv="${wd}/env.drv" - local dump="${wd}/dump.env" - - # Generate the environment if we do not have one generated already. - if [[ ! -f "${drv}" ]]; then - mkdir -p "${wd}" - - log_status "use nix: deriving new environment" - IN_NIX_SHELL=1 nix-instantiate --add-root "${drv}" --indirect "${shell}" > /dev/null - nix-store -r $(nix-store --query --references "${drv}") --add-root "${wd}/dep" --indirect > /dev/null - if [[ "${?}" -ne 0 ]] || [[ ! -f "${drv}" ]]; then - rm -rf "${wd}" - fail "use nix: was not able to derive the new environment. Please run 'direnv reload' to try again." - fi - - log_status "use nix: updating cache" - nix-shell "${drv}" --show-trace --run "$(join_args "$direnv" dump bash)" > "${dump}" - if [[ "${?}" -ne 0 ]] || [[ ! -f "${dump}" ]] || ! grep -q IN_NIX_SHELL "${dump}"; then - rm -rf "${wd}" - fail "use nix: was not able to update the cache of the environment. Please run 'direnv reload' to try again." - fi - fi - - # evaluate the dump created by nix-shell earlier, but have to merge the PATH - # with the current PATH - # NOTE: we eval the dump here as opposed to direnv_load it because we don't - # want to persist environment variables coming from the shell at the time of - # the dump. See https://github.com/direnv/direnv/issues/405 for context. - local path_backup="${PATH}" - eval $(cat "${dump}") - export PATH="${PATH}:${path_backup}" - - # cleanup the environment of variables that are not requried, or are causing issues. - unset shellHook # when shellHook is present, then any nix-shell'd script will execute it! - - # watch all the files we were asked to watch for the environment - for f in "${files_to_watch[@]}"; do - watch_file "${f}" - done -} - -fail() { - log_error "${@}" - exit 1 -} - -hash_contents() { - if has md5sum; then - cat "${@}" | md5sum | cut -c -32 - elif has md5; then - cat "${@}" | md5 -q - fi -} - -hash_file() { - if has md5sum; then - md5sum "${@}" | cut -c -32 - elif has md5; then - md5 -q "${@}" - fi -} - -validate_version() { - local version="$("${direnv}" version)" - local major="$(echo "${version}" | cut -d. -f1)" - local minor="$(echo "${version}" | cut -d. -f2)" - local patch="$(echo "${version}" | cut -d. -f3)" - - if [[ "${major}" -gt 2 ]]; then return 0; fi - if [[ "${major}" -eq 2 ]] && [[ "${minor}" -gt 18 ]]; then return 0; fi - if [[ "${major}" -eq 2 ]] && [[ "${minor}" -eq 18 ]] && [[ "${patch}" -ge 2 ]]; then return 0; fi - return 1 -} - -use_nix -s shell.nix +use nix diff --git a/compiler/build/src/link.rs b/compiler/build/src/link.rs index 95983e1a8d..c535f86e57 100644 --- a/compiler/build/src/link.rs +++ b/compiler/build/src/link.rs @@ -466,8 +466,8 @@ fn link_macos( "-lc++", // "-lc++abi", // "-lunwind", // TODO will eventually need this, see https://github.com/rtfeldman/roc/pull/554#discussion_r496370840 - "-framework", - "Security", // This "-framework Security" arg is needed for the `rand` crate in examples/cli + // "-framework", // Uncomment this line & the following ro run the `rand` crate in examples/cli + // "Security", // Output "-o", output_path.to_str().unwrap(), // app diff --git a/nix/zig.nix b/nix/zig.nix index 6f5f49bd18..ae8708625d 100644 --- a/nix/zig.nix +++ b/nix/zig.nix @@ -1,37 +1,37 @@ -{ pkgs, isMacOS, isAarch64 }: +{ pkgs, isDarwin, isAarch64 }: -if isMacOS then +if isDarwin then let version = "0.7.1"; osName = - if isMacOS - then "macos" - else "linux"; + if isDarwin + then "macos" + else "linux"; archName = if isAarch64 - then "aarch64" - else "x86_64"; + then "aarch64" + else "x86_64"; archiveName = "zig-${osName}-${archName}-${version}"; sha256 = "845cb17562978af0cf67e3993f4e33330525eaf01ead9386df9105111e3bc519"; in - pkgs.stdenv.mkDerivation { - pname = "zig"; - version = version; - buildInputs = [ pkgs.gzip ]; - src = pkgs.fetchurl { - inherit sha256; - name = "${archiveName}.tar.xz"; - url = "https://ziglang.org/download/${version}/${archiveName}.tar.xz"; - }; - phases = [ "installPhase" ]; - installPhase = '' - mkdir -p $out/bin - tar -xf $src - cp ${archiveName}/zig $out/zig - cp -r ${archiveName}/lib $out/lib - ln -s "$out/zig" "$out/bin/zig" - chmod +x $out/bin/zig - ''; - } + pkgs.stdenv.mkDerivation { + pname = "zig"; + version = version; + buildInputs = [ pkgs.gzip ]; + src = pkgs.fetchurl { + inherit sha256; + name = "${archiveName}.tar.xz"; + url = "https://ziglang.org/download/${version}/${archiveName}.tar.xz"; + }; + phases = [ "unpackPhase" ]; + unpackPhase = '' + mkdir -p $out/bin + tar -xf $src + cp ${archiveName}/zig $out/zig + cp -r ${archiveName}/lib $out/lib + ln -s "$out/zig" "$out/bin/zig" + chmod +x $out/bin/zig + ''; + } else pkgs.zig diff --git a/nix/zls.nix b/nix/zls.nix deleted file mode 100644 index 29f0feb033..0000000000 --- a/nix/zls.nix +++ /dev/null @@ -1,28 +0,0 @@ -{ pkgs, zig }: - -# As of 2020-10-25, building zls is not available on Nix. For some reason, -# this hangs on `zig build`. I'll try to figure it our later. - -let - rev = "e8c20351d85da8eb4bf22480045b994007284d69"; -in -pkgs.stdenv.mkDerivation { - pname = "zig-language-server"; - version = rev; - src = pkgs.fetchgit { - inherit rev; - fetchSubmodules = true; - url = "https://github.com/zigtools/zls.git"; - sha256 = "06g8gml1g0fmvcfysy93bd1hb64vjd2v12x3kgxz58kmk5z0168y"; - }; - phases = [ "buildPhase" "installPhase" ]; - buildInputs = [ zig ]; - buildPhase = '' - zig build - ''; - installPhase = '' - mkdir -p $out/bin - cp ./zig-cache/bin/zls $out/bin/zls - chmod +x $out/bin/zls - ''; -} diff --git a/shell.nix b/shell.nix index 7b7d61fd55..ddb786a8b3 100644 --- a/shell.nix +++ b/shell.nix @@ -1,116 +1,113 @@ {}: let - splitSystem = builtins.split "-" builtins.currentSystem; - currentArch = builtins.elemAt splitSystem 0; - currentOS = builtins.elemAt splitSystem 2; -in - with { - # Look here for information about how pin version of nixpkgs - # → https://nixos.wiki/wiki/FAQ/Pinning_Nixpkgs - pkgs = import ( - builtins.fetchGit { - # name = "nixpkgs-2021-04-23"; - url = "https://github.com/nixos/nixpkgs/"; - ref = "refs/heads/nixpkgs-unstable"; - rev = "8d0340aee5caac3807c58ad7fa4ebdbbdd9134d6"; - } - ) {}; + # Look here for information about how pin version of nixpkgs + # → https://nixos.wiki/wiki/FAQ/Pinning_Nixpkgs + # TODO: We should probably use flakes at somepoint + pkgs = import ( + builtins.fetchGit { + # name = "nixpkgs-2021-04-23"; + url = "https://github.com/nixos/nixpkgs/"; + ref = "refs/heads/nixpkgs-unstable"; + rev = "8d0340aee5caac3807c58ad7fa4ebdbbdd9134d6"; + } + ) {}; - isMacOS = currentOS == "darwin"; - isLinux = currentOS == "linux"; - isAarch64 = currentArch == "aarch64"; - }; + currentArch = builtins.elemAt (builtins.split "-" builtins.currentSystem) 0; + isAarch64 = currentArch == "aarch64"; - with (pkgs); + darwinInputs = + with pkgs; + lib.optionals stdenv.isDarwin ( + with pkgs.darwin.apple_sdk.frameworks; [ + AppKit + CoreFoundation + CoreServices + CoreVideo + Foundation + Metal + Security + ] + ); - let - darwin-inputs = - if isMacOS then - with pkgs.darwin.apple_sdk.frameworks; [ - AppKit - CoreFoundation - CoreServices - CoreVideo - Foundation - Metal - Security - ] - else - []; - - linux-inputs = - if isLinux then - [ - valgrind - vulkan-headers - vulkan-loader - vulkan-tools - vulkan-validation-layers - xorg.libX11 - xorg.libXcursor - xorg.libXrandr - xorg.libXi - xorg.libxcb - ] - else - []; - - llvmPkgs = pkgs.llvmPackages_10; - zig = import ./nix/zig.nix { inherit pkgs isMacOS isAarch64; }; - inputs = [ - # build libraries - rustc - cargo - clippy - rustfmt - cmake - git - python3 - llvmPkgs.llvm - llvmPkgs.clang - pkg-config - zig - # lib deps - llvmPkgs.libcxx - llvmPkgs.libcxxabi - libffi - libunwind - libxml2 - ncurses - zlib - libiconv - # faster builds - see https://github.com/rtfeldman/roc/blob/trunk/BUILDING_FROM_SOURCE.md#use-lld-for-the-linker - llvmPkgs.lld - # dev tools - # rust-analyzer - # (import ./nix/zls.nix { inherit pkgs zig; }) + linuxInputs = + with pkgs; + lib.optionals stdenv.isLinux [ + valgrind + vulkan-headers + vulkan-loader + vulkan-tools + vulkan-validation-layers + xorg.libX11 + xorg.libXcursor + xorg.libXrandr + xorg.libXi + xorg.libxcb ]; - in - mkShell ( - { - buildInputs = inputs ++ darwin-inputs ++ linux-inputs; + llvmPkgs = pkgs.llvmPackages_10; - # Additional Env vars - LLVM_SYS_100_PREFIX = "${llvmPkgs.llvm}"; - LD_LIBRARY_PATH = stdenv.lib.makeLibraryPath - ( - [ - pkg-config - stdenv.cc.cc.lib - llvmPkgs.libcxx - llvmPkgs.libcxxabi - libunwind - libffi - ncurses - zlib - ] ++ linux-inputs - ); + zig = import ./nix/zig.nix { + pkgs = pkgs; + isDarwin = pkgs.stdenv.isDarwin; + isAarch64 = isAarch64; + }; - # Aliases don't work cross shell, so we do this - shellHook = '' - export PATH="$PATH:$PWD/nix/bin" - ''; - } - ) + inputs = with pkgs;[ + # build libraries + rustc + cargo + clippy + rustfmt + cmake + git + python3 + llvmPkgs.llvm + llvmPkgs.clang + pkg-config + zig + + # lib deps + llvmPkgs.libcxx + llvmPkgs.libcxxabi + libffi + libunwind + libxml2 + ncurses + zlib + libiconv + + # faster builds - see https://github.com/rtfeldman/roc/blob/trunk/BUILDING_FROM_SOURCE.md#use-lld-for-the-linker + llvmPkgs.lld + ]; +in +pkgs.mkShell + { + buildInputs = inputs ++ darwinInputs ++ linuxInputs; + + # Additional Env vars + LLVM_SYS_100_PREFIX = "${llvmPkgs.llvm}"; + LD_LIBRARY_PATH = + with pkgs; + lib.makeLibraryPath + ( + [ + pkg-config + stdenv.cc.cc.lib + llvmPkgs.libcxx + llvmPkgs.libcxxabi + libunwind + libffi + ncurses + zlib + ] + ++ linuxInputs + ); + + # Non Nix llvm installs names the bin llvm-as-${version}, so we + # alias `llvm` to `llvm-as-${version}` here. + # This the name of the file in nix/bin will need to be updated whenever llvm is updated + shellHook = '' + export PATH="$PATH:$PWD/nix/bin" + ''; + }