vfarcic / crossplane-tutorial

43 stars 276 forks source link

feat: use a nix flake with direnv to manage the development environment #20

Closed cameronraysmith closed 3 months ago

cameronraysmith commented 3 months ago

Many thanks for this tutorial!

This PR illustrates how I would personally use this tutorial repository and may not be of interest to others. No offense will be taken in closing the PR without review aside from retaining its history for those who might like to see how it would function with a nix flake to establish the development environment.

The PR shows how one could use a nix flake with or without direnv to automate the setup of the development environment upon entering the directory containing a local copy of this repository using the relatively common nix+direnv pattern. I have no affiliation with Determinate Systems despite linking to a couple of their resources. Briefly this PR's commits

In the proposed usage pattern, the user would be able to bootstrap the nix package manager via the Determinate Systems nix installer that comes preconfigured with flakes enabled and direnv together via execution of a single make target (make is proposed for the bootstrap since it's available almost everywhere without additional installation steps and a brief description of each target will be rendered when running make alone)

make setup-dev

This would produce a printed notice to add the appropriate direnv shell configuration hook to their chosen shell's profile. This would complete the one-time setup.

Finally, a user would execute

direnv allow

from within the repository, which, via the configuration in .envrc, would result in loading the default devShell essentially equivalent to activating shell.nix. One notable distinction from the current configuration with devbox is that the versions pinned in devbox.json

devbox.json version pins

```json "bat@0.24.0", "kind@0.22.0", "kubernetes-helm@3.14.3", "kubectl@1.29.3", "gum@0.13.0", "awscli2@2.15.15", "teller@1.5.6", "yq-go@4.43.1", "crossplane-cli@1.15.1" ```

would be indirectly determined by the chosen nixpkgs channel and indirectly fixed in the flake.lock file.

If there is interest in reviewing this PR in consideration of merging it, then, at a minimum, we should also update the README.md to explain how to use this functionality. I will close the PR after a few weeks if there's no interest in reviewing it. There are also comments in the flake.nix and .envrc that could be considered extraneous.

vfarcic commented 3 months ago

That's great! However, I'm not sure it's a good idea to do it in this repo. It's focused on Crossplane and Nix is there only to facilitate dependent CLIs rather than to showcase Nix itself. WDYT? Would a different repo make more sense?

cameronraysmith commented 3 months ago

Yes it's a good point and I agree with it.

The potential reason to include something closer to this PR in this repo would be to

facilitate [installation of] dependent CLIs

using the widely utilized nix flake + direnv pattern. Obviously this could be easily rolled back, but technically it would allow the removal of the shell.nix and devbox.{json,lock} as I have done since your comment to illustrate that point.

From this perspective the prospect of reviewing and merging something like this PR could be viewed as no more a nix showcase than it currently is; however, it's true that devbox abstracts away a bit of the initial complexity of setting up a devShell via a nix flake while also supporting individual package level version-pinning in a way that is not completely trivial to replicate in nix.

The Makefile in this PR reduces the setup process to something not all that different from needing to install devbox where

curl -fsSL https://get.jetify.com/devbox | bash

is approximately replaced by

make setup-dev

However, I would imagine the devbox shell script is somewhat more robust than this setup-dev make target would be without additional refinement. On the other hand, the vast majority of those who use nix or end up adopting nix will already have nix and direnv installed and so, with the PR source branch, simply cloning this repository, entering the directory, and running direnv allow would complete the setup.

I'll close this PR for now, but am more than happy to continue the discussion and reopen it here or in a separate repository if you think that would be helpful.

cameronraysmith commented 3 months ago

Even https://github.com/vfarcic/crossplane-kubernetes would probably be a more sensible place than this repo to experiment with a similar setup.

vfarcic commented 3 months ago

The problem is that repo is already used in quite a few places with the instructions to run devbox shell. It would need to be a new repo or, even better, a repo I made more than 4 months ago (that's, more or less, when I started adding devbox.json or shell.nix to all repos I work with. Any older repo would be great.

cameronraysmith commented 3 months ago

Could you suggest one or a few you would like to have added devbox to but have not yet?

vfarcic commented 3 months ago

How about https://github.com/vfarcic/devops-catalog-code?

cameronraysmith commented 3 months ago

Sounds good. I'll take a look there.