.envrc
used by direnv to automatically load the environment using Nix. If you have Nix installed but not direnv, then simply running nix-shell
should give you the environment described in shell.nix
.
Last active
November 22, 2018 05:54
-
-
Save kalbasit/7b536f7d2688bd0d5d0246740989d72d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage: use_nix [...] | |
# | |
# Load environment variables from `nix-shell`. | |
# If you have a `default.nix` or `shell.nix` one of these will be used and | |
# the derived environment will be stored at ./.direnv/env-<hash> | |
# and symlink to it will be created at ./.direnv/default. | |
# Dependencies are added to the GC roots, such that the environment remains persistent. | |
# | |
# Packages can also be specified directly via e.g `use nix -p ocaml`, | |
# however those will not be added to the GC roots. | |
# | |
# The resulting environment is cached for better performance. | |
# | |
# To trigger switch to a different environment: | |
# `rm -f .direnv/default` | |
# | |
# To derive a new environment: | |
# `rm -rf .direnv/env-$(md5sum {shell,default}.nix 2> /dev/null | cut -c -32)` | |
# | |
# To remove cache: | |
# `rm -f .direnv/dump-*` | |
# | |
# To remove all environments: | |
# `rm -rf .direnv/env-*` | |
# | |
# To remove only old environments: | |
# `find .direnv -name 'env-*' -and -not -name `readlink .direnv/default` -exec rm -rf {} +` | |
# | |
use_nix() { | |
set -e | |
local shell="shell.nix" | |
if [[ ! -f "${shell}" ]]; then | |
shell="default.nix" | |
fi | |
local nixpkgs_version=".nixpkgs-version.json" | |
if [[ ! -f "${nixpkgs_version}" ]]; then | |
fail "use nix: .nixpkgs-version.json not found in the folder" | |
fi | |
if [[ ! -f "${shell}" ]]; then | |
fail "use nix: shell.nix or default.nix not found in the folder" | |
fi | |
local dir="$(direnv_layout_dir)" | |
local default="${dir}/default" | |
if [[ ! -L "${default}" ]] || [[ ! -d $(readlink "${default}") ]]; then | |
local wd="${dir}/env-$(hashFile "${shell}")-$(hashFile "${nixpkgs_version}")" | |
mkdir -p "${wd}" | |
local drv="${wd}/env.drv" | |
if [[ ! -f "${drv}" ]]; then | |
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 | |
fi | |
rm -f "${default}" | |
ln -s $(basename "${wd}") "${default}" | |
fi | |
local drv=$(readlink "${default}/env.drv") | |
local dump="${dir}/dump-$(hashFile ".envrc")-$(hashFile ${drv})" | |
if [[ ! -f "${dump}" ]] || [[ "${XDG_CONFIG_DIR}/direnv/direnvrc" -nt "${dump}" ]]; then | |
log_status "use nix: updating cache" | |
old=$(find "${dir}" -name 'dump-*') | |
nix-shell "${drv}" --show-trace "$@" --run "$(join_args "$direnv" dump)" > "${dump}" | |
rm -f ${old} | |
fi | |
direnv_load cat "${dump}" | |
watch_file "${default}" | |
watch_file "${nixpkgs_version}" | |
watch_file "${shell}" | |
} | |
hashFile() { | |
if has md5sum; then | |
md5sum "${@}" | cut -c -32 | |
elif has md5; then | |
md5 -q "${@}" | |
fi | |
} | |
fail() { | |
log_error "${@}" | |
exit 1 | |
} | |
use_nix |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"url": "https://github.com/NixOS/nixpkgs-channels.git", | |
"rev": "45a419ab5a23c93421c18f3d9cde015ded22e712", | |
"date": "2018-10-16T10:33:58+01:00", | |
"sha256": "00mpq5p351xsk0p682xjggw17qgd079i45yj0aa6awawpckfx37s", | |
"fetchSubmodules": false | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let | |
# Look here for information about how to generate `nixpkgs-version.json`. | |
# → https://nixos.wiki/wiki/FAQ/Pinning_Nixpkgs | |
pinnedVersion = builtins.fromJSON (builtins.readFile ./.nixpkgs-version.json); | |
pinnedPkgs = import (builtins.fetchGit { | |
inherit (pinnedVersion) url rev; | |
ref = "nixos-unstable"; | |
}) {}; | |
in | |
# This allows overriding pkgs by passing `--arg pkgs ...` | |
{ pkgs ? pinnedPkgs }: | |
with pkgs; | |
mkShell { | |
buildInputs = [ | |
(texlive.combine { | |
inherit (texlive) scheme-medium ucs import moderncv; | |
}) | |
git | |
gnumake | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment