Format & rename some Nix stuff; Remove custom .envrc use nix stuff in favor of stdlib

This commit is contained in:
Jared Ramirez 2021-05-31 15:40:26 -07:00
parent 8d9f324951
commit 966bb80cdc
5 changed files with 132 additions and 316 deletions

155
.envrc
View file

@ -1,154 +1 @@
# Load environment variables from `nix-shell` and export it out. use nix
#
# Usage: use_nix [-s <nix-expression>] [-w <path>] [-w <path>] ...
# -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

View file

@ -466,8 +466,8 @@ fn link_macos(
"-lc++", "-lc++",
// "-lc++abi", // "-lc++abi",
// "-lunwind", // TODO will eventually need this, see https://github.com/rtfeldman/roc/pull/554#discussion_r496370840 // "-lunwind", // TODO will eventually need this, see https://github.com/rtfeldman/roc/pull/554#discussion_r496370840
"-framework", // "-framework", // Uncomment this line & the following ro run the `rand` crate in examples/cli
"Security", // This "-framework Security" arg is needed for the `rand` crate in examples/cli // "Security",
// Output // Output
"-o", "-o",
output_path.to_str().unwrap(), // app output_path.to_str().unwrap(), // app

View file

@ -1,37 +1,37 @@
{ pkgs, isMacOS, isAarch64 }: { pkgs, isDarwin, isAarch64 }:
if isMacOS then if isDarwin then
let let
version = "0.7.1"; version = "0.7.1";
osName = osName =
if isMacOS if isDarwin
then "macos" then "macos"
else "linux"; else "linux";
archName = archName =
if isAarch64 if isAarch64
then "aarch64" then "aarch64"
else "x86_64"; else "x86_64";
archiveName = "zig-${osName}-${archName}-${version}"; archiveName = "zig-${osName}-${archName}-${version}";
sha256 = "845cb17562978af0cf67e3993f4e33330525eaf01ead9386df9105111e3bc519"; sha256 = "845cb17562978af0cf67e3993f4e33330525eaf01ead9386df9105111e3bc519";
in in
pkgs.stdenv.mkDerivation { pkgs.stdenv.mkDerivation {
pname = "zig"; pname = "zig";
version = version; version = version;
buildInputs = [ pkgs.gzip ]; buildInputs = [ pkgs.gzip ];
src = pkgs.fetchurl { src = pkgs.fetchurl {
inherit sha256; inherit sha256;
name = "${archiveName}.tar.xz"; name = "${archiveName}.tar.xz";
url = "https://ziglang.org/download/${version}/${archiveName}.tar.xz"; url = "https://ziglang.org/download/${version}/${archiveName}.tar.xz";
}; };
phases = [ "installPhase" ]; phases = [ "unpackPhase" ];
installPhase = '' unpackPhase = ''
mkdir -p $out/bin mkdir -p $out/bin
tar -xf $src tar -xf $src
cp ${archiveName}/zig $out/zig cp ${archiveName}/zig $out/zig
cp -r ${archiveName}/lib $out/lib cp -r ${archiveName}/lib $out/lib
ln -s "$out/zig" "$out/bin/zig" ln -s "$out/zig" "$out/bin/zig"
chmod +x $out/bin/zig chmod +x $out/bin/zig
''; '';
} }
else else
pkgs.zig pkgs.zig

View file

@ -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
'';
}

209
shell.nix
View file

@ -1,116 +1,113 @@
{}: {}:
let let
splitSystem = builtins.split "-" builtins.currentSystem; # Look here for information about how pin version of nixpkgs
currentArch = builtins.elemAt splitSystem 0; # → https://nixos.wiki/wiki/FAQ/Pinning_Nixpkgs
currentOS = builtins.elemAt splitSystem 2; # TODO: We should probably use flakes at somepoint
in pkgs = import (
with { builtins.fetchGit {
# Look here for information about how pin version of nixpkgs # name = "nixpkgs-2021-04-23";
# → https://nixos.wiki/wiki/FAQ/Pinning_Nixpkgs url = "https://github.com/nixos/nixpkgs/";
pkgs = import ( ref = "refs/heads/nixpkgs-unstable";
builtins.fetchGit { rev = "8d0340aee5caac3807c58ad7fa4ebdbbdd9134d6";
# name = "nixpkgs-2021-04-23"; }
url = "https://github.com/nixos/nixpkgs/"; ) {};
ref = "refs/heads/nixpkgs-unstable";
rev = "8d0340aee5caac3807c58ad7fa4ebdbbdd9134d6";
}
) {};
isMacOS = currentOS == "darwin"; currentArch = builtins.elemAt (builtins.split "-" builtins.currentSystem) 0;
isLinux = currentOS == "linux"; isAarch64 = currentArch == "aarch64";
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 linuxInputs =
darwin-inputs = with pkgs;
if isMacOS then lib.optionals stdenv.isLinux [
with pkgs.darwin.apple_sdk.frameworks; [ valgrind
AppKit vulkan-headers
CoreFoundation vulkan-loader
CoreServices vulkan-tools
CoreVideo vulkan-validation-layers
Foundation xorg.libX11
Metal xorg.libXcursor
Security xorg.libXrandr
] xorg.libXi
else xorg.libxcb
[];
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; })
]; ];
in llvmPkgs = pkgs.llvmPackages_10;
mkShell (
{
buildInputs = inputs ++ darwin-inputs ++ linux-inputs;
# Additional Env vars zig = import ./nix/zig.nix {
LLVM_SYS_100_PREFIX = "${llvmPkgs.llvm}"; pkgs = pkgs;
LD_LIBRARY_PATH = stdenv.lib.makeLibraryPath isDarwin = pkgs.stdenv.isDarwin;
( isAarch64 = isAarch64;
[ };
pkg-config
stdenv.cc.cc.lib
llvmPkgs.libcxx
llvmPkgs.libcxxabi
libunwind
libffi
ncurses
zlib
] ++ linux-inputs
);
# Aliases don't work cross shell, so we do this inputs = with pkgs;[
shellHook = '' # build libraries
export PATH="$PATH:$PWD/nix/bin" 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"
'';
}