Skip to content

Commit

Permalink
Import direnv
Browse files Browse the repository at this point in the history
  • Loading branch information
srid committed Dec 6, 2023
1 parent 6516246 commit 9a2bdf7
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
9 changes: 6 additions & 3 deletions en/dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

While package management is the key purpose of #[[nix]], its [[drv|derivations]] can also be used to produce non-package types, such as development environments (aka. "devShell").

## Development Environments
## Language support

- Languages
- [[haskell]]#
- [[haskell]]#

## Tools

- [[direnv]]#
80 changes: 80 additions & 0 deletions en/direnv.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# `direnv`: manage dev environments

`direnv` (along with [nix-direnv]) allows one to persist[^gc] nix #[[dev|devshell]] environments and share them seamlessly with text editors and IDEs. It obviates having to run `nix develop` manually every time you open a new terminal. The moment you `cd` into your project directory, the devshell is automatically activated, thanks to `direnv`.

[^gc]: [nix-direnv] prevents garbage collection of the devshell, so you do not have to re-download things again. direnv also enables activating the devshell in your current shell, without needing to use a customized bash.

>[!tip] Starship
> It is recommended to use [**starship**](https://starship.rs/) along with nix-direnv, because it gives a visual indication of the current environment. For example, if you are in a `nix develop` shell, your terminal prompt automatically changes to something like this:
>
> ```sh
> srid on nixos haskell-template on  master [!] via λ 9.2.6 via ❄️ impure (ghc-shell-for-haskell-template-0.1.0.0-0-env)
>
> ```
## Setup
:::{class="flex items-center justify-center mb-8"}
<iframe width="560" height="315" src="https://www.youtube.com/embed/1joZLTgYLxY?si=ljZLcFAIhrJ7XawV" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>
:::
If you use [[home-manager]], both `nix-direnv` and `starship` can be installed using the following configuration:
```nix
# home.nix
programs.direnv = {
enable = true;
nix-direnv.enable = true;
};
programs.starship = {
enable = true;
};
```
>[!tip] Newcomer Tip
> If you have never used [[home-manager]] before, we recommend that you set it up[^nf] by following the instrutions at https://github.com/juspay/nix-dev-home.
[^nf]: The "nix-dev-home" template uses [nixos-flake](https://community.flake.parts/nixos-flake), and works on both Linux or macOS.

### Text Editor configuration

#### VSCode

For VSCode, use [Martin Kühl's direnv extension](https://marketplace.visualstudio.com/items?itemName=mkhl.direnv).

#### Doom Emacs

Doom Emacs has the [`:tools` `direnv` module](https://github.com/doomemacs/doomemacs/tree/master/modules/tools/direnv) to automatically load the devshell environment when you open the project directory.

## Add a `.envrc`

To enable direnv on Flake-based projects, add the following to your `.envrc`:

```text title=".envrc"
use flake
```

Now run `direnv allow` to authorize the current `.envrc` file. You can now `cd` into the project directory in a terminal and the devshell will be automatically activated.

### Reload automatically when some files change

#### [[haskell]] - when `.cabal` files change

Since both [nixpkgs](https://community.flake.parts/haskell-flake/nixpkgs-haskell) and [haskell-flake](https://community.flake.parts/haskell-flake) use Nix expressions that read the `.cabal` file to get dependency information, you will want the devshell be recreated every time a `.cabal` file changes. This can be achieved using the `nix_direnv_watch_file` function. Modify your `.envrc` to contain:

```text title=".envrc"
nix_direnv_watch_file *.cabal
use flake
```

As a result of this whenever you change a `.cabal` file, direnv will reload the environment. If you are using VSCode, you will see a notification that the environment has changed, prompting you to restart it ([see example](https://github.com/nammayatri/nammayatri/tree/main/Backend#visual-studio-code)).

## External Links

- [Effortless dev environments with Nix and direnv](https://determinate.systems/posts/nix-direnv)

[nix-direnv]: https://github.com/nix-community/nix-direnv
[home-manager]: https://github.com/nix-community/home-manager
6 changes: 6 additions & 0 deletions en/home-manager.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

https://github.com/nix-community/home-manager

## Getting Started

https://github.com/juspay/nix-dev-home

0 comments on commit 9a2bdf7

Please sign in to comment.