Skip to content

Commit 04dc4ff

Browse files
authored
Flake Docs (#909)
## Summary Add Docs for our new Flake feature ## How was it tested? Localhost
1 parent 4478bf6 commit 04dc4ff

File tree

12 files changed

+315
-9
lines changed

12 files changed

+315
-9
lines changed

docs/app/docs/configuration.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,30 @@ This is a list of Nix packages that should be installed in your Devbox shell and
2525

2626
You can add packages to your devbox.json using `devbox add <package_name>`, and remove them using `devbox rm <package_name>`
2727

28+
#### Adding Packages from Flakes
29+
30+
You can add packages from flakes by adding a reference to the flake in the `packages` list in your `devbox.json`. We currently support installing Flakes from Github and local paths.
31+
32+
```json
33+
{
34+
"packages": [
35+
// Add the default package from a github repository
36+
"github:numtide/flake-utils",
37+
// Install a specific attribute or package from a Github hosted flake
38+
"github:nix-community/fenix#stable.toolchain",
39+
// Install a package from a specific channel of Nixpkgs
40+
"github:nixos/nixpkgs/21.05#hello",
41+
// Install a package form a specific commit of Nixpkgs
42+
"github:nixos/nixpkgs/5233fd2ba76a3accb5aaa999c00509a11fd0793c#hello",
43+
// Install a package from a local flake. This should point to a directory that contains a flake.nix file.
44+
"path:../my-flake#my-package"
45+
]
46+
}
47+
```
48+
49+
To learn more about using flakes, see the [Using Flakes](guides/using_flakes.md) guide.
50+
51+
2852
### Env
2953

3054
This is a a map of key-value pairs that should be set as Environment Variables when activating `devbox shell`, running a script with `devbox run`, or starting a service. These variables will only be set in your Devbox shell, and will have precedence over any environment variables set in your local machine or by [Devbox Plugins](guides/plugins.md).

docs/app/docs/guides/pinning_packages.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ This doc will explain how to select and pin specific package versions in Devbox
66

77
## Background
88

9-
The Nix Package Manager, which Devbox uses to install your shell packages, stores it's package definitions in a Github Repository at [NixOS/nixpkgs](https://github.com/NixOS/nixpkgs). This repository contains the Nix build definitions for over 80,000 different packages. New packages or changes to build definitions are added (and deprecated packages are removed) by committing to the repo.
9+
The Nix Package Manager, which Devbox uses to install your shell packages, stores its package definitions in a Github Repository at [NixOS/nixpkgs](https://github.com/NixOS/nixpkgs). This repository contains instructions for building over 80,000 different packages. Maintainers add new packages or remove deprecated packages by committing to the repo.
1010

11-
Because Nix uses Git to store it's package definitions, we can install specific packages from older versions of the Nix Store by specifying the commit that we want to use. You can also use this commit to pin your project to a specific version of Nixpkgs, so that any developer using your project will get the exact same packages.
11+
Because Nix uses Git to store its package definitions, we can install specific packages from older versions of the Nix Store by specifying the default commit we want to use. You can also use this commit to pin your project to a specific version of Nixpkgs, so any developer using your project will get the same packages.
1212

13-
## Pinning the Nixpkg commit in your Devbox.json
13+
14+
## Pinning the Default Nixpkg commit in your Devbox.json
1415

1516
Devbox stores the Nixpkg commit in your project's `devbox.json`, under the `nixpkgs.commit`. If you do not specify one in your config, Devbox will automatically add a default commit hash when you run a command like `devbox add`, `devbox shell`, or `devbox run`:
1617

@@ -25,11 +26,24 @@ This hash ensures that Devbox will install the same packages whenever you start
2526

2627
To use the latest available packages in Nix, you can replace the commit in `devbox.json` with the latest `nixpkgs-unstable` hash from [https://status.nixos.org](https://status.nixos.org).
2728

28-
## Look up a commit hash for a specific package
29+
## Pinning a Nixpkg Commit for a Single Package
30+
31+
If you want to use a different commit for a single package, you can use a Flake reference to use an older revision of Nixpkg for just that package. The example below shows how to install the `hello` package from a specific Nixpkg commit:
32+
33+
```json
34+
}
35+
"packages" : [
36+
"github:nixos/nixpkgs/5233fd2ba76a3accb5aaa999c00509a11fd0793c#hello"
37+
]
38+
}
39+
```
40+
Note that using a different nixpkg commit may install some duplicate packages and cause Nix Store bloat, so use this option sparingly.
41+
42+
## How to Find the Nixpkg Commit for a Package
2943

30-
In most cases, the packages available in Devbox's default commit should suffice for your use cases. However, if you need a specific minor version, or an older version of a package that is no longer included in Nixpkgs, you may need update the commit SHA. Unfortunately, Nix does not have an official way to find the Nixpkg commit SHA for a specific version of a package.
44+
In most cases, the packages available in Devbox's default commit should suffice for your use cases. However, if you want to install an older package no longer available in Nix, you must use an older commit reference in either your Flake reference or default nixpkg commit.
3145

32-
However, there is an unofficial search tool at [https://lazamar.co.uk/nix-versions/](https://lazamar.co.uk/nix-versions/) that can be used to list the Nixpkg commits for different versions of a specific package. To find the correct Nixpkg commit hash:
46+
Unfortunately, Nix does not have an official way to find the Nixpkg commit SHA for a specific package. However, an unofficial search tool at [https://lazamar.co.uk/nix-versions/](https://lazamar.co.uk/nix-versions/) can be used to list the Nixpkg commits for different versions of a specific package. To find the correct Nixpkg commit hash:
3347
1. Select `nixpkgs-unstable` in the dropdown
3448
2. Enter the name of the package you want to search, and hit Search
3549
3. In the search results, find the version you want in the Version Column
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
title: Using Packages from Nix Flakes
3+
---
4+
5+
Devbox supports installing packages with [Nix Flakes](https://nixos.wiki/wiki/Flakes). While Nixpkgs is a great
6+
7+
Devbox currently provides two ways to use Flakes to install packages in your project:
8+
9+
1. You can reference a Flake hosted in Github using the `github:` reference
10+
2. You can reference a local Flake using the `path:` reference
11+
12+
## What are Flakes?
13+
14+
[Flakes](https://www.jetpack.io/blog/powered-by-flakes/) are a new feature in the Nix language that lets us package software and create development shells in a declarative, fully reproducible way. You can use Nix Flakes to define packages, apps, templates, and dev environments.
15+
16+
Flakes are defined as a directory with a `flake.nix` and a `flake.lock` file. You import flakes to your project using a flake reference, which describes where to find the Flake, and what version or revision to use
17+
18+
## Using a Flake from Github
19+
20+
You can add a Flake hosted on Github using the following string in your packages list:
21+
22+
```json
23+
"packages": [
24+
"github:<org>/<repo>/<ref>#<optional_flake_attr>"
25+
]
26+
```
27+
28+
The Ref and Flake Attribute is optional and will default to the main branch and `packages.default|defaultPackage` attribute, respectively.
29+
30+
For example, to install [Process Compose](https://github.com/F1bbonac1/process-compose) from its repository using Nix Flakes, you can use the following string in your packages list. This will install the latest version of Process Compose from the `main` branch.
31+
32+
```nix
33+
github:F1bbonac1/process-compose
34+
```
35+
36+
### Installing a Flake from a specific branch or tag
37+
38+
You can install a specific release or branch by adding it to your flake reference. The following example will install Process Compose version 0.40.2 from the `v0.40.2` tag.
39+
40+
```nix
41+
github:F1bbonac1/process-compose/v0.40.2
42+
```
43+
44+
### Installing a specific attribute or package from a Flake
45+
46+
You can also install a specific attribute or package from a Flake by adding a `#` and the attribute name to the end of the package string. If you don't specify an attribute, Devbox will use `default` or `defaultPackage`
47+
48+
For example, if you want to use [Fenix](https://github.com/nix-community/fenix) to install a specific version of Nix, you can use the following string in your packages list. This example will install the `stable.toolchain` packages from the `fenix` package.
49+
50+
```nix
51+
github:nix-community/fenix#stable.toolchain
52+
```
53+
54+
### Using Flakes with Nixpkgs
55+
56+
The Nixpkgs repo on Github also provides a Flake for installing packages. You can use the following flake reference to install packages from a specific Nixpkgs commit or reference:
57+
58+
```nix
59+
github:NixOS/nixpkgs/<ref>#<package>
60+
```
61+
62+
For example, if you want to install the `hello` package from the `nixos-20.09` branch, you can use the following string in your packages list:
63+
64+
```nix
65+
github:NixOS/nixpkgs/nixos-20.09#hello
66+
```
67+
68+
## Using a Local Flake
69+
70+
You can also use a local Flake using the `path` attribute in your package list. Using a local flake can be helpful if you want to install your custom packages with Nix, or if you need to modify packages before using them in your Devbox project
71+
72+
Your flake reference should point to a directory that contains a `flake.nix` file.
73+
74+
```nix
75+
path:<path_to_flake>#<optional_flake_attr>
76+
```
77+
78+
For example, if you have a local Flake in the `./my-flake` directory, you can use the following string in your `packages` list. This example will install all the packages under the `my-package` attribute.
79+
80+
```nix
81+
path:./my-flake#my-package
82+
```
83+
84+
### Examples
85+
86+
For more examples of using Nix Flakes with Devbox, check out the examples in our Devbox Repo:
87+
88+
- [Using Nix Flakes from Github](https://github.com/jetpack-io/devbox/tree/main/examples/flakes/remote)
89+
- [Using a Local Flake](https://github.com/jetpack-io/devbox/tree/main/examples/flakes/php)
90+
- [Applying an Overlay with Nix Flakes](https://github.com/jetpack-io/devbox/tree/main/examples/flakes/overlay)

docs/app/sidebars.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ const sidebars = {
5353
}, {
5454
type: 'doc',
5555
id: 'guides/services'
56+
}, {
57+
type: 'doc',
58+
id: 'guides/using_flakes'
5659
}]
5760
}, {
5861
type: 'category',

examples/flakes/README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Examples that show how to add custom flakes to your devbox project.
44

5-
# Local flakes (usually committed to your project)
5+
## Local flakes (usually committed to your project)
66

77
In devbox.json use "path:/path/to/flake#output" as the package name.
88

@@ -23,6 +23,24 @@ In devbox.json use "path:/path/to/flake#output" as the package name.
2323

2424
This installs the "php" and "hello" outputs from the flake at `my-php-flake`. These outputs can also be part of packages or legacyPackages.
2525

26-
# Remote flakes
26+
## Remote flakes
2727

28-
TODO
28+
Use `github:<org>/<repo>/<ref>#<output>` as the package name to install from a Github repo.
29+
30+
```json
31+
{
32+
"packages": [
33+
"github:nixos/nixpkgs/5233fd2ba76a3accb5aaa999c00509a11fd0793c#hello",
34+
"github:nix-community/fenix#stable.toolchain",
35+
"github:F1bonacc1/process-compose"
36+
],
37+
"shell": {
38+
"init_hook": null
39+
},
40+
"nixpkgs": {
41+
"commit": "f80ac848e3d6f0c12c52758c0f25c10c97ca3b62"
42+
}
43+
}
44+
```
45+
46+
This installs the `hello` package from the 5233fd... commit of Nixpkgs, the `stable.toolchain` output from the `fenix` package in the `nix-community/fenix` repo, and the `default` output from the `F1bonacc1/process-compose` repo.

examples/flakes/overlay/.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
16

examples/flakes/overlay/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Adding Overlays with Flakes
2+
3+
This flake shows how to use an overlay Nix flake to override a Nixpkgs package and use it in your devbox configuration.
4+
5+
In this example, using the default `yarn` from Nixpkgs will cause `yarn start` to fail. To fix this issue, we use an overlay to modify the `yarn` package to use NodeJS 16 instead of it's default NodeJS 14.
6+
7+
```nix
8+
9+
overlay = (final: prev: {
10+
yarn = prev.yarn.override { nodejs = final.pkgs.nodejs-16_x; };
11+
});
12+
```
13+
14+
The yarn-overlay flake exports the modified yarn package in it's outputs. We can then use this package in our devbox shell by adding it to `packages` in our `devbox.json` file.
15+
16+
```json
17+
{
18+
"packages": [
19+
"path:./yarn-overlay#yarn"
20+
"fnm"
21+
]
22+
...
23+
}
24+
```
25+
26+
Note: you will need Devbox 0.4.7-dev or later for this to work. You can try it out by exporting `DEVBOX_VERSION=0.4.7-dev` before running `devbox shell`.
27+
28+
You can use the flake.nix in the yarn-overlay directory as a template for creating your own overlays.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"packages": [
3+
"fnm",
4+
"path:yarn-overlay#yarn"
5+
],
6+
"shell": {
7+
"init_hook": [
8+
"eval \"$(fnm env --use-on-cd)\""
9+
]
10+
},
11+
"nixpkgs": {
12+
"commit": "eabc38219184cc3e04a974fe31857d8e0eac098d"
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "nix-yarn-issue",
3+
"version": "1.0.0",
4+
"license": "MIT",
5+
"scripts": {
6+
"start": "echo 'it should run'"
7+
},
8+
"engines": {
9+
"node": ">= 16"
10+
}
11+
}

examples/flakes/overlay/yarn-overlay/flake.lock

Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)