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.
#
# 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
use nix

View file

@ -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

View file

@ -1,10 +1,10 @@
{ pkgs, isMacOS, isAarch64 }:
{ pkgs, isDarwin, isAarch64 }:
if isMacOS then
if isDarwin then
let
version = "0.7.1";
osName =
if isMacOS
if isDarwin
then "macos"
else "linux";
archName =
@ -23,8 +23,8 @@ if isMacOS then
name = "${archiveName}.tar.xz";
url = "https://ziglang.org/download/${version}/${archiveName}.tar.xz";
};
phases = [ "installPhase" ];
installPhase = ''
phases = [ "unpackPhase" ];
unpackPhase = ''
mkdir -p $out/bin
tar -xf $src
cp ${archiveName}/zig $out/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
'';
}

View file

@ -1,13 +1,9 @@
{}:
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
# TODO: We should probably use flakes at somepoint
pkgs = import (
builtins.fetchGit {
# name = "nixpkgs-2021-04-23";
@ -17,16 +13,12 @@ in
}
) {};
isMacOS = currentOS == "darwin";
isLinux = currentOS == "linux";
currentArch = builtins.elemAt (builtins.split "-" builtins.currentSystem) 0;
isAarch64 = currentArch == "aarch64";
};
with (pkgs);
let
darwin-inputs =
if isMacOS then
darwinInputs =
with pkgs;
lib.optionals stdenv.isDarwin (
with pkgs.darwin.apple_sdk.frameworks; [
AppKit
CoreFoundation
@ -36,12 +28,11 @@ in
Metal
Security
]
else
[];
);
linux-inputs =
if isLinux then
[
linuxInputs =
with pkgs;
lib.optionals stdenv.isLinux [
valgrind
vulkan-headers
vulkan-loader
@ -52,13 +43,17 @@ in
xorg.libXrandr
xorg.libXi
xorg.libxcb
]
else
[];
];
llvmPkgs = pkgs.llvmPackages_10;
zig = import ./nix/zig.nix { inherit pkgs isMacOS isAarch64; };
inputs = [
zig = import ./nix/zig.nix {
pkgs = pkgs;
isDarwin = pkgs.stdenv.isDarwin;
isAarch64 = isAarch64;
};
inputs = with pkgs;[
# build libraries
rustc
cargo
@ -71,6 +66,7 @@ in
llvmPkgs.clang
pkg-config
zig
# lib deps
llvmPkgs.libcxx
llvmPkgs.libcxxabi
@ -80,21 +76,20 @@ in
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
mkShell (
in
pkgs.mkShell
{
buildInputs = inputs ++ darwin-inputs ++ linux-inputs;
buildInputs = inputs ++ darwinInputs ++ linuxInputs;
# Additional Env vars
LLVM_SYS_100_PREFIX = "${llvmPkgs.llvm}";
LD_LIBRARY_PATH = stdenv.lib.makeLibraryPath
LD_LIBRARY_PATH =
with pkgs;
lib.makeLibraryPath
(
[
pkg-config
@ -105,12 +100,14 @@ in
libffi
ncurses
zlib
] ++ linux-inputs
]
++ linuxInputs
);
# Aliases don't work cross shell, so we do this
# 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"
'';
}
)