diff --git a/en/dev.md b/en/dev.md index a7783493..3d3b9404 100644 --- a/en/dev.md +++ b/en/dev.md @@ -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]]# \ No newline at end of file +- [[haskell]]# + +## Tools + +- [[direnv]]# \ No newline at end of file diff --git a/en/direnv.md b/en/direnv.md new file mode 100644 index 00000000..55f5ec82 --- /dev/null +++ b/en/direnv.md @@ -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"} + +::: + + + + +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 diff --git a/en/home-manager.md b/en/home-manager.md new file mode 100644 index 00000000..25be6ccb --- /dev/null +++ b/en/home-manager.md @@ -0,0 +1,6 @@ + +https://github.com/nix-community/home-manager + +## Getting Started + +https://github.com/juspay/nix-dev-home \ No newline at end of file