Skip to content

Commit

Permalink
posts/working-with-nix-shells.md
Browse files Browse the repository at this point in the history
fixes #3
  • Loading branch information
justinwoo committed Sep 1, 2019
1 parent baff986 commit f75bc41
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions posts/working-with-nix-shells.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Working with Nix Shell

Nix shells are quite useful for a number of different reasons, but the main reason is that it can provide you programs you want at specific versions.

## Shells with `-p`

Using `nix-shell` with `-p` can get you quickly set up with a shell with packages from NixPkgs installed.

```bash
# from your top level
$ nix-shell -p hello

# nix shell with hello installed
$ which hello
/nix/store/some-hash-hello-2.10/bin/hello
$ hello
Hello, world!
$ exit
exit

# back to your top level shell
```

### Expressions as arguments

But instead of only top level attributes, you can also put entire expressions in the `-p` argument. For example, with the [easy-purescript-nix](https://github.com/justinwoo/easy-purescript-nix/) project:

```bash
$ nix-shell \
-p 'let ep = import ./default.nix {}; in [ ep.purs ep.spago ]' \
--run 'which purs; which spago; purs --version; spago version'
/nix/store/some-hash-purescript/bin/purs
/nix/store/some-hash-spago/bin/spago
0.13.3
0.9.0.0
```

As mentioned before, we can use Nix shells to get specific versions of packages.

```bash
$ nix-shell -p '(import ./default.nix {}).purs' --run 'which purs; purs --version'
/nix/store/some-hash-purescript/bin/purs
0.13.3
```

```bash
$ nix-shell -p '(import ./default.nix {}).purs-0_13_0' --run 'which purs; purs --version'
/nix/store/some-hash-purescript/bin/purs
0.13.0
```

## NixPkgs mkShell

To work with Nix shells in some organized manner, you probably will want to use a source-controlled file defining a derivation. To make this concise and explicit, you can use `mkShell` from NixPkgs.

```nix
# shell.nix
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
buildInputs = [ pkgs.hello ];
}
```

And we can see this used like so:

```nix
$ nix-shell --run 'which hello; hello'
/nix/store/some-hash-hello-2.10/bin/hello
Hello, world!
```

## Links

* Nix manual nix-shell: <https://nixos.org/nix/manual/#sec-nix-shell>
* NixPkgs manual mkShell: <https://nixos.org/nixpkgs/manual/#sec-pkgs-mkShell>

0 comments on commit f75bc41

Please sign in to comment.