summaryrefslogtreecommitdiff
path: root/3 resources/nix.md
blob: d407d2e0ba3b00e5174a1e14023a13d2fc5afe8d (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
# Language
 `nix repl` to interactively evaluate Nix expressions. `:p` if output is not full.
 `nix-instantiate --eval <file>.nix` to evaluate a Nix expression from a file. `--strict`.

Nix is like JSON, but with functions. 
Recursive attribute sets can reference values declared earlier in the same set. 
```nix
rec {
  one = 1;
  two = one + 1;
}
```
 
A `let` binding is used to assign names to values just as attribute sets, they can then be used in expressions. Let bindings have a local scope.

A `with` allows referencing attributes of attribute sets without referencing the set.
# Flakes
Nix flakes are source trees containing a file `flake.nix` at their root. The file `flake.nix` provides a standardized way to provide [[Zettelkast/Index/Nix]] artifacts. It's like a package manager for [[Zettelkast/Index/Nix]]. A flake can be dependent on other Flakes and it's possible to pin dependencies to exact revisions by using a `flake.lock` file.

Nix flake evaluation is hermetic, meaning that it produces the same result wherever it's built.k

The feature can be enabled in `~/.config/nix/nix.conf`:

```
experimental-features = nix-command flakes
```

To initialize in a repo: `nix flake init`.

In flakes dependencies have to be specified explicitly and MUST be locked to specific versions therefore it's no longer allowed to use the nixpkgs found in `NIX_PATH` by referencing it like `<nixpkgs>`.

Output of a Flake is an arbitrary [[Zettelkast/Index/Nix]] value such as a package, [[NixOS]] module or library function.
Commands `nix build` and `nix shell` will build the output `packages.<system>.default` unless we specify another output, for example: `nix shell .#checks.aarch64-linux.build`.

# Overlays
[[TODO]]

# 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
```

---
[Flakes Wiki](https://nixos.wiki/wiki/Flakes)