Skip to content

Commit

Permalink
Using with flakes proofread
Browse files Browse the repository at this point in the history
  • Loading branch information
worktheclock committed Feb 20, 2023
1 parent ecd528f commit b38a119
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions docs/guides/using-with-flakes.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
`devenv` can be integrated with [Nix Flakes](https://www.tweag.io/blog/2020-05-25-flakes/) if you're more familiar with the Nix language and ecosystem.
If you're familiar with the Nix language and ecosystem, `devenv` can be integrated with [Nix Flakes](https://www.tweag.io/blog/2020-05-25-flakes/).

You can define your own packages, NixOS and home-manager modules, but still benefit from devenv's development features.
You can define your own packages, NixOS and Home Manager modules, and benefit from the `devenv` development features.
The development shell will share the same inputs and lock file as all the other outputs of your flake, ensuring that your entire project is using the same `nixpkgs` revision.

With flakes, you also no longer need dedicated configuration files for devenv:
With flakes, you no longer need dedicated configuration files for `devenv`:

* the inputs from `devenv.yaml` are replaced by the flake's inputs
* `devenv.nix` becomes a shell module in `flake.nix`
* the `devenv.lock` is replaced by the `flake.lock`
* The inputs from `devenv.yaml` are replaced by the flake's inputs.
* `devenv.nix` becomes a shell module in `flake.nix`.
* The `devenv.lock` is replaced by the `flake.lock`.

## Getting started

Expand All @@ -19,27 +19,27 @@ $ nix flake init --template github:cachix/devenv

This template will create:

* a `flake.nix` containing a basic devenv configuration
* an `.envrc` to optionally set up automatic shell activation with direnv.
* A `flake.nix` file containing a basic devenv configuration.
* A `.envrc` file to optionally set up automatic shell activation with direnv.

Open the devenv shell with:
Open the `devenv` shell with:

```console
$ nix develop --impure
```

This will create a `flake.lock` lock file and open up a new shell based on the devenv configuration stated in `flake.nix`.
This will create a `flake.lock` lock file and open a new shell based on the `devenv` configuration stated in `flake.nix`.

!!! note "Why do I need `--impure`?"
When working with flakes, pure mode prevents devenv from accessing and modifying its state data.
When working with flakes, pure mode prevents `devenv` from accessing and modifying its state data.
Certain features, like running processes with `devenv up`, won't work in pure mode.

## Modifying your `flake.nix`
## Modifying your `flake.nix` file

Here's a minimal `flake.nix` that includes:
Here's a minimal `flake.nix` file that includes:

* a single `devShell` for the `x86_64-linux` system created with `devenv.lib.mkShell`.
* a shell module containing the devenv configuration what you'd usually write in `devenv.nix`.
* A single `devShell` for the `x86_64-linux` system created with `devenv.lib.mkShell`.
* A shell module containing the `devenv` configuration. This is what you'd usually write in `devenv.nix`.
See [the reference documentation](/reference/options/) for the possible options to use here.

```nix
Expand Down Expand Up @@ -75,7 +75,7 @@ Here's a minimal `flake.nix` that includes:
}
```

Once inside the shell, you can launch [process and services with `devenv up`](/processes).
Once in the shell, you can launch [processes and services with `devenv up`](/processes).

```console
$ devenv up
Expand All @@ -96,13 +96,13 @@ nix flake --impure

## Multiple shells

Defining multiple development shells using flakes can be useful depending on your project's structure. We will handle 2 use-cases here.
Defining multiple development shells using flakes can be useful depending on your project's structure. We will handle two use cases here.

### Single project with multiple shells

Some projects lend themselves to define multiple development shells. For instance, a mono-repo where you want to define multiple development shells in a central flake.nix in the root of the repository. There you can centrally define the development shells for the different sub-projects in the repository.
Some projects lend themselves to defining multiple development shells. For instance, you may want to define multiple development shells for different subprojects in a monorepo. You can do this by defining the various development shells in a central `flake.nix` file in the root of the repository.

In this case you want to define a `flake.nix` file that contains multiple `devShells`. For example:
The `flake.nix` file contains multiple `devShells`. For example:

```nix
{
Expand Down Expand Up @@ -142,17 +142,17 @@ In this case you want to define a `flake.nix` file that contains multiple `devSh
}
```

Here you can see that there are 2 shells defined. Each one with a devenv configuration with differently defined `enterShell`.
Here we define two shells, each with a `devenv` configuration and differently defined `enterShell` command.

To enter the shell of `projectA`:
To enter the shell of `project A`:

```console
$ nix develop .#projectA
this is project A
(devenv) $
```

To enter the shell of `projectB`:
To enter the shell of `project B`:

```console
$ nix develop .#projectB
Expand All @@ -162,22 +162,22 @@ this is project B

### Projects with an external flake

Whenever you have projects where you cannot (or don't want to) add a flake.nix to its repository, you can refer to external flakes.
If you cannot or don't want to add a `flake.nix` file to your project's repository, you can refer to external flakes.

You can create a repository with a flake.nix like the one above. However, in a different project you can now refer to this flake using:
You can create a repository with a `flake.nix` file as in the example above. You can now refer to this flake in a different project:

```console
$ nix develop file:/path/to/central/flake#projectA
this is project A
(devenv) $
```

You can also add this to the `direnv` configuration of the project. Just make sure the following line is in `.envrc`:
You can also add this to the `direnv` configuration of the project. Make sure the following line is in `.envrc`:

```text
nix flake --impure file:/path/to/central/flake#projectA
```

Note that instead of referring to a directory on local file system that includes the `flake.nix`, like `/path/to/central/flake`, it is also possible to use different references to a flake. For instance `github:` or `git:`. See [Nix flake references](https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html#flake-references) for more information.
Note that instead of referring to a directory on the local file system that includes the `flake.nix` file, like `/path/to/central/flake`, it is also possible to use different references to a flake. For instance `github:` or `git:`. See [Nix flake references](https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html#flake-references) for more information.

One big caveat with this method is that there is no lock file. It is not 100% clear which version of the flake is used when referring to it this way. A local project flake file will give more control which version of the flake is used.
When using this method to refer to external flakes, it's important to remember that there is no lock file, so there is no certainty about which version of the flake is used. A local project flake file will give you more control over which version of the flake is used.

0 comments on commit b38a119

Please sign in to comment.