|
1 |
| -#################################### |
2 |
| -# Environment setup for Nix shells # |
3 |
| -#################################### |
4 |
| - |
5 |
| -# From https://github.com/direnv/direnv/wiki/Nix#persistent-cached-shell |
6 |
| -# |
7 |
| -# Usage: use_nix [...] |
8 |
| -# |
9 |
| -# Load environment variables from `nix-shell`. |
10 |
| -# If you have a `default.nix` or `shell.nix` one of these will be used and |
11 |
| -# the derived environment will be stored at ./.direnv/env-<hash> |
12 |
| -# and symlink to it will be created at ./.direnv/default. |
13 |
| -# Dependencies are added to the GC roots, such that the environment remains persistent. |
14 |
| -# |
15 |
| -# Packages can also be specified directly via e.g `use nix -p ocaml`, |
16 |
| -# however those will not be added to the GC roots. |
17 |
| -# |
18 |
| -# The resulting environment is cached for better performance. |
19 |
| -# |
20 |
| -# To trigger switch to a different environment: |
21 |
| -# `rm -f .direnv/default` |
22 |
| -# |
23 |
| -# To derive a new environment: |
24 |
| -# `rm -rf .direnv/env-$(md5sum {shell,default}.nix 2> /dev/null | cut -c -32)` |
25 |
| -# |
26 |
| -# To remove cache: |
27 |
| -# `rm -f .direnv/dump-*` |
28 |
| -# |
29 |
| -# To remove all environments: |
30 |
| -# `rm -rf .direnv/env-*` |
31 |
| -# |
32 |
| -# To remove only old environments: |
33 |
| -# `find .direnv -name 'env-*' -and -not -name `readlink .direnv/default` -exec rm -rf {} +` |
34 |
| -# |
35 |
| -use_nix() { |
36 |
| - set -e |
37 |
| - |
38 |
| - local shell="shell.nix" |
39 |
| - if [[ ! -f "${shell}" ]]; then |
40 |
| - shell="default.nix" |
41 |
| - fi |
42 |
| - |
43 |
| - if [[ ! -f "${shell}" ]]; then |
44 |
| - fail "use nix: shell.nix or default.nix not found in the folder" |
45 |
| - fi |
46 |
| - |
47 |
| - local dir="${PWD}"/.direnv |
48 |
| - local default="${dir}/default" |
49 |
| - if [[ ! -L "${default}" ]] || [[ ! -d `readlink "${default}"` ]]; then |
50 |
| - local wd="${dir}/env-`md5sum "${shell}" | cut -c -32`" # TODO: Hash also the nixpkgs version? |
51 |
| - mkdir -p "${wd}" |
52 |
| - |
53 |
| - local drv="${wd}/env.drv" |
54 |
| - if [[ ! -f "${drv}" ]]; then |
55 |
| - log_status "use nix: deriving new environment" |
56 |
| - IN_NIX_SHELL=1 nix-instantiate --add-root "${drv}" --indirect "${shell}" > /dev/null |
57 |
| - nix-store -r `nix-store --query --references "${drv}"` --add-root "${wd}/dep" --indirect > /dev/null |
58 |
| - fi |
59 |
| - |
60 |
| - rm -f "${default}" |
61 |
| - ln -s `basename "${wd}"` "${default}" |
62 |
| - fi |
63 |
| - |
64 |
| - local drv=`readlink -f "${default}/env.drv"` |
65 |
| - local dump="${dir}/dump-`md5sum ".envrc" | cut -c -32`-`md5sum ${drv} | cut -c -32`" |
66 |
| - |
67 |
| - if [[ ! -f "${dump}" ]] || [[ "${XDG_CONFIG_DIR}/direnv/direnvrc" -nt "${dump}" ]]; then |
68 |
| - log_status "use nix: updating cache" |
69 |
| - |
70 |
| - old=`find "${dir}" -name 'dump-*'` |
71 |
| - nix-shell "${drv}" --show-trace "$@" --run 'direnv dump' > "${dump}" |
72 |
| - rm -f ${old} |
73 |
| - fi |
74 |
| - |
75 |
| - direnv_load cat "${dump}" |
76 |
| - |
77 |
| - watch_file "${default}" |
78 |
| - watch_file shell.nix |
79 |
| - if [[ ${shell} == "default.nix" ]]; then |
80 |
| - watch_file default.nix |
81 |
| - fi |
82 |
| -} |
83 |
| - |
84 | 1 | use nix
|
0 commit comments