format with treefmt

This commit is contained in:
Jörg Thalheim 2023-11-29 07:13:20 +01:00
parent 1133b15e75
commit 037cc9cc25
8 changed files with 103 additions and 95 deletions

160
README.md
View file

@ -2,29 +2,30 @@
![Test](https://github.com/nix-community/nix-direnv/workflows/Test/badge.svg) ![Test](https://github.com/nix-community/nix-direnv/workflows/Test/badge.svg)
A faster, persistent implementation of `direnv`'s `use_nix` and `use_flake`, A faster, persistent implementation of `direnv`'s `use_nix` and `use_flake`, to
to replace the built-in one. replace the built-in one.
Prominent features: Prominent features:
- significantly faster after the first run by caching the `nix-shell` environment - significantly faster after the first run by caching the `nix-shell`
environment
- prevents garbage collection of build dependencies by symlinking the resulting - prevents garbage collection of build dependencies by symlinking the resulting
shell derivation in the user's `gcroots` (Life is too short to lose shell derivation in the user's `gcroots` (Life is too short to lose your
your project's build cache if you are on a flight with no internet connection) project's build cache if you are on a flight with no internet connection)
## Why not use `lorri` instead? ## Why not use `lorri` instead?
Compared to [lorri](https://github.com/nix-community/lorri), Compared to [lorri](https://github.com/nix-community/lorri), nix-direnv is
nix-direnv is simpler (and requires no external daemon) and supports flakes. simpler (and requires no external daemon) and supports flakes. Additionally,
Additionally, lorri can sometimes re-evaluate the entirety of nixpkgs on every change lorri can sometimes re-evaluate the entirety of nixpkgs on every change (leading
(leading to perpetual high CPU load). to perpetual high CPU load).
## Installation ## Installation
> **Heads up**: nix-direnv requires a modern Bash and GNU Grep. > **Heads up**: nix-direnv requires a modern Bash and GNU Grep. MacOS ships with
> MacOS ships with outdated or non-GNU versions of these tools, > outdated or non-GNU versions of these tools, As a work-around we suggest that
> As a work-around we suggest that macOS users install `direnv`/`grep` via Nix or Homebrew. > macOS users install `direnv`/`grep` via Nix or Homebrew. Discussion of these
> Discussion of these problems can be found > problems can be found
> [here](https://github.com/nix-community/nix-direnv/issues/3). > [here](https://github.com/nix-community/nix-direnv/issues/3).
There are different ways to install nix-direnv, pick your favourite: There are different ways to install nix-direnv, pick your favourite:
@ -34,10 +35,11 @@ There are different ways to install nix-direnv, pick your favourite:
### Via home-manager ### Via home-manager
Note that while the home-manager integration is recommended, Note that while the home-manager integration is recommended, some use cases
some use cases require the use of features only present in some versions of nix-direnv. require the use of features only present in some versions of nix-direnv. It is
It is much harder to control the version of nix-direnv installedwith this method. much harder to control the version of nix-direnv installedwith this method. If
If you require such specific control, please use another method of installing nix-direnv. you require such specific control, please use another method of installing
nix-direnv.
In `$HOME/.config/nixpkgs/home.nix` add In `$HOME/.config/nixpkgs/home.nix` add
@ -57,15 +59,16 @@ In `$HOME/.config/nixpkgs/home.nix` add
} }
``` ```
Check the current [Home Manager Options](https://mipmip.github.io/home-manager-option-search/?query=direnv) Check the current
for integration with shells other than Bash. Be sure to also allow `home-manager` to [Home Manager Options](https://mipmip.github.io/home-manager-option-search/?query=direnv)
manage your shell with `programs.<your_shell>.enable = true`. for integration with shells other than Bash. Be sure to also allow
`home-manager` to manage your shell with `programs.<your_shell>.enable = true`.
</details> </details>
<details> <details>
<summary>Direnv's source_url</summary> <summary>Direnv's source_url</summary>
### Direnv source\_url ### Direnv source_url
Put the following lines in your `.envrc`: Put the following lines in your `.envrc`:
@ -106,6 +109,7 @@ other available options are:
}; };
} }
``` ```
</details> </details>
<details> <details>
@ -127,7 +131,7 @@ source $HOME/.nix-profile/share/nix-direnv/direnvrc
</details> </details>
<details> <details>
<summary>With `nix profile`</summary> <summary>With `nix profile`</summary>
### With `nix profile` ### With `nix profile`
@ -151,9 +155,8 @@ source $HOME/.nix-profile/share/nix-direnv/direnvrc
### From source ### From source
Clone the repository to some directory Clone the repository to some directory and then source the direnvrc from this
and then source the direnvrc from this repository in your own repository in your own `~/.config/direnv/direnvrc`:
`~/.config/direnv/direnvrc`:
```bash ```bash
# put this in ~/.config/direnv/direnvrc # put this in ~/.config/direnv/direnvrc
@ -166,7 +169,7 @@ source $HOME/nix-direnv/direnvrc
Either add `shell.nix` or a `default.nix` to the project directory: Either add `shell.nix` or a `default.nix` to the project directory:
``` nix ```nix
# save this as shell.nix # save this as shell.nix
{ pkgs ? import <nixpkgs> {}}: { pkgs ? import <nixpkgs> {}}:
@ -182,13 +185,13 @@ $ echo "use nix" >> .envrc
$ direnv allow $ direnv allow
``` ```
If you haven't used direnv before, If you haven't used direnv before, make sure to
make sure to [hook it into your shell](https://direnv.net/docs/hook.html) first. [hook it into your shell](https://direnv.net/docs/hook.html) first.
### Using a non-standard file name ### Using a non-standard file name
You may use a different file name than `shell.nix` or `default.nix` You may use a different file name than `shell.nix` or `default.nix` by passing
by passing the file name in `.envrc`, e.g.: the file name in `.envrc`, e.g.:
```console ```console
$ echo "use nix foo.nix" >> .envrc $ echo "use nix foo.nix" >> .envrc
@ -196,35 +199,33 @@ $ echo "use nix foo.nix" >> .envrc
## Flakes support ## Flakes support
nix-direnv also comes with an alternative `use_flake` implementation. nix-direnv also comes with an alternative `use_flake` implementation. The code
The code is tested and does work but the upstream flake api is not finalized, is tested and does work but the upstream flake api is not finalized, so we we
so we we cannot guarantee stability after a nix upgrade. cannot guarantee stability after a nix upgrade.
Like `use_nix`, Like `use_nix`, our `use_flake` will prevent garbage collection of downloaded
our `use_flake` will prevent garbage collection of downloaded packages, packages, including flake inputs.
including flake inputs.
### Creating a new flake-native project ### Creating a new flake-native project
This repository ships with a [flake template](https://github.com/nix-community/nix-direnv/tree/master/templates/flake). This repository ships with a
[flake template](https://github.com/nix-community/nix-direnv/tree/master/templates/flake).
which provides a basic flake with devShell integration and a basic `.envrc`. which provides a basic flake with devShell integration and a basic `.envrc`.
To make use of this template, you may issue the following command: To make use of this template, you may issue the following command:
```console ```console
$ nix flake new -t github:nix-community/nix-direnv <desired output path> $ nix flake new -t github:nix-community/nix-direnv <desired output path>
``` ```
### Integrating with a existing flake ### Integrating with a existing flake
```console ```console
$ echo "use flake" >> .envrc && direnv allow $ echo "use flake" >> .envrc && direnv allow
``` ```
The `use flake` line also takes an additional arbitrary flake parameter, The `use flake` line also takes an additional arbitrary flake parameter, so you
so you can point at external flakes as follows: can point at external flakes as follows:
```bash ```bash
use flake ~/myflakes#project use flake ~/myflakes#project
@ -234,14 +235,14 @@ use flake ~/myflakes#project
#### use flake #### use flake
Under the covers, `use_flake` calls `nix print-dev-env`. Under the covers, `use_flake` calls `nix print-dev-env`. The first argument to
The first argument to the `use_flake` function is the flake expression to use, the `use_flake` function is the flake expression to use, and all other arguments
and all other arguments are proxied along to the call to `print-dev-env`. are proxied along to the call to `print-dev-env`. You may make use of this fact
You may make use of this fact for some more arcane invocations. for some more arcane invocations.
For instance, if you have a flake that needs to be called impurely under some conditions, For instance, if you have a flake that needs to be called impurely under some
you may wish to pass `--impure` to the `print-dev-env` invocation conditions, you may wish to pass `--impure` to the `print-dev-env` invocation so
so that the environment of the calling shell is passed in. that the environment of the calling shell is passed in.
You can do that as follows: You can do that as follows:
@ -252,13 +253,13 @@ $ direnv allow
#### use nix #### use nix
Like `use flake`, `use nix` now uses `nix print-dev-env`. Like `use flake`, `use nix` now uses `nix print-dev-env`. Due to historical
Due to historical reasons, the argument parsing emulates `nix shell`. reasons, the argument parsing emulates `nix shell`.
This leads to some limitations in what we can reasonably parse. This leads to some limitations in what we can reasonably parse.
Currently, all single-word arguments and some well-known double arguments Currently, all single-word arguments and some well-known double arguments will
will be interpeted or passed along. be interpeted or passed along.
#### Manual reload of the nix environment #### Manual reload of the nix environment
@ -267,7 +268,8 @@ nix-direnv in the "manual reload" mode. nix-direnv will then tell you when the
nix environment is no longer up to date. You can then decide yourself when you nix environment is no longer up to date. You can then decide yourself when you
want to reload the nix environment. want to reload the nix environment.
To activate manual mode, use `nix_direnv_manual_reload` in your `.envrc` like this: To activate manual mode, use `nix_direnv_manual_reload` in your `.envrc` like
this:
```shell ```shell
nix_direnv_manual_reload nix_direnv_manual_reload
@ -283,36 +285,38 @@ $ nix-direnv-reload
##### Known arguments ##### Known arguments
- `-p`: Starts a list of packages to install; consumes all remaining arguments - `-p`: Starts a list of packages to install; consumes all remaining arguments
- `--include` / `-I`: Add the following path to the list of lookup locations for `<...>` file names - `--include` / `-I`: Add the following path to the list of lookup locations for
`<...>` file names
- `--attr` / `-A`: Specify the output attribute to utilize - `--attr` / `-A`: Specify the output attribute to utilize
`--command`, `--run`, `--exclude`, `--pure`, `-i`, and `--keep` are explicitly ignored. `--command`, `--run`, `--exclude`, `--pure`, `-i`, and `--keep` are explicitly
ignored.
All single word arguments (`-j4`, `--impure` etc) All single word arguments (`-j4`, `--impure` etc) are passed to the underlying
are passed to the underlying nix invocation. nix invocation.
#### Tracked files #### Tracked files
`nix-direnv` makes a performance tradeoff `nix-direnv` makes a performance tradeoff and only considers changes in a
and only considers changes in a limited number of files limited number of files when deciding to update its cache.
when deciding to update its cache.
- for `use nix` this is: - for `use nix` this is:
* `~/.direnvrc`
* `~/.config/direnv/direnvrc` - `~/.direnvrc`
* `.envrc`, - `~/.config/direnv/direnvrc`
* A single nix file. In order of preference: - `.envrc`,
+ The file argument to `use nix` - A single nix file. In order of preference:
+ `shell.nix` if it exists - The file argument to `use nix`
+ `default.nix` if it exists - `shell.nix` if it exists
- `default.nix` if it exists
- for `use flake` this is: - for `use flake` this is:
* `~/.direnvrc` - `~/.direnvrc`
* `~/.config/direnv/direnvrc` - `~/.config/direnv/direnvrc`
* `.envrc` - `.envrc`
* `flake.nix` - `flake.nix`
* `flake.lock` - `flake.lock`
* `devshell.toml` if it exists - `devshell.toml` if it exists
To add more files to be checked use `watch_file` like this To add more files to be checked use `watch_file` like this
@ -321,16 +325,18 @@ watch_file your-file.nix
use nix # or use flake use nix # or use flake
``` ```
Or - if you don't mind the overhead (runtime and conceptual) of watching all nix-files: Or - if you don't mind the overhead (runtime and conceptual) of watching all
nix-files:
```shell ```shell
watch_file $(find . -name "*.nix" -printf '"%p" ') watch_file $(find . -name "*.nix" -printf '"%p" ')
``` ```
Note that this will re-execute direnv for any nix change, Note that this will re-execute direnv for any nix change, regardless of whether
regardless of whether that change is meaningful for the devShell in use. that change is meaningful for the devShell in use.
`watch_file` must be invoked before either `use flake` or `use nix` to take effect. `watch_file` must be invoked before either `use flake` or `use nix` to take
effect.
## General direnv tips ## General direnv tips

View file

@ -1,4 +1,4 @@
{ stdenv, nix, gnugrep, lib }: { stdenv, nix, lib }:
stdenv.mkDerivation { stdenv.mkDerivation {
name = "nix-direnv"; name = "nix-direnv";

View file

@ -45,7 +45,7 @@
packages // devShells; packages // devShells;
}; };
flake = { flake = {
overlay = final: prev: { overlay = final: _prev: {
nix-direnv = final.callPackage ./default.nix { }; nix-direnv = final.callPackage ./default.nix { };
}; };
templates.default = { templates.default = {

View file

@ -5,7 +5,7 @@
, ruff , ruff
, runCommand , runCommand
}: }:
runCommand "lint" {} '' runCommand "lint" { } ''
set -e set -e
mkdir source mkdir source
cp -r ${./.}/* source cp -r ${./.}/* source

View file

@ -1,4 +1,4 @@
{ pkgs ? import <nixpkgs> {} }: { pkgs ? import <nixpkgs> { } }:
with pkgs; with pkgs;
mkShell { mkShell {

View file

@ -3,12 +3,14 @@
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
inputs.flake-utils.url = "github:numtide/flake-utils"; inputs.flake-utils.url = "github:numtide/flake-utils";
outputs = { self, nixpkgs, flake-utils }: outputs = { nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system: let flake-utils.lib.eachDefaultSystem (system:
pkgs = nixpkgs.legacyPackages.${system}; let
in { pkgs = nixpkgs.legacyPackages.${system};
devShells.default = pkgs.mkShell { in
packages = [ pkgs.bashInteractive ]; {
}; devShells.default = pkgs.mkShell {
}); packages = [ pkgs.bashInteractive ];
};
});
} }

View file

@ -3,7 +3,7 @@
inputs.nixpkgs.url = "github:NixOS/nixpkgs"; inputs.nixpkgs.url = "github:NixOS/nixpkgs";
inputs.flake-utils.url = "github:numtide/flake-utils"; inputs.flake-utils.url = "github:numtide/flake-utils";
outputs = { self, nixpkgs, flake-utils }: outputs = { nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system: { flake-utils.lib.eachDefaultSystem (system: {
devShell = import ./shell.nix { devShell = import ./shell.nix {
pkgs = nixpkgs.legacyPackages.${system}; pkgs = nixpkgs.legacyPackages.${system};

View file

@ -1,8 +1,8 @@
{ pkgs ? import (builtins.getFlake (toString ./.)).inputs.nixpkgs { } { pkgs ? import (builtins.getFlake (toString ./.)).inputs.nixpkgs { }
, someArg ? null , someArg ? null
, shellHook ? '' , shellHook ? ''
echo "Executing shellHook." echo "Executing shellHook."
'' ''
}: }:
pkgs.mkShellNoCC { pkgs.mkShellNoCC {
inherit shellHook; inherit shellHook;