|
| 1 | +# Hacking on Bashcov |
| 2 | + |
| 3 | +[Nix development shell]: #entering-the-nix-development-shell |
| 4 | +[`Gemfile.nix.lock`]: ./Gemfile.nix.lock |
| 5 | +[`gemset.nix`]: ./gemset.nix |
| 6 | +[`bashcov.gemspec`]: ./bashcov.gemspec |
| 7 | + |
| 8 | +## Nix flake usage |
| 9 | + |
| 10 | +This project supplies a [`flake.nix`](./flake.nix) file defining a Nix |
| 11 | +flake[^nix-flakes] that makes it possible to build, test, run, and hack on |
| 12 | +Bashcov using the [Nix package manager](https://nixos.org) |
| 13 | + |
| 14 | +[^nix-flakes]: See the [NixOS wiki](https://nixos.wiki/wiki/Flakes) and the |
| 15 | + [`nix flake` page in the Nix package manager reference manual](https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html) |
| 16 | + for background on Nix flakes. |
| 17 | + |
| 18 | +This Nix flake defines three important important outputs: |
| 19 | + |
| 20 | +1. A [Nix package for Bashcov](#building-the-bashcov-package), |
| 21 | +2. A [Nix flake check](#nix-flake-checks) (test) that runs Bashcov's |
| 22 | + unit and feature tests, |
| 23 | +3. A [Nix application](#running-the-nix-application),[^app] and |
| 24 | +4. A [Nix development shell],[^devshell]. |
| 25 | + |
| 26 | +[^devshell]: Based on the [`numtide/devshell`](https://github.com/numtide/devshell) project. |
| 27 | +[^app]: Runnable with `nix run`. |
| 28 | + |
| 29 | +In order to work on the Bashcov project's Nix features, |
| 30 | +you'll need to [install the Nix package manager](https://nixos.org/download.html) and |
| 31 | +[ensure that the `flakes` and `nix-command` experimental features are enabled](https://nixos.wiki/wiki/Flakes#Enable_flakes). |
| 32 | + |
| 33 | +### Building the Bashcov package |
| 34 | + |
| 35 | +To build the Bashcov package exposed by this flake, run the |
| 36 | +following command:[^verbose-output] |
| 37 | + |
| 38 | +[^verbose-output]: Note that the `-L` flag can be omitted for terser output. |
| 39 | + |
| 40 | +```shell-session |
| 41 | +$ nix build -L '.#' |
| 42 | +``` |
| 43 | + |
| 44 | +Or: |
| 45 | + |
| 46 | +```shell-session |
| 47 | +$ nix build -L '.#bashcov' |
| 48 | +``` |
| 49 | + |
| 50 | +These two forms are functionally equivalent because the |
| 51 | +Bashcov package is the default package. |
| 52 | + |
| 53 | +In addition to building the package, `nix build` will place a symbolic link to |
| 54 | +its output path at `./result` (`ls -lAR ./result/`, `tree ./result/`, or |
| 55 | +similar to see what the package contains). |
| 56 | + |
| 57 | +### Nix flake checks |
| 58 | + |
| 59 | +This project includes a test of Bashcov's functionality and features, exposed |
| 60 | +as a Nix flake check. In essence, this runs the Bashcov test suite, but inside |
| 61 | +the Nix build environment[^nix-builder-execution] (which may be |
| 62 | +sandboxed[^nix-sandbox]). |
| 63 | + |
| 64 | +[^nix-builder-execution]: The Nix build environment is described [here](https://nixos.org/manual/nix/stable/language/derivations#builder-execution). |
| 65 | +[^nix-sandbox]: The Nix sandbox is described [here](https://nixos.org/manual/nix/stable/command-ref/conf-file.html#conf-sandbox). |
| 66 | + |
| 67 | +This project also includes a test that Nix source files are properly |
| 68 | +formatted.[^treefmt-nix-check] |
| 69 | + |
| 70 | +[^treefmt-nix-check]: Defined by [`treefmt-nix`](https://github.com/numtide/treefmt-nix#flakes). |
| 71 | + |
| 72 | +#### Running Nix flake checks |
| 73 | + |
| 74 | +To run Nix flake checks, execute the following command:[^verbose-output] |
| 75 | + |
| 76 | +```shell-session |
| 77 | +$ nix flake check -L |
| 78 | +``` |
| 79 | + |
| 80 | +If a check fails, `nix` will print a diagnostic message and exit with nonzero |
| 81 | +status. |
| 82 | + |
| 83 | +##### Running a check for a specific system |
| 84 | + |
| 85 | +Running `nix flake check` will execute Nix flake checks for all supported |
| 86 | +systems.[^supported-systems] To run a check for a particular system, instead |
| 87 | +use the `nix build` command. For instance, to execute the Bashcov unit and |
| 88 | +feature tests with Nix on the `x86_64-linux` system, run:[^verbose-output] |
| 89 | + |
| 90 | +```shell-session |
| 91 | +$ nix build -L '.#checks.x86_64-linux.bashcov' |
| 92 | +``` |
| 93 | + |
| 94 | +[^supported-systems]: Run `nix flake show` to view flake outputs namespaced by |
| 95 | + all supported systems. |
| 96 | + |
| 97 | +### Running the Nix application |
| 98 | + |
| 99 | +To run Bashcov itself: |
| 100 | + |
| 101 | +```shell-session |
| 102 | +$ nix run '.#' -- <args> |
| 103 | +``` |
| 104 | + |
| 105 | +To run commands from [the Nix development shell](#entering-the-nix-development-shell) |
| 106 | +but without entering the shell: |
| 107 | + |
| 108 | +```shell-session |
| 109 | +$ nix run '.#devshell' -- <command> <args> |
| 110 | +``` |
| 111 | + |
| 112 | +For instance, to run [the `update-deps` shell command](#summary-of-available-commands): |
| 113 | + |
| 114 | +```shell-session |
| 115 | +$ nix run '.#devshell' -- update-deps |
| 116 | +``` |
| 117 | + |
| 118 | +### Entering the Nix development shell |
| 119 | + |
| 120 | +To enter the Nix development shell, run the following command: |
| 121 | + |
| 122 | +```shell-session |
| 123 | +$ nix develop |
| 124 | +``` |
| 125 | + |
| 126 | +You will be presented with a menu of commands available within the development |
| 127 | +shell. |
| 128 | + |
| 129 | +#### Summary of available commands |
| 130 | + |
| 131 | +- `fmt`: format all Nix code in this project using |
| 132 | + [`alejandra`](https://github.com/kamadorueda/alejandra). |
| 133 | +- `bundix`: tool for managing Nix <=> Ruby integration assets (Bundix lives |
| 134 | + [here](https://github.com/nix-community/bundix)). |
| 135 | +- `update-deps`: update [the Nix-specific lockfile][`Gemfile.nix.lock`] and |
| 136 | + [Nix gemset][`gemset.nix`]. |
| 137 | +- `update-deps-conservative`: update [the Nix-specific lockfile][`Gemfile.nix.lock`] |
| 138 | + and [Nix gemset][`gemset.nix`] if (and only if) `nix build` fails _without_ |
| 139 | + updates to those assets **and** `nix build` succeeds _with_ updates to them. |
| 140 | + |
| 141 | +### Maintenance of Nix assets |
| 142 | + |
| 143 | +The Bashcov Nix package depends on [`nixpkgs`'s Ruby |
| 144 | +integration](https://nixos.org/manual/nixpkgs/stable/#developing-with-ruby); |
| 145 | +specifically, it uses the `bundlerEnv` function to create an environment with |
| 146 | +all of Bashcov's Ruby gem dependencies present. `bundlerEnv` requires a |
| 147 | +Bundler lockfile (here, [`Gemfile.nix.lock`]) and a Nix-specific [`gemset.nix`] |
| 148 | +that acts as a sort of translation layer between Bundler and Nix. |
| 149 | + |
| 150 | +Both of these files must be updated from time to time in order to reflect |
| 151 | +changes in [`bashcov.gemspec`], including certain changes to Bashcov itself |
| 152 | +(e.g. version bumps). |
| 153 | + |
| 154 | +> **Note** |
| 155 | +> If [`bashcov.gemspec`] is updated without updating the Bundler lockfile and |
| 156 | +> [`gemset.nix`], the Bashcov Nix package will fail to build. |
| 157 | +
|
| 158 | +The [Nix development shell] includes two convenience commands for managing |
| 159 | +these assets: |
| 160 | + |
| 161 | +- `update-deps` unconditionally updates [`Gemfile.nix.lock`] with |
| 162 | + [`bundle lock`](https://bundler.io/v2.4/man/bundle-lock.1.html), then updates |
| 163 | + [`gemset.nix`] to reflect any changes to the Bundler lockfile. |
| 164 | +- `update-deps-conservative` does the same, but if (and only if) doing so fixes |
| 165 | + failures running `nix build`. That is, it updates the assets if it looks |
| 166 | + like problems with those assets have broken the Bashcov Nix package. |
0 commit comments