Skip to content
This repository was archived by the owner on Apr 29, 2024. It is now read-only.

Commit 106734e

Browse files
committed
docs: part 3, using direnv
Signed-off-by: Austin Seipp <aseipp@pobox.com> Change-Id: I7eb780963cc13ca9bab44318d9690104
1 parent 20b650a commit 106734e

File tree

3 files changed

+159
-1
lines changed

3 files changed

+159
-1
lines changed

docs/01-USE-THIS-REPO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,4 @@ well...
181181

182182
## Next page
183183

184-
Not Invented Here.
184+
Next up: **[02 &mdash; Install `direnv`](./02-INSTALL-DIRENV.md)**

docs/02-INSTALL-DIRENV.md

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# 02 &mdash; Install `direnv`
2+
3+
Have you ever used a tool like `pip`'s `bin/activate` script, or `rbenv`? These
4+
tools populate your shell environment with the right tools and scripts and
5+
dependencies (e.g. `PYTHONPATH`) to run your software.
6+
7+
What if I told you there was a magical tool that worked like that, and could do
8+
it for arbitrary languages and tools?
9+
10+
That tool is called **[direnv](https://direnv.net)**.
11+
12+
## Install direnv and use it in your shell
13+
14+
First, install `direnv`:
15+
16+
```
17+
$ nix profile install nixpkgs#direnv
18+
```
19+
20+
```
21+
$ which direnv
22+
/home/austin/.nix-profile/bin/direnv
23+
```
24+
25+
Now, you need to activate it in your shell by hooking into it. If you're using
26+
**Bash**, try putting this in your `.bashrc` and starting up a new interactive
27+
shell:
28+
29+
```
30+
eval "$(direnv hook bash)"
31+
```
32+
33+
Not using bash? Check the
34+
[direnv hook documentation](https://direnv.net/docs/hook.html) for more.
35+
36+
## Set up `nix-postgres`
37+
38+
Let's go back to the `nix-postgres` source code.
39+
40+
```
41+
cd $HOME/tmp-nix-postgres
42+
```
43+
44+
Now, normally, direnv is going to look for a file called `.envrc` and load that
45+
if it exists. But to be polite, we don't do that by default; we keep a file
46+
named `.envrc.recommended` in the repository instead, and encourage people to do
47+
this:
48+
49+
```
50+
echo "source_env .envrc.recommended" >> .envrc
51+
```
52+
53+
All this says is "Load the code from `.envrc.recommended` directly", just like a
54+
normal bash script using `source`. The idea of this pattern is to allow users to
55+
have their own customized `.envrc` and piggyback on the committed code for
56+
utility &mdash; and `.envrc` is `.gitignore`'d, so you can put e.g. secret
57+
tokens inside without fear of committing them.
58+
59+
Run the above command, and then...
60+
61+
## What just happened?
62+
63+
Oops, a big red error appeared?
64+
65+
```
66+
$ echo "source_env .envrc.recommended" >> .envrc
67+
direnv: error /home/austin/work/nix-postgres/.envrc is blocked. Run `direnv allow` to approve its content
68+
```
69+
70+
What happened? By default, as a security measure, `direnv` _does not_ load or
71+
execute any code from an `.envrc` file, and instead it MUST be allowed
72+
explicitly.
73+
74+
## `direnv allow`
75+
76+
Our `.envrc.recommended` file will integrate with Nix directly. So run
77+
`direnv allow`, and you'll suddenly see the following:
78+
79+
```
80+
$ direnv allow
81+
direnv: loading ~/work/nix-postgres/.envrc
82+
direnv: loading ~/work/nix-postgres/.envrc.recommended
83+
direnv: loading https://raw.githubusercontent.com/nix-community/nix-direnv/2.3.0/direnvrc (sha256-Dmd+j63L84wuzgyjITIfSxSD57Tx7v51DMxVZOsiUD8=)
84+
direnv: using flake
85+
direnv: nix-direnv: renewed cache
86+
direnv: export +AR +AS +CC +CONFIG_SHELL +CXX +DETERMINISTIC_BUILD +HOST_PATH +IN_NIX_SHELL +LD +NIX_BINTOOLS +NIX_BINTOOLS_WRAPPER_TARGET_HOST_x86_64_unknown_linux_gnu +NIX_BUILD_CORES +NIX_CC +NIX_CC_WRAPPER_TARGET_HOST_x86_64_unknown_linux_gnu +NIX_CFLAGS_COMPILE +NIX_ENFORCE_NO_NATIVE +NIX_HARDENING_ENABLE +NIX_LDFLAGS +NIX_STORE +NM +OBJCOPY +OBJDUMP +PYTHONHASHSEED +PYTHONNOUSERSITE +PYTHONPATH +RANLIB +READELF +SIZE +SOURCE_DATE_EPOCH +STRINGS +STRIP +_PYTHON_HOST_PLATFORM +_PYTHON_SYSCONFIGDATA_NAME +__structuredAttrs +buildInputs +buildPhase +builder +cmakeFlags +configureFlags +depsBuildBuild +depsBuildBuildPropagated +depsBuildTarget +depsBuildTargetPropagated +depsHostHost +depsHostHostPropagated +depsTargetTarget +depsTargetTargetPropagated +doCheck +doInstallCheck +dontAddDisableDepTrack +mesonFlags +name +nativeBuildInputs +out +outputs +patches +phases +preferLocalBuild +propagatedBuildInputs +propagatedNativeBuildInputs +shell +shellHook +stdenv +strictDeps +system ~PATH ~XDG_DATA_DIRS
87+
```
88+
89+
What just happened is that we populated the ambient shell environment with tools
90+
specified inside of `flake.nix` &mdash; we'll cover Flakes later. But for now,
91+
your tools are provisioned!
92+
93+
## Run `just` to build the code
94+
95+
Previously, we needed to run `nix develop` to get access to the `just` command.
96+
No longer! It's now available in your shell:
97+
98+
```
99+
$ which just
100+
/nix/store/1di6nb4qsv86907l3xarw4llzdss2g89-just-1.14.0/bin/just
101+
```
102+
103+
```
104+
$ just b
105+
...
106+
```
107+
108+
Viola, done!
109+
110+
## Go back `$HOME` and return again
111+
112+
When you `cd ~`, direnv will automatically unload the `.envrc` file for you:
113+
114+
```
115+
$ cd ~
116+
direnv: unloading
117+
```
118+
119+
Now, `just` is no longer available:
120+
121+
```
122+
$ just build-all
123+
just: command not found
124+
```
125+
126+
And if you go back, it is!
127+
128+
```
129+
$ cd $HOME/nix-postgres
130+
direnv: loading ~/work/nix-postgres/.envrc
131+
direnv: loading ~/work/nix-postgres/.envrc.recommended
132+
direnv: loading https://raw.githubusercontent.com/nix-community/nix-direnv/2.3.0/direnvrc (sha256-Dmd+j63L84wuzgyjITIfSxSD57Tx7v51DMxVZOsiUD8=)
133+
direnv: using flake
134+
...
135+
```
136+
137+
```
138+
$ just build-all
139+
...
140+
```
141+
142+
## The power of `direnv`
143+
144+
`direnv` with Nix is a frighteningly good development combination for many
145+
purposes. This is its main power: you can use it to create on-demand developer
146+
shells for any language, tool, or environment, and all you need to do is `cd` to
147+
the right directory.
148+
149+
This is the power of `direnv`: your projects always, on demand, will have the
150+
right tools configured and available, no matter if you last worked on them a day
151+
ago or a year ago, or it was done by your teammate, or you have a brand new
152+
computer that you've never programmed on.
153+
154+
## Next page
155+
156+
Not Invented Here.

docs/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ probably other stuff to help educate you.
1010
and play around with it a little.
1111
- **[01 &mdash; Use this Repository](./01-USE-THIS-REPO.md)** &mdash; We'll
1212
build a PostreSQL distribution, and also use the magical `nix develop`
13+
- **[02 &mdash; Install `direnv`](./02-INSTALL-DIRENV.md)** &mdash; Using
14+
`direnv`, you can activate your development shell "transparently" on demand.

0 commit comments

Comments
 (0)