|
| 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) |
0 commit comments