Skip to content
This repository was archived by the owner on Aug 17, 2023. It is now read-only.

Commit 5b80d53

Browse files
sevanspowellrvl
authored andcommitted
Add documentation for Flakes
- Add some beginning resources for Nix Flakes.
1 parent 744f72c commit 5b80d53

File tree

7 files changed

+472
-0
lines changed

7 files changed

+472
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
title: Why Flakes?
3+
date: 2021-02-10
4+
---
5+
6+
Nix Flakes are a new feature of the "nix package manager". They unify the typical ways of declaring inputs and ouputs of a project under a common syntax.
7+
8+
But why use them?
9+
10+
## Why?
11+
12+
- Unify the management of inputs (make redundant tools like `niv`).
13+
- Remove sources of impurity that existed in the previous model.
14+
- Standardize the output format of repositories.
15+
- A Nix expression typically creates a "package" for a repository, for example, the cardano-wallet expression creates a binary package which is available at "cardano-wallet". The location of the attribute in the output attribute set is completely arbitrary. Another repository might put their "package" at "packages.cardano-doohickey".
16+
- Nix flakes attempt to standardize the typical output locations of a Nix expression, so that packages are always available at the same location.
17+
18+
## Concepts
19+
20+
From an abstract point of view, a derivation in Nix is a function from inputs to ouputs. Inputs are typically libraries and executables, and the output is typically another library or executable.
21+
22+
Flakes extend the input/output model of derivations to the entire repository. A single file sits at the root of the repository called `flake.nix`, that might look something like this:
23+
24+
```
25+
{
26+
description = "Example flake";
27+
28+
inputs = {
29+
cardano-db-sync.url = "github:input-output-hk/cardano-db-sync?ref=refs/tags/12.0.0";
30+
haskellNix.url = "github:input-output-hk/haskell.nix";
31+
# ... other inputs
32+
};
33+
34+
outputs = { cardano-db-sync, haskellNix }: {
35+
packages.x86_64-linux.linode-cli = pkgs.python3Packages.callPackage ./pkgs/linode-cli.nix {};
36+
# ... other outputs
37+
};
38+
}
39+
```
40+
41+
It's conceptually quite simple. Given these inputs, this repository produces a set of outputs.
42+
43+
IOHK previously made use of `niv`, `niv` was just a different method of pinning inputs that wrote to the file "sources.json". The flakes model isn't necessarily "better" than `niv`, but flakes have other benefits.
44+
45+
By running `nix flake lock`, the inputs of the repository are pinned down to specific revisions and SHAs and written to `flake.lock`.
46+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: Common Errors
3+
date: 2021-02-09
4+
---
5+
6+
```
7+
error: getting status of '/nix/store/1dg75ahm07ah26phnd1jy1jq06s680ps-source/some-file.nix': No such file or directory
8+
```
9+
10+
Make sure the file is staged to version control. Flakes can't see files that are not tracked by version control.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
title: Invocations and Pathing
3+
date: 2021-02-10
4+
---
5+
6+
## Invocations
7+
8+
`nix-build` is now `nix build`
9+
10+
`nix-shell` is now `nix develop`
11+
12+
`nix-repl default.nix` is now:
13+
14+
```
15+
$ nix repl
16+
> :lf .
17+
# Get some information:
18+
> legacyPackages.x86_64-linux.pkgs.stdenv.hostPlatform.libc
19+
# Build cardano-wallet:
20+
> :b packages.x86_64-linux.cardano-wallet
21+
```
22+
23+
## Pathing
24+
25+
`nix-build -A cardano-wallet` is now `nix build .#packages.x86_64-linux.cardano-wallet`
26+
27+
i.e.
28+
29+
```
30+
nix build $FLAKE_PATH
31+
$FLAKE_PATH = $PATH_TO_FLAKE_FILE#$ATTRIBUTE
32+
$FLAKE_PATH = .#packages.x86_64-linux.cardano-cli
33+
```
34+
35+
The `$PATH_TO_FLAKE_FILE` can be quite complicated:
36+
37+
```
38+
· .: The flake in the current directory.
39+
· /home/alice/src/patchelf: A flake in some other directory.
40+
· nixpkgs: The nixpkgs entry in the flake registry.
41+
· nixpkgs/a3a3dda3bacf61e8a39258a0ed9c924eeca8e293: The nixpkgs entry in the flake registry, with its Git revision overridden to a specific value.
42+
· github:NixOS/nixpkgs: The master branch of the NixOS/nixpkgs repository on GitHub.
43+
· github:NixOS/nixpkgs/nixos-20.09: The nixos-20.09 branch of the nixpkgs repository.
44+
· github:NixOS/nixpkgs/a3a3dda3bacf61e8a39258a0ed9c924eeca8e293: A specific revision of the nixpkgs repository.
45+
· github:edolstra/nix-warez?dir=blender: A flake in a subdirectory of a GitHub repository.
46+
· git+https://github.com/NixOS/patchelf: A Git repository.
47+
· git+https://github.com/NixOS/patchelf?ref=master: A specific branch of a Git repository.
48+
· git+https://github.com/NixOS/patchelf?ref=master&rev=f34751b88bd07d7f44f5cd3200fb4122bf916c7e: A specific branch and revision of a Git repository.
49+
· https://github.com/NixOS/patchelf/archive/master.tar.gz: A tarball flake.
50+
```
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
title: Nix Flake Schema
3+
date: 2021-02-09
4+
---
5+
6+
The `flake.nix` has the following schema:
7+
8+
```
9+
{
10+
description = "A string";
11+
inputs = *input_schema;
12+
outputs = *output_schema:
13+
}
14+
```
15+
16+
## Input schema
17+
18+
An example input attrset:
19+
20+
```
21+
{
22+
# A GitHub repository.
23+
import-cargo = {
24+
type = "github";
25+
owner = "edolstra";
26+
repo = "import-cargo";
27+
};
28+
29+
import-cargo-alt.url = github:edolstra/import-cargo;
30+
31+
nixpkgs.url = "nixpkgs";
32+
33+
cardano-db-sync.url = "github:input-output-hk/cardano-db-sync?ref=refs/tags/12.0.0";
34+
35+
nixpkgs-alt.url = "github:NixOS/nixpkgs/nixos-21.11";
36+
}
37+
```
38+
39+
40+
## Output schema
41+
42+
There is a standard schema for the outputs of a flake:
43+
44+
```
45+
{ self, cardano-db-sync, nixpkgs, import-cargo, ... } @ inputs: {
46+
# Packages built by the repository.
47+
# Executed by `nix build .#<name>`
48+
packages."<system>"."<name>" = derivation;
49+
50+
# Executed by `nix build .`
51+
defaultPackage."<system>" = derivation;
52+
53+
# A default development shell for the project.
54+
# Used by `nix develop`.
55+
devShell."<system>" = derivation;
56+
57+
# A development shell for the project.
58+
# Used by `nix develop .#<name>`
59+
devShells."<system>"."<name>" = derivation;
60+
61+
# Hydra build jobs
62+
hydraJobs."<attr>"."<system>" = derivation;
63+
64+
# Automated checks.
65+
# Executed by `nix flake check`
66+
checks."<system>"."<name>" = derivation;
67+
68+
# Used for nixpkgs packages, also accessible via `nix build .#<name>`
69+
# Typically the repository will provide the version of nixpkgs used in the repository here:
70+
# E.g.
71+
# legacyPackages = import nixpkgs {
72+
# inherit system overlays;
73+
# };
74+
legacyPackages."<system>"."<name>" = derivation;
75+
76+
# Used by `nix flake init -t <flake>#<name>`
77+
templates."<name>" = { path = "<store-path>"; description = ""; };
78+
79+
# Default overlay, consumed by other flakes. Overlays are typically overlaid onto nixpkgs to add extra packages to the package set. Potentially for another project or for a system configuration.
80+
overlay = final: prev: { };
81+
82+
# Same idea as overlay but a list or attrset of them.
83+
overlays = {};
84+
85+
# Default module, consumed by other flakes. NixOS modules are used by NixOS configurations to "modularize" configuration.
86+
# For example, by importing the "cardano-node.nixosModule" I can run a simple node on my computer by enabling the option: "services.cardano-node.enable = true".
87+
nixosModule = { config }: { options = {}; config = {}; };
88+
89+
# Same idea as nixosModule but a list or attrset of them.
90+
nixosModules = {};
91+
92+
# Used with `nixos-rebuild --flake .#<hostname>`
93+
# nixosConfigurations."<hostname>".config.system.build.toplevel must be a derivation
94+
nixosConfigurations."<hostname>" = {};
95+
96+
# Used by `nix flake init -t <flake>`
97+
defaultTemplate = {
98+
path = "<store-path>";
99+
description = "template description goes here?";
100+
};
101+
102+
# Executed by `nix run .#<name>`
103+
apps."<system>"."<name>" = {
104+
type = "app";
105+
program = "<store-path>";
106+
};
107+
108+
# Default application
109+
# Executed by `nix run . -- <args?>`
110+
defaultApp."<system>" = { type = "app"; program = "..."; };
111+
}
112+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: 03-Create a Development Environment
3+
date: 2021-02-09
4+
---
5+
6+
## Hacking
7+
8+
`nix-shell` has been replaced by `nix develop`, and by default it builds the attribute `devShell.x86_64-linux`, so a simple python development environment would look like this:
9+
10+
`flake.nix`
11+
```
12+
{
13+
description = "A very basic flake";
14+
15+
inputs = {
16+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-21.11";
17+
};
18+
19+
outputs = { self, nixpkgs }:
20+
let
21+
pkgs = nixpkgs.legacyPackages.x86_64-linux
22+
in {
23+
devShell.x86_64-linux = pkgs.mkShell {
24+
nativeBuildInputs = [
25+
pkgs.python3
26+
];
27+
};
28+
};
29+
}
30+
```
31+
32+
We could then run `nix develop` and we would enter a shell with access to python.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
title: 01-Installation
3+
date: 2021-02-09
4+
---
5+
6+
Presuming nix is already installed, Nix flakes are available from Nix version 2.4 and up.
7+
8+
## Non-NixOS
9+
10+
```
11+
$ nix-env -iA nixpkgs.nixUnstable # or nixpkgs.nix_2_4
12+
# or nixpkgs.nix_2_5
13+
# or nixpkgs.nix_2_6, etc.
14+
```
15+
16+
Then edit either `~/.config/nix/nix.conf` or `/etc/nix/nix.conf` and add:
17+
18+
```
19+
experimental-features = nix-command flakes
20+
```
21+
22+
## NixOS
23+
24+
Add the following to your NixOS configuration:
25+
26+
`/etc/nixos/configuration.nix`
27+
```
28+
nix = {
29+
package = pkgs.nixUnstable; # or pkgs.nix_2_4
30+
# or pkgs.nix_2_5
31+
# or pkgs.nix_2_6, etc.
32+
extraOptions = ''
33+
experimental-features = nix-command flakes
34+
'';
35+
};
36+
```
37+
38+
Then
39+
40+
`nixos-rebuild switch`

0 commit comments

Comments
 (0)