summaryrefslogtreecommitdiff
path: root/Working with Nix shells.md
blob: c042edd5d2dd32235cba005e39f57883cc8bb420 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Basic invocation: `nix-shell -p [ pkgs ... ]`

# Shebang
```
#! /usr/bin/env nix-shell
#! nix-shell -i bash -p bash

do stuff
```

Different interpreters can be set with the `-i` option such as python
```
#! /usr/bin/env nix-shell
#! nix-shell -i python3 -p python3

print("hello world")
```

The `-I` option can be used to pin nixpkgs
```
#! /usr/bin/env nix-shell
#! nix-shell -i bash
#! nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/aed4b19d312525ae7ca9bceb4e1efe3357d0e2eb.tar.gz

echo hello world
```
# Shell.nix
Can be used to set up per dir environments, e.g using direnv to automatically activate them.
## Python
https://nixos.org/manual/nixpkgs/stable/#python

```nix
{ pkgs ? import <nixpkgs> {}}:

pkgs.mkShell {
    packages = [ pkgs.virtualenv ];
}
```

```nix
with import <nixpkgs> {};
let
	my_toolz = python311.pkgs.buildPythonPackage rec { 
		pname = "toolz";
		version = "0.10.0";
		pyproject = true;
		src = fetchPypi {
			inherit pname version;
			hash = "sha256-CP3V73yWSArRHBLUct4hrNMjWZlvaaUlkpm1QP66RWA=";
		};
		nativeBuildInputs = [
			python311.pkgs.setuptools
			python311.pkgs.wheel
		]; # has no tests
		doCheck = false;
		meta = {
			homepage = "https://github.com/pytoolz/toolz/";
			description = "List processing tools and functional utilities";
			# [...]
		};
	};
in python311.withPackages (ps: with ps; [ numpy my_toolz ]) ).env
```

					The [[Import]] is required here because imports a nix expression from another source, in this case nixpkgs.
It is not required at line two, because python310Packages itself is already in the local scope.